@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 5.03 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import{equals as e}from"../../../core/arrayUtils.js";import{neverReached as t}from"../../../core/compilerUtils.js";import s from"../../../core/Evented.js";import i from"../../../geometry/Polygon.js";import r from"../../../geometry/Polyline.js";import{createCoordinateHelper as o}from"../coordinateHelper.js";import{unnormalize as n,getUnnormalizationInfo as c}from"./unnormalizationHelper.js";class h{constructor(e){this.component=e,this.leftEdge=null,this.rightEdge=null,this.type="vertex",this.index=null}get pos(){return this._pos}set pos(e){this._pos=e,this.component.unnormalizeVertexPositions()}}class l{constructor(e,t,s){this.component=e,this.leftVertex=t,this.rightVertex=s,this.type="edge",t.rightEdge=this,s.leftEdge=this}}class a{constructor(e,t){this._spatialReference=e,this._viewingMode=t,this.vertices=[],this.edges=[],this.index=null}unnormalizeVertexPositions(){this.vertices.length<=1||n(this.vertices,c(this._spatialReference,this._viewingMode))}updateVertexIndex(e,t){if(0===this.vertices.length)return;const s=this.vertices[0];let i=null,r=e,o=t;do{i=r,i.index=o++,r=i.rightEdge?i.rightEdge.rightVertex:null}while(null!=r&&r!==s);i.leftEdge&&i!==this.vertices[this.vertices.length-1]&&this.swapVertices(this.vertices.indexOf(i),this.vertices.length-1)}getFirstVertex(){return this.vertices.at(0)}getLastVertex(){return this.vertices.at(-1)}isClosed(){return this.vertices.length>2&&null!==this.vertices[0].leftEdge}swapVertices(e,t){const s=this.vertices[e];this.vertices[e]=this.vertices[t],this.vertices[t]=s}iterateVertices(e){if(0===this.vertices.length)return;const t=this.vertices[0];let s=t;do{e(s,s.index),s=null!=s.rightEdge?s.rightEdge.rightVertex:null}while(s!==t&&null!=s)}}class p extends s{constructor(e,t){super(),this.type=e,this.coordinateHelper=t,this._geometry=null,this._dirty=!0,this.components=[]}get geometry(){if(this._dirty){switch(this.type){case"point":this._geometry=this._toPoint();break;case"polyline":this._geometry=this._toPolyline();break;case"polygon":this._geometry=this._toPolygon();break;case"mesh":break;default:t(this.type)}this._dirty=!1}return this._geometry}get spatialReference(){return this.coordinateHelper.spatialReference}get allVertices(){return Array.from(this.iterateVertices())}*iterateVertices(){for(const e of this.components)for(const t of e.vertices)yield t}notifyChanges(e){this._dirty=!0,this.emit("change",e)}_toPoint(){return 0===this.components.length||0===this.components[0].vertices.length?null:this.coordinateHelper.vectorToPoint(this.components[0].vertices[0].pos)}_toPolyline(){const e=[],t=this.coordinateHelper.vectorToArray;for(const s of this.components){if(s.vertices.length<1)continue;const i=[];let r=s.vertices.find((e=>null==e.leftEdge));const o=r;do{i.push(t(r.pos)),r=r.rightEdge?r.rightEdge.rightVertex:null}while(r&&r!==o);e.push(i)}return new r({paths:e,spatialReference:this.spatialReference,hasZ:this.coordinateHelper.hasZ(),hasM:this.coordinateHelper.hasM()})}_toPolygon(){const e=[],t=this.coordinateHelper.vectorToArray;for(const s of this.components){if(s.vertices.length<1)continue;const i=[],r=s.vertices[0];let o=r;const n=o;do{i.push(t(o.pos)),o=null!=o.rightEdge?o.rightEdge.rightVertex:null}while(o&&o!==n);s.isClosed()&&i.push(t(r.pos)),e.push(i)}return new i({rings:e,spatialReference:this.spatialReference,hasZ:this.coordinateHelper.hasZ(),hasM:this.coordinateHelper.hasM()})}static fromGeometry(e,t){const s=e.spatialReference,i=o(e.hasZ,e.hasM,s),r=new p(e.type,i);switch(e.type){case"polygon":g(r,e,t);break;case"polyline":d(r,e,t);break;case"point":u(r,e,t);break;case"mesh":u(r,e.origin,t),r._geometry=e,r._dirty=!1}return r}}function g(t,s,i){const r=s.spatialReference,o=t.coordinateHelper,n=s.rings;for(let c=0;c<n.length;++c){const s=n[c],p=new a(r,i);p.index=c;const g=s.length>2&&e(s[0],s[s.length-1]),d=g?s.length-1:s.length;for(let e=0;e<d;++e){const t=o.arrayToVector(s[e]),i=new h(p);p.vertices.push(i),i.pos=t,i.index=e}const u=p.vertices.length-1;for(let e=0;e<u;++e){const t=p.vertices[e],s=p.vertices[e+1],i=new l(p,t,s);p.edges.push(i)}if(g){const e=new l(p,p.vertices[p.vertices.length-1],p.vertices[0]);p.edges.push(e)}t.components.push(p)}}function d(e,t,s){const i=t.spatialReference,r=e.coordinateHelper,o=t.paths;for(let n=0;n<o.length;++n){const t=o[n],c=new a(i,s);c.index=n;const p=t.length;for(let e=0;e<p;++e){const s=r.arrayToVector(t[e]),i=new h(c);c.vertices.push(i),i.pos=s,i.index=e}const g=c.vertices.length-1;for(let e=0;e<g;++e){const t=c.vertices[e],s=c.vertices[e+1],i=new l(c,t,s);c.edges.push(i)}e.components.push(c)}}function u(e,t,s){const i=t.spatialReference,r=e.coordinateHelper,o=new a(i,s);o.index=0;const n=new h(o);n.index=0,n.pos=r.pointToVector(t),o.vertices.push(n),e.components.push(o)}function f(e){return"mesh"===e.type}export{a as Component,l as Edge,p as EditGeometry,h as Vertex,f as isMeshEditGeometry};