// 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/vectorTiles/TileClipper",["require","exports","./Geometry","./GeometryUtils"],function(r,w,n,u){Object.defineProperty(w,"__esModule",{value:!0});var v=function(){return function(c,a,b){this.ratio=c;this.x=a;this.y=b}}();r=function(){function c(a,b,g){this.dz=a;this.yPos=b;this.xPos=g}c.prototype.setExtent=function(a){this.finalRatio=4096/a*(1<<this.dz);var b;b=64/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};c.prototype.reset=function(a){this.type=a;this._prevIsIn=!1;this.lines=[];this.line=null};c.prototype.moveTo=function(a,b){this._pushLine();this._prevIsIn=this._isIn(a,b);this._moveTo(a,b,this._prevIsIn);this._prevPt=new n.Point(a,b);this._firstPt=new n.Point(a,b)};c.prototype.lineTo=function(a,b){var g=this._isIn(a,b),e,h,l,d,f,c;if(g)this._prevIsIn?this._lineTo(a,b,!0):(e=this._prevPt,h=new n.Point(a,b),l=this._intersect(h,e),this._lineTo(l.x,
l.y,!0),this._lineTo(h.x,h.y,!0));else{if(this._prevIsIn)h=this._prevPt,e=new n.Point(a,b),l=this._intersect(h,e),this._lineTo(l.x,l.y,!0);else{var k=this._prevPt;e=new n.Point(a,b);if(!(k.x<=this.xmin&&e.x<=this.xmin||k.x>=this.xmax&&e.x>=this.xmax||k.y<=this.ymin&&e.y<=this.ymin||k.y>=this.ymax&&e.y>=this.ymax)){h=[];if(k.x<this.xmin&&e.x>this.xmin||k.x>this.xmin&&e.x<this.xmin)l=(this.xmin-k.x)/(e.x-k.x),c=k.y+l*(e.y-k.y),c<=this.ymin?f=!1:c>=this.ymax?f=!0:h.push(new v(l,this.xmin,c));if(k.x<
this.xmax&&e.x>this.xmax||k.x>this.xmax&&e.x<this.xmax)l=(this.xmax-k.x)/(e.x-k.x),c=k.y+l*(e.y-k.y),c<=this.ymin?f=!1:c>=this.ymax?f=!0:h.push(new v(l,this.xmax,c));if(k.y<this.ymin&&e.y>this.ymin||k.y>this.ymin&&e.y<this.ymin)l=(this.ymin-k.y)/(e.y-k.y),c=k.x+l*(e.x-k.x),c<=this.xmin?d=!1:c>=this.xmax?d=!0:h.push(new v(l,c,this.ymin));if(k.y<this.ymax&&e.y>this.ymax||k.y>this.ymax&&e.y<this.ymax)l=(this.ymax-k.y)/(e.y-k.y),c=k.x+l*(e.x-k.x),c<=this.xmin?d=!1:c>=this.xmax?d=!0:h.push(new v(l,c,this.ymax));
if(0===h.length)d?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._lineTo(h[1].x,h[1].y,!0),this._lineTo(h[0].x,h[0].y,!0);else for(l=0;l<h.length;l++)this._lineTo(h[l].x,h[l].y,!0)}}this._lineTo(e.x,e.y,!1)}this._prevIsIn=g;this._prevPt=new n.Point(a,b)};c.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))};c.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};c.prototype._isIn=function(a,b){return a>=this.xmin&&a<=this.xmax&&b>=this.ymin&&b<=this.ymax};c.prototype._intersect=function(a,b){var g,e;if(b.x>=
this.xmin&&b.x<=this.xmax)e=b.y<=this.ymin?this.ymin:this.ymax,g=a.x+(e-a.y)/(b.y-a.y)*(b.x-a.x);else if(b.y>=this.ymin&&b.y<=this.ymax)g=b.x<=this.xmin?this.xmin:this.xmax,e=a.y+(g-a.x)/(b.x-a.x)*(b.y-a.y);else{e=b.y<=this.ymin?this.ymin:this.ymax;g=b.x<=this.xmin?this.xmin:this.xmax;var h=(g-a.x)/(b.x-a.x),c=(e-a.y)/(b.y-a.y);h<c?e=a.y+h*(b.y-a.y):g=a.x+c*(b.x-a.x)}return new n.Point(g,e)};c.prototype._pushLine=function(){this.line&&(1===this.type?0<this.line.length&&this.lines.push(this.line):
2===this.type?1<this.line.length&&this.lines.push(this.line):3===this.type&&3<this.line.length&&this.lines.push(this.line));this.line=[]};c.prototype._moveTo=function(a,b,g){3!==this.type?g&&(a=Math.round((a-(this.xmin+this.margin))*this.finalRatio),b=Math.round((b-(this.ymin+this.margin))*this.finalRatio),this.line.push(new n.Point(a,b))):(g||(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 n.Point(a,b)),this._is_v=this._is_h=!1)};c.prototype._lineTo=function(a,b,g){if(3!==this.type)if(g){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&&(g=this.line[this.line.length-1],g.equals(a,b)))return;this.line.push(new n.Point(a,b))}else this.line&&0<this.line.length&&this._pushLine();else if(g||(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){g=this.line[this.line.length-1];var e=g.x===a,c=g.y===b;e&&c||(this._is_h&&e?(g.x=a,g.y=b,g=this.line[this.line.length-2],this._is_h=g.x===a,this._is_v=g.y===b):this._is_v&&c?(g.x=a,g.y=b,g=this.line[this.line.length-2],this._is_h=g.x===a,this._is_v=g.y===b):(this.line.push(new n.Point(a,
b)),this._is_h=e,this._is_v=c))}else this.line.push(new n.Point(a,b))};return c}();w.TileClipper=r;r=function(){function c(){}c.prototype.setExtent=function(a){this._ratio=4096===a?1:4096/a};c.prototype.reset=function(a){this.type=a;this.lines=[];this.line=null};c.prototype.moveTo=function(a,b){this.line&&this.lines.push(this.line);this.line=[];var c=this._ratio;this.line.push(new n.Point(Math.round(a*c),Math.round(b*c)))};c.prototype.lineTo=function(a,b){var c=this._ratio;this.line.push(new n.Point(Math.round(a*
c),Math.round(b*c)))};c.prototype.close=function(){var a=this.line;a&&!a[0].isEqual(a[a.length-1])&&a.push(a[0])};c.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 c}();w.SimpleBuilder=r;var x=function(){function c(){}c.simplify=function(a,b){if(b){for(var g=-a,e=4096+a,h=-a,l=4096+a,d=[],f=[],n=b.length,k=0;k<n;++k){var t=b[k];if(t&&!(2>t.length))for(var p=t[0],
q=void 0,r=t.length,m=1;m<r;++m)q=t[m],p.x===q.x&&(p.x<=g&&(p.y>q.y?(d.push(k),d.push(m),d.push(0),d.push(-1)):(f.push(k),f.push(m),f.push(0),f.push(-1))),p.x>=e&&(p.y<q.y?(d.push(k),d.push(m),d.push(1),d.push(-1)):(f.push(k),f.push(m),f.push(1),f.push(-1)))),p.y===q.y&&(p.y<=h&&(p.x<q.x?(d.push(k),d.push(m),d.push(2),d.push(-1)):(f.push(k),f.push(m),f.push(2),f.push(-1))),p.y>=l&&(p.x>q.x?(d.push(k),d.push(m),d.push(3),d.push(-1)):(f.push(k),f.push(m),f.push(3),f.push(-1)))),p=q}0!==d.length&&0!==
f.length&&(c.fillParent(b,f,d),c.fillParent(b,d,f),g=[],c.calcDeltas(g,f,d),c.calcDeltas(g,d,f),c.addDeltas(g,b))}};c.fillParent=function(a,b,c){for(var e=c.length,h=b.length,g=0;g<h;g+=4){for(var d=b[g],f=b[g+1],n=b[g+2],k=a[d][f-1],d=a[d][f],f=8092,t=-1,p=0;p<e;p+=4)if(c[p+2]===n){var q=c[p],r=c[p+1],m=a[q][r-1],q=a[q][r];switch(n){case 0:case 1:u.between(k.y,m.y,q.y)&&u.between(d.y,m.y,q.y)&&(m=Math.abs(q.y-m.y),m<f&&(f=m,t=p));break;case 2:case 3:u.between(k.x,m.x,q.x)&&u.between(d.x,m.x,q.x)&&
(m=Math.abs(q.x-m.x),m<f&&(f=m,t=p))}}b[g+3]=t}};c.calcDeltas=function(a,b,g){for(var e=b.length,h=0;h<e;h+=4){var l=c.calcDelta(h,b,g,[]);a.push(b[h]);a.push(b[h+1]);a.push(b[h+2]);a.push(l)}};c.calcDelta=function(a,b,g,e){a=b[a+3];if(-1===a)return 0;var h=e.length;if(1<h&&e[h-2]===a)return 0;e.push(a);return c.calcDelta(a,g,b,e)+1};c.addDeltas=function(a,b){for(var c=a.length,e=0,h=0;h<c;h+=4){var l=a[h+3];l>e&&(e=l)}for(h=0;h<c;h+=4){var d=b[a[h]],f=a[h+1],l=e-a[h+3];switch(a[h+2]){case 0:d[f-
1].x-=l;d[f].x-=l;break;case 1:d[f-1].x+=l;d[f].x+=l;break;case 2:d[f-1].y-=l;d[f].y-=l;break;case 3:d[f-1].y+=l,d[f].y+=l}}c=b.length;for(e=0;e<c;++e)d=b[e],!d||2>d.length||d[d.length-1].x!==d[0].x&&d[d.length-1].y!==d[0].y&&d.push(d[0])};return c}()});