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