/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import t from"../../../core/Logger.js";import{fromMat4 as e}from"../../../core/libs/gl-matrix-2/math/mat3.js";import{create as r}from"../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{fromRotationTranslationScaleOrigin as o,multiply as i,getScale as n,scale as s,getTranslation as a,fromRotation as l}from"../../../core/libs/gl-matrix-2/math/mat4.js";import{create as c}from"../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{setAxisAngle as m}from"../../../core/libs/gl-matrix-2/math/quat.js";import{create as f}from"../../../core/libs/gl-matrix-2/factories/quatf64.js";import{h as p,o as g,t as u,p as x}from"../../../chunks/vec32.js";import{create as j,ZEROS as v,ONES as b}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import h from"../../Point.js";import{getSphericalPCPF as A}from"../../spatialReferenceEllipsoidUtils.js";import{computeTranslationToOriginAndRotation as R}from"../../projection/computeTranslationToOriginAndRotation.js";import{projectPointToVector as d}from"../../projection/projectPointToVector.js";import{angleRad as y,axis as w,fromMatrix as F,create as S}from"../axisAngleDegrees.js";import T from"../MeshTransform.js";import{isMeshWithRelativeVertexSpace as U}from"../meshVertexSpaceUtils.js";import{performGlobalOperation as q}from"./geographicUtils.js";import{logProjectionError as C,loadProjectErrorMessage as k,projectToPCPF as z,projectNormalToPCPF as L,projectTangentToPCPF as M,projectFromPCPF as P,projectNormalFromPCPF as V,projectTangentFromPCPF as D}from"./projection.js";import{projectPointToVertexSpace as E}from"./vertexSpaceConversion.js";const O=()=>t.getLogger("esri.geometry.support.meshUtils.rotate");function B(t,e,r){if(!t.vertexAttributes?.position||0===e[3])return;const{spatialReference:o,vertexSpace:i}=t,n=r?.origin??t.origin,s=r?.geographic,a=q(O,i,o,s);U(t)?G(t,e,n):a?H(t,e,n):I(t,e,n)}function G(t,e,r){t.transform??=new T;const{vertexSpace:l,transform:c,spatialReference:f}=t,[u,x,A]=l.origin,R=new h({x:u,y:x,z:A,spatialReference:f}),d=K;if(R.equals(r))p(d,0,0,0);else if(!E(d,r,t))return void C(O(),r.spatialReference,f,k);m(Z,w(e),y(e));const S=o(N,Z,v,b,d),{localMatrix:U}=c,q=i(N,S,U);c.scale=n(j(),q),s(q,q,g(K,c.scale));const z=c.rotationAxis;c.rotation=F(q),0===c.rotationAngle&&(c.rotationAxis=z),c.translation=a(j(),q)}function H(t,r,o){const i=t.spatialReference,n=A(i),s=Y;if(!d(o,s,n)&&(C(O(),o.spatialReference,n,"Falling back to mesh origin"),!d(t.origin,s,n)))return void C(O(),t.origin.spatialReference,n);const a=t.vertexAttributes.position,l=t.vertexAttributes.normal,c=t.vertexAttributes.tangent,m=new Float64Array(a.length),f=null!=l?new Float32Array(l.length):null,p=null!=c?new Float32Array(c.length):null;R(n,s,W,n),e(X,W);const g=Q;u(w(Q),w(r),X),g[3]=r[3],z(a,i,m,n)&&(null==l||null==f||L(l,a,i,m,n,f))&&(null==c||null==p||M(c,a,i,m,n,p))?(J(m,g,3,s),P(m,n,a,i)&&(null==l||null==f||(J(f,g,3),V(f,a,i,m,n,l)))&&(null==c||null==p||(J(p,g,4),D(p,a,i,m,n,c)))?t.vertexAttributesChanged():C(O(),n,i)):C(O(),i,n)}function I(t,e,r){const o=Y;if(!d(r,o,t.spatialReference)){const e=t.origin;return o[0]=e.x,o[1]=e.y,o[2]=e.z,void C(O(),r.spatialReference,t.spatialReference,k)}J(t.vertexAttributes.position,e,3,o),J(t.vertexAttributes.normal,e,3),J(t.vertexAttributes.tangent,e,4),t.vertexAttributesChanged()}function J(t,e,r,o=v){if(null!=t){l(W,y(e),w(e));for(let e=0;e<t.length;e+=r){for(let r=0;r<3;r++)K[r]=t[e+r]-o[r];x(K,K,W);for(let r=0;r<3;r++)t[e+r]=K[r]+o[r]}}}const K=j(),N=c(),Q=S(),W=c(),X=r(),Y=j(),Z=f();export{B as rotate};
