!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("d3-array"),require("d3-collection"),require("d3-geo"),require("d3-voronoi")):"function"==typeof define&&define.amd?define(["exports","d3-array","d3-collection","d3-geo","d3-voronoi"],e):e(r.d3=r.d3||{},r.d3,r.d3,r.d3,r.d3)}(this,function(r,e,n,o,t){"use strict";function s(r,e){for(var n=0,o=0;o<3;o++)n+=r[o]*e[o];return n}function i(r,e){for(var n=new Array(3),o=0;o<3;o++){var t=o+1;t>=3&&(t-=3);var s=o+2;s>=3&&(s-=3),n[o]=r[t]*e[s]-r[s]*e[t]}return n}function a(r,e,n){return s(i(r,e),n)}function v(r,e){for(var n=0,o=0,t=0;t<3;t++){var s=e[t]-r[t];n+=s*s;var i=e[t]+r[t];o+=i*i}return Math.sqrt(n)-Math.sqrt(o)}function u(r){for(var e,n=new Array(3),o=0,t=0;t<3;t++){var s=r[t];o+=s*s}e=o>0?1/Math.sqrt(o):0;for(var t=0;t<3;t++)n[t]=e*r[t];return n}function f(r,e,n){return i(r[e],r[n])}function l(r,e,n,o){return a(r[e],r[n],r[o])}function p(r,e,n){return v(r[e],r[n])}function g(){for(var r=new Array(3),e=0;e<3;e++)r[e]=0;return r}function d(r){for(var e=new Array(3),n=0;n<3;n++)e[n]=r[n];return e}function h(r,e){for(var n=0;n<3;n++)r[n]+=e[n]}function c(r,e){for(var n=0;n<3;n++)r[n]*=e}function y(r,e){for(var n=g(),o=0;o<3;o++)n[o]=r[o]-e[o];return n}function _(r){return"undefined"==typeof r}function b(r,e){if(_(r))return!1;if(_(e))return!1;for(var n=0;n<3;n++)if(r.verts[n]!=e.verts[n])return!1;return!0}function I(r,e){if(_(r))return!1;if(_(e))return!1;for(var n=0;n<2;n++)if(r.verts[n]!=e.verts[n])return!1;return!0}function x(r,e){return e>r?e:r}function w(r,e){this.verts=e,this.edges=new Array(3),this.dirs=new Array(3);for(var n=0;n<3;n++){var o=n+1;o>=3&&(o-=3);var t=n+2;t>=3&&(t-=3),this.dirs[n]=f(r,e[o],e[t])}this.vol=l(r,e[0],e[1],e[2]);for(var n=0;n<3;n++)c(this.dirs[n],1/this.vol);for(var s=g(),n=0;n<3;n++)h(s,this.dirs[n]);this.ccdir=u(s);for(var i=0,n=0;n<3;n++)i+=v(this.ccdir,r[e[n]]);i/=3,this.ccdsq=i}function k(r){this.verts=r,this.polys=new Array(2)}function m(r,e){this.verts=e,this.pdst=p(r,e[0],e[1]),this.direc=u(f(r,e[0],e[1]));var n=g();h(n,r[e[0]]),h(n,r[e[1]]),this.midpnt=u(n)}function V(r,e){for(var n=0;n=3&&(p-=3),f[u]=new w(n,[i.verts[u],i.verts[p],e]),l[u]=new k([i.verts[u],e])}for(var u=0;u<3;u++){var p=u+1;p>=3&&(p-=3),f[u].edges[0]=l[p],f[u].edges[1]=l[u],l[u].polys[0]=f[u],l[p].polys[1]=f[u]}for(var u=0;u<3;u++)for(var g=a[u],d=v[u],h=0;h<3;h++)for(var c=f[h],y=0,_=0;_<2;_++)if(c.IsVertex(g.verts[_])&&y++,2==y){g.polys[d]=c,c.edges[2]=g;break}r.triangles[s]=f[0];for(var u=1;u<3;u++)r.triangles.push(f[u]);for(var u=0;u<3;u++)r.edges.push(l[u]);return!0}}return!1}function j(r){for(var e=r.positions,n=new Array(4),o=0;o<100;o++){for(var t=0,s=0;s=3&&(f-=3);var l=v+2;l>=3&&(l-=3),n[0]=u,n[1]=a[0].verts[f],n[3]=a[0].verts[l];for(var v=0;v<3;v++){var u=a[1].verts[v];if(!i.IsVertex(u))break}n[2]=u;var p=a[0].IsPointInCircumcircle(e[n[2]]),g=a[1].IsPointInCircumcircle(e[n[0]]);if(p||g){var d=new w(e,[n[0],n[1],n[2]]);if(d.IsVertexOrderCorrect()){var h=new w(e,[n[0],n[2],n[3]]);if(h.IsVertexOrderCorrect()){t++;for(var v=0;v<3;v++){var c=a[0].edges[v];if(!I(c,i)&&c.IsVertex(n[3])){var y=c,b=v;break}}for(var v=0;v<3;v++){var c=a[1].edges[v];if(!I(c,i)&&c.IsVertex(n[1])){var x=c,k=v;break}}var m=y.PolyIndexIn(a[0]),V=x.PolyIndexIn(a[1]);y.polys[m]=a[1],x.polys[V]=a[0],a[0].edges[b]=x,a[1].edges[k]=y,a[0].copy_vert_info(d),a[1].copy_vert_info(h),i.verts=[n[0],n[2]]}}}}}if(0==t)break}}function L(r){for(var e=new Object,n=-1,o=0;o=0){for(var f=n,l=[f];;){var p=e[f];if(p==n)break;l.push(p),f=p}r.hull=l}}function S(r){if(1!=r.triangles.length){if(0!=r.triangles.length){for(var e=0;e=3){for(var V=new Array,A=r.positions,M=r.hull.length,f=0;f=M&&(O=0);var C=r.hull[O],j=r.vor_polygons[s].edges,L=r.vor_polygons[C].edges,S=P(j,L),o=S[0],E=o.polys[0].index,F=A[s],z=A[C],D=y(z,F),T=[E,r.vor_positions[E],s,F,C,z,D];V.push(T)}for(;V.length>3;){for(var B=V.length,G=new Array,H=new Array,J=0;J=B&&(K=0);var N=u(i(V[J][6],V[K][6]));c(N,-1),H[J].push(G.length),H[K].push(G.length),G.push(N)}for(var J=0;J=2){for(var R=new Array,U=0;U=B&&(K=0);var er=J-1;er<0&&(er=B-1);var nr,or=0,Q=H[J];if(Q!=-1){var tr=H[er];Q==tr?or=2:(nr=H[K],Q==nr&&(or=1))}if(0==or)rr.push(V[J]);else if(1==or){var sr=V[J],ir=V[K],N=G[J],ar=sr[0],vr=ir[0],ur=vr!=ar;if(ur){for(var fr=r.vor_positions.length,lr=sr[2],F=sr[3],pr=ir[4],z=ir[5],gr=void 0,dr=void 0,hr=0;hr=B;)yr-=B;yr<=2?void 0==gr?gr=cr:cr=3&&a.boundary[0]>=0&&(n=new w(r.vor_positions,a.boundary.slice(0,3)),n.IsVertexOrderCorrect()||a.boundary.reverse())}else if(2==r.hull.length){var mr=r.positions[r.hull[0]],Vr=r.positions[r.hull[1]],F=g();h(F,mr),h(F,Vr),F=u(F),r.vor_positions.push(F);var z=u(i(mr,Vr));r.vor_positions.push(z);var qr=d(F);c(qr,-1),r.vor_positions.push(qr);var Pr=d(z);c(Pr,-1),r.vor_positions.push(Pr),r.vor_edges.push([0,1,2,3,0]),o=r.edges[0];for(var J=0;J<2;J++){var s=r.hull[J];r.vor_polygons[s]=new Object;var a=r.vor_polygons[s];a.edges=[o],a.triangles=[0],0==J?a.boundary=[0,1,2,3]:1==J&&(a.boundary=[0,3,2,1])}}}else if(3==r.hull.length){var n=r.triangles[0];r.vor_positions.push(n.ccdir);for(var J=0;J<3;J++){var K=J+1;K>=3&&(K=0);var er=J-1;er<0&&(er=2);var Vr=r.positions[r.hull[J]],Ar=r.positions[r.hull[K]],Mr=y(Ar,Vr);r.vor_positions.push(u(i(Mr,n.ccdir))),r.vor_edges.push([0,J+1,4]);var s=r.hull[J];r.vor_polygons[s]=new Object;for(var a=r.vor_polygons[s],Or=r.hull[er],Cr=0;Cr<3;Cr++){var o=r.edges[Cr],jr=q([Or,s],o.verts);if(2==jr.length)break}a.edges=[o],a.triangles=[n],a.boundary=[0,er+1,4,J+1]}var Lr=d(n.ccdir);c(Lr,-1),r.vor_positions.push(Lr)}}function E(r,e){var n=new Object;if(n.positions=r,n.indices=e,n.triangles=[],n.edges=[],n.hull=[],n.vor_positions=[],n.vor_edges=[],n.vor_polygons=new Object,e.length<3)return 2==e.length&&(n.edges.push(new k(e)),n.hull=e),S(n),n;var o=new w(r,e.slice(0,3));o.IsVertexOrderCorrect()||(o=new w(r,[e[0],e[2],e[1]])),n.triangles.push(o);for(var t=new Array(3),s=0;s<3;s++){var i=s+1;i>=3&&(i-=3);var a=e[s],v=e[i],u=[a,v],f=new k(u),l=new m(r,u);l.edge=f,t[s]=l,o.edges[s]=f,f.polys[0]=o,n.edges.push(f)}for(var p=e.slice(0,3),g=t,d=Object,s=0;s<3;s++){var i=s+2;i>=3&&(i-=3);var a=e[s];d[a]=[t[s],t[s+1]]}for(var h=3;hi&&(i=a,n=u)}s[n].properties.urquhart=!1}),v._links={type:"FeatureCollection",features:s}},v.triangles=d.triangles=function(r){if(r&&d(r),v._triangles)return v._triangles;var e=u.triangles.map(function(r){return r.spherical=r.verts.map(function(r){return u.positions[r]}).map(i),r.ccdsq<0&&(r.spherical=r.spherical.reverse(),r.ccdsq*=-1),r}).map(function(r){return{type:"Polygon",coordinates:[r.spherical.concat([r.spherical[0]])],properties:{sites:r.verts.map(function(r){return f[r]}),area:r.vol,circumcenter:i(r.ccdir),circumradius:Math.sqrt(r.ccdsq)}}});return v._triangles={type:"FeatureCollection",features:e}},v.polygons=d.polygons=function(r){if(r&&d(r),v._polygons)return v._polygons;var e=u.indices.map(function(r,e){var n={},t=u.vor_polygons[u.indices[r]];if(void 0==t)n.type="Sphere";else{var s=a(u.vor_positions,t.boundary.concat([t.boundary[0]])),i={type:"Polygon",coordinates:[[f[r],s[0],s[1],f[r]]]};o.geoArea(i)>2*Math.PI+1e-10&&(s=s.reverse()),n.type="Polygon",n.coordinates=[s]}return n.properties={site:f[r],sitecoordinates:l[r],neighbours:t.edges.map(function(e){return e.verts.filter(function(e){return e!==r})[0]})},n});return v._polygons={type:"FeatureCollection",features:e}},v.hull=d.hull=function(r){if(r&&d(r),v._hull)return v._hull;if(!u.hull.length)return null;var e=u.hull.reverse();return v._hull={type:"Polygon",coordinates:[e.concat([e[0]]).map(function(r){return l[r]})],properties:{sites:e.map(function(r){return f[r]})}}},v.find=function(r,e,n){var o,t=v.polygons().features,s=v.find.found||0,i=t[s]||t[s=0],a=d3.geoLength({type:"LineString",coordinates:[[r,e],i.properties.sitecoordinates]});do i=t[o=s],s=null,i.properties.neighbours.forEach(function(n){var o=d3.geoLength({type:"LineString",coordinates:[[r,e],t[n].properties.sitecoordinates]});if(o=0},w.prototype.IsPointInside=function(r){for(var e=0;e<3;e++)if(s(r,this.dirs[e])<0)return!1;return!0},w.prototype.IsPointInCircumcircle=function(r){return v(this.ccdir,r)0,f=s(t,e.midpnt)>0;if(f!=a)return!1;for(var l=[],n=0;n<2;n++){var p=v(t,r[this.verts[n]]);l.push(p)}for(var g=[],n=0;n<2;n++){var p=v(t,r[e.verts[n]]);g.push(p)}var d=x(l[0],l[1]),h=x(g[0],g[1]);if(d<=this.pdst&&h<=e.pdst&&a)return!0;c(t,-1),a=!a;for(var n=0;n<2;n++)l[n]=-l[n],g[n]=-g[n];return d=x(l[0],l[1]),h=x(g[0],g[1]),!!(d<=this.pdst&&h<=e.pdst&&a)},r.geoVoronoi=z,Object.defineProperty(r,"__esModule",{value:!0})});