// pnltri.js / raw.github.com/jahting/pnltri.js/master/LICENSE 'use strict';var PNLTRI={REVISION:"1.0"};PNLTRI.Math={log2:function(a){return Math.log(a)/Math.LN2},random:Math.random,array_shuffle:function(a){for(var c=a.length-1;0PNLTRI.Math.EPSILON_P)return 1;if(bPNLTRI.Math.EPSILON_P?1:bb},appendVertexEntry:function(a){a=a?a:{x:null,y:null,outSegs:[]};a.id=this.vertices.length;this.vertices.push(a);return a},createSegmentEntry:function(a,c){return{chainId:this.idNextPolyChain,vFrom:a,vTo:c,upward:1==this.compare_pts_yx(c,a),sprev:null,snext:null}},appendSegmentEntry:function(a){this.segments.push(a);0==this.monoSubPolyChains.length&&(this.monoSubPolyChains=[this.segments[0]]); return a},addVertexChain:function(a){function c(a,b){return Math.abs(a.x-b.x)a.length)return console.log("Polygon has < 3 vertices!",a),0;for(var c=this.segments.length,b,d,e,g=0;gf*p-n*m)b=!1;else{for(var l= a.mprev.mprev,s=a.mnext;s!=l;){var s=s.mnext,r=s.vFrom.x,t=s.vFrom.y,v=r-b,w=t-d;if(0!=v||0!=w){var x=r-e,y=t-g;if(0!=x||0!=y)if(r-=h,t-=k,(0!=r||0!=t)&&n*y-p*x>=PNLTRI.Math.EPSILON_N&&f*w-m*v>=PNLTRI.Math.EPSILON_N&&q*t-u*r>=PNLTRI.Math.EPSILON_N){b=!1;break a}}}b=!0}}if(b)this.polyData.addTriangle(a.mprev.vFrom,a.vFrom,a.mnext.vFrom),a.mprev.mnext=a.mnext,a.mnext.mprev=a.mprev,c=a=a.mnext;else if(a=a.mnext,a==c)return!1}return!0}};PNLTRI.trapCnt=0;PNLTRI.Trapezoid=function(a,c,b,d){this.trapID=PNLTRI.trapCnt++;this.vHigh=a?a:{x:Number.POSITIVE_INFINITY,y:Number.POSITIVE_INFINITY};this.vLow=c?c:{x:Number.NEGATIVE_INFINITY,y:Number.NEGATIVE_INFINITY};this.lseg=b;this.rseg=d;this.depth=-1;this.monoDiag=null}; PNLTRI.Trapezoid.prototype={constructor:PNLTRI.Trapezoid,clone:function(){var a=new PNLTRI.Trapezoid(this.vHigh,this.vLow,this.lseg,this.rseg);a.uL=this.uL;a.uR=this.uR;a.dL=this.dL;a.dR=this.dR;a.sink=this.sink;return a},setAbove:function(a,c){this.uL=a;this.uR=c},setBelow:function(a,c){this.dL=a;this.dR=c},splitOffLower:function(a){var c=this.clone();this.vLow=c.vHigh=a;this.setBelow(c,null);c.setAbove(this,null);c.dL&&(c.dL.uL=c);c.dR&&(c.dR.uR=c);return c}};PNLTRI.T_Y=1;PNLTRI.T_X=2; PNLTRI.T_SINK=3;PNLTRI.S_LEFT=1;PNLTRI.S_RIGHT=2;PNLTRI.QsNode=function(a){this.nodetype=PNLTRI.T_SINK;this.trap=a;a.sink=this};PNLTRI.QsNode.prototype={constructor:PNLTRI.QsNode}; PNLTRI.QueryStructure=function(a){PNLTRI.trapCnt=0;var c=new PNLTRI.Trapezoid(null,null,null,null);this.root=new PNLTRI.QsNode(c);this.trapArray=[c];this.segListArray=null;if(a){this.segListArray=a.getSegments();for(c=0;c--g){console.log("ERR add_segment: infinite loop",f,a,this);return}if(!f.dL&&!f.dR){console.log("ERR add_segment: missing successors", f,a,this);return}h=f.sink;h.nodetype=PNLTRI.T_X;h.seg=a;h.trap=null;n=p=!0;r&&r.rseg==f.rseg?(p=!1,m=f,l=r,l.vLow=f.vLow,h.right=r.sink,h.left=new PNLTRI.QsNode(m)):s&&s.lseg==f.lseg?(n=!1,l=f,m=s,m.vLow=f.vLow,h.left=s.sink,h.right=new PNLTRI.QsNode(l)):(m=f,l=this.cloneTrap(f),h.right=new PNLTRI.QsNode(l),h.left=new PNLTRI.QsNode(m));f.uL&&f.uR?f.usave?(f.uside==PNLTRI.S_LEFT?(l.setAbove(f.uR,f.usave),l.uL.setBelow(l,null),l.uR.setBelow(null,l)):(m.setAbove(f.usave,f.uL),m.uL.setBelow(m,null),m.uR.setBelow(null, m)),m.usave=l.usave=null):f.vHigh==k.vHigh?(l.setAbove(null,f.uR),l.uR.setBelow(null,l),m.setAbove(f.uL,null)):(p&&(l.setAbove(f.uR,null),l.uL.setBelow(l,l.uL.dR)),n&&m.setAbove(null,f.uL)):c();f.dL&&f.dR?n=d():(n=f.dL?f.dL:f.dR,b(n));m.rseg&&(m.rseg.trLeft=l);l.lseg&&(l.lseg.trRight=m);m.rseg=l.lseg=a;a.trLeft=m;a.trRight=l;f.vLow!=u.vLow?(s=m,r=l,f=n):f=null}a.is_inserted=!0}else console.log("ERR add_segment: missing trFirst.uX: ",k)},assignDepths:function(){var a=[this.trapArray[0]],c=[],b,d,e= 0;do{for(;b=a.pop();)-1==b.depth&&(b.depth=e,b.uL&&a.push(b.uL),b.uR&&a.push(b.uR),b.dL&&a.push(b.dL),b.dR&&a.push(b.dR),(d=b.lseg)&&-1==d.trLeft.depth&&c.push(d.trLeft),(d=b.rseg)&&-1==d.trRight.depth&&c.push(d.trRight));a=c;c=[];e++}while(0