<commit_before>// 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;0<c;c--){var b=Math.floor(PNLTRI.Math.random()*(c+1)),d=a[c];a[c]=a[b];a[b]=d}return a},ptsCrossProd:function(a,c,b){return(c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x)}};PNLTRI.Math.EPSILON_P=Math.pow(2,-43);PNLTRI.Math.EPSILON_N=-PNLTRI.Math.EPSILON_P;PNLTRI.PolygonData=function(a){this.vertices=[];this.segments=[];this.idNextPolyChain=0;this.monoSubPolyChains=[];this.triangles=[];if(a)for(var c=0,b=a.length;c<b;c++)this.addPolygonChain(a[c])};
PNLTRI.PolygonData.prototype={constructor:PNLTRI.PolygonData,getSegments:function(){return this.segments},getFirstSegment:function(){return this.segments[0]},getMonoSubPolys:function(){return this.monoSubPolyChains},getTriangles:function(){return this.triangles.concat()},nbPolyChains:function(){return this.idNextPolyChain},compare_pts_yx:function(a,c){var b=a.y-c.y;if(b>PNLTRI.Math.EPSILON_P)return 1;if(b<PNLTRI.Math.EPSILON_N)return-1;b=a.x-c.x;return b>PNLTRI.Math.EPSILON_P?1:b<PNLTRI.Math.EPSILON_N?
-1:0},isClockWise:function(a){var c=a,b=0;do b+=(c.vFrom.x-c.vTo.x)*(c.vFrom.y+c.vTo.y),c=c.snext;while(c!=a);return 0>b},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)<PNLTRI.Math.EPSILON_P&&Math.abs(a.y-b.y)<PNLTRI.Math.EPSILON_P}for(var b=[],d,e,g,h=0;h<a.length;h++)d=this.appendVertexEntry({x:a[h].x,y:a[h].y}),e=!0,g=b.length-1,0<=g&&c(d,b[g])&&(e=!1),e&&b.push(d);1<b.length&&c(b[b.length-1],b[0])&&b.pop();return b},addPolygonChain:function(a){a=this.addVertexChain(a);if(3>a.length)return console.log("Polygon has < 3 vertices!",a),0;for(var c=this.segments.length,b,d,e,g=0;g<a.length-
1;g++)b=this.createSegmentEntry(a[g],a[g+1]),e?(b.sprev=e,e.snext=b):d=b,e=b,this.appendSegmentEntry(b);b=this.createSegmentEntry(a[a.length-1],a[0]);b.sprev=e;e.snext=b;this.appendSegmentEntry(b);d.sprev=b;b.snext=d;this.idNextPolyChain++;return this.segments.length-c},initMonoChains:function(){for(var a=0;a<this.segments.length;a++)this.segments[a].marked=!1,this.segments[a].mprev=this.segments[a].sprev,this.segments[a].mnext=this.segments[a].snext,this.segments[a].vFrom.outSegs=[{segOut:this.segments[a],
vertTo:this.segments[a].vTo}]},appendVertexOutsegEntry:function(a,c){var b=c?c:{segOut:null,vertTo:null};a.outSegs.push(b);return b},splitPolygonChain:function(a,c,b){function d(a,b){for(var c,d,f=null,e=4,h=0;h<a.outSegs.length;h++){c=a.outSegs[h];var g=c.vertTo;d=g.x-a.x;var g=g.y-a.y,k=b.x-a.x,t=b.y-a.y,v=(d*k+g*t)/Math.sqrt(d*d+g*g)/Math.sqrt(k*k+t*t);(d=0<=d*t-k*g?1-v:3+v)<e&&(e=d,f=c)}return f}var e=d(c,b),g=d(b,c),e=e.segOut,g=g.segOut,h=1==this.compare_pts_yx(c,b),k=this.appendSegmentEntry({vFrom:c,
vTo:b,upward:!h,mprev:e.mprev,mnext:g}),h=this.appendSegmentEntry({vFrom:b,vTo:c,upward:h,mprev:g.mprev,mnext:e});e.mprev.mnext=k;g.mprev.mnext=h;e.mprev=h;g.mprev=k;this.appendVertexOutsegEntry(c,{segOut:k,vertTo:b});this.appendVertexOutsegEntry(b,{segOut:h,vertTo:c});this.monoSubPolyChains[a]=g;this.monoSubPolyChains.push(e);return this.monoSubPolyChains.length-1},unique_monotone_chains_max:function(){var a,c,b,d,e,g;for(g=0;g<this.segments.length;g++)this.segments[g].marked=!1;var h=[];for(g=0;g<
this.monoSubPolyChains.length;g++){a=c=this.monoSubPolyChains[g];d=e=a.vFrom;a.marked=!0;a=a.mnext;for(var k=!1;(b=a.vFrom)!=d;){if(a.marked){k=!0;break}else a.marked=!0;1==this.compare_pts_yx(b,e)&&(e=b,c=a);a=a.mnext}k||h.push(c)}return h},normalize_monotone_chains:function(){this.monoSubPolyChains=this.unique_monotone_chains_max();return this.monoSubPolyChains.length},clearTriangles:function(){this.triangles=[]},addTriangle:function(a,c,b){this.triangles.push([a.id,c.id,b.id])}};PNLTRI.EarClipTriangulator=function(a){this.polyData=a};
PNLTRI.EarClipTriangulator.prototype={constructor:PNLTRI.EarClipTriangulator,triangulate_polygon_no_holes:function(){var a=this.polyData,c=a.getFirstSegment(),b=c;if(a.isClockWise(c)){do b.mprev=b.snext,b=b.mnext=b.sprev;while(b!=c)}else{do b.mprev=b.sprev,b=b.mnext=b.snext;while(b!=c)}for(c=a=c;a.mnext!=a.mprev;){a:{var b=a.mprev.vFrom.x,d=a.mprev.vFrom.y,e=a.vFrom.x,g=a.vFrom.y,h=a.mnext.vFrom.x,k=a.mnext.vFrom.y,n=h-e,p=k-g,q=b-h,u=d-k,f=e-b,m=g-d;if(PNLTRI.Math.EPSILON_P>f*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<this.segListArray.length;c++)this.segListArray[c].rootFrom=this.segListArray[c].rootTo=this.root,this.segListArray[c].is_inserted=!1;this.compare_pts_yx=a.compare_pts_yx}else this.compare_pts_yx=PNLTRI.PolygonData.prototype.compare_pts_yx};
PNLTRI.QueryStructure.prototype={constructor:PNLTRI.QueryStructure,getRoot:function(){return this.root},getSegListArray:function(){return this.segListArray},cloneTrap:function(a){a=a.clone();this.trapArray.push(a);return a},splitNodeAtPoint:function(a,c,b){var d=a.trap;if(d.vHigh==c||d.vLow==c)return a;var e=d.splitOffLower(c);this.trapArray.push(e);a.nodetype=PNLTRI.T_Y;a.yval=c;a.trap=null;a.right=new PNLTRI.QsNode(d);a.left=new PNLTRI.QsNode(e);return b?d.sink:e.sink},fpEqual:function(a,c){return Math.abs(a-
c)<PNLTRI.Math.EPSILON_P},is_left_of:function(a,c,b){b=a.vFrom.x-c.x;var d=a.vTo.x-c.x;if(Math.abs(a.vTo.y-c.y)<PNLTRI.Math.EPSILON_P)a=d,c=b;else if(Math.abs(a.vFrom.y-c.y)<PNLTRI.Math.EPSILON_P)a=b,c=d;else return a.upward?PNLTRI.Math.ptsCrossProd(a.vFrom,a.vTo,c):PNLTRI.Math.ptsCrossProd(a.vTo,a.vFrom,c);return Math.abs(a)<PNLTRI.Math.EPSILON_P?Math.abs(c)<PNLTRI.Math.EPSILON_P?0:c:a},ptNode:function(a,c,b){var d,e=!0;switch(b.nodetype){case PNLTRI.T_SINK:return b;case PNLTRI.T_Y:d=a;d==b.yval&&
(d=c);d=this.compare_pts_yx(d,b.yval);-1==d&&(e=!1);break;case PNLTRI.T_X:if(a==b.seg.vFrom||a==b.seg.vTo){if(this.fpEqual(a.y,c.y)){c.x<a.x&&(e=!1);break}else d=this.is_left_of(b.seg,c,!1),0<d?e=!1:0==d&&(e=a==b.seg.vFrom?!0:!1);break}else d=this.is_left_of(b.seg,a,!0),0<d?e=!1:0==d&&(e=!0);break;default:console.log("ptNode: undef. NodeType: ",b.nodetype)}return e?this.ptNode(a,c,b.right):this.ptNode(a,c,b.left)},add_segment:function(a){function c(){var a=f.uL||f.uR;a.dL&&a.dR?f==a.dR?(m.setAbove(null,
null),l.setAbove(null,a),a.setBelow(a.dL,l)):(l.setAbove(null,null),m.setAbove(a,null),a.setBelow(m,a.dR)):(m.setAbove(a,null),l.setAbove(null,a),a.setBelow(m,l))}function b(a){f.vLow==u.vLow&&q?f.dL?(m.setBelow(a,null),l.setBelow(null,null),a.setAbove(m,a.uR)):(l.setBelow(null,a),m.setBelow(null,null),a.setAbove(a.uL,l)):(a.uL&&a.uR?a.uL==f?(a.usave=a.uR,a.uside=PNLTRI.S_LEFT,m.setBelow(a,null),l.setBelow(a,null)):(a.usave=a.uL,a.uside=PNLTRI.S_RIGHT,m.setBelow(null,a),l.setBelow(null,a)):f.vLow==
u.vLow?(m.setBelow(a,null),l.setBelow(null,a)):(m.setBelow(null,a),l.setBelow(a,null)),a.setAbove(m,l))}function d(){var b;f.vLow==u.vLow&&q?(f.dL.setAbove(m,null),f.dR.setAbove(null,l),b=f.dR,m.setBelow(f.dL,null),l.setBelow(null,b),b=null):0<e.is_left_of(a,f.vLow,!0)?(b=f.dR,f.dL.setAbove(m,null),f.dR.setAbove(m,l),m.setBelow(f.dL,f.dR),l.setBelow(null,f.dR)):(b=f.dL,f.dL.setAbove(m,l),f.dR.setAbove(null,l),l.setBelow(f.dL,f.dR),m.setBelow(f.dL,null));return b}var e=this,g,h,k,n,p,q;a.upward?(n=
a.vFrom,g=a.vTo,p=a.rootFrom,h=a.rootTo,q=a.sprev.is_inserted,k=a.snext.is_inserted):(n=a.vTo,g=a.vFrom,p=a.rootTo,h=a.rootFrom,q=a.snext.is_inserted,k=a.sprev.is_inserted);h=this.ptNode(g,n,h);k||(h=this.splitNodeAtPoint(h,g,!1));k=h.trap;if(k.uL||k.uR){h=this.ptNode(n,g,p);q||(h=this.splitNodeAtPoint(h,n,!0));var u=h.trap,f=k,m,l,s,r;for(g=this.trapArray.length+2;f;){if(0>--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<a.length)},reverse_polygon_chain:function(a){var c,b=a;do c=b.snext,b.snext=b.sprev,b.sprev=c,c=b.vTo,b.vTo=b.vFrom,b.vFrom=c,b.upward=!b.upward,b=b.sprev;while(b!=a)},normalize_segment_orientation:function(){for(var a,c=0;c<this.segListArray.length;c++)a=this.segListArray[c],
a.upward==(0==a.trLeft.depth%2)&&this.reverse_polygon_chain(a)},find_first_inside:function(){for(var a,c=0,b=this.trapArray.length;c<b;c++)if(a=this.trapArray[c],1==a.depth%2&&!a.monoDiag&&(!a.uL&&!a.uR||!a.dL&&!a.dR))return a;return null}};PNLTRI.Trapezoider=function(a){this.polyData=a;this.queryStructure=new PNLTRI.QueryStructure(this.polyData)};
PNLTRI.Trapezoider.prototype={constructor:PNLTRI.Trapezoider,find_first_inside:function(){return this.queryStructure.find_first_inside()},math_logstar_n:function(a){var c;for(c=0;1<=a;c++)a=PNLTRI.Math.log2(a);return c-1},math_NH:function(a,c){var b,d;b=0;for(d=a;b<c;b++)d=PNLTRI.Math.log2(d);return Math.ceil(1*a/d)},optimise_randomlist:function(a){var c=0,b=this.polyData.nbPolyChains();if(1!=b)for(var d=Array(b),e=a.concat(),g=0;g<e.length;g++){var h=e[g].chainId;d[h]?a[b++]=e[g]:(a[c++]=e[g],d[h]=
!0)}},find_new_roots:function(a){a.is_inserted||(a.rootFrom=this.queryStructure.ptNode(a.vFrom,a.vTo,a.rootFrom),a.rootTo=this.queryStructure.ptNode(a.vTo,a.vFrom,a.rootTo))},trapezoide_polygon:function(){var a=this.queryStructure,c=a.segListArray.concat();PNLTRI.Math.array_shuffle(c);this.optimise_randomlist(c);var b,d,e=c.length,g=this.math_logstar_n(e);for(d=1;d<=g;d++){for(b=this.math_NH(e,d-1);b<this.math_NH(e,d);b++)a.add_segment(c[b-1]);for(b=0;b<e;b++)this.find_new_roots(c[b])}for(b=this.math_NH(e,
g);b<=e;b++)a.add_segment(c[b-1]);a.assignDepths();a.normalize_segment_orientation()}};PNLTRI.MonoSplitter=function(a){this.polyData=a;this.startTrap=this.trapezoider=null};
PNLTRI.MonoSplitter.prototype={constructor:PNLTRI.MonoSplitter,monotonate_trapezoids:function(){this.trapezoider=new PNLTRI.Trapezoider(this.polyData);this.trapezoider.trapezoide_polygon();this.startTrap=this.trapezoider.find_first_inside();this.polyData.initMonoChains();for(var a=0,c=this.startTrap;c;)if(this.polyData.monoSubPolyChains[a]=c.lseg,this.alyTrap(a,c,null,null,null),c=this.trapezoider.find_first_inside())a=this.polyData.monoSubPolyChains.length;return this.polyData.normalize_monotone_chains()},
doSplit:function(a,c,b,d){return d?this.polyData.splitPolygonChain(a,c,b):this.polyData.splitPolygonChain(a,b,c)},alyTrap:function(a,c,b,d,e){function g(){var a;return(a=h.pop())?(k=a[0],n=a[1],p=a[2],q=a[3],!0):!1}var h=[],k,n,p,q,u;null==b&&(d=!0,c.uL?b=!0:c.dL?b=!1:(d=!1,b=c.uR?!0:!1));for(c&&h.push([c,b,d,a]);g();)if(!k.monoDiag){if(!k.lseg||!k.rseg)return console.log("ERR alyTrap: lseg/rseg missing",k),h;n?p?(a=k.uL,c=k.uR,b=k.dL,d=k.dR):(a=k.uR,c=k.uL,b=k.dR,d=k.dL):p?(a=k.dL,c=k.dR,b=k.uL,
d=k.uR):(a=k.dR,c=k.dL,b=k.uR,d=k.uL);if(c||d)u=this.doSplit(q,k.vLow,k.vHigh,p);d&&h.push([d,n,!p,u]);c&&h.push([c,!n,!p,u]);b&&h.push([b,n,p,q]);b||d||a&&h.push([a,!n,p,q]);k.monoDiag=!0;if(e)return h}return[]}};PNLTRI.MonoTriangulator=function(a){this.polyData=a};
PNLTRI.MonoTriangulator.prototype={constructor:PNLTRI.MonoTriangulator,triangulate_all_polygons:function(){var a=this.polyData.getMonoSubPolys();this.polyData.clearTriangles();for(var c=0;c<a.length;c++){var b=a[c],d=b.mprev,e=b.mnext;e.mnext==d?this.polyData.addTriangle(b.vFrom,e.vFrom,d.vFrom):this.triangulate_monotone_polygon(b)}},triangulate_monotone_polygon:function(a){var c=a.mnext;a=a.vFrom;var b=[c.vFrom],d=0,c=c.mnext,e=c.vFrom;if(e!=a){for(;e!=a||1<d;)if(0<d)if(0<PNLTRI.Math.ptsCrossProd(e,
b[d-1],b[d]))this.polyData.addTriangle(b[d-1],b[d],e),d--;else if(b[++d]=e,e==a)for(console.log("ERR uni-y-monotone: only concave angles left",b);1<d;)d--,this.polyData.addTriangle(b[d-1],b[d],b[d+1]);else c=c.mnext,e=c.vFrom;else b[++d]=e,c=c.mnext,e=c.vFrom;this.polyData.addTriangle(b[d-1],b[d],e)}}};PNLTRI.Triangulator=function(){this.lastPolyData=null};PNLTRI.Triangulator.prototype={constructor:PNLTRI.Triangulator,triangulate_polygon:function(a,c){if(!a||0==a.length)return[];var b=new PNLTRI.PolygonData(a),d;d=c?!1:1==b.nbPolyChains();d&&(d=new PNLTRI.EarClipTriangulator(b),d=d.triangulate_polygon_no_holes());d||((new PNLTRI.MonoSplitter(b)).monotonate_trapezoids(),d=new PNLTRI.MonoTriangulator(b),d.triangulate_all_polygons());this.lastPolyData=b;return b.getTriangles()}};<commit_msg>PnlTri.js Rev. 1.4; smaller & faster<commit_after>// pnltri.js / raw.github.com/jahting/pnltri.js/master/LICENSE
'use strict';var PNLTRI={REVISION:"1.4"};PNLTRI.Math={log2:function(a){return Math.log(a)/Math.LN2},random:Math.random,array_shuffle:function(a){for(var c=a.length-1;0<c;c--){var b=Math.floor(PNLTRI.Math.random()*(c+1)),d=a[c];a[c]=a[b];a[b]=d}return a},ptsCrossProd:function(a,c,b){return(c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x)}};PNLTRI.Math.EPSILON_P=Math.pow(2,-43);PNLTRI.Math.EPSILON_N=-PNLTRI.Math.EPSILON_P;PNLTRI.PolygonData=function(a){this.vertices=[];this.segments=[];this.idNextPolyChain=0;this.PolyLeftArr=[];this.monoSubPolyChains=[];this.triangles=[];if(a)for(var c=0,b=a.length;c<b;c++)this.addPolygonChain(a[c])};
PNLTRI.PolygonData.prototype={constructor:PNLTRI.PolygonData,getSegments:function(){return this.segments},getFirstSegment:function(){return this.segments[0]},getMonoSubPolys:function(){return this.monoSubPolyChains},getTriangles:function(){return this.triangles.concat()},nbPolyChains:function(){return this.idNextPolyChain},get_PolyLeftArr:function(){return this.PolyLeftArr.concat()},set_PolyLeft_wrong:function(a){this.PolyLeftArr[a]=!1},compare_pts_yx:function(a,c){var b=a.y-c.y;if(b<PNLTRI.Math.EPSILON_N)return-1;
if(b>PNLTRI.Math.EPSILON_P)return 1;b=a.x-c.x;return b<PNLTRI.Math.EPSILON_N?-1:b>PNLTRI.Math.EPSILON_P?1:0},isClockWise:function(a){var c=a,b=0;do b+=(c.vFrom.x-c.vTo.x)*(c.vFrom.y+c.vTo.y),c=c.snext;while(c!=a);return 0>b},appendVertexEntry:function(a,c){var b={id:this.vertices.length,x:a,y:c,outSegs:[]};this.vertices.push(b);return b},createSegmentEntry:function(a,c){return{chainId:this.idNextPolyChain,vFrom:a,vTo:c,upward:1==this.compare_pts_yx(c,a),sprev:null,snext:null,trLeft:null,trRight:null,
mprev:null,mnext:null,marked:!1}},appendSegmentEntry:function(a){this.segments.push(a);return a},addVertexChain:function(a){function c(a,b){return Math.abs(a.x-b.x)<PNLTRI.Math.EPSILON_P&&Math.abs(a.y-b.y)<PNLTRI.Math.EPSILON_P}for(var b=[],d,e,k,f=0;f<a.length;f++)d=this.appendVertexEntry(a[f].x,a[f].y),e=!0,k=b.length-1,0<=k&&c(d,b[k])&&(e=!1),e&&b.push(d);1<b.length&&c(b[b.length-1],b[0])&&b.pop();return b},addPolygonChain:function(a){a=this.addVertexChain(a);if(3>a.length)return console.log("Polygon has < 3 vertices!",
a),0;for(var c=this.segments.length,b,d,e,k=0;k<a.length-1;k++)b=this.createSegmentEntry(a[k],a[k+1]),e?(b.sprev=e,e.snext=b):d=b,e=b,this.appendSegmentEntry(b);b=this.createSegmentEntry(a[a.length-1],a[0]);b.sprev=e;e.snext=b;this.appendSegmentEntry(b);d.sprev=b;b.snext=d;this.PolyLeftArr[this.idNextPolyChain++]=!0;return this.segments.length-c},initMonoChains:function(){for(var a,c=0;c<this.segments.length;c++)a=this.segments[c],this.PolyLeftArr[a.chainId]?(a.mprev=a.sprev,a.mnext=a.snext,a.vFrom.outSegs.push({segOut:a,
vertTo:a.vTo})):(a=a.snext,a.mprev=a.snext,a.mnext=a.sprev,a.vFrom.outSegs.push({segOut:a,vertTo:this.segments[c].vFrom}))},createMonoSegment:function(a){this.appendSegmentEntry(a);a.vFrom.outSegs.push({segOut:a,vertTo:a.vTo});return a},newMonoChain:function(a){var c=this.monoSubPolyChains.length;this.monoSubPolyChains[c]=a;return c},splitPolygonChain:function(a,c,b,d){function e(a,b){for(var c,d,e=null,k=4,h=0;h<a.outSegs.length;h++){c=a.outSegs[h];var f=c.vertTo;d=f.x-a.x;var f=f.y-a.y,l=b.x-a.x,
t=b.y-a.y,v=(d*l+f*t)/Math.sqrt(d*d+f*f)/Math.sqrt(l*l+t*t);(d=0<=d*t-l*f?1-v:3+v)<k&&(k=d,e=c)}return e}var k,f;k=e(c,b);f=e(b,c);k=k.segOut;var l=f.segOut;f=this.createMonoSegment({vFrom:c,vTo:b,upward:!0,mprev:k.mprev,mnext:l});c=this.createMonoSegment({vFrom:b,vTo:c,upward:!1,mprev:l.mprev,mnext:k});k.mprev.mnext=f;l.mprev.mnext=c;k.mprev=c;l.mprev=f;b=this.monoSubPolyChains.length;d?(this.monoSubPolyChains[a]=f,this.monoSubPolyChains[b]=c):(this.monoSubPolyChains[a]=c,this.monoSubPolyChains[b]=
f);return b},unique_monotone_chains_max:function(){for(var a,c,b,d,e,k=[],f=0;f<this.monoSubPolyChains.length;f++){a=c=this.monoSubPolyChains[f];d=e=a.vFrom;a.marked=!0;a=a.mnext;for(var l=!1;(b=a.vFrom)!=d;){if(a.marked){l=!0;break}else a.marked=!0;1==this.compare_pts_yx(b,e)&&(e=b,c=a);a=a.mnext}l||k.push(c)}return k},normalize_monotone_chains:function(){this.monoSubPolyChains=this.unique_monotone_chains_max();return this.monoSubPolyChains.length},clearTriangles:function(){this.triangles=[]},addTriangle:function(a,
c,b){this.triangles.push([a.id,c.id,b.id])}};PNLTRI.EarClipTriangulator=function(a){this.polyData=a};
PNLTRI.EarClipTriangulator.prototype={constructor:PNLTRI.EarClipTriangulator,triangulate_polygon_no_holes:function(){var a=this.polyData,c=a.getFirstSegment(),b=c;if(a.isClockWise(c)){do b.mprev=b.snext,b=b.mnext=b.sprev;while(b!=c);a.set_PolyLeft_wrong(0)}else{do b.mprev=b.sprev,b=b.mnext=b.snext;while(b!=c)}for(c=a=c;a.mnext!=a.mprev;){a:{var b=a.mprev.vFrom.x,d=a.mprev.vFrom.y,e=a.vFrom.x,k=a.vFrom.y,f=a.mnext.vFrom.x,l=a.mnext.vFrom.y,n=f-e,p=l-k,q=b-f,u=d-l,g=e-b,m=k-d;if(PNLTRI.Math.EPSILON_P>
g*p-n*m)b=!1;else{for(var h=a.mprev.mprev,s=a.mnext;s!=h;){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-k;if(0!=x||0!=y)if(r-=f,t-=l,(0!=r||0!=t)&&n*y-p*x>=PNLTRI.Math.EPSILON_N&&g*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.Trapezoid=function(a,c,b,d){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.monoDone=!1};
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},splitOffLower:function(a){var c=this.clone();this.vLow=c.vHigh=a;this.dL=c;c.uL=this;this.dR=c.uR=null;c.dL&&(c.dL.uL=c);c.dR&&(c.dR.uR=c);return c}};PNLTRI.QsNode=function(a){this.trap=a;a.sink=this};PNLTRI.QsNode.prototype={constructor:PNLTRI.QsNode};
PNLTRI.QueryStructure=function(a){var c=new PNLTRI.Trapezoid(null,null,null,null);this.trapArray=[];this.appendTrapEntry(c);this.root=new PNLTRI.QsNode(c);if(a){for(var c=a.getSegments(),b=0;b<c.length;b++)c[b].rootFrom=c[b].rootTo=this.root,c[b].is_inserted=!1;this.compare_pts_yx=a.compare_pts_yx}else this.compare_pts_yx=PNLTRI.PolygonData.prototype.compare_pts_yx};
PNLTRI.QueryStructure.prototype={constructor:PNLTRI.QueryStructure,getRoot:function(){return this.root},appendTrapEntry:function(a){a.trapID=this.trapArray.length;this.trapArray.push(a)},cloneTrap:function(a){a=a.clone();this.appendTrapEntry(a);return a},splitNodeAtPoint:function(a,c,b){var d=a.trap;if(d.vHigh==c||d.vLow==c)return a;var e=d.splitOffLower(c);this.appendTrapEntry(e);a.yval=c;a.trap=null;a.right=new PNLTRI.QsNode(d);a.left=new PNLTRI.QsNode(e);return b?d.sink:e.sink},fpEqual:function(a,
c){return Math.abs(a-c)<PNLTRI.Math.EPSILON_P},is_left_of:function(a,c,b){b=a.vFrom.x-c.x;var d=a.vTo.x-c.x;if(Math.abs(a.vTo.y-c.y)<PNLTRI.Math.EPSILON_P)a=d,c=b;else if(Math.abs(a.vFrom.y-c.y)<PNLTRI.Math.EPSILON_P)a=b,c=d;else return a.upward?PNLTRI.Math.ptsCrossProd(a.vFrom,a.vTo,c):PNLTRI.Math.ptsCrossProd(a.vTo,a.vFrom,c);return Math.abs(a)<PNLTRI.Math.EPSILON_P?Math.abs(c)<PNLTRI.Math.EPSILON_P?0:c:a},ptNode:function(a,c,b){for(var d;b;)if(b.yval)d=a==b.yval?c:a,d=this.compare_pts_yx(d,b.yval),
b=-1==d?b.left:b.right;else if(b.seg)a==b.seg.vFrom||a==b.seg.vTo?this.fpEqual(a.y,c.y)?b=c.x<a.x?b.left:b.right:(d=this.is_left_of(b.seg,c,!1),b=0<d?b.left:0>d?b.right:a==b.seg.vFrom?b.right:b.left):(d=a,d=this.is_left_of(b.seg,d,!0),b=0<d?b.left:b.right);else return b.trap||console.log("ptNode: unknown type",b),b},add_segment:function(a){function c(){var a=g.uL||g.uR;a.dL&&a.dR?g==a.dL?(h.uL=null,a.dL=m):(m.uR=null,a.dR=h):(h.uL=null,h.uR=a,a.dR=h)}function b(a){g.vLow==u.vLow?(q?g.dL?(a.uL=m,m.dL=
a,h.dR=null):(a.uR=h,m.dL=null,h.dR=a):(a.uL=m,a.uR=h,m.dL=h.dR=a),m.dR=h.dL=null):(a.uL&&a.uR&&(a.uL==g?(a.usave=a.uR,a.uleft=!0):(a.usave=a.uL,a.uleft=!1)),a.uL=m,a.uR=h,m.dR=h.dL=a,m.dL=h.dR=null)}function d(){var b;g.vLow==u.vLow&&q?(g.dL.uL=m,g.dR.uR=h,m.dL=g.dL,h.dR=g.dR,b=m.dR=h.dL=null):(g.dL.uL=m,g.dR.uR=h,0<e.is_left_of(a,g.vLow,!0)?(b=g.dR,g.dR.uL=m,m.dL=g.dL,h.dR=null):(b=g.dL,g.dL.uR=h,h.dR=g.dR,m.dL=null),m.dR=h.dL=b);return b}var e=this,k,f,l,n,p,q;a.upward?(n=a.vFrom,k=a.vTo,p=a.rootFrom,
f=a.rootTo,q=a.sprev.is_inserted,l=a.snext.is_inserted):(n=a.vTo,k=a.vFrom,p=a.rootTo,f=a.rootFrom,q=a.snext.is_inserted,l=a.sprev.is_inserted);f=this.ptNode(k,n,f);l||(f=this.splitNodeAtPoint(f,k,!1));l=f.trap;if(l.uL||l.uR){f=this.ptNode(n,k,p);q||(f=this.splitNodeAtPoint(f,n,!0));var u=f.trap,g=l,m,h,s,r;for(k=this.trapArray.length+2;g;){if(0>--k){console.log("ERR add_segment: infinite loop",g,a,this);return}if(!g.dL&&!g.dR){console.log("ERR add_segment: missing successors",g,a,this);return}n=
g.sink;n.seg=a;n.trap=null;r&&r.rseg==g.rseg?(m=g,h=r,h.vLow=g.vLow,n.left=new PNLTRI.QsNode(m),n.right=r.sink):(s&&s.lseg==g.lseg?(h=g,m=s,m.vLow=g.vLow,n.left=s.sink):(m=g,h=this.cloneTrap(g),n.left=new PNLTRI.QsNode(m)),n.right=new PNLTRI.QsNode(h));g.uL&&g.uR?g.usave?(g.uleft?(h.uL=g.uR,h.uR=g.usave,h.uL.dL=h,h.uR.dR=h):(m.uR=g.uL,m.uL=g.usave,m.uL.dL=m,m.uR.dR=m),m.usave=h.usave=null):g.vHigh==l.vHigh?(h.uR.dR=h,m.uR=h.uL=null):h==g?(h.uL=h.uR,h.uR=null,h.uL.dL=h):(m.uR=m.uL,m.uL=null):c();g.dL&&
g.dR?n=d():(n=g.dL?g.dL:g.dR,b(n));m.rseg&&(m.rseg.trLeft=h);h.lseg&&(h.lseg.trRight=m);m.rseg=h.lseg=a;a.trLeft=m;a.trRight=h;g.vLow!=u.vLow?(s=m,r=h,g=n):g=null}a.is_inserted=!0}else console.log("ERR add_segment: missing trFirst.uX: ",l)},assignDepths:function(a){var c=[this.trapArray[0]],b=[],d,e,k=0;do{for(var f=1==k%2;d=c.pop();)-1==d.depth&&(d.depth=k,d.uL&&c.push(d.uL),d.uR&&c.push(d.uR),d.dL&&c.push(d.dL),d.dR&&c.push(d.dR),(e=d.lseg)&&-1==e.trLeft.depth&&b.push(e.trLeft),e=d.rseg)&&(-1==
e.trRight.depth&&b.push(e.trRight),e.upward!=f&&a.set_PolyLeft_wrong(e.chainId));c=b;b=[];k++}while(0<c.length)},find_first_inside:function(){for(var a,c=0,b=this.trapArray.length;c<b;c++)if(a=this.trapArray[c],1==a.depth%2&&!a.monoDone&&(!a.uL&&!a.uR||!a.dL&&!a.dR)&&a.lseg)return a;return null}};PNLTRI.Trapezoider=function(a){this.polyData=a;this.queryStructure=new PNLTRI.QueryStructure(this.polyData)};
PNLTRI.Trapezoider.prototype={constructor:PNLTRI.Trapezoider,find_first_inside:function(){return this.queryStructure.find_first_inside()},math_logstar_n:function(a){var c;for(c=0;1<=a;c++)a=PNLTRI.Math.log2(a);return c-1},math_NH:function(a,c){var b,d;b=0;for(d=a;b<c;b++)d=PNLTRI.Math.log2(d);return Math.ceil(1*a/d)-1},optimise_randomlist:function(a){var c=0,b=this.polyData.nbPolyChains();if(1!=b)for(var d=Array(b),e=a.concat(),k=0;k<e.length;k++){var f=e[k].chainId;d[f]?a[b++]=e[k]:(a[c++]=e[k],
d[f]=!0)}},trapezoide_polygon:function(){var a=this.polyData.getSegments().concat();PNLTRI.Math.array_shuffle(a);this.optimise_randomlist(a);var c=a.length,b=this.queryStructure,d,e,k,f=this.math_logstar_n(c);for(k=1;k<=f;k++){e=this.math_NH(c,k);for(d=this.math_NH(c,k-1);d<e;d++)b.add_segment(a[d]);for(d=e;d<c;d++)e=a[d],e.rootFrom=this.queryStructure.ptNode(e.vFrom,e.vTo,e.rootFrom),e.rootTo=this.queryStructure.ptNode(e.vTo,e.vFrom,e.rootTo)}for(d=this.math_NH(c,f);d<c;d++)b.add_segment(a[d]);b.assignDepths(this.polyData);
for(d=0;d<c;d++)a[d].trLeft=null,a[d].trRight=null}};PNLTRI.MonoSplitter=function(a){this.polyData=a;this.startTrap=this.trapezoider=null};
PNLTRI.MonoSplitter.prototype={constructor:PNLTRI.MonoSplitter,monotonate_trapezoids:function(){this.trapezoider=new PNLTRI.Trapezoider(this.polyData);this.trapezoider.trapezoide_polygon();this.startTrap=this.trapezoider.find_first_inside();this.polyData.initMonoChains();for(var a=this.startTrap;a;)this.alyTrap(this.polyData.newMonoChain(a.lseg),a,null,null,null),a=this.trapezoider.find_first_inside();return this.polyData.normalize_monotone_chains()},doSplit:function(a,c,b,d){return this.polyData.splitPolygonChain(a,
c,b,d)},alyTrap:function(a,c,b,d,e){function k(){var a;return(a=f.pop())?(l=a[0],n=a[1],p=a[2],q=a[3],!0):!1}var f=[],l,n,p,q,u;null==b&&(d=!0,c.uL?b=!0:c.dL?b=!1:(d=!1,b=c.uR?!0:!1));for(c&&f.push([c,b,d,a]);k();)if(!l.monoDone){l.monoDone=!0;if(!l.lseg||!l.rseg)return console.log("ERR alyTrap: lseg/rseg missing",l),f;n?p?(a=l.uL,c=l.uR,b=l.dL,d=l.dR):(a=l.uR,c=l.uL,b=l.dR,d=l.dL):p?(a=l.dL,c=l.dR,b=l.uL,d=l.uR):(a=l.dR,c=l.dL,b=l.uR,d=l.uL);if(c||d)u=this.doSplit(q,l.vLow,l.vHigh,p);d&&f.push([d,
n,!p,u]);c&&f.push([c,!n,!p,u]);b&&f.push([b,n,p,q]);b||d||a&&f.push([a,!n,p,q]);if(e)return f}return[]}};PNLTRI.MonoTriangulator=function(a){this.polyData=a};
PNLTRI.MonoTriangulator.prototype={constructor:PNLTRI.MonoTriangulator,triangulate_all_polygons:function(){var a=this.polyData.getMonoSubPolys();this.polyData.clearTriangles();for(var c=0;c<a.length;c++){var b=a[c],d=b.mprev,e=b.mnext;e.mnext==d?this.polyData.addTriangle(b.vFrom,e.vFrom,d.vFrom):this.triangulate_monotone_polygon(b)}},triangulate_monotone_polygon:function(a){var c=a.mnext;a=a.vFrom;var b=[c.vFrom],d=0,c=c.mnext,e=c.vFrom;if(e!=a){for(;e!=a||1<d;)if(0<d)if(0<PNLTRI.Math.ptsCrossProd(e,
b[d-1],b[d]))this.polyData.addTriangle(b[d-1],b[d],e),d--;else if(b[++d]=e,e==a)for(console.log("ERR uni-y-monotone: only concave angles left",b);1<d;)d--,this.polyData.addTriangle(b[d-1],b[d],b[d+1]);else c=c.mnext,e=c.vFrom;else b[++d]=e,c=c.mnext,e=c.vFrom;this.polyData.addTriangle(b[d-1],b[d],e)}}};PNLTRI.Triangulator=function(){this.lastPolyData=null};
PNLTRI.Triangulator.prototype={constructor:PNLTRI.Triangulator,clear_lastData:function(){this.lastPolyData=null},get_PolyLeftArr:function(){return this.lastPolyData?this.lastPolyData.get_PolyLeftArr():null},triangulate_polygon:function(a,c){this.clear_lastData();if(!a||0==a.length)return[];var b=new PNLTRI.PolygonData(a),d;d=c?!1:1==b.nbPolyChains();d&&(d=new PNLTRI.EarClipTriangulator(b),d=d.triangulate_polygon_no_holes());if(!d){(new PNLTRI.MonoSplitter(b)).monotonate_trapezoids();d=new PNLTRI.MonoTriangulator(b);
d.triangulate_all_polygons();d=b.getSegments();for(var e=0;e<d.length;e++)d[e].vFrom.outSegs=null}this.lastPolyData=b;return b.getTriangles()}};
{'fraction_non_alphanumeric': 0.2612336876093098, 'fraction_numerical': 0.010123772366473834, 'mean_word_length': 12.273660714285715, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2110494', 'n_tokens_mistral': 15667, 'n_tokens_neox': 15460, 'n_words': 392}