/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../core/Logger","../../../core/maybe","../../../chunks/mat3","../../../chunks/mat3f64","../../../chunks/mat4f64","../../../chunks/vec3","../../../chunks/vec3f64","../../projection","../../projectionEllipsoid","../spatialReferenceUtils","../webMercatorUtils","../buffer/BufferView","../../../chunks/vec32"],(function(r,e,o,t,f,n,a,c,i,u,s,m,P,y){"use strict";const V=e.getLogger("esri.geometry.support.meshUtils.normalProjection");function p(r,e,o,t,f){return w(t)?(M(h.TO_PCPF,P.BufferViewVec3f.fromTypedArray(r),P.BufferViewVec3f64.fromTypedArray(e),P.BufferViewVec3f64.fromTypedArray(o),t,P.BufferViewVec3f.fromTypedArray(f)),f):(V.error("Cannot convert spatial reference to PCPF"),f)}function l(r,e,o,t,f){return w(t)?(M(h.FROM_PCPF,P.BufferViewVec3f.fromTypedArray(r),P.BufferViewVec3f64.fromTypedArray(e),P.BufferViewVec3f64.fromTypedArray(o),t,P.BufferViewVec3f.fromTypedArray(f)),f):(V.error("Cannot convert to spatial reference from PCPF"),f)}function T(r,e,o){return i.projectBuffer(r,e,0,o,u.getSphericalPCPF(e),0,r.length/3),o}function F(r,e,o){return i.projectBuffer(r,u.getSphericalPCPF(o),0,e,o,0,r.length/3),e}function C(r,e,t){if(o.isNone(r))return e;const f=P.BufferViewVec3f64.fromTypedArray(r),n=P.BufferViewVec3f64.fromTypedArray(e);return y.transformMat4(n,f,t),e}function d(r,e,f){if(o.isNone(r))return e;t.normalFromMat4(N,f);const n=P.BufferViewVec3f.fromTypedArray(r),a=P.BufferViewVec3f.fromTypedArray(e);return y.transformMat3(a,n,N),t.isOrthoNormal(N)||y.normalize(a,a),e}function B(r,e,f){if(o.isNone(r))return e;t.normalFromMat4(N,f);const n=P.BufferViewVec3f.fromTypedArray(r,4*Float32Array.BYTES_PER_ELEMENT),a=P.BufferViewVec3f.fromTypedArray(e,4*Float32Array.BYTES_PER_ELEMENT);if(y.transformMat3(a,n,N),t.isOrthoNormal(N)||y.normalize(a,a),r!==e)for(let o=3;o<r.length;o+=4)e[o]=r[o];return e}function A(r,e,o,t,f){if(!w(t))return V.error("Cannot convert spatial reference to PCPF"),f;M(h.TO_PCPF,P.BufferViewVec3f.fromTypedArray(r,4*Float32Array.BYTES_PER_ELEMENT),P.BufferViewVec3f64.fromTypedArray(e),P.BufferViewVec3f64.fromTypedArray(o),t,P.BufferViewVec3f.fromTypedArray(f,4*Float32Array.BYTES_PER_ELEMENT));for(let n=3;n<r.length;n+=4)f[n]=r[n];return f}function g(r,e,o,t,f){if(!w(t))return V.error("Cannot convert to spatial reference from PCPF"),f;M(h.FROM_PCPF,P.BufferViewVec3f.fromTypedArray(r,16),P.BufferViewVec3f64.fromTypedArray(e),P.BufferViewVec3f64.fromTypedArray(o),t,P.BufferViewVec3f.fromTypedArray(f,16));for(let n=3;n<r.length;n+=4)f[n]=r[n];return f}function M(r,e,o,f,n,c){if(!e)return;const s=o.count,P=u.getSphericalPCPF(n);if(E(n))for(let u=0;u<s;u++)f.getVec(u,O),e.getVec(u,R),i.computeTranslationToOriginAndRotation(P,O,j,P),t.fromMat4(N,j),r===h.FROM_PCPF&&t.transpose(N,N),a.transformMat3(R,R,N),c.setVec(u,R);else for(let u=0;u<s;u++){f.getVec(u,O),e.getVec(u,R),i.computeTranslationToOriginAndRotation(P,O,j,P),t.fromMat4(N,j);const n=m.y2lat(o.get(u,1));let s=Math.cos(n);r===h.TO_PCPF&&(s=1/s),N[0]*=s,N[1]*=s,N[2]*=s,N[3]*=s,N[4]*=s,N[5]*=s,r===h.FROM_PCPF&&t.transpose(N,N),a.transformMat3(R,R,N),a.normalize(R,R),c.setVec(u,R)}return c}function w(r){return E(r)||_(r)}function E(r){return r.isWGS84||s.isCGCS2000(r)||s.isMars(r)||s.isMoon(r)}function _(r){return r.isWebMercator}var h;!function(r){r[r.TO_PCPF=0]="TO_PCPF",r[r.FROM_PCPF=1]="FROM_PCPF"}(h||(h={}));const O=c.create(),R=c.create(),j=n.create(),N=f.create();r.projectFromPCPF=F,r.projectNormalFromPCPF=l,r.projectNormalToPCPF=p,r.projectTangentFromPCPF=g,r.projectTangentToPCPF=A,r.projectToPCPF=T,r.transformNormal=d,r.transformPosition=C,r.transformTangent=B,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
