/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../core/mathUtils","../../../../chunks/mat3","../../../../chunks/mat3f64","../../../../chunks/vec3","../../../../chunks/vec3f64","../../../ViewingMode","../../support/mathUtils"],(function(t,e,i,o,n,s,a){"use strict";const c=n.create(),r=n.create(),h=n.create(),p=n.create(),l=n.create(),u=n.create(),d={upward:n.fromValues(0,0,1),forward:n.fromValues(0,1,0),sideway:n.fromValues(1,0,0)},m=i.create();return function(){function w(t=s.ViewingMode.Global){this.viewingMode=t,this.center=n.create(),this.pitch=0,this.yaw=0,this.distance=0,this.lookAtDirection=n.clone(d.forward)}var y=w.prototype;return y.pixelsPerPanAtZoom=function(t){return this.size/2/(this._zoomToPanScale*t)},y.zoomAtPixelsPerPan=function(t){return this.size/2/(this._zoomToPanScale*t)},y.pixelsPerRotateAtZoom=function(){const t=Math.max(Math.cos(Math.abs(this.pitch)),.5);return this.size/2/t},y.compareTo=function(t,e){if(e||(e={pan:0,rotate:0,sourceZoom:0,targetZoom:0}),this.viewingMode===s.ViewingMode.Global){const i=(o.length(this.center)+o.length(t.center))/2;e.pan=a.angle(this.center,t.center)*i}else e.pan=o.distance(this.center,t.center);let i=Math.abs(t.yaw-this.yaw);i>=Math.PI&&(i=2*Math.PI-i);const n=Math.abs(t.pitch-this.pitch);return e.rotate=Math.max(i,n),e.sourceZoom=this.distance,e.targetZoom=t.distance,e},y.interpolate=function(e,i,n){this.viewingMode===s.ViewingMode.Global?a.slerp(e.center,i.center,n.pan,this.center):o.lerp(this.center,e.center,i.center,n.pan),this.distance=t.lerp(e.distance,i.distance,n.zoom),this.pitch=t.lerp(e.pitch,i.pitch,n.rotate);let c=e.yaw;const r=i.yaw;Math.abs(r-c)>=Math.PI&&(c+=2*(c<r?1:-1)*Math.PI),this.yaw=t.lerp(c,r,n.rotate)},y.copyFrom=function(t){o.copy(this.center,t.center),this.pitch=t.pitch,this.yaw=t.yaw,this.distance=t.distance,o.copy(this.lookAtDirection,t.lookAtDirection),this.size=t.size,this.copyFromCommon(t),this.viewingMode=t.viewingMode},y.copyFromRenderCamera=function(t){const e=this._lookAtOrientation(t.center,m);o.copy(this.center,t.center),o.subtract(p,t.center,t.eye),o.transformMat3(p,p,e),o.transformMat3(l,t.up,e),this.distance=o.length(p),p[0]/=this.distance,p[1]/=this.distance,p[2]/=this.distance,this.pitch=this._eyeUpToPitch(p),this.yaw=this._eyeUpToYaw(p,l),this.size=Math.sqrt(t.width*t.width+t.height*t.height),this.copyFromCommon(t)},y.copyFromCommon=function(t){this.fov=t.fov,this._zoomToPanScale=Math.atan(.5*this.fov)},y.copyToRenderCamera=function(t){const i=this._lookAtOrientation(this.center,m);e.transpose(i,i),this._axisAngleVec3(d.sideway,this.pitch-Math.PI/2,d.forward,p),this._axisAngleVec3(d.upward,this.yaw,p),this._axisAngleVec3(d.sideway,this.pitch-Math.PI/2,d.upward,l),this._axisAngleVec3(d.upward,this.yaw,l),o.scale(p,p,this.distance),o.transformMat3(p,p,i),o.transformMat3(l,l,i),t.center=this.center,t.eye=o.subtract(p,this.center,p),t.up=l},y._axisAngleVec3=function(t,e,i,n=i){const s=Math.cos(e),a=Math.sin(e);return o.scale(c,i,s),o.cross(r,t,i),o.scale(r,r,a),o.scale(h,t,(1-s)*o.dot(t,i)),o.add(n,o.add(n,c,r),h)},y._lookAtOrientation=function(t,e=i.create()){return this._upAtLookAt(t,h),o.cross(c,this.lookAtDirection,h),o.normalize(c,c),0===c[0]&&0===c[1]&&0===c[2]&&o.copy(c,d.sideway),o.cross(r,h,c),o.normalize(r,r),e[0]=c[0],e[1]=r[0],e[2]=h[0],e[3]=c[1],e[4]=r[1],e[5]=h[1],e[6]=c[2],e[7]=r[2],e[8]=h[2],e},y._upAtLookAt=function(t,e){return this.viewingMode===s.ViewingMode.Local?o.copy(e,d.upward):o.normalize(e,t)},y._eyeUpToPitch=function(t){return Math.PI-a.angle(d.upward,t)},y._eyeUpToYaw=function(t,e){const i=u;return Math.abs(e[2])<.5?(o.copy(i,e),t[2]>0&&o.scale(i,i,-1)):o.copy(i,t),o.cross(r,i,d.upward),o.normalize(r,r),a.angle(d.sideway,r,d.upward)},w}()}));
