/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{clamp as t}from"../../../../core/mathUtils.js";import{estimateNumberArrayMemory as e}from"../../../../core/memoryEstimations.js";import{create as n}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{nan as i,fromValues as r}from"../../../../geometry/support/aaBoundingBox.js";import{newContinuousIndexArray as s}from"../../../../geometry/support/Indices.js";import{MeshIntersectionOptions as a,intersectRayTriangles as o}from"./RayIntersections.js";const c=40,h=.8,d=10,m=1e-6;class l{constructor(e,i,r){this.globalTriangleVertexIndices=e,this.numTriangles=i,this.positions=r,this._rayDirection=n(),this._callback=I,this._intersectionOptions=M,this.bspNodeTree=new Array,this.triangleIndexMap=s(i);const a=x(e,i,r.data,r.stride),o=t(Math.log2(i/c),2,d),m=(t,e,n)=>{const i=u(this.triangleIndexMap,a,t,e),r=e-t;if(r<=c){const n=new g(i,void 0,0,t,e);return this.bspNodeTree.push(n),n}const{axis:s,midValue:d}=p(i),l=f(this.triangleIndexMap,a,t,e,s,d),x=(t,e)=>{if(n>o)return;const i=e-t;return i<c||i>=h*r?void 0:m(t,e,n+1)},y=new g(i,s,d,l.next,l.mid);return this.bspNodeTree.push(y),y.leftNode=x(t,l.next),y.rightNode=x(l.mid,e),y};m(0,i,0)}intersectRayTriangleRange(t,e){if(t>=e)return;const{data:n,stride:i}=this.positions;o(this._rayFrom,this._rayDirection,t,e,this.globalTriangleVertexIndices,n,i,this._intersectionOptions.normalRequired,this._callback,this.triangleIndexMap),l.numFacesTested+=e-t}intersectRay(t,e,n,i){l.numFacesTested=0;const r=t,s=e,a=s[0]-r[0],o=s[1]-r[1],c=s[2]-r[2];if(a*a+o*o+c*c<m)return;this._rayFrom=r;const h=this._rayDirection;h[0]=a,h[1]=o,h[2]=c;const d=this.triangleIndexMap.length;this._callback=i,this._intersectionOptions=n,this.intersectRayBSP(this.bspNodeTree[0],0,d),this._callback=I,this._intersectionOptions=M}intersectRayBSP(t,e,n){const i=this._rayFrom,r=this._rayDirection,s=[{node:t,rangeStart:e,rangeEnd:n}];for(;s.length>0;){const{node:t,rangeStart:e,rangeEnd:n}=s.pop();if(!y(t.aabb,i,r))continue;const a=t.axis,o=t.d;if(i[a]<o||r[a]<0){const n=e,i=t.midStartIndex;if(n<i){const e=t.leftNode;void 0!==e?s.push({node:e,rangeStart:n,rangeEnd:i}):this.intersectRayTriangleRange(n,i)}}if(this.intersectRayTriangleRange(t.midStartIndex,t.rightStartIndex),i[a]>o||r[a]>0){const e=t.rightStartIndex,i=n;if(e<i){const n=t.rightNode;void 0!==n?s.push({node:n,rangeStart:e,rangeEnd:i}):this.intersectRayTriangleRange(e,i)}}}}static{this.numFacesTested=0}get estimatedMemoryUsage(){return e(this.triangleIndexMap)}}class g{constructor(t,e,n,i,r){this.aabb=t,this.axis=e,this.d=n,this.midStartIndex=i,this.rightStartIndex=r}}function f(t,e,n,i,r,s){const a=r+3;let o=n,c=n,h=i;for(;c<h;){const n=t[c],i=6*n;e[i+a]<=s?(t[c]=t[o],t[o]=n,++o,++c):e[i+r]>=s?(--h,t[c]=t[h],t[h]=n):++c}return{next:o,mid:h}}function u(t,e,n,s){if(s<=n)return i;let a=6*t[n],o=e[a++],c=e[a++],h=e[a++],d=e[a++],m=e[a++],l=e[a];for(let i=n+1;i<s;++i)a=6*t[i],o=Math.min(o,e[a++]),c=Math.min(c,e[a++]),h=Math.min(h,e[a++]),d=Math.max(d,e[a++]),m=Math.max(m,e[a++]),l=Math.max(l,e[a]);return r(o,c,h,d,m,l)}function p(t){const e=t[3]-t[0],n=t[4]-t[1],i=t[5]-t[2],r=e>n?e>i?0:n>i?1:2:n>i?1:i>e?2:0;return{axis:r,midValue:(t[r]+t[r+3])/2}}function x(t,e,n,i){const r=new Float64Array(6*e);for(let s=0;s<e;++s){const e=3*s,a=t[e]*i,o=t[e+1]*i,c=t[e+2]*i;for(let t=0;t<3;++t){const e=n[a+t],i=n[o+t],h=n[c+t];r[6*s+t]=Math.min(e,i,h),r[6*s+3+t]=Math.max(e,i,h)}}return r}function y(t,e,n){const i=e,r=n;let s=0,a=1/0;for(let o=0;o<3;++o){{const e=t[o];if(i[o]<e){if(r[o]<=m)return!1;const t=(e-i[o])/r[o];s=Math.max(s,t)}else if(r[o]<=-m){const t=(e-i[o])/r[o];a=Math.min(a,t)}if(s>a)return!1}{const e=t[o+3];if(i[o]>e){if(r[o]>=-m)return!1;const t=(e-i[o])/r[o];s=Math.max(s,t)}else if(r[o]>=m){const t=(e-i[o])/r[o];a=Math.min(a,t)}if(s>a)return!1}}return!0}const M=new a,I=()=>{};export{l as TriangleIntersectionData};
