@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 4.67 kB
JavaScript
import{isNone as t,unwrap as e}from"../../../../../../core/maybe.js";import{dist as s,sub as n,normalize as i}from"./util.js";function r(t,e){return t[e+1]}function h(t){return t.length-1}function a(t){let e=0;for(let s=0;s<h(t);s++)e+=d(t,s);return e}function d(t,e,s=1){const[n,i]=r(t,e);return Math.sqrt(n*n+i*i)*s}class c{constructor(t,e,s,n,i){this._segments=t,this._index=e,this._distance=s,this._xStart=n,this._yStart=i,this._done=!1}static create(t){return new c(t,0,0,t[0][0],t[0][1])}clone(){return new c(this._segments,this._index,this._distance,this.xStart,this.yStart)}equals(t){return this._index===t._index||t._index===this._index-1&&(0===this._distance||1===t._distance)||t._index===this._index+1&&(1===this._distance||0===t._distance)}leq(t){return this._index<t._index||this._index===t._index&&this._distance<=t._distance}geq(t){return this._index>t._index||this._index===t._index&&this._distance>=t._distance}get _segment(){return this._segments[this._index+1]}get angle(){const t=this.dy,e=(0*t+-1*-this.dx)/(1*this.length);let s=Math.acos(e);return t>0&&(s=2*Math.PI-s),s}get xStart(){return this._xStart}get yStart(){return this._yStart}get x(){return this.xStart+this.distance*this.dx}get y(){return this.yStart+this.distance*this.dy}get dx(){return this._segment[0]}get dy(){return this._segment[1]}get xMidpoint(){return this.xStart+.5*this.dx}get yMidpoint(){return this.yStart+.5*this.dy}get xEnd(){return this.xStart+this.dx}get yEnd(){return this.yStart+this.dy}get length(){const{dx:t,dy:e}=this;return Math.sqrt(t*t+e*e)}get remainingLength(){return this.length*(1-this._distance)}get backwardLength(){return this.length*this._distance}get distance(){return this._distance}get done(){return this._done}hasPrev(){return this._index-1>=0}hasNext(){return this._index+1<h(this._segments)}next(){return this.hasNext()?(this._xStart+=this.dx,this._yStart+=this.dy,this._distance=0,this._index+=1,this):null}prev(){return this.hasPrev()?(this._index-=1,this._xStart-=this.dx,this._yStart-=this.dy,this._distance=1,this):(this._done=!0,null)}_seekBackwards(t,e){const s=this.backwardLength;if(t<=s)return this._distance=(s-t)/this.length,this;let n=this.backwardLength;for(;this.prev();){if(n+this.length>t)return this._seekBackwards(t-n);n+=this.length}return this._distance=0,e?this:null}seek(t,e=!1){if(t<0)return this._seekBackwards(Math.abs(t),e);if(t<=this.remainingLength)return this._distance=(this.backwardLength+t)/this.length,this;let s=this.remainingLength;for(;this.next();){if(s+this.length>t)return this.seek(t-s,e);s+=this.length}return this._distance=1,e?this:null}}function u(t,e,s,n=!0){const i=a(t),r=c.create(t),h=i/2;if(!n)return r.seek(h),void s(r.clone(),0,h+0*e,i);const d=Math.max((i-e)/2,0),u=Math.floor(d/e),o=h-u*e;r.seek(o);for(let a=-u;a<=u;a++)r.x<512&&r.x>=0&&r.y<512&&r.y>=0&&s(r.clone(),a,h+a*e,i),r.seek(e)}function o(t,e,s){_(s,e,a(t),c.create(t),0)}function _(s,n,i,r,h){if(i<n)return;const a=r.clone().seek(i/2);if(t(a))return;s(a.clone(),i,h),i=(i-n)/2;const d=e(a.seek(n/2));_(s,n,i,r,h+1),_(s,n,i,d,h+1)}function l(t,e){const s=e;for(let n=0;n<t.length;n++){let e=t[n];const i=[];i.push(e[0]);for(let t=1;t<e.length;t++){let[s,n]=i[t-1];s+=e[t][0],n+=e[t][1],i.push([s,n])}g(i,s);const r=[];r.push(i[0]);for(let t=1;t<i.length;t++){const[e,s]=i[t-1],[n,h]=i[t],a=Math.round(n-e),d=Math.round(h-s);r.push([a,d])}t[n]=r,e=r}return t}function g(t,e){const r=1e-6;if(e<=0)return;const h=t.length;if(h<3)return;const a=[];let d=0;a.push(0);for(let n=1;n<h;n++)d+=s(t[n],t[n-1]),a.push(d);e=Math.min(e,.2*d);const c=[];c.push(t[0][0]),c.push(t[0][1]);const u=t[h-1][0],o=t[h-1][1],_=n([0,0],t[0],t[1]);i(_),t[0][0]+=e*_[0],t[0][1]+=e*_[1],n(_,t[h-1],t[h-2]),i(_),t[h-1][0]+=e*_[0],t[h-1][1]+=e*_[1];for(let s=1;s<h;s++)a[s]+=e;a[h-1]+=e;const l=.5*e;for(let s=1;s<h-1;s++){let n=0,i=0,d=0;for(let h=s-1;h>=0&&!(a[h+1]<a[s]-l);h--){const c=l+a[h+1]-a[s],u=a[h+1]-a[h],o=a[s]-a[h]<l?1:c/u;if(Math.abs(o)<r)break;const _=o*o,g=o*c-.5*_*u,x=o*u/e,f=t[h+1],y=t[h][0]-f[0],k=t[h][1]-f[1];n+=x/g*(f[0]*o*c+.5*_*(c*y-u*f[0])-_*o*u*y/3),i+=x/g*(f[1]*o*c+.5*_*(c*k-u*f[1])-_*o*u*k/3),d+=x}for(let c=s+1;c<h&&!(a[c-1]>a[s]+l);c++){const h=l-a[c-1]+a[s],u=a[c]-a[c-1],o=a[c]-a[s]<l?1:h/u;if(Math.abs(o)<r)break;const _=o*o,g=o*h-.5*_*u,x=o*u/e,f=t[c-1],y=t[c][0]-f[0],k=t[c][1]-f[1];n+=x/g*(f[0]*o*h+.5*_*(h*y-u*f[0])-_*o*u*y/3),i+=x/g*(f[1]*o*h+.5*_*(h*k-u*f[1])-_*o*u*k/3),d+=x}c.push(n/d),c.push(i/d)}c.push(u),c.push(o);for(let s=0,n=0;s<h;s++)t[s][0]=c[n++],t[s][1]=c[n++]}export{c as SegmentCursor,u as pathDivide,a as pathLength,o as pathSubdivide,r as segmentAt,h as segmentCount,d as segmentLength,l as smoothPaths};