@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 2.41 kB
JavaScript
import i from"../../../../core/PooledArray.js";import{h as t,d as e}from"../../../../chunks/vec3.js";import{f as s,a as n,c as r}from"../../../../chunks/vec3f64.js";import{assert as h}from"./Util.js";class o{constructor(i,e,o,c){this.primitiveIndices=i,this._numIndexPerPrimitive=e,this.indices=o,this.position=c,this._children=void 0,h(i.length>=1),h(o.length%this._numIndexPerPrimitive==0),h(o.length>=i.length*this._numIndexPerPrimitive),h(3===c.size||4===c.size);const{data:d,size:l}=c,m=i.length;let u=l*o[this._numIndexPerPrimitive*i[0]];a.clear(),a.push(u);const f=s(d[u],d[u+1],d[u+2]),x=n(f);for(let t=0;t<m;++t){const e=this._numIndexPerPrimitive*i[t];for(let i=0;i<this._numIndexPerPrimitive;++i){u=l*o[e+i],a.push(u);let t=d[u];f[0]=Math.min(t,f[0]),x[0]=Math.max(t,x[0]),t=d[u+1],f[1]=Math.min(t,f[1]),x[1]=Math.max(t,x[1]),t=d[u+2],f[2]=Math.min(t,f[2]),x[2]=Math.max(t,x[2])}}this.bbMin=f,this.bbMax=x;const P=t(r(),this.bbMin,this.bbMax,.5);this.radius=.5*Math.max(Math.max(x[0]-f[0],x[1]-f[1]),x[2]-f[2]);let v=this.radius*this.radius;for(let t=0;t<a.length;++t){u=a.getItemAt(t);const i=d[u]-P[0],e=d[u+1]-P[1],s=d[u+2]-P[2],n=i*i+e*e+s*s;if(n<=v)continue;const r=Math.sqrt(n),h=.5*(r-this.radius);this.radius=this.radius+h,v=this.radius*this.radius;const o=h/r;P[0]+=i*o,P[1]+=e*o,P[2]+=s*o}this.center=P,a.clear()}getChildren(){if(this._children||e(this.bbMin,this.bbMax)<=1)return this._children;const i=t(r(),this.bbMin,this.bbMax,.5),s=this.primitiveIndices.length,n=new Uint8Array(s),h=new Array(8);for(let t=0;t<8;++t)h[t]=0;const{data:a,size:c}=this.position;for(let t=0;t<s;++t){let e=0;const s=this._numIndexPerPrimitive*this.primitiveIndices[t];let r=c*this.indices[s],o=a[r],d=a[r+1],l=a[r+2];for(let i=1;i<this._numIndexPerPrimitive;++i){r=c*this.indices[s+i];const t=a[r],e=a[r+1],n=a[r+2];t<o&&(o=t),e<d&&(d=e),n<l&&(l=n)}o<i[0]&&(e|=1),d<i[1]&&(e|=2),l<i[2]&&(e|=4),n[t]=e,++h[e]}let d=0;for(let t=0;t<8;++t)h[t]>0&&++d;if(d<2)return;const l=new Array(8);for(let t=0;t<8;++t)l[t]=h[t]>0?new Uint32Array(h[t]):void 0;for(let t=0;t<8;++t)h[t]=0;for(let t=0;t<s;++t){const i=n[t];l[i][h[i]++]=this.primitiveIndices[t]}this._children=new Array;for(let t=0;t<8;++t)void 0!==l[t]&&this._children.push(new o(l[t],this._numIndexPerPrimitive,this.indices,this.position));return this._children}static prune(){a.prune()}}const a=new i({deallocator:null});export{o as BoundingInfo};