/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../geometry/support/aaBoundingBox","./I3SUtil","../../support/orientedBoundingBox"],(function(e,t,n){"use strict";function s(t,s,i){const o=t.index;if(!o.hasNodes(0,1))return;const u=t.queue;u.length=0,u.push(0);const a=t.masks;for(a.length=0,a.push(0);u.length>0;){const h=u.pop();let p=a.pop();const c=o.getNode(h),g=o.getRenderObb(h);let f=!0;if(null!=s.clippingBox){const i=1<<s.frustum.length;if(0==(p&i)){const o=n.toAaBoundingBox(g,t.tempAabb);e.contains(s.clippingBox,o)?p|=i:e.intersects(s.clippingBox,o)||(f=!1)}}for(let e=0;e<s.frustum.length&&f;e++){const t=1<<e;if(0==(p&t)){const i=n.intersectPlane(g,s.frustum[e]);i>0?f=!1:i<0&&(p|=t)}}if(i.predicate(h,c,f)){const e=c.firstChild,t=c.childCount;let n=!1;const s=r(e,o.pageSize),g=r(e+t-1,o.pageSize);for(let r=s;r<=g;r++)if(!o.hasPage(r)){i.pageMiss(h,r),n=!0;break}if(!n)for(let i=0;i<t;i++)u.push(e+i),a.push(p)}}}function i(e,s,i,o){const r=new n.ObbArray(e.length);for(let n=0;n<e.length;n++)t.transformObb(e[n].obb,s,r.obbs[n],i,o);return r.obbs}function o(e,t){const n=[0];for(;n.length;){const s=n.pop(),i=e[r(s,t)].nodes[u(s,t)];for(let o=0;o<i.childCount;o++){const a=i.firstChild+o;null!=e[r(a,t)]&&(e[r(a,t)].parents[u(a,t)]=s,n.push(a))}}}function r(e,t){return e/t|0}function u(e,t){return e%t}return function(){function t(e,t,n){this._pages=[],this.pageSize=0,this._nodeSR=null,this._renderSR=null,this._nodeSR=e,this._renderSR=t,this.pageSize=n}var a=t.prototype;return a.addPage=function(e,t,n=0){for(;this._pages.length<e;)this._pages.push(null);const s=i(t,this._nodeSR,this._renderSR,n);this._pages[e]={nodes:t,renderObbs:s,parents:new Uint32Array(this.pageSize)},o(this._pages,this.pageSize)},a.hasPage=function(e){return!!this._pages[e]},a.getNode=function(e){const t=this.pageSize;return this._pages[r(e,t)].nodes[u(e,t)]},a.getRenderObb=function(e){const t=this.pageSize;return this._pages[r(e,t)].renderObbs[u(e,t)]},a.getRenderCenter=function(e){return this.getRenderObb(e).center},a.setRenderObb=function(e,t){const s=this.pageSize;n.set(t,this._pages[r(e,s)].renderObbs[u(e,s)])},a.getParentId=function(e){const t=this.pageSize;return this._pages[r(e,t)].parents[u(e,t)]},a.hasNodes=function(e,t){const n=r(e,this.pageSize),s=r(e+t-1,this.pageSize);for(let i=n;i<=s;i++)if(null==this._pages[i])return!1;return!0},a.forEachNodeId=function(e){for(let t=0;t<this._pages.length;t++){const n=this._pages[t];if(n)for(let s=0;s<n.nodes.length;s++)e(t*this.pageSize+s)}},a.createVisibilityTraverse=function(){const t={index:this,queue:[],masks:[],tempAabb:e.create()};return(e,n)=>s(t,e,n)},t}()}));
