@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 23.8 kB
JavaScript
import{isSome as e,isNone as t,unwrap as i}from"../../../../core/maybe.js";import s from"../../../../core/PooledArray.js";import{isAbortError as n}from"../../../../core/promiseUtils.js";import{l as r}from"../../../../chunks/vec3.js";import{b as o}from"../../../../chunks/vec4f64.js";import{LodMetric as a,NodeBase as d,Node as l,ElevationRange as h,NodeFilterImpact as u,NodeIMModificationImpact as c,NodeTraversalState as _}from"./I3SNode.js";import{invalidateMbs as g,invalidateObb as m,addWraparound as f}from"./I3SUtil.js";import{clone as v}from"../../support/orientedBoundingBox.js";class b{constructor(e,t,i,s,n){this.childOffset=e,this.childCount=t,this.visibilityCache=i,this.ref=s,this.node=n,this.useAsHole=0,this.filterImpact=u.NotChecked}}class p{constructor(e,t,i,n,r,o,d,l,h,u,c,_,g,m){this._streamDataController=i,this._viewportQueries=n,this._logger=r,this.holeFilling=o,this._isLoaded=d,this._isReloading=l,this._isSelected=h,this._enable=u,this._needsUpdate=c,this._canRequest=_,this._computeVisibilityObb=g,this._computeNodeFiltering=m,this._dirty=!0,this._nodePages=[],this._nodeCount=0,this._nodesPerPage=0,this._rootIndex=0,this._lodMetric=a.None,this._lodConversion=e=>e,this._urlPrefix="",this._loading=new Set,this._failedNodes=new Set,this._failedPages=new Set,this._indexMissing=1,this._maxUnloadedPrio=Number.NEGATIVE_INFINITY,this._maxProcessingPrio=Number.POSITIVE_INFINITY,this._nodeTraversalState=new Map,this._version=I(0),this._visibilityCacheVersion=I(0),this._maxLevel=1,this._featureEstimate={estimate:0,leavesReached:!1},this._unloadedMemoryEstimate=0,this._missing=new s({deallocator:null}),this._prefetch=new s({deallocator:null}),this._updates=new N(this._missing),this._imModificationUncategorized=new s({deallocator:null}),this.ignoreServiceObb=!1,this.progressiveLoadPenalty=0,this._pageQueue=[],this.needNodeElevationRange=!1,this.layerHasModifications=!1,this._layerHasFilter=!1,this._logLayer=e,e.serviceUpdateTimeStamp&&e.serviceUpdateTimeStamp.lastUpdate&&(this._lastUpdate=`${e.serviceUpdateTimeStamp.lastUpdate}`),this._maxLodLevel=this._viewportQueries?this._viewportQueries.maxLodLevel:1,this._init(t)}_init(e){if("page"===e.type){switch(this._urlPrefix=e.urlPrefix,this._nodesPerPage=e.pageSize,this._rootIndex=e.rootIndex,e.lodMetric){case"maxScreenThreshold":this._lodMetric=a.MaxScreenThreshold;break;case"maxScreenThresholdSQ":this._lodMetric=a.MaxScreenThreshold,this._lodConversion=V}this._addPage(S(this._rootIndex,this._nodesPerPage),e.rootPage),this._updateParentsAndLevel()}else if("node"===e.type){this._urlPrefix=e.urlPrefix,this._nodePages.push({nodes:[],children:[],parents:[]}),this._makeRefNode(new d(e.rootNode.id,null),-1);const t=this._validateNode(e.rootNode.id,e.rootNode);t&&this._addNode(t,0)}}_loadPage(e){this._loading.add(e);const t=this._urlPrefix+e;this._streamDataController.request(t,"json").then((t=>{this._pageQueue.push({pageIndex:e,page:t})})).catch((t=>{this._loading.delete(e),n(t)||(this._failedPages.add(e),this._logger.error("#loadPage()",this._logLayer,`Error when loading page ${e}`,t))}))}_addQueuedPages(e){for(;this._pageQueue.length>0&&!e.done;){const{pageIndex:t,page:i}=this._pageQueue.shift();this._addPage(t,i),this._loading.delete(t),e.madeProgress()}this._updateParentsAndLevel()}_addPage(e,t){for(let r=this._nodePages.length;r<e;r++)this._nodePages[r]=null;const i=[],s=[],n=t.nodes.map(((t,n)=>{const a=i.length,d=t.children?t.children.length:0;s.push(-1);for(let e=0;e<d;e++)i.push(t.children[e]);const h=`${t.index}`,u=v(t.obb),c=o([u.center[0],u.center[1],u.center[2],r(u.halfSize)]),_=t.mesh&&t.mesh.attribute,g=t.mesh&&t.mesh.geometry,m=t.mesh&&t.mesh.material,f={hasSharedResource:!1,hasFeatureData:!!g,attributes:_&&null!=_.resource?`${_.resource}`:void 0,geometry:g&&null!=g.resource?`${g.resource}`:void 0,texture:m&&null!=m.resource?`${m.resource}`:void 0,geometryDefinition:g?g.definition:-1,materialDefinition:m?m.definition:-1},p=new l(h,e*this._nodesPerPage+n,c,d,0,f,this._lastUpdate,this._lodMetric,this._lodConversion(t.lodThreshold),g?g.featureCount:null);return p.serviceObb=u,p.visibilityObb=this._computeVisibilityObb(p),p.vertexCount=g?g.vertexCount:0,new b(a,d,x(this._visibilityCacheVersion),null,p)}));this._nodePages[e]={nodes:n,children:i,parents:s},this._nodeCount+=n.length}_updateParentsAndLevel(){const t=new Array,i=(i,s,n)=>{const r=this._getPage(i);if(e(r)){const o=R(i,this._nodesPerPage);r.parents[o]=s;const a=r.nodes[o].node;e(a)&&(a.level=n,t.push(i))}};for(i(this._rootIndex,-1,0);t.length;){const s=t.pop(),n=this.getNode(s);if(e(n))for(let e=0;e<n.childCount;e++){i(this.getChildIndex(n.index,e),s,n.level+1),this._maxLevel=Math.max(this._maxLevel,n.level+1)}}}_getPage(e){return this._nodePages[S(e,this._nodesPerPage)]}_getNodeInternal(e){const i=this._getPage(e);return t(i)?null:i.nodes[R(e,this._nodesPerPage)]}_addNode(t,s){null!=t.children&&this.populateChildren(s,t.children);const n=this.getParent(s),r=e(n)?n.level+1:0;this._maxLevel=Math.max(this._maxLevel,t.children?r+1:r);const{lodMetric:a,maxError:d}=E(t.lodSelection),h=i(this._getNodeInternal(s));return h.node=new l(t.id,s,o(t.mbs),h.childCount,r,t.resources,t.version,a,d,t.numFeatures),t.obb&&(h.node.serviceObb=v(t.obb)),h.node.visibilityObb=this._computeVisibilityObb(h.node),e(h.ref)&&(null==h.ref.mbs&&(h.ref.mbs=t.mbs),h.node.renderMbs=h.ref.renderMbs,h.node.serviceObbInRenderSR=h.ref.serviceObbInRenderSR,h.ref.visibilityObb=h.node.visibilityObb),h.node}_makeRefNode(e,t){const i=this._nodePages[0];if(null==i)return-1;const s=i.nodes.length;return i.nodes.push(new b(0,0,x(this._visibilityCacheVersion),e,null)),this._nodeCount++,i.parents.push(t),g(e.renderMbs),m(e.serviceObbInRenderSR),s}populateChildren(e,t){const s=i(this._getNodeInternal(e)),n=i(this._getPage(e));s.childOffset=n.children.length,s.childCount=t.length;for(let i=0;i<t.length;i++){const s=this._makeRefNode(t[i],e);n.children.push(s)}}getNode(t){const i=this._getNodeInternal(t);return e(i)?i.node:null}getIndexById(t){let i;return this._forAllNodes(((s,n)=>{(e(s.node)&&s.node.id===t||e(s.ref)&&s.ref.id===t)&&(i=n)})),i}getNodeById(e){const t=this.getIndexById(e);return null!=t&&t>=0?this.getNode(t):null}getChildIndex(e,i){const s=this._getPage(e);if(t(s))return-1;const n=s.nodes[R(e,this._nodesPerPage)];return s.children[n.childOffset+i]}getParentIndex(t){const i=this._getPage(t);return e(i)?i.parents[R(t,this._nodesPerPage)]:-1}getParent(e){return(e=this.getParentIndex(e))>=0?this.getNode(e):null}isLeaf(t){const i=this._getNodeInternal(t);return e(i)&&0===i.childCount}get rootNode(){return this.getNode(this._rootIndex)}get size(){return this._nodeCount}removeAllGeometryObbs(){this._forAllNodes((t=>{e(t.node)&&(t.node.geometryObb=null)}))}invalidateVisibilityCache(){this._visibilityCacheVersion=I(this._visibilityCacheVersion)}invalidateNodeVisibilityCache(t){const i=this._getNodeInternal(t);e(i)&&this.invalidateNodeVisibilityCacheInternal(i)}invalidateNodeVisibilityCacheInternal(e){e.visibilityCache=x(this._visibilityCacheVersion)}invalidateBoundingVolumeCache(t){const i=this._getNodeInternal(t);e(i)&&(P(i),this.invalidateNodeVisibilityCacheInternal(i))}updateElevationChanged(i){const s=this._getNodeInternal(i);if(t(s))return;if(!this.needNodeElevationRange)return void this.invalidateBoundingVolumeCache(i);const n=e(s.node)?s.node:s.ref;if(t(n))return;const r=n.elevationRange;t(r)||(r.valid=!1)}invalidateGeometryVisibility(t){const i=this._getNodeInternal(t);e(i)&&e(i.node)&&(i.node.geometryObb=null,g(i.node.renderMbs),m(i.node.serviceObbInRenderSR))}invalidateVisibilityObbs(){t(this.rootNode)||this.traverse(this.rootNode,(e=>(e.visibilityObb=this._computeVisibilityObb(e),e.geometryObb=null,!0)))}_updateElevationRange(i){const s=this._getNodeInternal(i);if(t(s))return null;const n=e(s.node)?s.node:s.ref;if(t(n))return null;const r=n.elevationRange;if(e(r)&&r.valid)return r;const o=new h;let a=!1;for(let e=0;e<s.childCount;e++){const s=this._updateElevationRange(this.getChildIndex(i,e));t(s)?a=!0:(o.min=Math.min(o.min,s.min),o.max=Math.max(o.max,s.max))}if(0===s.childCount||a&&e(s.node)&&s.node.resources.geometry){const t=this._viewportQueries.getElevationRange(n);e(t)&&(o.min=Math.min(o.min,t.min),o.max=Math.max(o.max,t.max))}return e(r)&&r.min===o.min&&r.max===o.max?(r.valid=!0,r):(o.valid=!0,n.elevationRange=o,this.invalidateBoundingVolumeCache(i),o)}isNodeVisible(i){const s=this._getNodeInternal(i);if(t(s)||e(s.ref)&&!s.ref.mbs)return!0;if(this.needNodeElevationRange&&this._updateElevationRange(i),!w(s.visibilityCache,this._visibilityCacheVersion)){const i=s.node,n=e(i)&&(t(s.ref)||e(i.visibilityObb))?i:e(s.ref)?s.ref:null;if(this._layerHasFilter&&this._computeNodeFiltering&&(e(i)||e(s.ref))&&s.filterImpact===u.NotChecked){const t=e(i)?i.mbs:e(s.ref)?s.ref.mbs:null;s.filterImpact=null!=t?this._computeNodeFiltering(t):u.Unmodified}const r=e(i)&&s.filterImpact===u.Culled,o=!(e(i)&&i.imModificationImpact===c.Culled)&&(!n||this._viewportQueries.isNodeVisible(n))&&!r;return s.visibilityCache=C(o,this._visibilityCacheVersion),o}return M(s.visibilityCache)}isGeometryVisible(t){if(!this.isNodeVisible(t))return!1;const i=this._getNodeInternal(t);return!(e(i)&&e(i.node)&&e(i.node.geometryObb)&&(!this.layerHasModifications||i.node.imModificationImpact!==c.NotChecked))||this._viewportQueries.isGeometryVisible(i.node)}_traverseCoverage(s,n,r,o,a){const d=this._getPage(s);if(t(d)||0===n.childCount)return;const l=n.childOffset+n.childCount,h=new Array;for(let t=n.childOffset;t<l;++t){const i=d.children[t],s=this._getNodeInternal(i);e(s)&&e(s.node)&&this.isGeometryVisible(i)&&h.push(s)}o/=h.length;for(const e of h){const t=i(e.node).index;this._isLoaded(t)||this._isReloading(t)?(a.delta=Math.max(a.delta,r),a.coverage+=o):this._traverseCoverage(t,e,r+1,o,a)}}useNodeAsHole(e){if("off"===this.holeFilling)return!1;const i=this._getNodeInternal(e);if(t(i))return!1;if("always"===this.holeFilling)return!0;if(w(i.useAsHole,this._version))return M(i.useAsHole);const s={delta:0,coverage:0};this._traverseCoverage(e,i,0,1,s);const n=s.delta*s.coverage<=.5;return i.useAsHole=C(n,this._version),n}get maxLevel(){return this._maxLevel}get dirty(){return this._dirty}destroy(){this._updates.add.prune(),this._updates.update.prune()}requestUpdate(){this._dirty=!0,this._indexMissing=1,this._version=I(this._version)}imModificationsChanged(t){this.layerHasModifications=t,this._forAllNodes((({node:t})=>{e(t)&&(t.imModificationImpact=c.NotChecked,t.visibilityObb=this._computeVisibilityObb(t),t.hasModifications&&this.invalidateGeometryVisibility(t.index))})),this.invalidateVisibilityCache()}layerFilterChanged(t){this._layerHasFilter=t,this._forAllNodes((t=>{if(e(t)){t.filterImpact=u.NotChecked;const i=t.node;e(i)&&this.invalidateNodeVisibilityCache(i.index)}})),this.invalidateVisibilityCache()}update(s,n,r){if(!this._dirty)return;this._pageQueue.length>0&&this._addQueuedPages(n),this._maxUnloadedPrio=Number.NEGATIVE_INFINITY,this._maxProcessingPrio=Number.NEGATIVE_INFINITY,this._missing.clear(),this._prefetch.clear(),this._updates.reset(s),y.clear();let o=!0;const a=new O,d=new O,l=this._imModificationUncategorized;l.clear();const h=new Set,u=(u,_,g)=>{if(t(_)){let e=this._entryPriority(u);e===1/0&&(e=this._entryPriority(g));const t=S(u,this._nodesPerPage);return y.set(t,Math.max(e,y.get(t)||0)),this._loading.has(t)||this._failedPages.has(t)||this._missing.push(t),void(this._maxProcessingPrio=Math.max(this._maxProcessingPrio,e))}const m=_.node;if(this._updateNodeFeatureEstimate(m,d),t(m)){const e=this._entryPriority(u);return this._loading.has(u)||this._failedNodes.has(u)||(this._missing.push(u),y.set(u,e)),void(this._maxProcessingPrio=Math.max(this._maxProcessingPrio,e))}const f=i(this._getPage(u));if(0===this._missing.length&&0===this._nodesPerPage)for(let t=0;t<_.childCount;t++){const i=f.children[_.childOffset+t],s=this._getNodeInternal(i);!e(s)||s.node||this._loading.has(i)||this._failedNodes.has(i)||(y.set(i,this._entryPriority(i)),this._prefetch.push(i))}if(m.failed||!m.resources.hasFeatureData)return void(o&&_.childCount>0&&this._isSelected(m)&&(o=!1));if(h.add(m.id),this._isLoaded(u)){if(a.known+=m.memory,++a.knownNodes,this._isSelected(m)?_.childCount>0&&(o=!1):(a.unremoved+=m.memory,o=!1),this._needsUpdate(m)){const e=this._entryPriority(u);y.set(u,e),this._maxProcessingPrio=Math.max(this._maxProcessingPrio,e),this._updates.update.push(u)}return}if(m.memory&&(a.known+=m.memory,++a.knownNodes),!this._isSelected(m))return void(this._isReloading(u)&&this._updates.remove.push(u));if(_.childCount>0&&(o=!1),m.memory?(a.missing+=m.memory,a.known+=m.memory,++a.knownNodes):++a.missingNodes,s.includes(m.index))return this._maxProcessingPrio=Math.max(this._maxProcessingPrio,this._entryPriority(u)),void(this._updates.cancel=this._updates.cancel.filter((e=>e!==m.index)));if(!n.done&&this._enable(m))return void n.madeProgress();const v=this._entryPriority(u);y.set(u,v),this._maxProcessingPrio=Math.max(this._maxProcessingPrio,v),this._updates.add.push(u),this.layerHasModifications&&r&&e(m)&&m.imModificationImpact===c.NotChecked&&l.push(u)};this.traverseVisible(u);const _=this._updates.add;_.length>0&&this.layerHasModifications&&(l.length>0&&r?.(l),_.filterInPlace((e=>{const i=this._getNodeInternal(e),s=t(i)||t(i.node)||i.node.imModificationImpact!==c.Culled;return s||this.invalidateNodeVisibilityCache(e),s}))),this._unloadedMemoryEstimate=a.missing-a.unremoved,a.knownNodes>3&&a.missingNodes>0&&(this._unloadedMemoryEstimate+=a.known/a.knownNodes*a.missingNodes),this._unloadedMemoryEstimate=.8*Math.max(0,this._unloadedMemoryEstimate),this._featureEstimate.estimate=this._computeFeatureEstimate(d),this._featureEstimate.leavesReached=o,this._missing.sort(((e,t)=>e-t)),this._missing.filterInPlace(((e,t)=>t<1||this._missing.data[t-1]!==e)),this._missing.sort(((e,t)=>y.get(e)-y.get(t))),this._missing.length>0&&(this._maxUnloadedPrio=y.get(this._missing.back()),this._prefetch.clear()),this._updates.add.filterInPlace((e=>y.get(e)>=this._maxUnloadedPrio)).sort(((e,t)=>y.get(e)-y.get(t))),this._updates.update.sort(((e,t)=>y.get(e)-y.get(t))),this._indexMissing=this._loading.size+this._missing.length,this._dirty=this._indexMissing>0,y.clear()}checkFeatureTarget(e,t){const i=this._viewportQueries.updateScreenSpaceErrorBias(t);let s=t,n=t,r=i,o=10;for(;o--;){const i=new O;this._updateFeatureEstimate(s,i);if(this._computeFeatureEstimate(i)<=e){if(s>=t||i.missingNodes>0||0===o)break;r=s,s=.5*(s+n)}else n=s,s=.5*(s+r)}return this._version=I(this._version),this._viewportQueries.updateScreenSpaceErrorBias(i),Math.min(t,s)}_updateFeatureEstimate(t,i){this._version=I(this._version),this._viewportQueries.updateScreenSpaceErrorBias(t),this.traverseVisible(((t,s)=>this._updateNodeFeatureEstimate(e(s)?s.node:void 0,i)))}_updateNodeFeatureEstimate(i,s){if(!(t(i)||i.failed||t(i.numFeatures)))return this._isLoaded(i.index)?(s.known+=i.numFeatures,++s.knownNodes,void(this._isSelected(i)||(s.unremoved+=i.numFeatures))):void(this._isSelected(i)&&(e(i.numFeatures)?(s.missing+=i.numFeatures,s.known+=i.numFeatures,++s.knownNodes):++s.missingNodes))}_computeFeatureEstimate(e){let t=e.known-e.unremoved;return e.knownNodes>3&&e.missingNodes>0&&(t+=e.known/e.knownNodes*e.missingNodes),Math.max(0,t)}load(){return this._load(this._missing)}prefetch(){return this._prefetch.sort(((e,t)=>y.get(e)-y.get(t))),this._load(this._prefetch)}_load(e){if(0===e.length||!this._canRequest())return!1;for(;e.length>0&&this._canRequest();)0===this._nodesPerPage?this._loadNode(e.pop()):this._loadPage(e.pop());return!0}get isLoading(){return this._indexMissing>0}get isPrefetching(){return this._prefetch.length>0}get indexLoading(){return this._loading.size}get indexMissing(){return this._indexMissing}get unloadedMemoryEstimate(){return this._unloadedMemoryEstimate}get updates(){return this._updates}get featureEstimate(){return this._featureEstimate}get maxPriority(){return Math.max(this._maxProcessingPrio,this._maxUnloadedPrio)}nodeTraversalState(e){if(t(e))return null;let i=this._nodeTraversalState.get(e.index);if(i&&w(i.version,this._version))return i;const s=this._viewportQueries.getLodLevel(e),n=this._viewportQueries.hasLOD(e);let r=!0;if(n){const t=this.getParentIndex(e.index);if(t>=0){const e=this._nodeTraversalState.get(t);r=!!e&&s>e.lodLevel}else r=s>0}else r=0===e.childCount;return i?(i.lodLevel=s,i.isChosen=r,i.version=C(!0,this._version),i):(i=new _(n,r,s,C(!0,this._version)),this._nodeTraversalState.set(e.index,i),i)}_loadNode(e){this._loading.add(e);const s=i(this._getNodeInternal(e)).ref;if(t(s))return void this._failedNodes.add(e);const r=s.id,o=this._urlPrefix+r,a=()=>{this._loading.delete(e),0===this._missing.length&&0===this._loading.size&&this.requestUpdate()};this._streamDataController.request(o,"json").then((t=>{a();const i=this._validateNode(r,t);if(null==i)return;i.obb&&this.invalidateNodeVisibilityCache(e);const s=this._addNode(i,e);this.nodeTraversalState(s)}),(t=>{a(),n(t)||(this._logger.error("#loadNode()",this._logLayer,"Error loading node: "+o),this._failedNodes.add(e))}))}_validateNode(e,t){if(null==t||"object"!=typeof t||t.id!==e)return this._logger.error("#validateNode()",this._logLayer,`Invalid node. Wrong type or wrong id "${e}"`),null;if(!Array.isArray(t.mbs))return this._logger.error("#validateNode()",this._logLayer,`Invalid bounding volume on node ${e}.`),null;t.sharedResource&&"./shared"!==t.sharedResource.href&&"./shared/"!==t.sharedResource.href&&this._logger.warn("#validateNode()",this._logLayer,`Invalid shared resource href on node "${e}"`),null==t.geometryData||Array.isArray(t.geometryData)&&1===t.geometryData.length&&"./geometries/0"===t.geometryData[0].href||this._logger.warn("#validateNode()",this._logLayer,`Invalid geometry data on node "${e}"`),null==t.attributeData||Array.isArray(t.attributeData)&&!t.attributeData.some(((e,t)=>e.href!==`./attributes/f_${t}/0`))||this._logger.warn("#validateNode()",this._logLayer,`Invalid attribute data on node "${e}"`),t.featureData&&t.featureData.length>1&&this._logger.warn("#validateNode()",this._logLayer,`Node ${e} has ${t.featureData.length} bundles. Only the first bundle will be loaded.`);const i=t.hasOwnProperty("obb")&&!this.ignoreServiceObb?t.obb:null,s=t.featureData&&1===t.featureData.length&&t.featureData[0].featureRange?t.featureData[0].featureRange[1]-t.featureData[0].featureRange[0]+1:void 0,n=t=>{if(null==t)return null;const i=t=>this._logger.error("#validateNode()",this._logLayer,`Invalid node reference on node ${e}: ${t}`);if("number"==typeof t.id)i(`id ${t.id} is a number instead of a string.`);else if("string"!=typeof t.id||!Array.isArray(t.mbs))return i("Missing or invalid id."),null;if(!Array.isArray(t.mbs))return i(`Invalid bounding volume on reference ${t.id}.`),null;t.href&&t.href!=="../"+t.id&&this._logger.error("#validateNode()",this._logLayer,`Invalid node href on node "${e}"`);const s=t.hasOwnProperty("obb")&&!this.ignoreServiceObb?t.obb:null,n=new d(`${t.id}`,t.mbs);return n.serviceObb=s,n.visibilityObb=this._computeVisibilityObb(n),n},r=Array.isArray(t.children)?t.children.map(n).filter((e=>null!=e)):null;return{id:e,mbs:t.mbs,obb:i,children:r,resources:{hasFeatureData:t.featureData&&t.featureData.length>0,hasSharedResource:null!=t.sharedResource,attributes:t.attributeData?e:void 0,texture:t.textureData&&t.textureData.length>0?e:void 0,geometry:null!=t.geometryData?e:void 0},version:"string"==typeof t.version?t.version:null,lodSelection:Array.isArray(t.lodSelection)?t.lodSelection:null,numFeatures:s}}resetFailedNodes(){this._failedNodes.clear(),this._failedPages.clear(),this._forAllNodes((t=>{e(t.node)&&(t.node.failed=!1)}))}_entryPriority(i){const s=this._getNodeInternal(i),n=this.getParentIndex(i);if(t(s)||n<0&&null==s.node)return n<0?1/0:this._entryPriority(n);let r=0;if(s.node&&n>=0){const e=this._nodeTraversalState.get(n);null!=e&&(r=e.lodLevel)}let o=this.progressiveLoadPenalty;for(let e=i;e>=0;e=this.getParentIndex(e))if(this._isLoaded(e)){o=0;break}const a=e(s.ref)?this._viewportQueries.distToPOI(s.ref):e(s.node)?this._viewportQueries.distToPOI(s.node):0;return-a-r*(a+this.progressiveLoadPenalty)+o}traverseVisible(e){const i=this._getNodeInternal(this._rootIndex);t(i)?e(this._rootIndex,null,null):this._traverseVisible(this._rootIndex,-1,i,e)}_traverseVisible(t,s,n,r){if(n.node&&0===n.childCount)return void(this.isGeometryVisible(t)&&r(t,n,s));if(!this.isNodeVisible(t))return;if(r(t,n,s),null==n.node)return;const o=this.nodeTraversalState(n.node);if(o?.nodeHasLOD&&o.lodLevel===this._maxLodLevel)return;const a=i(this._getPage(t));for(let i=0;i<n.childCount;i++){const s=a.children[n.childOffset+i],o=this._getNodeInternal(s);e(o)?this._traverseVisible(s,t,o,r):r(s,null,t)}}traverse(e,t){t(e)&&this.traverseChildren(e,t)}traverseChildren(t,i){const s=t.index,n=this._getNodeInternal(s);e(n)&&this._traverseChildren(s,n,i)}_traverseChildren(i,s,n){const r=this._getPage(i);if(t(r))return;const o=s.childOffset+s.childCount;for(let t=s.childOffset;t<o;++t){const i=r.children[t],s=this._getNodeInternal(i);e(s)&&e(s.node)&&n(s.node)&&this._traverseChildren(i,s,n)}}updateChildrenLoaded(t,i){let s=this.getNode(t);for(;e(s);)s.childrenLoaded+=i,s=this.getParent(s.index)}checkChildrenLoadedInvariant(){if(t(this.rootNode))return!0;const e=[],i=t=>{let s=this._isLoaded(t.index)||this._isReloading(t.index)?1:0;return this.traverseChildren(t,(e=>(s+=i(e),!1))),t.childrenLoaded!==s&&e.push(t.index),s};return i(this.rootNode),e.length&&this._logger.error("childrenLoaded invariant broken at following nodes: "+e.join(",")),e.length>0}updateStats(e){if(this._updates.add.length>0&&(e.nodes+=" + "+this._updates.add.length),(this._indexMissing||this._prefetch.length>0)&&(e.index+=" + "+this._indexMissing||this._prefetch.length),e.prio=this._maxProcessingPrio,this._featureEstimate.estimate){const t=this._featureEstimate.estimate-e.features;t>0?e.features+=" + "+t:t<0&&(e.features+=" - "+-t)}}updateElevationInfo(e,t){this.needNodeElevationRange=t&&!!e&&("relative-to-ground"===e.mode||"on-the-ground"===e.mode),this._viewportQueries.updateElevationInfo(e),this.invalidateAllElevationRanges()}invalidateAllElevationRanges(){this._forAllNodes((t=>{P(t),e(t.node)&&(t.node.elevationRange=null),e(t.ref)&&(t.ref.elevationRange=null)}))}_forAllNodes(e){for(let t=0;t<this._nodePages.length;t++){const i=this._nodePages[t];if(i){const s=t*this._nodesPerPage;for(let t=0;t<i.nodes.length;t++)e(i.nodes[t],s+t)}}}get test(){return{addNode:(e,t)=>this._addNode(e,t)}}}const y=new Map;class N{constructor(e){this.missing=e,this.update=new s({deallocator:null}),this.add=new s({deallocator:null}),this.remove=new s({deallocator:null}),this.cancel=[]}reset(e){this.add.clear(),this.update.clear(),this.cancel=e}}function P(t){e(t.node)&&(g(t.node.renderMbs),m(t.node.serviceObbInRenderSR)),e(t.ref)&&(g(t.ref.renderMbs),m(t.ref.serviceObbInRenderSR))}function x(e){return f(e,-2)}function I(e){return f(e,2)}function C(e,t){return t+(e?1:0)}function w(e,t){return(-2&e)===t}function M(e){return 1==(1&e)}function S(e,t){return 0===t?0:e/t|0}function R(e,t){return 0===t?e:e%t}const L=[["maxScreenThreshold",a.MaxScreenThreshold],["screenSpaceRelative",a.ScreenSpaceRelative],["removedFeatureDiameter",a.RemovedFeatureDiameter],["distanceRangeFromDefaultCamera",a.DistanceRangeFromDefaultCamera]];function E(e){if(e)for(let t=0;t<e.length;t++)for(const i of L)if(i[0]===e[t].metricType)return{lodMetric:i[1],maxError:e[t].maxError};return{lodMetric:a.None,maxError:0}}class O{constructor(){this.known=0,this.knownNodes=0,this.missing=0,this.missingNodes=0,this.unremoved=0}}function V(e){return Math.sqrt(e*(4/Math.PI))}export{p as I3SIndex,E as selectErrorMetric};