GeometryWalker.js 6.2 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import{getId as t}from"./CIMCursor.js";import{CurveHelper as s,PIXEL_TOLERANCE as e}from"./CurveHelper.js";const i=1e-7;class n{constructor(){this._values=[],this.extPtGap=0,this.ctrlPtGap=0,this._length=0,this._currentValue=0}isEmpty(){return 0===this._values.length}size(){return this._values.length}init(t,s,e=!0){if(this._setEmpty(),!t||0===t.length)return!1;for(let n=0;n<t.length;n++){let s=Math.abs(t[n]);e&&s<i&&(s=i),this._values.push(s),this._length+=s}return s&&1&t.length&&(this._length*=2),0!==this._length&&(this.ctrlPtGap=this.extPtGap=0,this._currentValue=-1,!0)}scale(t){const s=this._values?this._values.length:0;for(let e=0;e<s;++e)this._values[e]*=t;this._length*=t,this.extPtGap*=t,this.ctrlPtGap*=t}addValue(t){this._length+=t,this._values.push(t)}firstValue(){return this._values[0]}lastValue(){return this._values[this._values.length-1]}nextValue(){return this._currentValue++,this._currentValue===this._values.length&&(this._currentValue=0),this._values[this._currentValue]}reset(){this._currentValue=-1}length(){return this._length}_setEmpty(){this.extPtGap=this.ctrlPtGap=this._length=0,this._currentValue=-1,this._values.length=0}}class h{constructor(){this.pt=null,this.ca=0,this.sa=0}}var r;!function(t){t[t.FAIL=0]="FAIL",t[t.END=1]="END",t[t.CONTINUE=2]="CONTINUE"}(r||(r={}));class a{constructor(){this.reset()}reset(){this.segment=-1,this.segmentLength=0,this.abscissa=0,this.isPathEnd=!1,this.isPartEnd=!1}isValid(){return-1!==this.segment}copyTo(t){t.segment=this.segment,t.segmentLength=this.segmentLength,t.abscissa=this.abscissa,t.isPathEnd=this.isPathEnd,t.isPartEnd=this.isPartEnd}}class g extends s{constructor(t=0,s=!1){super(t,s),this._tolerance=e,this._currentPosition=new a}updateTolerance(t){this._tolerance=e*t}init(t,s,e=!0){return e?(this._patternLength=s.length(),this._partExtPtGap=s.extPtGap,this._partCtrlPtGap=s.ctrlPtGap):(this._patternLength=0,this._partExtPtGap=0,this._partCtrlPtGap=0),this._currentPosition.reset(),this._partSegCount=0,this._path=t,this._seg=-1,this._setPosAtNextPart()}curPositionIsValid(){return this._currentPosition.isValid()}nextPosition(t,s=r.FAIL){const e=new a;return!!this._nextPosition(t,e,null,s)&&(e.copyTo(this._currentPosition),!0)}curPointAndAngle(t){t.pt=this._getPoint(this._currentPosition);const[s,e]=this._getAngle(this._currentPosition);t.ca=s,t.sa=e}nextPointAndAngle(t,s,e=r.FAIL){const i=new a;if(!this._nextPosition(t,i,null,e))return!1;i.copyTo(this._currentPosition),s.pt=this._getPoint(i);const[n,h]=this._getAngle(i);return s.ca=n,s.sa=h,!0}nextCurve(t){if(0===t)return null;const s=[],e=new a;return this._nextPosition(t,e,s,r.END)?(e.copyTo(this._currentPosition),s):null}isPathEnd(){return this._currentPosition.isPathEnd}getPathEnd(){if(-1===this._currentPosition.segment)throw new Error("missing segment");return this._path[this._currentPosition.segment+1]}_nextPosition(t,s,e,i){if(this._currentPosition.isPathEnd)return!1;let n=this._currentPosition.abscissa;for(this._currentPosition.segmentLength>0&&(n/=this._currentPosition.segmentLength),this._currentPosition.copyTo(s);s.abscissa+t*this._partLengthRatio>s.segmentLength+this._tolerance;){if(e){if(0===e.length)if(0===n){const t=this._path[s.segment];e.push([t[0],t[1]])}else e.push(this.getSegCoord2D(this._path,s.segment,n));const t=this._path[s.segment+1];e.push([t[0],t[1]])}if(n=0,t-=(s.segmentLength-s.abscissa)/this._partLengthRatio,this._partSegCount)s.segment=this._nextSegment(),s.segmentLength=this.calculateSegLength(this._path,s.segment),s.abscissa=0,this._partSegCount--;else{if(!this._setPosAtNextPart())return i!==r.FAIL&&(s.segmentLength=this.calculateSegLength(this._path,s.segment),s.isPartEnd=!0,i===r.END?(s.abscissa=s.segmentLength,s.isPathEnd=!0):s.abscissa=s.segmentLength+t,!0);this._currentPosition.copyTo(s)}}if(s.abscissa+=t*this._partLengthRatio,e){if(0===e.length)if(0===n){const t=this._path[s.segment];e.push([t[0],t[1]])}else e.push(this.getSegCoord2D(this._path,s.segment,n));const t=s.abscissa/s.segmentLength;if(1===t){const t=this._path[s.segment+1];e.push([t[0],t[1]])}else e.push(this.getSegCoord2D(this._path,s.segment,t))}return this._partSegCount||Math.abs(s.abscissa-s.segmentLength)<this._tolerance&&(s.isPathEnd=this._partIsLast,s.isPartEnd=!0),!0}_getPoint(t){if(-1===t.segment)throw new Error("missing segment");const s=t.segmentLength<=0?0:t.abscissa/t.segmentLength;return this.getSegCoord2D(this._path,t.segment,s)}_getAngle(t){if(-1===t.segment)throw new Error("missing segment");const s=t.segmentLength<=0?0:t.abscissa/t.segmentLength;return this.getSegAngleCS(this._path,t.segment,s)}_setPosAtNextPart(){for(;this._partSegCount;)this._hasNextSegment()&&this._nextSegment(),this._partSegCount--;if(!this._hasNextSegment())return!1;for(this._partLength=0,this._partIsLast=!0,this._partSegCount=0;this._hasNextSegment();)if(this._partLength+=this.calculateSegLength(this._path,this._nextSegment()),this._partSegCount++,1===t(this._path[this._getEndPointIndex()])){this._partIsLast=!this._hasNextSegment();break}let s=this._partSegCount;for(;s;)this._previousSegment(),--s;this._currentPosition.segment=this._nextSegment(),this._currentPosition.segmentLength=this.calculateSegLength(this._path,this._currentPosition.segment),this._currentPosition.abscissa=0,this._currentPosition.isPathEnd=this._currentPosition.isPartEnd=!1,--this._partSegCount;const e=this._getStartPointIndex();this._ctrlPtBegin=1===t(this._path[e]);let i=e+this._partSegCount+1;if(i>=this._path.length&&(i=0),this._ctrlPtEnd=1===t(this._path[i]),this._patternLength>0){const t=this._ctrlPtBegin?this._partCtrlPtGap:this._partExtPtGap,s=this._ctrlPtEnd?this._partCtrlPtGap:this._partExtPtGap;let e=Math.round((this._partLength-(t+s))/this._patternLength);e<=0&&(e=t+s>0?0:1),this._partLengthRatio=this._partLength/(t+s+e*this._patternLength),this._partLengthRatio<.01&&(this._partLengthRatio=1)}else this._partLengthRatio=1;return!0}_hasNextSegment(){return this._seg<this._path.length-2}_previousSegment(){return--this._seg}_nextSegment(){return++this._seg}_getStartPointIndex(){return this._seg}_getEndPointIndex(){return this._seg+1}}export{n as DashPattern,r as EndType,g as GeometryWalker,h as Pos};