// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/layers/vectorTiles/views/2d/engine/webgl/TileClipper",["require","exports","./Geometry","./GeometryUtils"],function(t,w,r,u){Object.defineProperty(w,"__esModule",{value:!0});var v=function(){return function(f,a,b){this.ratio=f;this.x=a;this.y=b}}();t=function(){function f(a,b,e,d,l){void 0===d&&(d=8);void 0===l&&(l=8);this.lines=[];this.starts=[];this.pixelRatio=d;this.pixelMargin=l;this.tileSize=512*d;this.dz=a;this.yPos=b;this.xPos=e}f.prototype.setExtent=function(a){this.finalRatio=
this.tileSize/a*(1<<this.dz);var b=this.pixelRatio*this.pixelMargin,b=b/this.finalRatio;a>>=this.dz;b>a&&(b=a);this.margin=b;this.xmin=a*this.xPos-b;this.ymin=a*this.yPos-b;this.xmax=this.xmin+a+2*b;this.ymax=this.ymin+a+2*b};f.prototype.reset=function(a){this.type=a;this.lines=[];this.starts=[];this.line=null;this.start=0};f.prototype.moveTo=function(a,b){this._pushLine();this._prevIsIn=this._isIn(a,b);this._moveTo(a,b,this._prevIsIn);this._prevPt=new r.Point(a,b);this._firstPt=new r.Point(a,b);
this._dist=0};f.prototype.lineTo=function(a,b){var e=this._isIn(a,b),d=new r.Point(a,b),l=r.Point.distance(this._prevPt,d),h,c,g,f,m;if(e)this._prevIsIn?this._lineTo(a,b,!0):(h=this._prevPt,c=d,g=this._intersect(c,h),this.start=this._dist+l*(1-this._r),this._lineTo(g.x,g.y,!0),this._lineTo(c.x,c.y,!0));else if(this._prevIsIn)c=this._prevPt,h=d,g=this._intersect(c,h),this._lineTo(g.x,g.y,!0),this._lineTo(h.x,h.y,!1);else{var k=this._prevPt;if(!(k.x<=this.xmin&&d.x<=this.xmin||k.x>=this.xmax&&d.x>=
this.xmax||k.y<=this.ymin&&d.y<=this.ymin||k.y>=this.ymax&&d.y>=this.ymax)){h=[];if(k.x<this.xmin&&d.x>this.xmin||k.x>this.xmin&&d.x<this.xmin)c=(this.xmin-k.x)/(d.x-k.x),m=k.y+c*(d.y-k.y),m<=this.ymin?f=!1:m>=this.ymax?f=!0:h.push(new v(c,this.xmin,m));if(k.x<this.xmax&&d.x>this.xmax||k.x>this.xmax&&d.x<this.xmax)c=(this.xmax-k.x)/(d.x-k.x),m=k.y+c*(d.y-k.y),m<=this.ymin?f=!1:m>=this.ymax?f=!0:h.push(new v(c,this.xmax,m));if(k.y<this.ymin&&d.y>this.ymin||k.y>this.ymin&&d.y<this.ymin)c=(this.ymin-
k.y)/(d.y-k.y),m=k.x+c*(d.x-k.x),m<=this.xmin?g=!1:m>=this.xmax?g=!0:h.push(new v(c,m,this.ymin));if(k.y<this.ymax&&d.y>this.ymax||k.y>this.ymax&&d.y<this.ymax)c=(this.ymax-k.y)/(d.y-k.y),m=k.x+c*(d.x-k.x),m<=this.xmin?g=!1:m>=this.xmax?g=!0:h.push(new v(c,m,this.ymax));if(0===h.length)g?f?this._lineTo(this.xmax,this.ymax,!0):this._lineTo(this.xmax,this.ymin,!0):f?this._lineTo(this.xmin,this.ymax,!0):this._lineTo(this.xmin,this.ymin,!0);else if(1<h.length&&h[0].ratio>h[1].ratio)this.start=this._dist+
l*h[1].ratio,this._lineTo(h[1].x,h[1].y,!0),this._lineTo(h[0].x,h[0].y,!0);else for(this.start=this._dist+l*h[0].ratio,c=0;c<h.length;c++)this._lineTo(h[c].x,h[c].y,!0)}this._lineTo(d.x,d.y,!1)}this._dist+=l;this._prevIsIn=e;this._prevPt=d};f.prototype.close=function(){var a,b;0<this.line.length&&(a=this._firstPt,b=this._prevPt,a.x===b.x&&a.y===b.y||this.lineTo(a.x,a.y),a=this.line,b=a.length,4<=b&&(a[0].x===a[1].x&&a[0].x===a[b-2].x||a[0].y===a[1].y&&a[0].y===a[b-2].y)&&(a.pop(),a[0].x=a[b-2].x,
a[0].y=a[b-2].y))};f.prototype.result=function(){this._pushLine();if(0===this.lines.length)return null;3===this.type&&x.simplify(this.margin*this.finalRatio,this.lines);return this.lines};f.prototype.resultWithStarts=function(){if(2!==this.type)throw Error("Only valid for lines");this._pushLine();var a=this.lines,b=a.length;if(0===b)return null;for(var e=[],d=0;d<b;d++)e.push({line:a[d],start:this.starts[d]||0});return e};f.prototype._isIn=function(a,b){return a>=this.xmin&&a<=this.xmax&&b>=this.ymin&&
b<=this.ymax};f.prototype._intersect=function(a,b){var e,d,l;if(b.x>=this.xmin&&b.x<=this.xmax)d=b.y<=this.ymin?this.ymin:this.ymax,l=(d-a.y)/(b.y-a.y),e=a.x+l*(b.x-a.x);else if(b.y>=this.ymin&&b.y<=this.ymax)e=b.x<=this.xmin?this.xmin:this.xmax,l=(e-a.x)/(b.x-a.x),d=a.y+l*(b.y-a.y);else{d=b.y<=this.ymin?this.ymin:this.ymax;e=b.x<=this.xmin?this.xmin:this.xmax;var h=(e-a.x)/(b.x-a.x),c=(d-a.y)/(b.y-a.y);h<c?(l=h,d=a.y+h*(b.y-a.y)):(l=c,e=a.x+c*(b.x-a.x))}this._r=l;return new r.Point(e,d)};f.prototype._pushLine=
function(){this.line&&(1===this.type?0<this.line.length&&(this.lines.push(this.line),this.starts.push(this.start)):2===this.type?1<this.line.length&&(this.lines.push(this.line),this.starts.push(this.start)):3===this.type&&3<this.line.length&&(this.lines.push(this.line),this.starts.push(this.start)));this.line=[];this.start=0};f.prototype._moveTo=function(a,b,e){3!==this.type?e&&(a=Math.round((a-(this.xmin+this.margin))*this.finalRatio),b=Math.round((b-(this.ymin+this.margin))*this.finalRatio),this.line.push(new r.Point(a,
b))):(e||(a<this.xmin&&(a=this.xmin),a>this.xmax&&(a=this.xmax),b<this.ymin&&(b=this.ymin),b>this.ymax&&(b=this.ymax)),a=Math.round((a-(this.xmin+this.margin))*this.finalRatio),b=Math.round((b-(this.ymin+this.margin))*this.finalRatio),this.line.push(new r.Point(a,b)),this._is_v=this._is_h=!1)};f.prototype._lineTo=function(a,b,e){if(3!==this.type)if(e){a=Math.round((a-(this.xmin+this.margin))*this.finalRatio);b=Math.round((b-(this.ymin+this.margin))*this.finalRatio);if(0<this.line.length&&(e=this.line[this.line.length-
1],e.equals(a,b)))return;this.line.push(new r.Point(a,b))}else this.line&&0<this.line.length&&this._pushLine();else if(e||(a<this.xmin&&(a=this.xmin),a>this.xmax&&(a=this.xmax),b<this.ymin&&(b=this.ymin),b>this.ymax&&(b=this.ymax)),a=Math.round((a-(this.xmin+this.margin))*this.finalRatio),b=Math.round((b-(this.ymin+this.margin))*this.finalRatio),this.line&&0<this.line.length){e=this.line[this.line.length-1];var d=e.x===a,l=e.y===b;d&&l||(this._is_h&&d?(e.x=a,e.y=b,e=this.line[this.line.length-2],
this._is_h=e.x===a,this._is_v=e.y===b):this._is_v&&l?(e.x=a,e.y=b,e=this.line[this.line.length-2],this._is_h=e.x===a,this._is_v=e.y===b):(this.line.push(new r.Point(a,b)),this._is_h=d,this._is_v=l))}else this.line.push(new r.Point(a,b))};return f}();w.TileClipper=t;t=function(){function f(){}f.prototype.setExtent=function(a){this._ratio=4096===a?1:4096/a};f.prototype.reset=function(a){this.type=a;this.lines=[];this.line=null};f.prototype.moveTo=function(a,b){this.line&&this.lines.push(this.line);
this.line=[];var e=this._ratio;this.line.push(new r.Point(Math.round(a*e),Math.round(b*e)))};f.prototype.lineTo=function(a,b){var e=this._ratio;this.line.push(new r.Point(Math.round(a*e),Math.round(b*e)))};f.prototype.close=function(){var a=this.line;a&&!a[0].isEqual(a[a.length-1])&&a.push(a[0])};f.prototype.result=function(){this.line&&this.lines.push(this.line);if(0===this.lines.length)return null;3===this.type&&1!==this._ratio&&x.simplify(64,this.lines);return this.lines};return f}();w.SimpleBuilder=
t;var x=function(){function f(){}f.simplify=function(a,b){if(b){for(var e=-a,d=4096+a,l=-a,h=4096+a,c=[],g=[],r=b.length,m=0;m<r;++m){var k=b[m];if(k&&!(2>k.length))for(var p=k[0],q=void 0,t=k.length,n=1;n<t;++n)q=k[n],p.x===q.x&&(p.x<=e&&(p.y>q.y?(c.push(m),c.push(n),c.push(0),c.push(-1)):(g.push(m),g.push(n),g.push(0),g.push(-1))),p.x>=d&&(p.y<q.y?(c.push(m),c.push(n),c.push(1),c.push(-1)):(g.push(m),g.push(n),g.push(1),g.push(-1)))),p.y===q.y&&(p.y<=l&&(p.x<q.x?(c.push(m),c.push(n),c.push(2),c.push(-1)):
(g.push(m),g.push(n),g.push(2),g.push(-1))),p.y>=h&&(p.x>q.x?(c.push(m),c.push(n),c.push(3),c.push(-1)):(g.push(m),g.push(n),g.push(3),g.push(-1)))),p=q}0!==c.length&&0!==g.length&&(f.fillParent(b,g,c),f.fillParent(b,c,g),e=[],f.calcDeltas(e,g,c),f.calcDeltas(e,c,g),f.addDeltas(e,b))}};f.fillParent=function(a,b,e){for(var d=e.length,l=b.length,h=0;h<l;h+=4){for(var c=b[h],g=b[h+1],f=b[h+2],m=a[c][g-1],c=a[c][g],g=8092,k=-1,p=0;p<d;p+=4)if(e[p+2]===f){var q=e[p],r=e[p+1],n=a[q][r-1],q=a[q][r];switch(f){case 0:case 1:u.between(m.y,
n.y,q.y)&&u.between(c.y,n.y,q.y)&&(n=Math.abs(q.y-n.y),n<g&&(g=n,k=p));break;case 2:case 3:u.between(m.x,n.x,q.x)&&u.between(c.x,n.x,q.x)&&(n=Math.abs(q.x-n.x),n<g&&(g=n,k=p))}}b[h+3]=k}};f.calcDeltas=function(a,b,e){for(var d=b.length,l=0;l<d;l+=4){var h=f.calcDelta(l,b,e,[]);a.push(b[l]);a.push(b[l+1]);a.push(b[l+2]);a.push(h)}};f.calcDelta=function(a,b,e,d){a=b[a+3];if(-1===a)return 0;var l=d.length;if(1<l&&d[l-2]===a)return 0;d.push(a);return f.calcDelta(a,e,b,d)+1};f.addDeltas=function(a,b){for(var e=
a.length,d=0,f=0;f<e;f+=4){var h=a[f+3];h>d&&(d=h)}for(f=0;f<e;f+=4){var c=b[a[f]],g=a[f+1],h=d-a[f+3];switch(a[f+2]){case 0:c[g-1].x-=h;c[g].x-=h;break;case 1:c[g-1].x+=h;c[g].x+=h;break;case 2:c[g-1].y-=h;c[g].y-=h;break;case 3:c[g-1].y+=h,c[g].y+=h}}e=b.length;for(d=0;d<e;++d)c=b[d],!c||2>c.length||c[c.length-1].x!==c[0].x&&c[c.length-1].y!==c[0].y&&c.push(c[0])};return f}()});