UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 15.2 kB
import"../../../geometry.js";import{neverReached as e}from"../../../core/compilerUtils.js";import t from"../../../core/Evented.js";import{deg2rad as i}from"../../../core/mathUtils.js";import{isSome as s,isNone as o,unwrapOr as n,unwrap as r}from"../../../core/maybe.js";import{createScreenPointArray as a,createRenderScreenPointArray3 as c,screenPointObjectToArray as l,castRenderScreenPointArray as h}from"../../../core/screenUtils.js";import{once as d}from"../../../core/accessorSupport/utils.js";import{f as _}from"../../../chunks/mat3.js";import{c as u}from"../../../chunks/mat3f64.js";import{c as g,s as m,m as f,i as p}from"../../../chunks/mat4.js";import{c as b}from"../../../chunks/mat4f64.js";import{k as v}from"../../../chunks/vec2.js";import{a as y,t as j,m as L,g as S,c as R,d as w,s as O,b as T}from"../../../chunks/vec3.js";import{c as A,Z as E}from"../../../chunks/vec3f64.js";import{getReferenceEllipsoid as D}from"../../../geometry/ellipsoidUtils.js";import{canProjectWithoutEngine as F,projectPoint as k,sphericalPCPFtoLonLatElevation as z,computeENUToSphericalPCPFLocalRotation as P}from"../../../geometry/projection.js";import{containsPointObject as C}from"../../../geometry/support/aaBoundingRect.js";import{create as I,distance2 as M,fromPoints as x,closestRayDistance2 as U}from"../../../geometry/support/lineSegment.js";import{create as B,fromPositionAndNormal as W,intersectRay as N}from"../../../geometry/support/plane.js";import{create as H}from"../../../geometry/support/ray.js";import{sv3d as V}from"../../../geometry/support/vectorStacks.js";import{clonePoint as G,hydratedSpatialReference as Y}from"../../../layers/graphics/hydratedFeatures.js";import{getElevationAtPoint as Z}from"../support/ElevationProvider.js";import{fromScreen as q}from"../support/geometryUtils/ray.js";import{Camera as J}from"../webgl-engine/lib/Camera.js";import{Object3D as K}from"../webgl-engine/lib/Object3D.js";import{UpdatePolicy as Q}from"../webgl-engine/lib/UpdatePolicy.js";import{WebGLLayer as X}from"../webgl-engine/lib/WebGLLayer.js";import{ManipulatorStateCustomFlags as $,ManipulatorStateFlags as ee}from"../../interactive/interfaces.js";import te from"../../../geometry/Point.js";class ie{constructor(e){this.metadata=void 0,this._camera=new J,this._elevation={offset:0,override:null},this.collisionType={type:"point"},this.collisionPriority=0,this._renderObjects=new Array,this.autoScaleRenderObjects=!0,this._available=!0,this._noDisplayCount=0,this._radius=10,this._worldSized=!1,this.focusMultiplier=2,this.touchMultiplier=2.5,this.worldOriented=!1,this._modelTransform=b(),this._worldFrame=null,this._renderLocation=A(),this._renderLocationDirty=!0,this._location=new te({x:0,y:0,z:0}),this._elevationAlignedLocation=new te,this._elevationAlignedLocationDirty=!0,this.interactive=!0,this.selectable=!1,this.grabbable=!0,this.cursor=null,this.grabCursor=null,this._grabbing=!1,this.dragging=!1,this._hovering=!1,this._selected=!1,this._state=$.None,this._focused=!1,this.events=new t.EventEmitter,this._screenLocation={screenPointArray:a(),renderScreenPointArray:c(),pixelSize:0},this._screenLocationDirty=!0,this._engineResourcesAddedToStage=!1,this._attached=!1,this._location.spatialReference=e.view.spatialReference;for(const t in e)this[t]=e[t];const i=this.view.state?.camera;i&&this._camera.copyFrom(i)}destroy(){this._removeResourcesFromStage(),this._engineResources=null,this.view=null,this._camera=null}get _stage(){return this.view?._stage}get elevationInfo(){return this._elevationInfo}set elevationInfo(e){this._elevationInfo=e,this._elevationAlignedLocationDirty=!0,this._renderLocationDirty=!0,this._updateEngineObject()}get renderObjects(){return this._renderObjects}set renderObjects(e){this._removeResourcesFromStage(),this._engineResources=null,this._renderObjects=e.slice(),this._updateEngineObject()}set available(e){e!==this._available&&(this._available=e,this._updateEngineObject())}get available(){return this._available}disableDisplay(){return this._noDisplayCount++,1===this._noDisplayCount&&this._updateEngineObject(),{remove:d((()=>{this._noDisplayCount--,0===this._noDisplayCount&&this._updateEngineObject()}))}}set radius(e){e!==this._radius&&(this._radius=e,this._updateEngineObject())}get radius(){return this._radius}set worldSized(e){e!==this._worldSized&&(this._worldSized=e,this._updateEngineObject())}get worldSized(){return this._worldSized}get modelTransform(){return this._modelTransform}set modelTransform(e){se(e)&&(this._screenLocationDirty=!0),g(this._modelTransform,e),this._updateEngineObject()}get renderLocation(){return this._renderLocationDirty&&(this._renderLocationDirty=!1,this.view.renderCoordsHelper.toRenderCoords(this.elevationAlignedLocation,this._renderLocation),this.worldOriented?(this._worldFrame||(this._worldFrame=b()),oe(this.view,this._renderLocation,this._worldFrame)):this._worldFrame&&(this._worldFrame=null)),this._renderLocation}set renderLocation(e){this.view.renderCoordsHelper.fromRenderCoords(e,this._location),this.elevationAlignedLocation=this._location}get location(){return this._location}set location(e){G(e,this._location),this._notifyLocationChanged()}_notifyLocationChanged(){this._renderLocationDirty=!0,this._screenLocationDirty=!0,this._elevationAlignedLocationDirty=!0,this._updateEngineObject(),this.events.emit("location-update",{location:this._location})}get elevationAlignedLocation(){return this._elevationAlignedLocationDirty?(this._evaluateElevationAlignment(),this._updateElevationAlignedLocation(),this._elevationAlignedLocation):this._elevationAlignedLocation}set elevationAlignedLocation(e){G(e,this._location),this._evaluateElevationAlignment(),this._location.z-=this._elevation.offset,this._updateElevationAlignedLocation(),this._updateEngineObject(),this.events.emit("location-update",{location:this._location})}_updateElevationAlignedLocation(){const e=s(this._elevation.override)?this._elevation.override:this.location.z||0;this._elevationAlignedLocation.x=this.location.x,this._elevationAlignedLocation.y=this.location.y,this._elevationAlignedLocation.z=e+this._elevation.offset,this._elevationAlignedLocation.spatialReference=Y(this.location.spatialReference),this._renderLocationDirty=!0,this._screenLocationDirty=!0,this._elevationAlignedLocationDirty=!1}grabbableForEvent(){return!0}get grabbing(){return this._grabbing}set grabbing(e){e!==this._grabbing&&(this._grabbing=e,this._setFocused(this._hovering||this._grabbing),this._updateEngineObject())}get hovering(){return this._hovering}set hovering(e){e!==this._hovering&&(this._hovering=e,this._setFocused(this._hovering||this._grabbing),this._updateEngineObject())}get selected(){return this._selected}set selected(e){e!==this._selected&&(this._selected=e,this._updateEngineObject(),this.events.emit("select-changed",{action:e?"select":"deselect"}))}get state(){return this._state}set state(e){e!==this._state&&(this._state=e,this._updateEngineObject())}updateStateEnabled(e,t){t?this.state|=e:this.state&=~e}_setFocused(e){e!==this._focused&&(this._focused=e,this.events.emit("focus-changed",{action:!0===e?"focus":"unfocus"}))}get focused(){return this._focused}get screenLocation(){return this._ensureScreenLocation(),this._screenLocation}_ensureScreenLocation(){if(!this._screenLocationDirty)return;this._screenLocation.pixelSize=this._camera.computeScreenPixelSizeAt(this.renderLocation),this._screenLocationDirty=!1;let e;if(se(this._modelTransform)){const t=this._calculateModelTransformOffset(pe);e=y(t,t,this.renderLocation)}else e=this.renderLocation;this._camera.projectToRenderScreen(e,this._screenLocation.renderScreenPointArray),this._camera.renderToScreen(this._screenLocation.renderScreenPointArray,this._screenLocation.screenPointArray)}get applyObjectTransform(){return this._applyObjectTransform}set applyObjectTransform(e){this._applyObjectTransform=e,this._screenLocationDirty=!0,this._updateEngineObject()}get attached(){return this._attached}intersectionDistance(t,i){if(!this.available)return null;const s=l(t,ne),n=this._getCollisionRadius(i),r=-1*this.collisionPriority;switch(this.collisionType.type){case"point":if(v(this.screenLocation.screenPointArray,s)<n*n)return this.screenLocation.renderScreenPointArray[2]+r;break;case"line":{const e=this.collisionType.paths,t=this._getWorldToScreenObjectScale(),i=this._calculateObjectTransform(t,he),a=n*this.screenLocation.pixelSize,c=q(this._camera,s,ae);if(o(c))return null;for(const s of e){if(0===s.length)continue;const e=L(_e,s[0],i);for(let t=1;t<s.length;t++){const o=L(ue,s[t],i),n=U(x(e,o,re),c);if(null!=n&&n<a*a){const t=y(V.get(),e,o);S(t,t,.5);const i=h(V.get());return this._camera.projectToRenderScreen(t,i),i[2]+r}R(e,o)}}break}case"disc":{const e=this.collisionType.direction,t=this.collisionType.offset??E,i=this._getWorldToScreenObjectScale(),a=this._calculateObjectTransform(i,he),c=n*this.screenLocation.pixelSize,l=q(this._camera,s,ae);if(o(l))return null;const h=_(ce,a),d=j(me,e,h),u=L(fe,t,a);W(u,d,de);const g=ge;if(N(de,l,g)&&w(g,u)<c*c)return this.screenLocation.renderScreenPointArray[2]+r;break}case"ribbon":{const{paths:e,direction:t}=this.collisionType,i=this._getWorldToScreenObjectScale(),a=this._calculateObjectTransform(i,he),c=n*this._camera.computeScreenPixelSizeAt(this.renderLocation),l=q(this._camera,s,ae);if(o(l))return null;const d=_(ce,a),u=j(me,t,d),g=this._calculateModelTransformPosition(fe);W(g,u,de);const m=ge;if(!N(de,l,m))break;for(const s of e){if(0===s.length)continue;const e=L(_e,s[0],a);for(let t=1;t<s.length;t++){const i=L(ue,s[t],a),o=M(x(e,i,re),m);if(null!=o&&o<c*c){const t=y(V.get(),e,i);S(t,t,.5);const s=h(V.get());return this._camera.projectToRenderScreen(t,s),s[2]+r}R(e,i)}}break}default:e(this.collisionType)}return null}attach(e={manipulator3D:{}}){const t=this._stage;if(!t)return;const i=e.manipulator3D;if(o(i.engineLayerId)){const e=new X({pickable:!1,updatePolicy:Q.SYNC});t.add(e),i.engineLayerId=e.id,this._engineLayer=e}else t?.getObject&&(this._engineLayer=t.getObject(i.engineLayerId));i.engineLayerReferences=(i.engineLayerReferences||0)+1,this._materialIdReferences=i.materialIdReferences,o(this._materialIdReferences)&&(this._materialIdReferences=new Map,i.materialIdReferences=this._materialIdReferences),this._camera.copyFrom(this.view.state.camera),this._attached=!0,this._updateEngineObject(),F(this._location.spatialReference,this.view.spatialReference)||(this.location=new te({x:0,y:0,z:0,spatialReference:this.view.spatialReference}))}detach(e={manipulator3D:{}}){const t=e.manipulator3D;t.engineLayerReferences--;const i=0===t.engineLayerReferences;i&&(t.engineLayerId=null),this._removeResourcesFromStage(i),this._engineResources=null,this._engineLayer=null,this._materialIdReferences=null,this._attached=!1}onViewChange(){this._camera.copyFrom(this.view.state.camera),this._screenLocationDirty=!0,this._updateEngineObject()}onElevationChange(e){k(this.location,be,e.spatialReference)&&C(e.extent,be)&&this._notifyLocationChanged()}_evaluateElevationAlignment(){if(o(this.elevationInfo))return;let e=null,t=0;const i=n(this.elevationInfo.offset,0);switch(this.elevationInfo.mode){case"on-the-ground":e=n(Z(this.view.elevationProvider,this.location,"ground"),0);break;case"relative-to-ground":t=n(Z(this.view.elevationProvider,this.location,"ground"),0)+i;break;case"relative-to-scene":t=n(Z(this.view.elevationProvider,this.location,"scene"),0)+i;break;case"absolute-height":t=i}return t!==this._elevation.offset||e!==this._elevation.override?(this._elevation.offset=t,void(this._elevation.override=e)):void 0}_updateEngineObject(){if(!this._attached)return;if(!this.available)return void this._removeResourcesFromStage();const e=this._getWorldToScreenObjectScale(),t=he;if(!0===this.autoScaleRenderObjects){const i=this._getFocusedSize(this._radius,this.focused)*e;this._calculateObjectTransform(i,t)}else this._calculateObjectTransform(e,t);const{objectsByState:i}=this._ensureEngineResources(),s=(this.focused?ee.Focused:ee.Unfocused)|(this.selected?ee.Selected:ee.Unselected),o=this._noDisplayCount>0;for(const{stateMask:n,objects:r}of i){if(o){for(const e of r)e.visible=!1;continue}const e=(n&ee.All)!==ee.None,i=(n&$.All)!==$.None,a=!e||(s&n)==(n&ee.All),c=!i||(this.state&n)==(n&$.All);if(a&&c)for(const s of r)s.visible=!0,s.transformation=t;else for(const t of r)t.visible=!1}}_ensureEngineResources(){if(o(this._engineResources)){const e=r(this._engineLayer),t=[],i=new Set;this.renderObjects.forEach((({geometry:{material:e}})=>{i.has(e)||(t.push(e),i.add(e))}));const s=new Map;this._renderObjects.forEach((e=>{const t=new K({castShadow:!1,geometries:[e.geometry]}),i=s.get(e.stateMask)||[];i.push(t),s.set(e.stateMask,i)}));const o=[];s.forEach(((e,t)=>o.push({stateMask:t,objects:e}))),this._engineResources={objectsByState:o,layer:e,materials:t}}return this._addResourcesToStage(),this._engineResources}_addResourcesToStage(){const e=this._stage;if(this._engineResourcesAddedToStage||o(this._engineResources)||!e)return;const{objectsByState:t,layer:i,materials:s}=this._engineResources;s.forEach((t=>{const i=r(this._materialIdReferences),s=i.get(t.id)||0;0===s&&e.add(t),i.set(t.id,s+1)})),t.forEach((({objects:t})=>{i.addMany(t),e.addMany(t)})),this._engineResourcesAddedToStage=!0}_removeResourcesFromStage(e=!1){const t=this._stage;if(!this._engineResourcesAddedToStage||o(this._engineResources)||!t)return;const{objectsByState:i,layer:s,materials:n}=this._engineResources;i.forEach((({objects:e})=>{s.removeMany(e),t.removeMany(e)})),n.forEach((e=>{const i=r(this._materialIdReferences),s=i.get(e.id);1===s?(t.remove(e),i.delete(e.id)):i.set(e.id,s-1)})),e&&t.remove(s),this._engineResourcesAddedToStage=!1}_getCollisionRadius(e){return this._getFocusedSize(this.radius,!0)*("touch"===e?this.touchMultiplier:1)}_getFocusedSize(e,t){return e*(t?this.focusMultiplier:1)}_getWorldToScreenObjectScale(){return this._worldSized?1:this.screenLocation.pixelSize}_calculateModelTransformPosition(e){const t=this._getWorldToScreenObjectScale(),i=this._calculateObjectTransform(t,le);return O(e,i[12],i[13],i[14])}_calculateModelTransformOffset(e){const t=this._calculateModelTransformPosition(e);return T(e,t,this.renderLocation)}_calculateObjectTransform(e,t){return m(t,e,0,0,0,0,e,0,0,0,0,e,0,0,0,0,1),this._worldFrame&&f(t,t,this._worldFrame),f(t,t,this._modelTransform),t[12]+=this.renderLocation[0],t[13]+=this.renderLocation[1],t[14]+=this.renderLocation[2],t[15]=1,s(this._applyObjectTransform)&&this._applyObjectTransform(t),t}get test(){let e=!1;if(s(this._engineResources))for(const t in this._engineResources.objectsByState){const i=this._engineResources.objectsByState[t];for(const t of i.objects)if(t.visible){e=!0;break}if(e)break}return{areAnyResourcesVisible:e}}}function se(e){return 0!==e[12]||0!==e[13]||0!==e[14]}function oe(e,t,s){switch(e.viewingMode){case"local":return p(s),!0;case"global":{const o=D(e.renderCoordsHelper.spatialReference);return z(t,0,_e,0,o.radius),P(i(_e[0]),i(_e[1]),s),!0}}}const ne=a(),re=I(),ae=H(),ce=u(),le=b(),he=b(),de=B(),_e=A(),ue=A(),ge=A(),me=A(),fe=A(),pe=A(),be=new te({x:0,y:0,z:0,spatialReference:null});export{ie as Manipulator3D};