-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeom.min.js
3 lines (3 loc) · 48.1 KB
/
geom.min.js
1
2
3
// Copyright Erik Weitnauer 2017. [v1.0.5]
Circle=function(t,e,r){this.x=t;this.y=e;this.r=r};Circle.prototype.copy=function(){return new Circle(this.x,this.y,this.r)};Circle.prototype.centroid=function(){return new Point(this.x,this.y)};Circle.prototype.area=function(){return Math.PI*this.r*this.r};Circle.prototype.move_to_origin=function(){this.x=0;this.y=0};Circle.prototype.bounding_box=function(){return{x:this.x-this.r,y:this.y-this.r,width:2*this.r,height:2*this.r}};Circle.prototype.intersect_with_ray=function(t,e){var r=t.sub(this),i=e.x*e.x+e.y*e.y,n=2*r.x*e.x+2*r.y*e.y,s=r.x*r.x+r.y*r.y-this.r*this.r,a=n*n-4*i*s;if(a<0)return[];if(a<Point.EPS){var o=-n/(2*i);if(o<0)return[];return[new Point(t).add(e.scale(o))]}var u=[],h=(-n-Math.sqrt(a))/(2*i),l=(-n+Math.sqrt(a))/(2*i);if(h>l){var v=h;h=l;l=v}if(h>=0)u.push(new Point(t).add(e.scale(h)));if(l>=0)u.push(new Point(t).add(e.scale(l)));return u};Circle.fromSVGCircle=function(t){var e=t.attributes;if(e.cx&&e.cy&&e.r){return new Circle(Number(e.cx.value),Number(e.cy.value),Number(e.r.value))}else return null};Circle.fromSVGPath=function(t){if(typeof exclude_ellipse=="undefined")exclude_ellipse=true;var e=t.lookupNamespaceURI("sodipodi");var r=function(r){var i=t.getAttributeNS(e,r);if(i!==null)return i;return t.getAttribute("sodipodi:"+r)};var i,n,s,a;if(r("type")=="arc"&&(i=r("cx"))&&(n=r("cy"))&&(s=r("rx"))&&(a=r("ry"))){var o=r("start"),u=r("end");if(o&&u){var h=Number(u)-Number(o);if(Math.abs(Math.abs(h)-2*Math.PI)>.01&&!(h<0&&h>-.01)){console.log("Warning: this is a circle segment! ||start-end|-2*PI| =",h);return null}}s=Number(s);a=Number(a);i=Number(i);n=Number(n);if(Math.abs(s/a-1)>.05){console.log("Warning: This is an ellipse! rx",s,"ry",a);return null}return new Circle(i,n,(s+a)/2)}return null};Circle.prototype.renderInSvg=function(t,e){var r=t.createElementNS("http://www.w3.org/2000/svg","circle");r.setAttribute("cx",this.x);r.setAttribute("cy",this.y);r.setAttribute("r",this.r);r.style.setProperty("stroke","red");r.style.setProperty("stroke-width",".5px");r.style.setProperty("fill","none");e.appendChild(r);return r};Circle.prototype.renderOnCanvas=function(t,e,r){t.beginPath();t.arc(this.x,this.y,this.r,0,2*Math.PI,true);if(e)t.stroke();if(r)t.fill()};function Matrix(t){var e=[];e.M=t?t.length:0;if(t){e.N=t[0].length;for(var r=0;r<t.length;r++){if(t[r].length!=e.N)throw"all rows must have the same length";if(t[r]instanceof Vector)e.push(t[r]);else e.push(new Vector(t[r]))}}else e.N=0;e.__proto__=Matrix.prototype;return e}Matrix.prototype=new Array;Matrix.construct=function(t,e,r){if(!r)r=0;var i=new Matrix;for(var n=0;n<t;n++)i.push(Vector.construct(e,r));i.M=t;i.N=e;return i};Matrix.random=function(t,e){var r=new Matrix;for(var i=0;i<t;i++)r.push(Vector.random(e));return r};Matrix.prototype.get=function(t,e){return this[t][e]};Matrix.prototype.Set=function(t,e,r){this[t][e]=r;return this};Matrix.prototype.mul=function(t){if(t instanceof Vector){if(this.N!=t.length)throw"dimension mismatch";var e=new Vector;for(var r=0;r<this.M;r++)e.push(this[r].mul(t));return e}else if(t instanceof Matrix){if(this.N!=t.M)throw"dimension mismatch";var e=Matrix.construct(this.M,t.N);for(var r=0;r<e.M;r++)for(var i=0;i<e.N;i++){for(var n=0;n<this.N;n++)e[r][i]+=this[r][n]*t[n][i]}return e}else throw"type mismatch"};Matrix.prototype.Sub=function(t){if(t.N!=this.N||t.M!=this.M)throw"dimension mismatch";for(var e=0;e<this.M;e++)this[e].Sub(t[e]);return this};Matrix.prototype.sub=function(t){if(t.N!=this.N||t.M!=this.M)throw"dimension mismatch";var e=new Matrix;for(var r=0;r<this.M;r++)e.push(this[r].sub(t[r]));e.M=t.M;e.N=t.N;return e};Matrix.prototype.Add=function(t){if(t.N!=this.N||t.M!=this.M)throw"dimension mismatch";for(var e=0;e<this.M;e++)this[e].Sub(t[e]);return this};Matrix.prototype.add=function(t){if(t.N!=this.N||t.M!=this.M)throw"dimension mismatch";var e=new Matrix;for(var r=0;r<this.M;r++)e.push(this[r].add(t[r]));e.M=t.M;e.N=t.N;return e};Matrix.prototype.max=function(){var t=-Infinity;for(var e=0;e<this.M;e++)t=Math.max(t,this[e].max());return t};Matrix.prototype.combine=function(t,e){if(t.N!=this.N||t.M!=this.M)throw"dimension mismatch";var r=Matrix.construct(this.M,this.N);for(var i=0;i<r.M;i++)for(var n=0;n<r.N;n++)r[i][n]=e(this[i][n],t[i][n]);return r};Matrix.prototype.map=function(t){var e=Matrix.construct(this.M,this.N);for(var r=0;r<e.M;r++)for(var i=0;i<e.N;i++)e[r][i]=t(this[r][i]);return e};Matrix.prototype.min=function(){var t=Infinity;for(var e=0;e<this.M;e++)t=Math.min(t,this[e].min());return t};Point=function(t,e){if(arguments.length==0){this.x=0;this.y=0}else if(arguments.length==1){this.x=t.x;this.y=t.y}else{this.x=t;this.y=e}};Point.norm_angle=function(t){t=t%(Math.PI*2);if(t<-Math.PI)t+=Math.PI*2;else if(t>Math.PI)t-=Math.PI*2;return t};Point.prototype.rotate=function(t){return new Point(this.x*Math.cos(t)-this.y*Math.sin(t),this.y*Math.cos(t)+this.x*Math.sin(t))};Point.prototype.Rotate=function(t){return this.Set(this.x*Math.cos(t)-this.y*Math.sin(t),this.y*Math.cos(t)+this.x*Math.sin(t))};Point.prototype.Set=function(t,e){if(arguments.length==1){this.x=t.x;this.y=t.y}else{this.x=t;this.y=e}return this};Point.prototype.dist=function(t){var e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)};Point.prototype.dist2=function(t){var e=this.x-t.x,r=this.y-t.y;return e*e+r*r};Point.len=function(t,e){return Math.sqrt(t*t+e*e)};Point.prototype.len=function(){return Math.sqrt(this.x*this.x+this.y*this.y)};Point.prototype.len2=function(){return this.x*this.x+this.y*this.y};Point.prototype.add=function(t){return new Point(this.x+t.x,this.y+t.y)};Point.prototype.Add=function(t){this.x+=t.x;this.y+=t.y;return this};Point.prototype.sub=function(t){return new Point(this.x-t.x,this.y-t.y)};Point.prototype.Sub=function(t){this.x-=t.x;this.y-=t.y;return this};Point.prototype.mul=function(t){return this.x*t.x+this.y*t.y};Point.prototype.cross=function(t){return this.x*t.y-this.y*t.x};Point.prototype.equals=function(t,e){return Math.abs(this.x-t.x)<=e&&Math.abs(this.y-t.y)<=e};Point.prototype.normalize=function(){var t=1/this.len();return new Point(this.x*t,this.y*t)};Point.prototype.Normalize=function(){var t=1/this.len();this.x*=t;this.y*=t;return this};Point.prototype.get_perpendicular=function(){return new Point(-this.y,this.x)};Point.prototype.scale=function(t){return new Point(this.x*t,this.y*t)};Point.prototype.Scale=function(t){this.x*=t;this.y*=t;return this};Point.prototype.toString=function(){return"("+this.x+","+this.y+")"};Point.prototype.copy=function(){return new Point(this.x,this.y)};Point.EPS=1e-6;Point.get_closest_point_on_segment=function(t,e,r){var i=e.sub(t),n=i.len();if(n<Point.EPS)return t;var s=i.mul(r.sub(t))/n;if(s<0)return t;if(s>i.len())return e;return t.add(i.scale(s/n))};Point.intersect_ray_with_segment=function(t,e,r,i,n,s){if(typeof n=="undefined")var n=new Point;if(typeof s=="undefined")var s=Point.EPS;var a=i.sub(r);var o=e.cross(a);if(Math.abs(o)>Point.EPS){var u=r.sub(t).cross(e)/o;if(u<-s||u-1>s)return false;var h=r.add(a.scale(u));n.x=h.x;n.y=h.y}else{if(e.len2()<Point.EPS||a.len2()<Point.EPS)return false;var l=t.sub(r).mul(a)/a.mul(a);if(r.add(a.scale(l)).dist(t)>Point.EPS)return false;if(l<-s){n.x=r.x;n.y=r.y}else if(l-1>s){n.x=i.x;n.y=i.y}else{n.x=t.x;n.y=t.y}}if(n.sub(t).mul(e)>=0)return true;else return false};Point.intersect_inner_ray_with_rect=function(t,e,r){var i=new Point(r.x,r.y),n=new Point(r.x+r.width,r.y),s=new Point(r.x,r.y+r.height),a=new Point(r.x+r.width,r.y+r.height),o=r.r;var u=new Point,h,l;if(Point.intersect_ray_with_segment(t,e,i,s,u)){l=new Point(0,1)}else if(Point.intersect_ray_with_segment(t,e,n,a,u)){l=new Point(0,-1)}else if(Point.intersect_ray_with_segment(t,e,i,n,u)){l=new Point(-1,0)}else if(Point.intersect_ray_with_segment(t,e,s,a,u)){l=new Point(1,0)}else return null;if(o===0)return{point:u,tangent:l};var v;if(u.x<i.x+o&&u.y<i.y+o){v=new Circle(i.x+o,i.y+o,o).intersect_with_ray(t,e);u=v[1]||v[0]||u;l=u.sub(new Point(i.x+o,i.y+o)).get_perpendicular().scale(-1).Normalize()}else if(u.x>n.x-o&&u.y<n.y+o){v=new Circle(n.x-o,n.y+o,o).intersect_with_ray(t,e);u=v[1]||v[0]||u;l=u.sub(new Point(n.x-o,n.y+o)).get_perpendicular().scale(-1).Normalize()}else if(u.x<s.x+o&&u.y>s.y-o){v=new Circle(s.x+o,s.y-o,o).intersect_with_ray(t,e);u=v[1]||v[0]||u;l=u.sub(new Point(s.x+o,s.y-o)).get_perpendicular().scale(-1).Normalize()}else if(u.x>a.x-o&&u.y>a.y-o){v=new Circle(a.x-o,a.y-o,o).intersect_with_ray(t,e);u=v[1]||v[0]||u;l=u.sub(new Point(a.x-o,a.y-o)).get_perpendicular().scale(-1).Normalize()}return{point:u,tangent:l}};Point.prototype.is_inside_rect=function(t,e){return t.x<=this.x&&this.x<=e.x&&t.y>=this.y&&this.y>=e.y};Point.intersect_seg_with_rect=function(t,e,r,i){var n=new Point(r.x,r.y);var s=new Point(i.x,r.y);var a=new Point(r.x,i.y);var o=new Point(i.x,i.y);var u=[n,s,o,a];if(t.is_inside_rect(r,i)&&e.is_inside_rect(r,i)){return true}else{for(var h=0;h<u.length;h++){var l=h+1;if(l>u.length-1){l=0}if(Point.intersect_segments(t,e,u[h],u[l])){return true}}}return false};Point.intersect_segments=function(t,e,r,i){if(t.x==r.x&&t.y==r.y&&e.x==i.x&&e.y==i.y){return true}else if(t.x==i.x&&t.y==i.y&&e.x==r.x&&e.y==r.y){return true}var n=((r.y-i.y)*(t.x-r.x)+(i.x-r.x)*(t.y-r.y))/((i.x-r.x)*(t.y-e.y)-(t.x-e.x)*(i.y-r.y));var s=((t.y-e.y)*(t.x-r.x)+(e.x-t.x)*(t.y-r.y))/((i.x-r.x)*(t.y-e.y)-(t.x-e.x)*(i.y-r.y));if(n>=0&&n<=1&&s>=0&&s<=1){return true}return false};if(typeof exports!="undefined"){exports.Point=Point}if(!SVGPathElement.prototype.getPathData||!SVGPathElement.prototype.setPathData){(function(){var t={Z:"Z",M:"M",L:"L",C:"C",Q:"Q",A:"A",H:"H",V:"V",S:"S",T:"T",z:"Z",m:"m",l:"l",c:"c",q:"q",a:"a",h:"h",v:"v",s:"s",t:"t"};var e=function(t){this._string=t;this._currentIndex=0;this._endIndex=this._string.length;this._prevCommand=null;this._skipOptionalSpaces()};var r=window.navigator.userAgent.indexOf("MSIE ")!==-1;e.prototype={parseSegment:function(){var e=this._string[this._currentIndex];var r=t[e]?t[e]:null;if(r===null){if(this._prevCommand===null){return null}if((e==="+"||e==="-"||e==="."||e>="0"&&e<="9")&&this._prevCommand!=="Z"){if(this._prevCommand==="M"){r="L"}else if(this._prevCommand==="m"){r="l"}else{r=this._prevCommand}}else{r=null}if(r===null){return null}}else{this._currentIndex+=1}this._prevCommand=r;var i=null;var n=r.toUpperCase();if(n==="H"||n==="V"){i=[this._parseNumber()]}else if(n==="M"||n==="L"||n==="T"){i=[this._parseNumber(),this._parseNumber()]}else if(n==="S"||n==="Q"){i=[this._parseNumber(),this._parseNumber(),this._parseNumber(),this._parseNumber()]}else if(n==="C"){i=[this._parseNumber(),this._parseNumber(),this._parseNumber(),this._parseNumber(),this._parseNumber(),this._parseNumber()]}else if(n==="A"){i=[this._parseNumber(),this._parseNumber(),this._parseNumber(),this._parseArcFlag(),this._parseArcFlag(),this._parseNumber(),this._parseNumber()]}else if(n==="Z"){this._skipOptionalSpaces();i=[]}if(i===null||i.indexOf(null)>=0){return null}else{return{type:r,values:i}}},hasMoreData:function(){return this._currentIndex<this._endIndex},peekSegmentType:function(){var e=this._string[this._currentIndex];return t[e]?t[e]:null},initialCommandIsMoveTo:function(){if(!this.hasMoreData()){return true}var t=this.peekSegmentType();return t==="M"||t==="m"},_isCurrentSpace:function(){var t=this._string[this._currentIndex];return t<=" "&&(t===" "||t==="\n"||t==="\t"||t==="\r"||t==="\f")},_skipOptionalSpaces:function(){while(this._currentIndex<this._endIndex&&this._isCurrentSpace()){this._currentIndex+=1}return this._currentIndex<this._endIndex},_skipOptionalSpacesOrDelimiter:function(){if(this._currentIndex<this._endIndex&&!this._isCurrentSpace()&&this._string[this._currentIndex]!==","){return false}if(this._skipOptionalSpaces()){if(this._currentIndex<this._endIndex&&this._string[this._currentIndex]===","){this._currentIndex+=1;this._skipOptionalSpaces()}}return this._currentIndex<this._endIndex},_parseNumber:function(){var t=0;var e=0;var r=1;var i=0;var n=1;var s=1;var a=this._currentIndex;this._skipOptionalSpaces();if(this._currentIndex<this._endIndex&&this._string[this._currentIndex]==="+"){this._currentIndex+=1}else if(this._currentIndex<this._endIndex&&this._string[this._currentIndex]==="-"){this._currentIndex+=1;n=-1}if(this._currentIndex===this._endIndex||(this._string[this._currentIndex]<"0"||this._string[this._currentIndex]>"9")&&this._string[this._currentIndex]!=="."){return null}var o=this._currentIndex;while(this._currentIndex<this._endIndex&&this._string[this._currentIndex]>="0"&&this._string[this._currentIndex]<="9"){this._currentIndex+=1}if(this._currentIndex!==o){var u=this._currentIndex-1;var h=1;while(u>=o){e+=h*(this._string[u]-"0");u-=1;h*=10}}if(this._currentIndex<this._endIndex&&this._string[this._currentIndex]==="."){this._currentIndex+=1;if(this._currentIndex>=this._endIndex||this._string[this._currentIndex]<"0"||this._string[this._currentIndex]>"9"){return null}while(this._currentIndex<this._endIndex&&this._string[this._currentIndex]>="0"&&this._string[this._currentIndex]<="9"){r*=10;i+=(this._string.charAt(this._currentIndex)-"0")/r;this._currentIndex+=1}}if(this._currentIndex!==a&&this._currentIndex+1<this._endIndex&&(this._string[this._currentIndex]==="e"||this._string[this._currentIndex]==="E")&&(this._string[this._currentIndex+1]!=="x"&&this._string[this._currentIndex+1]!=="m")){this._currentIndex+=1;if(this._string[this._currentIndex]==="+"){this._currentIndex+=1}else if(this._string[this._currentIndex]==="-"){this._currentIndex+=1;s=-1}if(this._currentIndex>=this._endIndex||this._string[this._currentIndex]<"0"||this._string[this._currentIndex]>"9"){return null}while(this._currentIndex<this._endIndex&&this._string[this._currentIndex]>="0"&&this._string[this._currentIndex]<="9"){t*=10;t+=this._string[this._currentIndex]-"0";this._currentIndex+=1}}var l=e+i;l*=n;if(t){l*=Math.pow(10,s*t)}if(a===this._currentIndex){return null}this._skipOptionalSpacesOrDelimiter();return l},_parseArcFlag:function(){if(this._currentIndex>=this._endIndex){return null}var t=null;var e=this._string[this._currentIndex];this._currentIndex+=1;if(e==="0"){t=0}else if(e==="1"){t=1}else{return null}this._skipOptionalSpacesOrDelimiter();return t}};var i=function(t){if(!t||t.length===0)return[];var r=new e(t);var i=[];if(r.initialCommandIsMoveTo()){while(r.hasMoreData()){var n=r.parseSegment();if(n===null){break}else{i.push(n)}}}return i};var n=SVGPathElement.prototype.setAttribute;var s=SVGPathElement.prototype.removeAttribute;var a=window.Symbol?Symbol():"__cachedPathData";var o=window.Symbol?Symbol():"__cachedNormalizedPathData";var u=function(t,e,r,i,n,s,a,o,h,l){var v=function(t){return Math.PI*t/180};var p=function(t,e,r){var i=t*Math.cos(r)-e*Math.sin(r);var n=t*Math.sin(r)+e*Math.cos(r);return{x:i,y:n}};var f=v(a);var c=[];var y,d,x,_;if(l){y=l[0];d=l[1];x=l[2];_=l[3]}else{var g=p(t,e,-f);t=g.x;e=g.y;var m=p(r,i,-f);r=m.x;i=m.y;var b=(t-r)/2;var M=(e-i)/2;var P=b*b/(n*n)+M*M/(s*s);if(P>1){P=Math.sqrt(P);n=P*n;s=P*s}var w;if(o===h){w=-1}else{w=1}var S=n*n;var I=s*s;var N=S*I-S*M*M-I*b*b;var V=S*M*M+I*b*b;var A=w*Math.sqrt(Math.abs(N/V));x=A*n*M/s+(t+r)/2;_=A*-s*b/n+(e+i)/2;y=Math.asin(parseFloat(((e-_)/s).toFixed(9)));d=Math.asin(parseFloat(((i-_)/s).toFixed(9)));if(t<x){y=Math.PI-y}if(r<x){d=Math.PI-d}if(y<0){y=Math.PI*2+y}if(d<0){d=Math.PI*2+d}if(h&&y>d){y=y-Math.PI*2}if(!h&&d>y){d=d-Math.PI*2}}var C=d-y;if(Math.abs(C)>Math.PI*120/180){var k=d;var z=r;var E=i;if(h&&d>y){d=y+Math.PI*120/180*1}else{d=y+Math.PI*120/180*-1}r=x+n*Math.cos(d);i=_+s*Math.sin(d);c=u(r,i,z,E,n,s,a,0,h,[d,k,x,_])}C=d-y;var q=Math.cos(y);var O=Math.sin(y);var D=Math.cos(d);var G=Math.sin(d);var B=Math.tan(C/4);var T=4/3*n*B;var j=4/3*s*B;var L=[t,e];var R=[t+T*O,e-j*q];var Z=[r+T*G,i-j*D];var H=[r,i];R[0]=2*L[0]-R[0];R[1]=2*L[1]-R[1];if(l){return[R,Z,H].concat(c)}else{c=[R,Z,H].concat(c).join().split(",");var F=[];var Q=[];c.forEach(function(t,e){if(e%2){Q.push(p(c[e-1],c[e],f).y)}else{Q.push(p(c[e],c[e+1],f).x)}if(Q.length===6){F.push(Q);Q=[]}});return F}};var h=function(t){return t.map(function(t){return{type:t.type,values:Array.prototype.slice.call(t.values)}})};var l=function(t){var e=[];var r=null;var i=null;var n=null;var s=null;t.forEach(function(t){var a=t.type;if(a==="M"){var o=t.values[0];var u=t.values[1];e.push({type:"M",values:[o,u]});n=o;s=u;r=o;i=u}else if(a==="m"){var o=r+t.values[0];var u=i+t.values[1];e.push({type:"M",values:[o,u]});n=o;s=u;r=o;i=u}else if(a==="L"){var o=t.values[0];var u=t.values[1];e.push({type:"L",values:[o,u]});r=o;i=u}else if(a==="l"){var o=r+t.values[0];var u=i+t.values[1];e.push({type:"L",values:[o,u]});r=o;i=u}else if(a==="C"){var h=t.values[0];var l=t.values[1];var v=t.values[2];var p=t.values[3];var o=t.values[4];var u=t.values[5];e.push({type:"C",values:[h,l,v,p,o,u]});r=o;i=u}else if(a==="c"){var h=r+t.values[0];var l=i+t.values[1];var v=r+t.values[2];var p=i+t.values[3];var o=r+t.values[4];var u=i+t.values[5];e.push({type:"C",values:[h,l,v,p,o,u]});r=o;i=u}else if(a==="Q"){var h=t.values[0];var l=t.values[1];var o=t.values[2];var u=t.values[3];e.push({type:"Q",values:[h,l,o,u]});r=o;i=u}else if(a==="q"){var h=r+t.values[0];var l=i+t.values[1];var o=r+t.values[2];var u=i+t.values[3];e.push({type:"Q",values:[h,l,o,u]});r=o;i=u}else if(a==="A"){var o=t.values[5];var u=t.values[6];e.push({type:"A",values:[t.values[0],t.values[1],t.values[2],t.values[3],t.values[4],o,u]});r=o;i=u}else if(a==="a"){var o=r+t.values[5];var u=i+t.values[6];e.push({type:"A",values:[t.values[0],t.values[1],t.values[2],t.values[3],t.values[4],o,u]});r=o;i=u}else if(a==="H"){var o=t.values[0];e.push({type:"H",values:[o]});r=o}else if(a==="h"){var o=r+t.values[0];e.push({type:"H",values:[o]});r=o}else if(a==="V"){var u=t.values[0];e.push({type:"V",values:[u]});i=u}else if(a==="v"){var u=i+t.values[0];e.push({type:"V",values:[u]});i=u}else if(a==="S"){var v=t.values[0];var p=t.values[1];var o=t.values[2];var u=t.values[3];e.push({type:"S",values:[v,p,o,u]});r=o;i=u}else if(a==="s"){var v=r+t.values[0];var p=i+t.values[1];var o=r+t.values[2];var u=i+t.values[3];e.push({type:"S",values:[v,p,o,u]});r=o;i=u}else if(a==="T"){var o=t.values[0];var u=t.values[1];e.push({type:"T",values:[o,u]});r=o;i=u}else if(a==="t"){var o=r+t.values[0];var u=i+t.values[1];e.push({type:"T",values:[o,u]});r=o;i=u}else if(a==="Z"||a==="z"){e.push({type:"Z",values:[]});r=n;i=s}});return e};var v=function(t){var e=[];var r=null;var i=null;var n=null;var s=null;var a=null;var o=null;var h=null;t.forEach(function(t){if(t.type==="M"){var l=t.values[0];var v=t.values[1];e.push({type:"M",values:[l,v]});o=l;h=v;s=l;a=v}else if(t.type==="C"){var p=t.values[0];var f=t.values[1];var c=t.values[2];var y=t.values[3];var l=t.values[4];var v=t.values[5];e.push({type:"C",values:[p,f,c,y,l,v]});i=c;n=y;s=l;a=v}else if(t.type==="L"){var l=t.values[0];var v=t.values[1];e.push({type:"L",values:[l,v]});s=l;a=v}else if(t.type==="H"){var l=t.values[0];e.push({type:"L",values:[l,a]});s=l}else if(t.type==="V"){var v=t.values[0];e.push({type:"L",values:[s,v]});a=v}else if(t.type==="S"){var c=t.values[0];var y=t.values[1];var l=t.values[2];var v=t.values[3];var d,x;if(r==="C"||r==="S"){d=s+(s-i);x=a+(a-n)}else{d=s;x=a}e.push({type:"C",values:[d,x,c,y,l,v]});i=c;n=y;s=l;a=v}else if(t.type==="T"){var l=t.values[0];var v=t.values[1];var p,f;if(r==="Q"||r==="T"){p=s+(s-i);f=a+(a-n)}else{p=s;f=a}var d=s+2*(p-s)/3;var x=a+2*(f-a)/3;var _=l+2*(p-l)/3;var g=v+2*(f-v)/3;e.push({type:"C",values:[d,x,_,g,l,v]});i=p;n=f;s=l;a=v}else if(t.type==="Q"){var p=t.values[0];var f=t.values[1];var l=t.values[2];var v=t.values[3];var d=s+2*(p-s)/3;var x=a+2*(f-a)/3;var _=l+2*(p-l)/3;var g=v+2*(f-v)/3;e.push({type:"C",values:[d,x,_,g,l,v]});i=p;n=f;s=l;a=v}else if(t.type==="A"){var m=t.values[0];var b=t.values[1];var M=t.values[2];var P=t.values[3];var w=t.values[4];var l=t.values[5];var v=t.values[6];if(m===0||b===0){e.push({type:"C",values:[s,a,l,v,l,v]});s=l;a=v}else{if(s!==l||a!==v){var S=u(s,a,l,v,m,b,M,P,w);S.forEach(function(t){e.push({type:"C",values:t});s=l;a=v})}}}else if(t.type==="Z"){e.push(t);s=o;a=h}r=t.type});return e};SVGPathElement.prototype.setAttribute=function(t,e){if(t==="d"){this[a]=null;this[o]=null}n.call(this,t,e)};SVGPathElement.prototype.removeAttribute=function(t,e){if(t==="d"){this[a]=null;this[o]=null}s.call(this,t)};SVGPathElement.prototype.getPathData=function(t){if(t&&t.normalize){if(this[o]){return h(this[o])}else{var e;if(this[a]){e=h(this[a])}else{e=i(this.getAttribute("d")||"");this[a]=h(e)}var r=v(l(e));this[o]=h(r);return r}}else{if(this[a]){return h(this[a])}else{var e=i(this.getAttribute("d")||"");this[a]=h(e);return e}}};SVGPathElement.prototype.setPathData=function(t){if(t.length===0){if(r){this.setAttribute("d","")}else{this.removeAttribute("d")}}else{var e="";for(var i=0,n=t.length;i<n;i+=1){var s=t[i];if(i>0){e+=" "}e+=s.type;if(s.values&&s.values.length>0){e+=" "+s.values.join(" ")}}this.setAttribute("d",e)}};SVGRectElement.prototype.getPathData=function(t){var e=this.x.baseVal.value;var r=this.y.baseVal.value;var i=this.width.baseVal.value;var n=this.height.baseVal.value;var s=this.hasAttribute("rx")?this.rx.baseVal.value:this.ry.baseVal.value;var a=this.hasAttribute("ry")?this.ry.baseVal.value:this.rx.baseVal.value;if(s>i/2){s=i/2}if(a>n/2){a=n/2}var o=[{type:"M",values:[e+s,r]},{type:"H",values:[e+i-s]},{type:"A",values:[s,a,0,0,1,e+i,r+a]},{type:"V",values:[r+n-a]},{type:"A",values:[s,a,0,0,1,e+i-s,r+n]},{type:"H",values:[e+s]},{type:"A",values:[s,a,0,0,1,e,r+n-a]},{type:"V",values:[r+a]},{type:"A",values:[s,a,0,0,1,e+s,r]},{type:"Z",values:[]}];o=o.filter(function(t){return t.type==="A"&&(t.values[0]===0||t.values[1]===0)?false:true});if(t&&t.normalize===true){o=v(o)}return o};SVGCircleElement.prototype.getPathData=function(t){var e=this.cx.baseVal.value;var r=this.cy.baseVal.value;var i=this.r.baseVal.value;var n=[{type:"M",values:[e+i,r]},{type:"A",values:[i,i,0,0,1,e,r+i]},{type:"A",values:[i,i,0,0,1,e-i,r]},{type:"A",values:[i,i,0,0,1,e,r-i]},{type:"A",values:[i,i,0,0,1,e+i,r]},{type:"Z",values:[]}];if(t&&t.normalize===true){n=v(n)}return n};SVGEllipseElement.prototype.getPathData=function(t){var e=this.cx.baseVal.value;var r=this.cy.baseVal.value;var i=this.rx.baseVal.value;var n=this.ry.baseVal.value;var s=[{type:"M",values:[e+i,r]},{type:"A",values:[i,n,0,0,1,e,r+n]},{type:"A",values:[i,n,0,0,1,e-i,r]},{type:"A",values:[i,n,0,0,1,e,r-n]},{type:"A",values:[i,n,0,0,1,e+i,r]},{type:"Z",values:[]}];if(t&&t.normalize===true){s=v(s)}return s};SVGLineElement.prototype.getPathData=function(){return[{type:"M",values:[this.x1.baseVal.value,this.y1.baseVal.value]},{type:"L",values:[this.x2.baseVal.value,this.y2.baseVal.value]}]};SVGPolylineElement.prototype.getPathData=function(){var t=[];for(var e=0;e<this.points.numberOfItems;e+=1){var r=this.points.getItem(e);t.push({type:e===0?"M":"L",values:[r.x,r.y]})}return t};SVGPolygonElement.prototype.getPathData=function(){var t=[];for(var e=0;e<this.points.numberOfItems;e+=1){var r=this.points.getItem(e);t.push({type:e===0?"M":"L",values:[r.x,r.y]})}t.push({type:"Z",values:[]});return t}})()}Array.remove=function(t,e,r){var i=t.slice((r||e)+1||t.length);t.length=Math.max(0,e<0?t.length+e:e);for(var n=0;n<i.length;n++)t.push(i[n]);return t.length};Polygon=function(t){this.pts=[];this.closed=true;this.max_error=.2;if(t)this.add_points(t)};Polygon.prototype.copy=function(){var t=new Polygon;t.closed=this.closed;t.max_error=this.max_error;for(var e=0;e<this.pts.length;e++)t.pts.push(this.pts[e].copy());return t};Polygon.prototype.move_to_origin=function(){var t=this.pts.length;var e=this.centroid();for(var r=0;r<t;r++)this.pts[r].Sub(e)};Polygon.prototype.push=function(t){this.pts.push(t);return this.pts};Polygon.prototype.add_points=function(t){for(var e=0;e<t.length;++e)this.pts.push(new Point(t[e][0],t[e][1]))};Polygon.prototype.back=function(){return this.pts[this.pts.length-1]};Polygon.prototype.order_vertices=function(){if(this.area()<0)this.pts.reverse()};Polygon.prototype.bounding_box=function(){var t=this.pts[0].x,e=t,r=this.pts[0].y,i=r;for(var n=1;n<this.pts.length;n++){t=Math.min(t,this.pts[n].x);e=Math.max(e,this.pts[n].x);r=Math.min(r,this.pts[n].y);i=Math.max(i,this.pts[n].y)}return{x:t,y:r,width:e-t,height:i-r}};Polygon.prototype.area=function(){var t=0;var e=this.back();for(var r=0;r<this.pts.length;r++){t+=e.cross(this.pts[r]);e=this.pts[r]}return t*.5};Polygon.prototype.centroid=function(){var t=new Point(0,0);var e=this.pts.length;if(e===0)return t;var r=this.area();if(this.closed&&Math.abs(r)>=Point.EPS){var i=this.back();for(var n=0;n<e;++n){t=t.add(i.add(this.pts[n]).scale(i.cross(this.pts[n])));i=this.pts[n]}t=t.scale(1/(6*r))}else{var s=this.bounding_box();return new Point(s.x+s.width/2,s.y+s.height/2)}return t};Polygon.prototype.get_edge_lengths=function(t){var e=[],r=this.pts.length;for(var i=0;i<r-1;++i)e.push(this.pts[i].dist(this.pts[i+1]));if(this.closed&&r>1)e.push(this.pts[0].dist(this.pts[r-1]));if(t)e.sort(function(t,e){return t-e});return e};Polygon.prototype.is_convex=function(t){var e=this.pts.length;return this.pts[t].sub(this.pts[(t+e-1)%e]).cross(this.pts[(t+1)%e].sub(this.pts[t]))>=0};Polygon.prototype.find_notch=function(){var t=this.pts.length;for(var e=0;e<t;++e)if(!this.is_convex(e))return e;return t};Polygon.prototype.find_intersection=function(t,e,r,i,n){var s=this.pts.length;var a=s;if(typeof i=="undefined")var i=s;if(typeof n=="undefined")var n=s;if(typeof r=="undefined")var r=new Point;for(var o=0;o<s;++o){if(i<s&&(o==i||(s+o+1-i)%s==0))continue;if(n<s&&(o==n||(s+o+1-n)%s==0))continue;var u=new Point;if(Point.intersect_ray_with_segment(t,e,this.pts[(o+1)%s],this.pts[o],u)){if(a==s||u.dist2(t)<r.dist2(t)){r.x=u.x;r.y=u.y;a=o}}}return a};Polygon.prototype.is_visible=function(t,e){var r=this.pts.length;if(t==e||t==(e+1)%r||e==(t+1)%r)return true;var i=this.pts[(r+t-1)%r],n=this.pts[(t+1)%r];var s=this.pts[t].sub(i).cross(this.pts[e].sub(this.pts[t]))>=0;var a=n.sub(this.pts[t]).cross(this.pts[e].sub(n))>=0;if(this.is_convex(t)){if(!(s&&a))return false}else{if(!(s||a))return false}var o=new Point;var u=this.find_intersection(this.pts[t],this.pts[e].sub(this.pts[t]),o,t,e);if(u==r)return true;return o.dist2(this.pts[t])>this.pts[e].dist2(this.pts[t])};Polygon.prototype.split_at=function(t,e){var r=[new Polygon,new Polygon];var i=this.pts.length;for(var n=t;;++n){if(n==i)n=0;r[0].pts.push(this.pts[n].copy());if(n==e)break}for(var n=e;;++n){if(n==i)n=0;r[1].pts.push(this.pts[n].copy());if(n==t)break}return r};Polygon.prototype.split=function(t){if(t<3)return[];var e=this.pts.length;if(e<=t){return[this]}var r=this.find_biggest_angle();var i=(r+Math.round(e/2))%e;var n=this.split_at(r,i);var s=n[0].split(t);return s.concat(n[1].split(t))};Polygon.prototype.angle=function(t){var e=this.pts.length;var r=this.pts[(e+t-1)%e],i=this.pts[t],n=this.pts[(t+1)%e];var s=Math.acos(r.sub(i).normalize().mul(n.sub(i).normalize()));if(!this.is_convex(t))return 2*Math.PI-s;else return s};Polygon.prototype.find_biggest_angle=function(){var t=this.pts.length;var e;var r=t;for(var i=0;i<t;++i){var n=this.angle(i);if(r==t||e<n){r=i;e=n}}return r};Polygon.prototype.merge_vertices=function(t){if(t.min_dist==undefined)t.min_dist=Point.EPS;if(t.min_vertex_count==undefined)t.min_vertex_count=3;if(t.min_vertex_count<1)t.min_vertex_count=1;for(;;){var e=false;var r=this.pts.length;if(r<=t.min_vertex_count)return;var i=[];if(this.pts[0].dist(this.back())<t.min_dist){i.push(this.back().add(this.pts[0]).scale(.5));r-=1;e=true}else{i.push(this.pts[0])}for(var n=1;n<r;++n){if(i[i.length-1].dist(this.pts[n])<t.min_dist){i[i.length-1]=i[i.length-1].add(this.pts[n]).scale(.5);e=true}else i.push(this.pts[n])}if(e)this.pts=i;else return}};Polygon.prototype.remove_superfical_vertices=function(t){if(t.max_error==undefined)t.max_error=Point.EPS;if(t.min_vertex_count==undefined)t.min_vertex_count=3;if(t.min_vertex_count<3)t.min_vertex_count=3;for(;;){var e=false;var r=this.pts.length;var i=r-1;for(;;){if(r<=t.min_vertex_count)return;var n=this.pts[(r+i-1)%r],s=this.pts[(i+1)%r];var a=s.sub(n),o=a.normalize();var u=a.mul(this.pts[i].sub(n));var h;if(u>=0&&u<a.mul(a)){var l=n.add(o.scale(this.pts[i].sub(n).mul(o)));h=this.pts[i].dist(l)}else{if(u<0)h=this.pts[i].dist(n);else h=this.pts[i].dist(s)}if(h<=t.max_error){Array.remove(this.pts,i);r-=1;e=true}if(i==0)break;else i-=1}if(!e)return}};Polygon.prototype.toString=function(){var t=[];for(var e=0;e<this.pts.length;e++)t.push(this.pts[e].x+","+this.pts[e].y);return"("+t.join(" ")+")"};Polygon.prototype.contains_point=function(t){var e=t[0];var r=t[1];var i=this.pts;var n=false;for(var s=0,a=i.length-1;s<i.length;a=s++){var o=i[s].x;var u=i[s].y;var h=i[a].x;var l=i[a].y;var v=u>r!=l>r&&e<(h-o)*(r-u)/(l-u)+o;if(v)n=!n}return n};Polygon.prototype.intersects_with_rect=function(t,e){var r=this.pts;var i=this.bounding_box();if(t.x>i.x+i.width||i.x>e.x)return false;if(t.y<i.y||i.y+i.height<e.y)return false;for(var n=0;n<r.length;n++){if(r[n].is_inside_rect(t,e))return true}if(this.contains_point(t)||this.contains_point(e)||this.contains_point(new Point(e.x,t.y))||this.contains_point(new Point(t.x,e.y)))return true;var s=r.length;for(var n=0;n<s;n++){if(Point.intersect_seg_with_rect(r[n],r[(n+1)%s],t,e))return true}return false};if(typeof exports!="undefined"){exports.Polygon=Polygon}Polygon.fromSVGRect=function(t){var e=new Polygon;var r=Number(t.getAttribute("x")),i=Number(t.getAttribute("y")),n=Number(t.getAttribute("width")),s=Number(t.getAttribute("height"));e.add_points([[r,i],[r+n,i],[r+n,i+s],[r,i+s]]);e.order_vertices();return e};Polygon.fromSVGPath=function(t,e,r){var i=new Polygon;i.closed=false;i.max_error=e;var n=t.getPathData({normalize:true});var s=new Point(0,0),a=null;for(var o=0;o<n.length;++o){var u=n[o];if(u.type==="z"||u.type==="Z"){i.closed=true;break}else{switch(u.type){case"M":s=new Point(u.values[0],u.values[1]);a=s;i.push(s);break;case"m":s=s.add(new Point(u.values[0],u.values[1]));a=s;i.push(s);break;case"L":s=new Point(u.values[0],u.values[1]);a=s;i.push(s);break;case"l":s=s.add(new Point(u.values[0],u.values[1]));a=s;i.push(s);break;case"C":var h=u.values[0],l=u.values[1],v=u.values[2],p=u.values[3],f=u.values[4],c=u.values[5];a=new Point(v,p);s=i.sampleBezier(s,new Point(h,l),a,new Point(f,c));break;case"c":var h=u.values[0],l=u.values[1],v=u.values[2],p=u.values[3],f=u.values[4],c=u.values[5];a=new Point(v+s.x,p+s.y);s=i.sampleBezier(s,new Point(h+s.x,l+s.y),a,new Point(f+s.x,c+s.y));break;case"Q":var h=u.values[0],l=u.values[1],f=u.values[2],c=u.values[3];a=new Point(h,l);s=i.sampleBezier2(s,a,new Point(f,c));break;case"q":var h=u.values[0],l=u.values[1],f=u.values[2],c=u.values[3];a=new Point(h+s.x,l+s.y);s=i.sampleBezier2(s,a,new Point(f+s.x,c+s.y));break;case"A":throw"not implemented!";case"a":throw"not implemented!";case"H":s=new Point(u.values[0],s.y);a=s;i.push(s);break;case"h":s=new Point(u.values[0]+s.x,s.y);a=s;i.push(s);break;case"V":s=new Point(s.x,u.values[0]);a=s;i.push(s);break;case"v":s=new Point(s.x,u.values[0]+s.y);a=s;i.push(s);break;case"S":var v=u.values[0],p=u.values[1],f=u.values[2],c=u.values[3];var y=new Point(2*s.x-a.x,2*s.y-a.y);a=new Point(v,p);s=i.sampleBezier(s,y,a,new Point(f,c));break;case"s":var v=u.values[0],p=u.values[1],f=u.values[2],c=u.values[3];var y=new Point(2*s.x-a.x,2*s.y-a.y);a=new Point(v+s.x,p+s.y);s=i.sampleBezier(s,y,a,new Point(f+s.x,c+s.y));break;case"T":a=new Point(2*s.x-a.x,2*s.y-a.y);s=i.sampleBezier2(s,a,new Point(u.values[0],u.values[1]));break;case"t":a=new Point(2*s.x-a.x,2*s.y-a.y);s=i.sampleBezier2(s,a,new Point(u.values[0]+s.x,u.values[1]+s.y));break;default:unknown=true;break}}}if(i.pts.length>1&&i.pts[0].equals(i.pts[i.pts.length-1],.001))i.closed=true;if(r)i.remove_superfical_vertices({max_error:.001});i.order_vertices();return i};Polygon.prototype.sampleBezier=function(t,e,r,i){var n=1e-6
;if(this.max_error<=n)throw"max_error must be bigger than EPS";if(t.equals(e,n)&&t.equals(r,n)&&t.equals(i,n)){return}var s=0;var a=0;var o=i.sub(t);if(o.len()<n){o=e.dist(t)>e.dist(r)?e.sub(t):e.sub(r)}var u=new Point(-o.y,o.x);u.Normalize();var h=e.sub(t).mul(u);var l=r.sub(t).mul(u);var v=3*(h-l);var p=2*(l-2*h);var f=h;if(Math.abs(v)<n){s=.375*Math.abs(h+l);a=.5}else{var c=p*p-4*v*f;if(Math.abs(c)<n){var y=-.5*p/v;if(0<y&&y<1){s=3*Math.abs(h*(1-y)*(1-y)*y+l*(1-y)*y*y);a=y}}else if(c>0){var d=.5*(-p-Math.sqrt(c))/v;var x=.5*(-p+Math.sqrt(c))/v;var _=0,g=0;var m=0,b=0;if(0<d&&d<1){m=3*(h*(1-d)*(1-d)*d+l*(1-d)*d*d);_=Math.min(_,m);g=Math.max(g,m)}if(0<x&&x<1){b=3*(h*(1-x)*(1-x)*x+l*(1-x)*x*x);_=Math.min(_,b);g=Math.max(g,b)}s=g-_;a=Math.abs(m)>Math.abs(b)?d:x}else{}}if(s>this.max_error){var y=a;var M=t;var P=t.scale(1-y).add(e.scale(y));var w=t.scale((1-y)*(1-y)).add(e.scale(2*(1-y)*y)).add(r.scale(y*y));var S=t.scale((1-y)*(1-y)*(1-y)).add(e.scale(3*(1-y)*(1-y)*y)).add(r.scale(3*(1-y)*y*y)).add(i.scale(y*y*y));var I=S;var N=e.scale((1-y)*(1-y)).add(r.scale(2*(1-y)*y)).add(i.scale(y*y));var V=r.scale(1-y).add(i.scale(y));var A=i;this.sampleBezier(M,P,w,S);this.sampleBezier(I,N,V,A)}else{this.pts.push(i)}return i};Polygon.prototype.sampleBezier2=function(t,e,r){return this.sampleBezier(t,new Point((t.x+2*e.x)/3,(t.y+2*e.y)/3),new Point((2*e.x+r.x)/3,(2*e.y+r.y)/3),r)};Polygon.prototype.renderInSvg=function(t,e,r){if(arguments.length<3)var r=false;var i;if(this.closed)i=t.createElementNS("http://www.w3.org/2000/svg","polygon");else i=t.createElementNS("http://www.w3.org/2000/svg","polyline");var n=this.x||0,s=this.y||0;var a=[];for(var o=0;o<this.pts.length;++o)a.push(this.pts[o].x+n+","+(this.pts[o].y+s));i.setAttribute("points",a.join(" "));i.style.setProperty("stroke","red");i.style.setProperty("stroke-width",".5px");i.style.setProperty("fill","none");e.appendChild(i);if(r)for(var o=0;o<this.pts.length;++o){var u=r.cloneNode(true);u.setAttribute("cx",this.pts[o].x+n);u.setAttribute("cy",this.pts[o].y+s);e.appendChild(u)}return i};Polygon.prototype.renderOnCanvas=function(t,e,r){var i=this.x||0,n=this.y||0;t.beginPath();t.moveTo(this.pts[0].x+i,this.pts[0].y+n);for(var s=1;s<this.pts.length;s++)t.lineTo(this.pts[s].x+i,this.pts[s].y+n);if(this.closed)t.closePath();if(e)t.stroke();if(r)t.fill()};Polygon.prototype.convex_decomposition=function(t,e){if(typeof e=="undefined")e=0;var r=[];if(t.debug_text){console.log("convex_decomposition on depth "+e+" called for "+this)}if(t.preprocess&&e==0){if(t.pre_order_vertices)this.order_vertices();if(t.pre_merge_vertices_min_dist>0)this.merge_vertices({min_dist:t.pre_merge_vertices_min_dist});if(t.pre_remove_vertices_max_error>0)this.remove_superfical_vertices({max_error:t.pre_remove_vertices_max_error});if(t.debug_text)console.log("after preprocessing: "+this)}var i=this.pts;var n=i.length;if(n<3)return[];var s=this.find_notch();if(t.debug_text)console.log("notch_idx is "+s);if(s<n){var a=i[(n+s-1)%n],o=i[s],u=i[(s+1)%n];var h=[];h=this.cd_strategy_1(s,t);if(h.length==0)h=this.cd_strategy_2(s,t);r=r.concat(h[0].convex_decomposition(t,e+1));r=r.concat(h[1].convex_decomposition(t,e+1))}else{if(t.max_vertices<n){if(t.debug_text)console.log("Too much vertices, splitting...");r=r.concat(this.split(t.max_vertices))}else{r.push(this)}}return r};Polygon.prototype.cd_strategy_1=function(t,e){if(e.debug_text)console.log("Applying strategy 1 to notch at "+t+"...");var r=this.pts;var i=r.length;var n=r[(i+t-1)%i],s=r[t],a=r[(t+1)%i],o=s.sub(n),u=n.sub(s),h=s.sub(a),l=a.sub(s);var v=i;for(var p=0;p<i;++p){if(p==t||(i+p-t-1)%i==0||(i+p-t+1)%i==0)continue;var f=r[p].sub(s);if(u.cross(f)*l.cross(f)>=0){if(e.debug_text)console.log("==> "+p+" is outside of the angle");continue}var c=Math.acos(u.normalize().mul(f.normalize()));var y=Math.acos(l.normalize().mul(f.normalize()));if(Math.abs(c)<e.s1_min_angle||Math.abs(y)<e.s1_min_angle){if(e.debug_text)console.log("==> "+p+" has angle < "+e.s1_min_angle*180/Math.PI+" deg with edges of vertex "+t);continue}if(!this.is_visible(t,p)){if(e.debug_text)console.log("==> "+p+" is not visible from "+t);continue}if(v==i||r[p].dist2(s)<r[v].dist2(s)){if(e.debug_text)console.log("==> "+p+" is new nearest split point");v=p}}if(v<i){if(e.debug_text)console.log("Strategy1 ==> using vertex "+v+" as splitting point.");return this.split_at(t,v)}else return[]};Polygon.prototype.cd_strategy_2=function(t,e){if(e.debug_text)console.log("Applying strategy 2 to notch at "+t+"...");var r=this.pts;var i=r.length;var n=r[(i+t-1)%i],s=r[t],a=r[(t+1)%i],o=s.sub(n),u=n.sub(s),h=s.sub(a),l=a.sub(s);var v,p=new Point,f=new Point;var c,y=this.find_intersection(s,o,p,(i+t-1)%i,t),d=this.find_intersection(s,h,f,(t+1)%i,t);if(s.dist2(p)<=s.dist2(f)){v=p;c=y}else{v=f;c=d}if(e.debug_text)console.log("==> the closest intersection found is "+v);if(e.debug_text)console.log("==> the vertex in front of it has the index "+c);var x=new Polygon;for(var _=t;;++_){if(_==i)_=0;x.pts.push(r[_].copy());if(_==c)break}x.pts.push(v.copy());var g=new Polygon;for(var _=c+1;;++_){if(_==i)_=0;if(_==t)break;g.pts.push(r[_].copy())}g.pts.push(v.copy());return[x,g]};var SpatialRelationAnalyzer=function(t,e,r,i,n){var s={};var a=s.res=t;s.scale=e;var o=2*Math.PI/180;function u(t,e){if(t==0&&e==0)return 0;else return Math.acos(t/Math.sqrt(t*t+e*e))}function h(t,e){if(t==0&&e==0)return 0;else return Math.acos(-t/Math.sqrt(t*t+e*e))}function l(t,e){if(t==0&&e==0)return 0;else return Math.acos(-e/Math.sqrt(t*t+e*e))}function v(t,e){if(t==0&&e==0)return 0;else return Math.acos(e/Math.sqrt(t*t+e*e))}function p(t){var e=2*Math.abs(t)/Math.PI-1;return Math.max(0,-e*e*e)}function f(t,e){return Math.sqrt(t*t+e*e)}function c(t){return 1-1/(1+Math.exp(30*(.2-t/a/e)))}function y(t){return 1/(1+Math.exp(20*(.35-t/a/e)))}s.beta_fs={right:u,left:h,above:l,below:v,near:f,far:f};s.member_fs={right:p,left:p,above:p,below:p,near:c,far:y};s.f_beta=i||s.beta_fs[r];s.f_member=n||s.member_fs[r];s.getMembership=function(t,r,i){var n=s.getSpatialMembershipMap(r),a=s.getBodyMatrix(t),o=e*(t.x-r.x),u=e*(t.y-r.y);var h=M(a,n,o,u);if(i){var l=s.debug_get_canvas("debug");s.debug_draw_A_R(a,n,l,o,u)}return h};var d=function(t,e,r){return e<=t&&t<=r};s.getSpatialMembershipMap=function(t){if(!t._spatial_maps)t._spatial_maps={};var e=t._spatial_maps[r];if(!e||e.res!=a||!d(e.rot,t.rot-o,t.rot+o)){e=t._spatial_maps[r]={res:a,rot:t.rot,data:b(s.getBodyMatrix(t),s.f_beta,s.f_member)}}return e.data};s.getBodyMatrix=function(t){var e=t._body_matrix;if(!e||e.res!=a||!d(e.rot,t.rot-o,t.rot+o)){e=t._body_matrix={res:a,rot:t.rot,data:x(t)}}return e.data};var x=s.calcBodyMatrix=function(t){var r=a,i=a;var n=t.bounding_box();if(Math.ceil(n.width*e)%2!=a%2)r=a-1;if(Math.ceil(n.height*e)%2!=a%2)i=a-1;var s=document.createElement("canvas");s.width=r;s.height=i;var o=s.getContext("2d");o.clearRect(0,0,s.width,s.height);o.fillStyle="black";o.translate(r/2,i/2);o.scale(e,e);if(t.rot!=0)o.rotate(t.rot);o.translate(-t.x||0,-t.y||0);t.renderOnCanvas(o,false,true);var u=_(s);if(u.bounding_box.x0>u.bounding_box.x1)u[Math.floor(i/2)][Math.floor(r/2)]=1;return u};var _=function(t){var e=t.width,r=t.height;var i=t.getContext("2d").getImageData(0,0,e,r).data;var n=Matrix.construct(r,e,0);var s=e,a=0,o=r,u=0;for(var h=0;h<r;h++)for(var l=0;l<e;l++){if(i[4*(h*e+l)+3]>0){n[h][l]=1;s=Math.min(s,l);a=Math.max(a,l);o=Math.min(o,h);u=Math.max(u,h)}}n.bounding_box={x0:s,y0:o,x1:a,y1:u};return n};var g=s.calcSpatialMembershipMapAccurate=function(t,e,r){var i=t.N,n=t.M;var s=0,a=0,o=t.N-1,u=t.M-1;if(t.bounding_box){var h=t.bounding_box;s=h.x0;o=h.x1;a=h.y0;u=h.y1}var l=Matrix.construct(n,i,Infinity);for(var v=0;v<n;v++)for(var p=0;p<i;p++){if(t[v][p]==1)l[v][p]=NaN;else{for(var f=a;f<=u;f++)for(var c=s;c<=o;c++){if(t[f][c]!=1)continue;l[v][p]=Math.min(l[v][p],e(p-c,v-f))}}}l=l.map(r);return l};var m=s.calcSpatialMembershipMapFast=function(t,e,r){var i=t.N,n=t.M;var s=Matrix.construct(n,i);for(var a=0;a<n;a++)for(var o=0;o<i;o++){if(t[a][o]==1)s[a][o]=[a,o,0]}var u=function(t,r){var a=s[t][r];var o=function(i){var n=e(r-i[1],t-i[0]);if(a==0||n<=a[2]){a=[i[0],i[1],n]}};if(t>0){if(r>0&&s[t-1][r-1])o(s[t-1][r-1]);if(s[t-1][r])o(s[t-1][r]);if(r<i-1&&s[t-1][r+1])o(s[t-1][r+1])}if(t<n-1){if(r>0&&s[t+1][r-1])o(s[t+1][r-1]);if(s[t+1][r])o(s[t+1][r]);if(r<i-1&&s[t+1][r+1])o(s[t+1][r+1])}if(r>0&&s[t][r-1])o(s[t][r-1]);if(r<i-1&&s[t][r+1])o(s[t][r+1]);s[t][r]=a};for(var a=0;a<n;a++)for(var o=0;o<i;o++)u(a,o);for(var a=n-1;a>=0;a--)for(var o=i-1;o>=0;o--)u(a,o);for(var a=0;a<n;a++)for(var o=0;o<i;o++){if(t[a][o]==1)s[a][o]=NaN;else if(s[a][o])s[a][o]=r(s[a][o][2]);else s[a][o]=NaN}return s};var b=s.calcSpatialMembershipMapFaster=function(t,e,r){var i=t.N,n=t.N-1,s=t.M,a=t.M-1;var o=Matrix.construct(s,i,0);for(var u=0;u<s;u++)for(var h=0;h<i;h++){o[u][h]=t[u][h]==1?[u,h,0]:[-1,-1,Infinity]}var l=function(t,r,i,s){var u=o[t][r];var h=false;var l=function(i){if(i[0]==-1)return;var n=e(r-i[1],t-i[0]);if(n<u[2]){u[0]=i[0];u[1]=i[1];u[2]=n;h=true}};if(i&&r>0)l(o[t][r-1]);if(!i&&r<n)l(o[t][r+1]);if(s&&t>0)l(o[t-1][r]);if(!s&&t<a)l(o[t+1][r]);if(i&&s&&r>0&&t>0)l(o[t-1][r-1]);if(i&&!s&&r>0&&t<a)l(o[t+1][r-1]);if(!i&&s&&r<n&&t>0)l(o[t-1][r+1]);if(!i&&!s&&r<n&&t<a)l(o[t+1][r+1]);if(h)o[t][r]=u};var v=t.bounding_box?Math.max(0,t.bounding_box.y0):0;var p=t.bounding_box?Math.max(0,t.bounding_box.x0):0;var f=t.bounding_box?Math.max(0,t.bounding_box.y1):a;var c=t.bounding_box?Math.max(0,t.bounding_box.x1):n;for(var u=v;u<s;u++)for(var h=p;h<i;h++)l(u,h,true,true);for(var u=v;u<s;u++)for(var h=c;h>=0;h--)l(u,h,false,true);for(var u=f;u>=0;u--)for(var h=p;h<i;h++)l(u,h,true,false);for(var u=f;u>=0;u--)for(var h=c;h>=0;h--)l(u,h,false,false);for(var u=0;u<s;u++)for(var h=0;h<i;h++){if(t[u][h]==1)o[u][h]=NaN;else if(o[u][h][2]!=Infinity)o[u][h]=r(o[u][h][2]);else o[u][h]=NaN}return o};var M=s.calcObjectMembership=function(t,e,r,i){var n=null;var s=0;var a=null;var o=0,u=0,h=t.N,l=t.M;if(t.bounding_box){var v=t.bounding_box;o=v.x0;h=v.x1;u=v.y0;l=v.y1}var p=0;for(var f=u;f<=l;f++)for(var c=o;c<=h;c++){if(!t[f][c])continue;var y=Math.round(f+i),d=Math.round(c+r);if(y>=e.M||y<0)continue;if(d>=e.N||d<0)continue;var x=e[y][d];if(isNaN(x))continue;a=a===null?x:Math.min(a,x);s+=x;p++;n=n===null?x:Math.max(n,x)}s/=p;return[a,s,n]};s.debug_get_canvas=function(t,e,r,i){r=r||200;i=i||200;var n=d3.select("canvas#"+t);if(n.empty()){var s=d3.select("body").append("div").classed("debug-can-div",true).style("text-align","center");n=s.append("canvas").attr("id",t);if(e){s.append("br");s.append("span").text(e)}}n.attr("width",r).attr("height",i);n[0][0].getContext("2d").clearRect(0,0,r,i);return n[0][0]};s.debug_draw_matrix=function(t,e,r,i,n){r=r||"white";i=i||0;n=n||0;i=Math.round(i);n=Math.round(n);var s=t.N,a=t.M;var o=Math.min(e.width/s,e.height/a);var u=e.getContext("2d");for(var h=0;h<t.M;h++)for(var l=0;l<t.N;l++){var v;if(isNaN(t[h][l])){v="red";u.globalAlpha=1}else{v=r;u.globalAlpha=t[h][l]>0?t[h][l]:0}u.fillStyle=v;var p=Math.round((l+i)*o),f=Math.round((h+n)*o),c=Math.round((l+1+i)*o),y=Math.round((h+1+n)*o);u.fillRect(p,f,c-p,y-f)}};s.debug_draw_A_R=function(t,e,r,i,n){var a=r.getContext("2d");a.fillStyle="black";a.fillRect(0,0,r.width,r.height);if(e)s.debug_draw_matrix(e,r);if(t)s.debug_draw_matrix(t,r,"rgba(0,0,255,0.5)",i,n)};return s};SpatialRelationAnalyzer.conjunction=function(t,e){return t.combine(e,function(t,e){return Math.min(t,e)})};SpatialRelationAnalyzer.disjunction=function(t,e){return t.combine(e,function(t,e){return Math.max(t,e)})};SpatialRelationAnalyzer.subtract=function(t,e){return t.combine(e,function(t,e){return Math.max(0,t-e)})};function Vector(t){if(t instanceof Vector)return t.copy();var e=[];if(t&&t!=[])for(var r=0;r<t.length;r++)e.push(t[r]);e.__proto__=Vector.prototype;return e}Vector.prototype=new Array;Vector.prototype.has_index=function(t){return this.hasOwnProperty(t)};Vector.construct=function(t,e){if(!e)e=0;var r=new Vector;for(var i=0;i<t;i++)r.push(e);return r};Vector.random=function(t){var e=new Vector;for(var r=0;r<t;r++)e.push(Math.random());return e};Vector.prototype.copy=function(){var t=new Vector;for(var e=0;e<this.length;e++){t.push(this[e])}return t};Vector.prototype.add=function(t){if(this.length!=t.length)throw"dimension mismatch";var e=new Vector;for(var r=0;r<this.length;r++)e[r]=this[r]+t[r];return e};Vector.prototype.Add=function(t){if(this.length!=t.length)throw"dimension mismatch";for(var e=0;e<this.length;e++)this[e]+=t[e];return this};Vector.prototype.sub=function(t){if(this.length!=t.length)throw"dimension mismatch";var e=new Vector;for(var r=0;r<this.length;r++)e[r]=this[r]-t[r];return e};Vector.prototype.Sub=function(t){if(this.length!=t.length)throw"dimension mismatch";for(var e=0;e<this.length;e++)this[e]-=t[e];return this};Vector.prototype.len=function(){var t=0;for(var e=0;e<this.length;e++)t+=this[e]*this[e];return Math.sqrt(t)};Vector.prototype.len2=function(){var t=0;for(var e=0;e<this.length;e++)t+=this[e]*this[e];return t};Vector.prototype.scale=function(t){var e=new Vector;for(var r=0;r<this.length;r++)e.push(this[r]*t);return e};Vector.prototype.Scale=function(t){for(var e=0;e<this.length;e++)this[e]*=t;return this};Vector.prototype.normalize=function(){var t=1/this.len();return this.scale(t)};Vector.prototype.Normalize=function(){var t=1/this.len();return this.Scale(t)};Vector.prototype.mul=function(t){if(this.length!=t.length)throw"dimension mismatch";var e=0;for(var r=0;r<this.length;r++)e+=this[r]*t[r];return e};Vector.prototype.max=function(){var t=-Infinity;for(var e=0;e<this.length;e++)t=Math.max(t,this[e]);return t};Vector.prototype.min=function(){var t=Infinity;for(var e=0;e<this.length;e++)t=Math.min(t,this[e]);return t};if(typeof exports!="undefined"){exports.Vector=Vector}function SparseVector(t){var e=[];e.N=t||0;e.__proto__=SparseVector.prototype;e.el={};return e}SparseVector.prototype=new Array;SparseVector.prototype.has_index=function(t){return this.hasOwnProperty(t)&&t!="N"};SparseVector.prototype.copy=function(){var t=new SparseVector;t.N=this.N;for(var e in this)if(this.hasOwnProperty(e))t[e]=this[e];return t};SparseVector.prototype.get=function(t){return t in this?this[t]:0};SparseVector.prototype.add=function(t){if(!(t instanceof SparseVector))throw"no mixing of sparse and non sparse vectors!";if(this.N!=t.N)throw"Can only add vectors of same length";var e=this.copy();for(var r in t)if(t.has_index(r))e[r]=(e[r]||0)+t[r];return e};SparseVector.prototype.Add=function(t){if(!(t instanceof SparseVector))throw"no mixing of sparse and non sparse vectors!";if(this.N!=t.N)throw"Can only add vectors of same length";for(var e in t)if(t.has_index(e))this[e]=(this[e]||0)+t[e];return this};SparseVector.prototype.sub=function(t){if(!(t instanceof SparseVector))throw"no mixing of sparse and non sparse vectors!";if(this.N!=t.N)throw"Can only add vectors of same length";var e=this.copy();for(var r in t)if(t.has_index(r))e[r]=(e[r]||0)-t[r];return e};SparseVector.prototype.Sub=function(t){if(!(t instanceof SparseVector))throw"no mixing of sparse and non sparse vectors!";if(this.N!=t.N)throw"Can only add vectors of same length";for(var e in t)if(t.has_index(e))this[e]=(this[e]||0)-t[e];return this};SparseVector.prototype.len=function(){var t=0;for(var e in this)if(this.has_index(e))t+=this[e]*this[e];return Math.sqrt(t)};SparseVector.prototype.len2=function(){var t=0;for(var e in this)if(this.has_index(e))t+=this[e]*this[e];return t};SparseVector.prototype.scale=function(t){var e=this.copy();for(var r in e)if(e.has_index(r))e[r]*=t;return e};SparseVector.prototype.Scale=function(t){for(var e in this)if(v.has_index(e))this[e]*=t;return this};SparseVector.prototype.normalize=function(){var t=1/this.len();return this.scale(t)};SparseVector.prototype.Normalize=function(){var t=1/this.len();return this.Scale(t)};SparseVector.prototype.mul=function(t){var e=0;var r=t instanceof SparseVector?t.N:t.length;if(this.N!=r)throw"Can only mul vectors of same length";for(var i in this.el)e+=this.el[i]*t[i];return e};function SparseMatrix(t,e){this.N=e||0;this.M=t||0;this.i=[];this.j=[];this.v=[]}SparseMatrix.prototype.get=function(t,e){for(var r=0;r<this.i.length;r++){if(this.i[r]==t&&this.j[r]==e)return this.v[r]}return 0};SparseMatrix.prototype.Set=function(t,e,r){this.i.push(t);this.j.push(e);this.v.push(r);return this};SparseMatrix.prototype.mul=function(t){if(this.N!=t.length)throw"dimensions do not match, SparseMatrix-Vector multiplication not possible";var e=Vector.construct(t.length,0);for(var r=0;r<this.i.length;r++)e[this.i[r]]+=this.v[r]*t[this.j[r]];return e};function SparseMatrixCRS(t){this.M=t.M;this.N=t.N;this.j=[];this.v=[];this.rp=[];var e=Array(this.M);var r=Array(this.M);for(var i=0;i<this.M;i++){e[i]=[];r[i]=[]}for(var i=0;i<t.i.length;i++){e[t.i[i]].push(t.j[i]);r[t.i[i]].push(t.v[i])}var n=0;this.rp.push(n);for(var i=0;i<this.M;i++){this.j=this.j.concat(e[i]);this.v=this.v.concat(r[i]);n+=e[i].length;this.rp.push(n)}}SparseMatrixCRS.prototype.mul=function(t){if(this.N!=t.length)throw"dimensions do not match, SparseMatrix-Vector multiplication not possible";var e=Vector.construct(t.length,0);for(var r=0;r<this.M;r++)for(var i=this.rp[r];i<this.rp[r+1];i++)e[r]+=this.v[i]*t[this.j[i]];return e};