/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{copy as t}from"../../../../core/libs/gl-matrix-2/math/mat4.js";import{create as e}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{set as o}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as n}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{computeTranslationToOriginAndRotation as r}from"../../../../geometry/projection/computeTranslationToOriginAndRotation.js";import{projectBuffer as a}from"../../../../geometry/projection/projectBuffer.js";import{SampleElevationInfo as i,updateVertexPointGroundDistance as s}from"./elevationAlignmentUtils.js";import{debugFlags as l}from"../../support/debugFlags.js";import{SamplePosition as m}from"../../support/ElevationProvider.js";import{isGeometryWithMapPositions as c}from"../../webgl-engine/lib/GeometryWithMapPositions.js";function f(t,e,o,n,r){const a=t.stageObject,i=a.geometries;let s=0;for(const l of i){if(!c(l))continue;const{update:t,averageGeometrySampledElevation:i}=j(l,e,o,n,r);s+=i,t&&a.geometryVertexAttributeUpdated(l,"position")}return s/i.length}function p(e,n,a,i,m,c){const f=e.stageObject,p=n.centerInElevationSR;let g=0;if(i(p,T),f.usesVerticalDistanceToGround)s(f,T.verticalDistanceToGround),g=T.sampledElevation;else{"absolute-height"!==n.mode&&(g=T.sampledElevation)}const d=t(u,c??f.transformation),v=o(M,d[12],d[13],d[14]);l.ENABLE_OPTIMIZATIONS?m.setAltitudeOfTransformation(T.z,d):(h[0]=p[0],h[1]=p[1],h[2]=T.z,r(a,h,d,m.spatialReference)&&(c?t(c,d):f.transformation=d));const I=b/m.unitInMeters;return(Math.abs(d[12]-v[0])>=I||Math.abs(d[13]-v[1])>=I||Math.abs(d[14]-v[2])>=I)&&(c?t(c,d):f.transformation=d),g}const u=e();function g(t,e,n,a,i){const s=t.graphics3DSymbolLayer.lodRenderer;if(null==s)return 0;const m=e.centerInElevationSR;a(m,T);const c="absolute-height"!==e.mode?T.sampledElevation:0,f=s.instanceData,p=t.instanceIndex,u=E;f.getGlobalTransform(p,u);const g=o(M,u[12],u[13],u[14]);l.ENABLE_OPTIMIZATIONS?i.setAltitudeOfTransformation(T.z,u):(h[0]=m[0],h[1]=m[1],h[2]=T.z,r(n,h,u,i.spatialReference)&&f.setGlobalTransform(p,u));const d=b/i.unitInMeters;return(!l.ENABLE_OPTIMIZATIONS||Math.abs(u[12]-g[0])>=d||Math.abs(u[13]-g[1])>=d||Math.abs(u[14]-g[2])>=d)&&f.setGlobalTransform(p,u),c}function d(t,e,o,n,r){const a=t.stageObject,i=a.geometries;if(0===i.length)return 0;let s=0,l=null,m=0,f=!1;for(const p of i){if(!c(p))continue;const t=p.attributes.get("position");if(t!==l){const{update:a,averageGeometrySampledElevation:i}=j(p,e,o,n,r);m=i,l=t,f=a}f&&a.geometryVertexAttributeUpdated(p,"position"),s+=m}return s/i.length}const b=.01,h=n(),v=n(),I=n(),E=e(),M=n(),T=new i;function j(t,e,o,n,r){let i=!1;const s=t.transformation,c=e.requiresSampledElevationInfo;v[0]=s[12],v[1]=s[13],v[2]=s[14],t.invalidateBoundingInfo();const f=t.getMutableAttribute("position"),p=f.data,u=f.size,g=p.length/u,d=new m(t.mapPositions,o);let E=0,M=0;for(let m=0;m<g;m++){if(I[0]=p[E],I[1]=p[E+1],I[2]=p[E+2],n(d,T),c&&(M+=T.sampledElevation),l.ENABLE_OPTIMIZATIONS){h[0]=p[E]+v[0],h[1]=p[E+1]+v[1],h[2]=p[E+2]+v[2],r.setAltitude(h,T.z),p[E]=h[0]-v[0],p[E+1]=h[1]-v[1],p[E+2]=h[2]-v[2];const t=b/r.unitInMeters;(Math.abs(I[0]-p[E])>=t||Math.abs(I[1]-p[E+1])>=t||Math.abs(I[2]-p[E+2])>=t)&&(i=!0)}else p[E]=d.array[d.offset],p[E+1]=d.array[d.offset+1],p[E+2]=T.z,a(p,o,E,p,r.spatialReference,E,1),p[E]-=v[0],p[E+1]-=v[1],p[E+2]-=v[2],i=!0;E+=u,d.offset+=3}return M/=g,{update:i,averageGeometrySampledElevation:M}}export{g as perLodInstanceElevationAligner,p as perObjectElevationAligner,f as perVertexElevationAligner,d as sharedGeometryElevationAligner};
