/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{neverReached as e}from"../../../../core/compilerUtils.js";import{acosClamped as t,clamp as r,asinClamped as n,deg2rad as i}from"../../../../core/mathUtils.js";import{fromRotation as a}from"../../../../core/libs/gl-matrix-2/math/mat4.js";import{create as s}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{e as o,p as c,j as u,i as l,D as m,B as d,g as f,n as p,b as h}from"../../../../chunks/vec32.js";import{create as M}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{getReferenceEllipsoid as y}from"../../../../geometry/ellipsoidUtils.js";import{Sphere as C}from"../../../../geometry/support/sphere.js";import{defaultConstraintOptions as I,hasConstraintType as S,adjustRangeForInteraction as x}from"./common.js";import{viewAngle as v}from"../../state/utils/viewUtils.js";function w(t,r,n,i=!0){N.eyeCenterDistance=0,N.requiresTwoSteps=!1;const s=A(t,r,n,I,N);if(0===s)return!1;switch(a(L,-s,r.viewRight),n.tiltMode){case 1:c(k,r.viewForward,L),u(k,k,N.eyeCenterDistance),r.center=l(B,r.eye,k);break;case 0:o(k,r.center,r.eye),c(k,k,L),r.eye=o(B,r.center,k);break;default:e(n.tiltMode)}return r.up=c(B,r.up,L),!N.requiresTwoSteps||!i||w(t,r,n,!1)}function A(e,t,r,n=I,i){if(!e.state.constraints.tilt)return 0;const a=Math.min(t.relativeElevation*G,t.distance),s=e.state.constraints.tilt(a,J);return E(e,r,s),2===n.interactionType&&S(n.selection,2)&&U(e,n.interactionStartCamera,s),1===r.tiltMode||1===n.tiltMode?R(e,t,s,i):P(e,t,s)}function P(e,t,n){const i=v(e.renderCoordsHelper,t.center,t.eye),a=i-r(i,n.min,n.max);return O(a)?a:0}function R(e,t,r,n){switch(n&&(n.requiresTwoSteps=!1),e.viewingMode){case"global":return j(e,t,r,n);case"local":return T(e,t,r,n)}}function T(e,t,n,i){const a=v(e.renderCoordsHelper,t.center,t.eye),s=r(a,n.min,n.max),o=a-s;if(!O(o))return 0;if(i){const r=Math.abs(e.pointsOfInterest.centerOnSurfaceFrequent.estimatedSurfaceAltitude),n=e.renderCoordsHelper.getAltitude(t.eye)-r,a=Math.max(Math.cos(s),1e-4);Math.abs(a)>1e-4?i.eyeCenterDistance=n/a:i.eyeCenterDistance=t.distance}return o}function j(e,t,n,i){const a=b(e,t,K),s=r(a.tiltAtCenter,n.min,n.max);if(!O(a.tiltAtCenter-s))return 0;let o,c;return a.centerIsOnSurface?(o=q(a),c=H(a,o)):(o=a.constraints.clampTilt(a.distance,a.tiltAtCenter),i&&o<Math.PI/2&&(i.requiresTwoSteps=!0,o=Math.PI/2-1e-5),c=F(a,o)),i&&(i.eyeCenterDistance=D(a,o)),c}function b(e,r,n){const i=e.pointsOfInterest.centerOnSurfaceFrequent.estimatedSurfaceAltitude,a=i+y(e.spatialReference).radius,s=e.renderCoordsHelper.intersectManifold(r.ray,i,B);return n.distance=Math.min(r.relativeElevation*G,r.distance),n.centerIsOnSurface=!1,null!=s?(n.distance=Math.min(r.relativeElevation*G,d(r.eye,s)),n.tiltAtCenter=v(e.renderCoordsHelper,s,r.eye),n.centerIsOnSurface=!0):e.state.isLocal?n.tiltAtCenter=v(e.renderCoordsHelper,r.center,r.eye):(g.radius=a,g.closestPointOnSilhouette(r.ray,B),n.distance=Math.min(r.relativeElevation*G,d(r.eye,B)),n.tiltAtCenter=t(-f(r.viewForward,p(B,B)))),n.radius=a,n.eyeRadius=h(r.eye),n.constraints=e.state.constraints,n}const g=new C;function O(e){return Math.abs(e)>1e-9}function q(e){const{constraints:t,distance:r,tiltAtCenter:n}=e;let i=n,a=t.clampTilt(r,n);const s=D(e,a);if(t.clampTilt(s,n)===a)return a;let o=0;for(;o<10&&O(a-i);){const r=(i+a)/2,n=D(e,r);O(t.clampTilt(n,r)-r)?i=r:a=r,o++}return a}function D(e,t){if(!e.centerIsOnSurface)return e.distance;const i=Math.PI-r(t,0,Math.PI),a=n(e.radius/e.eyeRadius*Math.sin(i)),s=Math.PI-i-a,o=Math.sin(s)/Math.sin(i);if(e.eyeRadius<e.radius&&o>1){const t=Math.PI-a,r=Math.PI-i-t;return Math.sin(r)/Math.sin(i)*e.eyeRadius}return o*e.eyeRadius}function H(e,t){const r=n(e.radius/e.eyeRadius*Math.sin(e.tiltAtCenter)),i=n(e.radius/e.eyeRadius*Math.sin(t));return e.eyeRadius>e.radius?r-i:i-r}function F(e,t){return e.tiltAtCenter-Math.PI/2-(t-Math.PI/2)}function E(e,t,r){if(0===t.interactionType)return;const{interactionStartCamera:n,interactionFactor:i}=t;if(!n)return;const{min:a,max:s}=r,o=A(e,n,I,t),c=0===o?0:v(e.renderCoordsHelper,n.center,n.eye);r.min=a,r.max=s,2===t.interactionType?(S(t.selection,2)&&U(e,n,r),x(o,c,!0,i,z,r)):x(o,c,!1,i,z,r)}function U(e,r,n){const i=e.state.constraints;if(e.state.isLocal||!i.altitude||!r)return;const a=m(r.center),s=Math.sqrt(a),o=r.distance,c=y(e.spatialReference).radius,u=i.altitude.min+c,l=i.altitude.max+c,d=(u*u-o*o-a)/(-2*s*o),f=(l*l-o*o-a)/(-2*s*o);n.min=Math.max(n.min,Math.min(Math.PI-t(f),n.max)),n.max=Math.min(n.max,Math.PI-t(d))}const k=M(),L=s(),B=M(),z=i(5),G=30,J={min:0,max:0},K={constraints:null,radius:0,eyeRadius:0,centerIsOnSurface:!0,distance:0,tiltAtCenter:0},N={eyeCenterDistance:0,requiresTwoSteps:!1};export{w as applyTiltConstraint,A as getTiltConstraintError};
