@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 9.72 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
const t=512;class i{constructor(t,i){this.x=t,this.y=i}static fromArray(t){return new i(t[0],t[1])}clone(){return new i(this.x,this.y)}equals(t,i){return t===this.x&&i===this.y}isEqual(t){return t.x===this.x&&t.y===this.y}setCoords(t,i){return this.x=t,this.y=i,this}normalize(){const t=this.x,i=this.y,s=Math.sqrt(t*t+i*i);return this.x/=s,this.y/=s,this}rightPerpendicular(){const t=this.x;return this.x=this.y,this.y=-t,this}leftPerpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}move(t,i){return this.x+=t,this.y+=i,this}assign(t){return this.x=t.x,this.y=t.y,this}assignAdd(t,i){return this.x=t.x+i.x,this.y=t.y+i.y,this}assignSub(t,i){return this.x=t.x-i.x,this.y=t.y-i.y,this}rotate(t,i){const s=this.x,h=this.y;return this.x=s*t-h*i,this.y=s*i+h*t,this}rotateReverse(t,i){const s=this.x,h=this.y;return this.x=s*t+h*i,this.y=-s*i+h*t,this}scale(t){return this.x*=t,this.y*=t,this}length(){const t=this.x,i=this.y;return Math.sqrt(t*t+i*i)}sub(t){return this.x-=t.x,this.y-=t.y,this}add(t){return this.x+=t.x,this.y+=t.y,this}static distance(t,i){const s=i.x-t.x,h=i.y-t.y;return Math.sqrt(s*s+h*h)}static add(t,s){return new i(t.x+s.x,t.y+s.y)}static sub(t,s){return new i(t.x-s.x,t.y-s.y)}}class s{constructor(t,i,s){this.ratio=t,this.x=i,this.y=s}}class h{constructor(i,s,h,n=8,e=8){this._lines=[],this._starts=[],this.validateTessellation=!0,this._pixelRatio=n,this._pixelMargin=e,this._tileSize=t*n,this._dz=i,this._yPos=s,this._xPos=h}setPixelMargin(t){t!==this._pixelMargin&&(this._pixelMargin=t,this.setExtent(this._extent))}setExtent(t){this._extent=t,this._finalRatio=this._tileSize/t*(1<<this._dz);let i=this._pixelRatio*this._pixelMargin;i/=this._finalRatio;const s=t>>this._dz;i>s&&(i=s),this._margin=i,this._xmin=s*this._xPos-i,this._ymin=s*this._yPos-i,this._xmax=this._xmin+s+2*i,this._ymax=this._ymin+s+2*i}reset(t){this._type=t,this._lines=[],this._starts=[],this._line=null,this._start=0}moveTo(t,s){this._pushLine(),this._prevIsIn=this._isIn(t,s),this._moveTo(t,s,this._prevIsIn),this._prevPt=new i(t,s),this._firstPt=new i(t,s),this._dist=0}lineTo(t,h){const n=this._isIn(t,h),e=new i(t,h),_=i.distance(this._prevPt,e);let x,l,a,y,r,o,u,m;if(n)this._prevIsIn?this._lineTo(t,h,!0):(x=this._prevPt,l=e,a=this._intersect(l,x),this._start=this._dist+_*(1-this._r),this._lineTo(a.x,a.y,!0),this._lineTo(l.x,l.y,!0));else if(this._prevIsIn)l=this._prevPt,x=e,a=this._intersect(l,x),this._lineTo(a.x,a.y,!0),this._lineTo(x.x,x.y,!1);else{const t=this._prevPt,i=e;if(t.x<=this._xmin&&i.x<=this._xmin||t.x>=this._xmax&&i.x>=this._xmax||t.y<=this._ymin&&i.y<=this._ymin||t.y>=this._ymax&&i.y>=this._ymax)this._lineTo(i.x,i.y,!1);else{const h=[];if((t.x<this._xmin&&i.x>this._xmin||t.x>this._xmin&&i.x<this._xmin)&&(y=(this._xmin-t.x)/(i.x-t.x),m=t.y+y*(i.y-t.y),m<=this._ymin?o=!1:m>=this._ymax?o=!0:h.push(new s(y,this._xmin,m))),(t.x<this._xmax&&i.x>this._xmax||t.x>this._xmax&&i.x<this._xmax)&&(y=(this._xmax-t.x)/(i.x-t.x),m=t.y+y*(i.y-t.y),m<=this._ymin?o=!1:m>=this._ymax?o=!0:h.push(new s(y,this._xmax,m))),(t.y<this._ymin&&i.y>this._ymin||t.y>this._ymin&&i.y<this._ymin)&&(y=(this._ymin-t.y)/(i.y-t.y),u=t.x+y*(i.x-t.x),u<=this._xmin?r=!1:u>=this._xmax?r=!0:h.push(new s(y,u,this._ymin))),(t.y<this._ymax&&i.y>this._ymax||t.y>this._ymax&&i.y<this._ymax)&&(y=(this._ymax-t.y)/(i.y-t.y),u=t.x+y*(i.x-t.x),u<=this._xmin?r=!1:u>=this._xmax?r=!0:h.push(new s(y,u,this._ymax))),0===h.length)r?o?this._lineTo(this._xmax,this._ymax,!0):this._lineTo(this._xmax,this._ymin,!0):o?this._lineTo(this._xmin,this._ymax,!0):this._lineTo(this._xmin,this._ymin,!0);else if(h.length>1&&h[0].ratio>h[1].ratio)this._start=this._dist+_*h[1].ratio,this._lineTo(h[1].x,h[1].y,!0),this._lineTo(h[0].x,h[0].y,!0);else{this._start=this._dist+_*h[0].ratio;for(let t=0;t<h.length;t++)this._lineTo(h[t].x,h[t].y,!0)}this._lineTo(i.x,i.y,!1)}}this._dist+=_,this._prevIsIn=n,this._prevPt=e}close(){if(this._line.length>2){const t=this._firstPt,i=this._prevPt;t.x===i.x&&t.y===i.y||this.lineTo(t.x,t.y);const s=this._line;let h=s.length;for(;h>=4&&(s[0].x===s[1].x&&s[0].x===s[h-2].x||s[0].y===s[1].y&&s[0].y===s[h-2].y);)s.pop(),s[0].x=s[h-2].x,s[0].y=s[h-2].y,--h}}result(t=!0){return this._pushLine(),0===this._lines.length?null:(3===this._type&&t&&e.simplify(this._tileSize,this._margin*this._finalRatio,this._lines),this._lines)}resultWithStarts(){if(2!==this._type)throw new Error("Only valid for lines");this._pushLine();const t=this._lines,i=t.length;if(0===i)return null;const s=[];for(let h=0;h<i;h++)s.push({line:t[h],start:this._starts[h]||0});return s}_isIn(t,i){return t>=this._xmin&&t<=this._xmax&&i>=this._ymin&&i<=this._ymax}_intersect(t,s){let h,n,e;if(s.x>=this._xmin&&s.x<=this._xmax)n=s.y<=this._ymin?this._ymin:this._ymax,e=(n-t.y)/(s.y-t.y),h=t.x+e*(s.x-t.x);else if(s.y>=this._ymin&&s.y<=this._ymax)h=s.x<=this._xmin?this._xmin:this._xmax,e=(h-t.x)/(s.x-t.x),n=t.y+e*(s.y-t.y);else{n=s.y<=this._ymin?this._ymin:this._ymax,h=s.x<=this._xmin?this._xmin:this._xmax;const i=(h-t.x)/(s.x-t.x),_=(n-t.y)/(s.y-t.y);i<_?(e=i,n=t.y+i*(s.y-t.y)):(e=_,h=t.x+_*(s.x-t.x))}return this._r=e,new i(h,n)}_pushLine(){this._line&&(1===this._type?this._line.length>0&&(this._lines.push(this._line),this._starts.push(this._start)):2===this._type?this._line.length>1&&(this._lines.push(this._line),this._starts.push(this._start)):3===this._type&&this._line.length>3&&(this._lines.push(this._line),this._starts.push(this._start))),this._line=[],this._start=0}_moveTo(t,s,h){3!==this._type?h&&(t=Math.round((t-(this._xmin+this._margin))*this._finalRatio),s=Math.round((s-(this._ymin+this._margin))*this._finalRatio),this._line.push(new i(t,s))):(h||(t<this._xmin&&(t=this._xmin),t>this._xmax&&(t=this._xmax),s<this._ymin&&(s=this._ymin),s>this._ymax&&(s=this._ymax)),t=Math.round((t-(this._xmin+this._margin))*this._finalRatio),s=Math.round((s-(this._ymin+this._margin))*this._finalRatio),this._line.push(new i(t,s)),this._isH=!1,this._isV=!1)}_lineTo(t,s,h){let n,e;if(3!==this._type)if(h){if(t=Math.round((t-(this._xmin+this._margin))*this._finalRatio),s=Math.round((s-(this._ymin+this._margin))*this._finalRatio),this._line.length>0&&(n=this._line[this._line.length-1],n.equals(t,s)))return;this._line.push(new i(t,s))}else this._line&&this._line.length>0&&this._pushLine();else if(h||(t<this._xmin&&(t=this._xmin),t>this._xmax&&(t=this._xmax),s<this._ymin&&(s=this._ymin),s>this._ymax&&(s=this._ymax)),t=Math.round((t-(this._xmin+this._margin))*this._finalRatio),s=Math.round((s-(this._ymin+this._margin))*this._finalRatio),this._line&&this._line.length>0){n=this._line[this._line.length-1];const h=n.x===t,_=n.y===s;if(h&&_)return;this._isH&&h||this._isV&&_?(n.x=t,n.y=s,e=this._line[this._line.length-2],e.x===t&&e.y===s?(this._line.pop(),this._line.length<=1?(this._isH=!1,this._isV=!1):(e=this._line[this._line.length-2],this._isH=e.x===t,this._isV=e.y===s)):(this._isH=e.x===t,this._isV=e.y===s)):(this._line.push(new i(t,s)),this._isH=h,this._isV=_)}else this._line.push(new i(t,s))}}class n{setExtent(t){this._ratio=4096===t?1:4096/t}get validateTessellation(){return this._ratio<1}reset(t){this._lines=[],this._line=null}moveTo(t,s){this._line&&this._lines.push(this._line),this._line=[];const h=this._ratio;this._line.push(new i(t*h,s*h))}lineTo(t,s){const h=this._ratio;this._line.push(new i(t*h,s*h))}close(){const t=this._line;t&&!t[0].isEqual(t[t.length-1])&&t.push(t[0])}result(){return this._line&&this._lines.push(this._line),0===this._lines.length?null:this._lines}}class e{static simplify(t,i,s){if(!s)return;const h=-i,n=t+i,_=-i,x=t+i,l=[],a=[],y=s.length;for(let e=0;e<y;++e){const t=s[e];if(!t||t.length<2)continue;let i,y=t[0];const r=t.length;for(let s=1;s<r;++s)i=t[s],y.x===i.x&&(y.x<=h&&(y.y>i.y?(l.push(e),l.push(s),l.push(0),l.push(-1)):(a.push(e),a.push(s),a.push(0),a.push(-1))),y.x>=n&&(y.y<i.y?(l.push(e),l.push(s),l.push(1),l.push(-1)):(a.push(e),a.push(s),a.push(1),a.push(-1)))),y.y===i.y&&(y.y<=_&&(y.x<i.x?(l.push(e),l.push(s),l.push(2),l.push(-1)):(a.push(e),a.push(s),a.push(2),a.push(-1))),y.y>=x&&(y.x>i.x?(l.push(e),l.push(s),l.push(3),l.push(-1)):(a.push(e),a.push(s),a.push(3),a.push(-1)))),y=i}if(0===l.length||0===a.length)return;e.fillParent(s,a,l),e.fillParent(s,l,a);const r=[];e.calcDeltas(r,a,l),e.calcDeltas(r,l,a),e.addDeltas(r,s)}static fillParent(t,i,s){const h=s.length,n=i.length;for(let e=0;e<n;e+=4){const n=i[e],x=i[e+1],l=i[e+2],a=t[n][x-1],y=t[n][x];let r=8092,o=-1;for(let i=0;i<h;i+=4){if(s[i+2]!==l)continue;const h=s[i],n=s[i+1],e=t[h][n-1],x=t[h][n];switch(l){case 0:case 1:if(_(a.y,e.y,x.y)&&_(y.y,e.y,x.y)){const t=Math.abs(x.y-e.y);t<r&&(r=t,o=i)}break;case 2:case 3:if(_(a.x,e.x,x.x)&&_(y.x,e.x,x.x)){const t=Math.abs(x.x-e.x);t<r&&(r=t,o=i)}}}i[e+3]=o}}static calcDeltas(t,i,s){const h=i.length;for(let n=0;n<h;n+=4){const h=[],_=e.calcDelta(n,i,s,h);t.push(i[n]),t.push(i[n+1]),t.push(i[n+2]),t.push(_)}}static calcDelta(t,i,s,h){const n=i[t+3];if(-1===n)return 0;const _=h.length;return _>1&&h[_-2]===n?0:(h.push(n),e.calcDelta(n,s,i,h)+1)}static addDeltas(t,i){const s=t.length;let h=0;for(let n=0;n<s;n+=4){const i=t[n+3];i>h&&(h=i)}for(let n=0;n<s;n+=4){const s=i[t[n]],e=t[n+1],_=h-t[n+3];switch(t[n+2]){case 0:s[e-1].x-=_,s[e].x-=_,1===e&&(s[s.length-1].x-=_),e===s.length-1&&(s[0].x-=_);break;case 1:s[e-1].x+=_,s[e].x+=_,1===e&&(s[s.length-1].x+=_),e===s.length-1&&(s[0].x+=_);break;case 2:s[e-1].y-=_,s[e].y-=_,1===e&&(s[s.length-1].y-=_),e===s.length-1&&(s[0].y-=_);break;case 3:s[e-1].y+=_,s[e].y+=_,1===e&&(s[s.length-1].y+=_),e===s.length-1&&(s[0].y+=_)}}}}const _=(t,i,s)=>t>=i&&t<=s||t>=s&&t<=i;export{i as Point,n as SimpleBuilder,h as TileClipper};