/* COPYRIGHT Esri - https://js.arcgis.com/5.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}=M(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,E),f.usesVerticalDistanceToGround)s(f,E.verticalDistanceToGround),g=E.sampledElevation;else{"absolute-height"!==n.mode&&(g=E.sampledElevation)}const d=t(u,c??f.transformation),I=o(v,d[12],d[13],d[14]);l.TESTS_DISABLE_OPTIMIZATIONS?(T[0]=p[0],T[1]=p[1],T[2]=E.z,r(a,T,d,m.spatialReference)&&(c?t(c,d):f.transformation=d)):m.setAltitudeOfTransformation(E.z,d);const S=b/m.unitInMeters;return(Math.abs(d[12]-I[0])>=S||Math.abs(d[13]-I[1])>=S||Math.abs(d[14]-I[2])>=S)&&(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,E);const c="absolute-height"!==e.mode?E.sampledElevation:0,f=s.instanceData,p=t.instanceIndex,u=h;f.getGlobalTransform(p,u);const g=o(v,u[12],u[13],u[14]);l.TESTS_DISABLE_OPTIMIZATIONS?(T[0]=m[0],T[1]=m[1],T[2]=E.z,r(n,T,u,i.spatialReference)&&f.setGlobalTransform(p,u)):i.setAltitudeOfTransformation(E.z,u);const d=b/i.unitInMeters;return(l.TESTS_DISABLE_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}=M(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,T=n(),I=n(),S=n(),h=e(),v=n(),E=new i;function M(t,e,o,n,r){let i=!1;const s=t.transformation,c=e.requiresSampledElevationInfo;I[0]=s[12],I[1]=s[13],I[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 h=0,v=0;for(let m=0;m<g;m++){if(S[0]=p[h],S[1]=p[h+1],S[2]=p[h+2],n(d,E),c&&(v+=E.sampledElevation),l.TESTS_DISABLE_OPTIMIZATIONS)p[h]=d.array[d.offset],p[h+1]=d.array[d.offset+1],p[h+2]=E.z,a(p,o,h,p,r.spatialReference,h,1),p[h]-=I[0],p[h+1]-=I[1],p[h+2]-=I[2],i=!0;else{T[0]=p[h]+I[0],T[1]=p[h+1]+I[1],T[2]=p[h+2]+I[2],r.setAltitude(T,E.z),p[h]=T[0]-I[0],p[h+1]=T[1]-I[1],p[h+2]=T[2]-I[2];const t=b/r.unitInMeters;(Math.abs(S[0]-p[h])>=t||Math.abs(S[1]-p[h+1])>=t||Math.abs(S[2]-p[h+2])>=t)&&(i=!0)}h+=u,d.offset+=3}return v/=g,{update:i,averageGeometrySampledElevation:v}}export{g as perLodInstanceElevationAligner,p as perObjectElevationAligner,f as perVertexElevationAligner,d as sharedGeometryElevationAligner};
