/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
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 m}from"../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{setAxisAngle as c}from"../../../core/libs/gl-matrix-2/math/quat.js";import{create as f}from"../../../core/libs/gl-matrix-2/factories/quatf64.js";import{set as p,inverse as g,transformMat3 as u,transformMat4 as x}from"../../../core/libs/gl-matrix-2/math/vec3.js";import{ZEROS as j,ONES as b,create as v}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import A from"../../Point.js";import{getSphericalPCPF as h}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 z,projectToPCPF as L,projectNormalToPCPF as M,projectTangentToPCPF as P,projectFromPCPF as V,projectNormalFromPCPF as k,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;if(U(t))G(t,e,n);else{q(i,o)?H(t,e,n):I(t,e,n)}}function G(t,e,r){t.transform??=new T;const{vertexSpace:l,transform:m,spatialReference:f}=t,[u,x,h]=l.origin,R=new A({x:u,y:x,z:h,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,z);c(Z,w(e),y(e));const S=o(N,Z,j,b,d),{localMatrix:U}=m,q=i(N,S,U);m.scale=n(v(),q),s(q,q,g(K,m.scale));const L=m.rotationAxis;m.rotation=F(q),0===m.rotationAngle&&(m.rotationAxis=L),m.translation=a(v(),q)}function H(t,r,o){const i=t.spatialReference,n=h(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,m=t.vertexAttributes.tangent,c=new Float64Array(a.length),f=null!=l?new Float32Array(l.length):null,p=null!=m?new Float32Array(m.length):null;R(n,s,W,n),e(X,W);const g=Q;u(w(Q),w(r),X),g[3]=r[3],L(a,i,c,n)&&(null==l||null==f||M(l,a,i,c,n,f))&&(null==m||null==p||P(m,a,i,c,n,p))?(J(c,g,3,s),V(c,n,a,i)&&(null==l||null==f||(J(f,g,3),k(f,a,i,c,n,l)))&&(null==m||null==p||(J(p,g,4),D(p,a,i,c,n,m)))?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,z)}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=j){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=v(),N=m(),Q=S(),W=m(),X=r(),Y=v(),Z=f();export{B as rotate};
