UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) • 12.1 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import t from"../../../Camera.js";import e from"../../../Graphic.js";import n from"../../../Viewpoint.js";import{result as r}from"../../../core/asyncUtils.js";import{cyclicalDegrees as o}from"../../../core/Cyclical.js";import a from"../../../core/Error.js";import{throwIfAborted as i}from"../../../core/promiseUtils.js";import{fromMat4 as s,transpose as c}from"../../../core/libs/gl-matrix-2/math/mat3.js";import{create as l}from"../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{create as m}from"../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{scale as u,add as f,distance as p,copy as g,subtract as y,set as h,transformMat3 as d}from"../../../core/libs/gl-matrix-2/math/vec3.js";import{create as w}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import x from"../../../geometry/Extent.js";import j from"../../../geometry/Geometry.js";import v from"../../../geometry/Multipoint.js";import b from"../../../geometry/Point.js";import{projectAsync as R,tryProject as z}from"../../../geometry/projectionUtils.js";import G from"../../../geometry/SpatialReference.js";import{computeTranslationToOriginAndRotation as B}from"../../../geometry/projection/computeTranslationToOriginAndRotation.js";import{projectPointToVectorAsync as M}from"../../../geometry/projection/projectPointToVector.js";import{projectVectorToPoint as S}from"../../../geometry/projection/projectVectorToPoint.js";import{projectVectorToVector as Z}from"../../../geometry/projection/projectVectorToVector.js";import{create as F,center as T,toRect as P,isPoint as A,empty as E,expandWithVec3 as V,expandWithAABB as O,width as U,height as k,depth as N}from"../../../geometry/support/aaBoundingBox.js";import{create as I,isPoint as C}from"../../../geometry/support/aaBoundingRect.js";import{polygonCentroidPoint as W}from"../../../geometry/support/centroid.js";import{intersectsPoint as Y}from"../../../geometry/support/frustum.js";import{getResolutionInMetersForScale as q}from"../../../geometry/support/scaleUtils.js";import{fromRenderCamera as D,toRenderCamera as J}from"../webgl.js";import{cameraOnContentAlongViewDirection as X}from"../camera/intersectionUtils.js";import{getViewSR as _,fromExtentAsync as H,scaleToDistance as K,fromCenterDistanceAsync as L,fromExtentSync as Q,distanceToScale as $,fromCenterDistanceSync as tt,fromCenterScale as et,zoomToScale as nt}from"./cameraUtils.js";import{getElevationAtPoint as rt}from"./ElevationProvider.js";import{directionToHeadingTilt as ot}from"./viewingModeUtils.js";const at=.66;function it(t){return 360-o.normalize(t)}function st(t){return o.normalize(360-t)}function ct(t,e,n){const r=e.camera;if(null!=r)return mt(r,_(t));const{targetGeometry:o}=e;if(null==o)return null;const{camera:a,mode:i}=ft(t,e.rotation,n);if("point"===o.type)return pt(t,e,o,a,i);const s=o.extent;return null==s?null:Q(t,s,a.heading,a.tilt,i)}async function lt(t,e,n,r){const o=e.camera;if(null!=o)return ut(o,_(t),r);const{targetGeometry:a}=e;if(null==a)throw new Error("Viewpoint has no targetGeometry!");const{camera:i,mode:s}=ft(t,e.rotation,n);if("point"===a.type)return gt(t,e,a,i,s,r);const c=a.extent;if(null==c)throw new Error("Target geometry has no extent!");return H(t,c,i.heading,i.tilt,s,r)}function mt(t,e){const n=t.position,r=z(n,e);if(!r)return null;const o=t.clone();return o.position=r.clone(),o}async function ut(t,e,n){const r=t.position,o=await R(r,e,{signal:n});i(n);const a=t.clone();return a.position=o.clone(),a}function ft(t,e,n){const r=D(t,t.state.camera);let o=1;return null!=e&&(r.heading=it(e),o=0),null!=n&&(r.tilt=n),{camera:r,mode:o}}function pt(t,e,n,r,o){const a=t.spatialReference,i=z(n.clone(),a);if(!i)return null;const s=null!=e.scale?K(t,e.scale):t.state.camera.distance;return tt(t,i,s,r,o)}async function gt(t,e,n,r,o,a){const s=t.spatialReference,c=await R(n.clone(),s,{signal:a});i(a);const l=null!=e.scale?K(t,e.scale):t.state.camera.distance;return L(t,c,l,r,o,a)}function yt(t,e,r=null){return null==r&&(r=new n),jt(t,null,e.clone(),r)}async function ht(e,r,o){const i=At(e,r);if(!i)throw new a("viewpointutils-create:no-target","Missing target for creating viewpoint");const s=new t({fov:e.camera.fov}),c=new n({camera:s});if(i.target instanceof n){return Et(await zt(e,i.target,i,o,c))}if(i.target instanceof t)return Et(await Gt(e,i.target,o,c));const l=null!=i.scale||null!=i.zoom;if(i.target instanceof x){const t=i.target.xmin===i.target.xmax||i.target.ymin===i.target.ymax;return Et(l||t?await Mt(e,i,i.target.center,s,o,c):await Ft(e,i,i.target,s,o,c))}const m=new Ot,u=l?wt(e,i):void 0;if(await Rt(e,i.target,u,m,o),isFinite(m.boundingBox[0])){let t;if(T(m.boundingBox,Ut),Jt.x=Ut[0],Jt.y=Ut[1],Jt.z=Ut[2],Jt.spatialReference=e.spatialReference,isFinite(Jt.z)&&m.hasZ?t=A(m.boundingBox):(Jt.z=void 0,t=C(P(m.boundingBox,Ct))),l||t)return Et(await Mt(e,i,Jt,s,o,c));const n=Vt(e,m.screenSpaceObjects);return Et(await Pt(e,i,Jt,m.boundingBox,n,s,o,c))}return i.position?Et(await St(e,i,s,c,o)):Et(await Zt(e,i,s,o,c))}function dt(t,e){return null==e.scale&&null!=e.zoom?nt(t,e.zoom):e.scale}function wt(t,e){const n=dt(t,e);return n?q(n):void 0}function xt(t,e){let n=!1;return null!=e.heading?(t.heading=e.heading,n=!0):null!=e.rotation&&(t.heading=it(e.rotation),n=!0),null!=e.tilt&&(t.tilt=e.tilt,n=!0),null!=e.fov&&(t.fov=e.fov),n}function jt(t,e,n,r){const o=t.spatialReference||G.WGS84;if(e??=J(t,n),null==e)return r;const a=new b({spatialReference:o});return S(e.center,t.renderSpatialReference,a)?(r.targetGeometry=a,r.scale=$(t,e.distance),r.rotation=st(n.heading),r.camera=n,r):r}async function vt(t,e,n,r){const o=()=>new a("viewpointutils:invalid-geometry","The target is missing a valid geometry");if(!e)throw o();"mesh"===e.type&&(e=e.extent);const i=t.basemapTerrain.spatialReference;if(!e.hasZ&&t.basemapTerrain){let n;switch(e.type){case"point":n=e;break;case"multipoint":case"polyline":n=e.extent?.center;break;case"extent":n=e.center;break;case"polygon":{const t=W(e);n=t?b.fromJSON(t):null;break}}null!=n&&i&&t.elevationProvider?(n=await R(n,i,{signal:r}),Ut[2]=rt(t.elevationProvider,n)??0):Ut[2]=0}const s=Xt[e.type],c=new Array;if(s(e,e.hasZ?t=>{c.push([t[0],t[1],t[2]])}:t=>{c.push([t[0],t[1]])},Ut),0===c.length)throw o();const l=e.spatialReference,m=t.spatialReference,u=await R(new v({spatialReference:l,hasZ:e.hasZ,hasM:!1,points:c}),m,{signal:r});if(e.hasZ&&(n.hasZ=!0),e.hasZ)for(const[a,f,p]of u.points)Ut[0]=a,Ut[1]=f,Ut[2]=p,V(n.boundingBox,Ut);else for(const[a,f]of u.points)Ut[0]=a,Ut[1]=f,V(n.boundingBox,Ut)}async function bt(t,e,n,o,a){const i=await r(t.whenViewForGraphic(e));if(!1===i.ok||null==i.value||!("whenGraphicBounds"in i.value))return void await vt(t,e.geometry,o,a);const s=i.value,c=await r(s.whenGraphicBounds(e,{minDemResolution:n}));if(!1===c.ok||!c.value)return void await vt(t,e.geometry,o,a);const{screenSpaceObjects:l,boundingBox:m}=c.value;O(o.boundingBox,m),l&&l.forEach(t=>{o.screenSpaceObjects.push(t)}),isFinite(m[2])&&(o.hasZ=!0)}async function Rt(t,n,r,o,a){if(Array.isArray(n)&&2===n.length){const e=n[0],r=n[1];if("number"==typeof e&&"number"==typeof r)return Jt.x=e,Jt.y=r,Jt.z=void 0,Jt.spatialReference=t.spatialReference?.isGeographic?t.spatialReference:G.WGS84,void await vt(t,Jt,o,a)}n&&"map"in n&&"function"==typeof n.map?await Promise.allSettled(n.map(e=>Rt(t,e,r,o,a))):n instanceof j?await vt(t,n,o,a):n instanceof e&&await bt(t,n,r,o,a)}async function zt(t,e,n,r,o){if(e.camera)return Gt(t,e.camera,r,o);o.scale=e.scale,o.rotation=e.rotation,o.targetGeometry=null!=e.targetGeometry?e.targetGeometry.clone():null,o.camera=null,null!=n.heading?o.rotation=st(n.heading):null!=n.rotation&&(o.rotation=n.rotation);const a=dt(t,n);return null!=a&&(o.scale=a),o.camera=await lt(t,o,n.tilt,r),o}async function Gt(t,e,n,r){const o=t.spatialReference,a=await R(e.position,o,{signal:n}),i=e.clone();return i.position=a,jt(t,null,i,r)}async function Bt(t,e,n,r,o,a,i){const s=t.renderSpatialReference;return await M(e,qt,s,{signal:i}),await M(n,Yt,s,{signal:i}),a.targetGeometry=new b(e),o.position=new b(n),y(Wt,qt,Yt),ot(t,Yt,Wt,r.up,o),a.scale=$(t,p(Yt,qt)),a.rotation=st(o.heading),a.camera=o,a}async function Mt(t,e,n,r,o,a){a.targetGeometry=n.clone();const i=X(t);if(e.position)return Bt(t,a.targetGeometry,e.position,i,r,a,o);if(e.zoomFactor){const n=i.distance/e.zoomFactor,r=u(Ut,i.viewForward,-n);i.eye=f(Ut,i.center,r),a.scale=$(t,n)}D(t,i,r);const s=xt(r,e)?0:1;if(!e.zoomFactor){const n=dt(t,e);if(null==n){await M(a.targetGeometry,Ut,t.renderSpatialReference,{signal:o});const e=Y(i.frustum,Ut)?p(i.eye,Ut):i.distance;a.camera=await L(t,a.targetGeometry,e,r,s),a.scale=$(t,e)}else a.scale=n,a.camera=await et(t,a.targetGeometry,a.scale,r,s,o)}return a}async function St(t,e,n,r,o){const a=X(t);g(Wt,a.viewForward),ot(t,a.eye,Wt,a.up,Dt);const i=t.spatialReference,{position:s}=e;if(s){const t=await R(s,i,{signal:o});n.position=t}else n.position=new b;return n.heading=null!=e.heading?e.heading:Dt.heading,n.tilt=null!=e.tilt?e.tilt:Dt.tilt,jt(t,null,n,r)}async function Zt(t,e,n,r,o){if(null!=e.heading||null!=e.rotation||null!=e.scale||null!=e.tilt||null!=e.zoom||null!=e.zoomFactor){const a=X(t),{spatialReference:i,renderSpatialReference:s}=t,c=new b({spatialReference:i});return S(a.center,s,c)?Mt(t,e,c,n,r,o):o}return o.scale=t.scale,o.camera=t.camera.clone(),xt(o.camera,e),o}async function Ft(t,e,n,r,o,a){a.targetGeometry=n.clone();const i=X(t);D(t,i,r);const s=xt(r,e)?0:1;return a.camera=await H(t,n,r.heading,r.tilt,s,o),a}function Tt(t,e,n,r,o){let a=0;null!=n.z?a=n.z:t.basemapTerrain&&t.elevationProvider&&(a=rt(t.elevationProvider,n)),h(Ut,n.x,n.y,a),B(t.spatialReference,Ut,kt,t.renderSpatialReference),s(Nt,kt),c(Nt,Nt),E(It);const i=[[0,1,2],[3,1,2],[0,4,2],[3,4,2],[0,1,5],[3,1,5],[0,4,5],[3,4,5]];for(let s=0;s<i.length;s++){const e=i[s];let n=r[e[2]];isFinite(n)||(n=a),h(Ut,r[e[0]],r[e[1]],n),Z(Ut,t.spatialReference,Ut,t.renderSpatialReference),V(It,d(Ut,Ut,Nt))}const l=U(It),m=k(It),u=N(It),f=1/Math.tan(e.fovX/2),p=1/Math.tan(e.fovY/2),g=.5*Math.sqrt(l*l+u*u)*Math.max(p,f)+.5*m,y=.5*m*p+.5*Math.max(l,u);return Math.max(g,y)/o}async function Pt(t,e,n,r,o,a,i,s){s.targetGeometry=n.clone();const c=X(t),l=Tt(t,c,n,r,o);D(t,c,a);const m=xt(a,e)?0:1;return s.camera=await L(t,s.targetGeometry,l,a,m,i),s.scale=$(t,l),s}function At(t,e){if(!e||!t.spatialReference)return null;const n={target:void 0};return"declaredClass"in e||Array.isArray(e)?n.target=e:(Object.assign(n,e),!n.target&&"center"in e&&e.center&&(n.target=e.center)),n}function Et(t){return null!=t?.camera&&(t.rotation=st(t.camera.heading)),t}function Vt(t,e){const n=at;if(!e.length)return n;let r=Number.NEGATIVE_INFINITY;for(let o=0;o<e.length;o++){const t=e[o].screenSpaceBoundingRect;r=Math.max(r,Math.abs(t[0]),Math.abs(t[1]),Math.abs(t[2]),Math.abs(t[3]))}return n-r/Math.min(t.width,t.height)*2}class Ot{constructor(){this.hasZ=!1,this.boundingBox=E(),this.screenSpaceObjects=new Array}}const Ut=w(),kt=m(),Nt=l(),It=F(),Ct=I(),Wt=w(),Yt=w(),qt=w(),Dt={heading:0,tilt:0},Jt=new b,Xt={point(t,e,n){n[0]=t.x,n[1]=t.y,null!=t.z&&(n[2]=t.z),e(n)},polygon(t,e,n){const r=t.hasZ;for(let o=0;o<t.rings.length;o++){const a=t.rings[o];for(let t=0;t<a.length;t++)n[0]=a[t][0],n[1]=a[t][1],r&&(n[2]=a[t][2]),e(n)}},polyline(t,e,n){const r=t.hasZ;for(let o=0;o<t.paths.length;o++){const a=t.paths[o];for(let t=0;t<a.length;t++)n[0]=a[t][0],n[1]=a[t][1],r&&(n[2]=a[t][2]),e(n)}},multipoint(t,e,n){const r=t.points,o=t.hasZ;for(let a=0;a<r.length;a++)n[0]=r[a][0],n[1]=r[a][1],o&&(n[2]=r[a][2]),e(n)},extent(t,e,n){null!=t.zmin&&null!=t.zmax?(e(h(n,t.xmin,t.ymin,t.zmin)),e(h(n,t.xmax,t.ymin,t.zmin)),e(h(n,t.xmin,t.ymax,t.zmin)),e(h(n,t.xmax,t.ymax,t.zmin)),e(h(n,t.xmin,t.ymin,t.zmax)),e(h(n,t.xmax,t.ymin,t.zmax)),e(h(n,t.xmin,t.ymax,t.zmax)),e(h(n,t.xmax,t.ymax,t.zmax))):(e(h(n,t.xmin,t.ymin,n[2])),e(h(n,t.xmax,t.ymin,n[2])),e(h(n,t.xmin,t.ymax,n[2])),e(h(n,t.xmax,t.ymax,n[2])))}};export{ht as create,yt as fromCamera,lt as toCameraAsync,ct as toCameraSync};