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