/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{subtract as t,length as i,scale as e,negate as n,dot as s}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as o,fromValues as r}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{set as l}from"../../../../core/libs/gl-matrix-2/math/vec4.js";import{create as a}from"../../../../geometry/support/plane.js";import{fromPoints as c}from"../../../../geometry/support/ray.js";import{GaussianSplatTarget as u}from"../../layers/i3s/Intersector.js";import{ElevationRange as m}from"../ElevationRange.js";import{IntersectorResult as d}from"../../webgl-engine/lib/IntersectorResult.js";class p{constructor(t){this.layerView=t,this.type=0,this.slicePlaneEnabled=!1,this.isGround=!1,this.intersectionNormal=o(),this.intersectionRayDir=o(),this.intersectionPlane=a(),this.layerViewUid=t.uid}intersect(o,a,m,p){const{intersectionRayDir:g,intersectionPlane:w,layerViewUid:y,intersectionNormal:b}=this,V=c(m,p);t(g,p,m);const R=1/i(g);e(g,g,R),n(b,g),l(w,g[0],g[1],g[2],-s(g,m));const j=new h,v=new h,x=o.options.store,E=2===x,I=0!==x,P=E?new Array:null,U=(t,i,e,n,s)=>(t.point=t.point?t.point.fill(e,n,s):r(e,n,s),t.dist=i,t.normal=b,t.layerViewUid=y,t),D=m[0],A=m[1],N=m[2],S=g[0],T=g[1],q=g[2];this.layerView.data.forEachTile(t=>{const i=t.obb.minimumDistancePlane(w),e=t.obb.maximumDistancePlane(w),n=e<0,s=null!=j.dist&&null!=v.dist&&j.dist<i*R&&v.dist>e*R;if(n||s)return;if(!t.boundingVolumeIntersectsRay(m,g))return;const{positions:o,squaredScales:r,gaussianAtlasIndices:l}=t,c=l.length;for(let u=0,d=0;u<c;u++,d+=3){const t=o[d],i=o[d+1],e=o[d+2],n=t-D,s=i-A,l=e-N,c=n*S+s*T+l*q;if(c<0)continue;if(n*n+s*s+l*l-c*c>r[u])continue;const f=c*R;if((!a||a(m,p,f))&&((null==j.dist||f<j.dist)&&U(j,f,t,i,e),I&&(null==v.dist||f>v.dist)&&U(v,f,t,i,e),E)){const n=new h;P.push(U(n,f,t,i,e))}}});const G=(t,i)=>{const{layerViewUid:e}=i,n=new u(i.point,e);t.set(0,n,i.dist,i.normal)};if(f(j)){const t=o.results.min;(null==t.distance||j.dist<t.distance)&&G(t,j)}if(f(v)&&0!==o.options.store){const t=o.results.max;(null==t.distance||v.dist>t.distance)&&G(t,v)}if(E&&P?.length)for(const t of P){const i=new d(V);G(i,t),o.results.all.push(i)}}getElevationRange(t){let i=null;return this.layerView.data.forEachTile(e=>{i?.contains(e.elevationRange)||e.boundingVolumeIntersectsSphere(t)&&(i||(i=new m),i.expandElevationRange(e.elevationRange))}),i||(i=new m(0,0)),i}}function f(t){return null!=t.dist&&null!=t.point}class h{constructor(){this.point=null,this.dist=null,this.normal=null,this.layerViewUid=""}}export{p as GaussianSplatIntersectionHandler};
