/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{rad2deg as e,clamp as r}from"../../../../core/mathUtils.js";import{fromRotation as t}from"../../../../core/libs/gl-matrix-2/math/mat4.js";import{create as n}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{transformMat4 as i,normalize as o,copy as a,angle as s,dot as c,sub as m,add as l,scale as d,len as p,set as f}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as g}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{create as u,getNormal as h,fromPositionAndNormal as b,projectPoint as j}from"../../../../geometry/support/plane.js";import{viewshedToolManipulatorConfiguration as v}from"./ViewshedConfiguration.js";import{calculateInputRotationTransform as x,calculateTranslateRotateFromBases as S}from"../../interactive/manipulatorUtils.js";import{screenToRenderPlane as A}from"../../interactive/editingTools/dragEventPipeline3D.js";import{headingTiltToDirectionUp as R}from"../../support/cameraUtils.js";import{createDirectionUp as U}from"../../support/cameraUtilsInternal.js";function C({tiltedUpVector:e,rightVector:r,elevationAlignedObserverRenderSpace:t},n){const i=S(e,r,t,n);return i[12]=0,i[13]=0,i[14]=0,i}function D(t,n,i,s){const p=g(),f=h(i.plane),j=E(n,f);let S;if(Math.abs(j)>v.viewAngleThreshold)S=t.next(A(n,i.plane));else{const e=b(s.elevationAlignedTargetRenderSpace,i.basis1,u()),a=o(T,i.basis1),p=o(V,i.basis2);S=t.next(A(n,e)).next(e=>{const t=e=>{const t=c(m(w,e,i.origin),p),n=Math.acos(r(t/s.farDistanceRenderSpace,-1,1)),o=Math.sin(n)*s.farDistanceRenderSpace;return l(g(),i.origin,l(g(),d(w,a,o),d(H,p,t)))},n=t(e.renderStart),o=t(e.renderEnd);return{...e,renderStart:n,renderEnd:o}})}return S.next(r=>{"start"===r.action&&a(p,r.renderStart);const t=e(x(p,r.renderEnd,i.origin,f));return{...r,deltaAngle:t}})}function E(r,t){const n=T;r.renderCoordsHelper.toRenderCoords(r.camera.position,n);const i=R(r,n,r.camera.heading,r.camera.tilt,U()).direction;return e(s(i,t))-90}function M(e,r,n,o){return t(O,n,o),i(e,r,O)}function P(r,t,n){const i=t.elevationAlignedObserverRenderSpace,o=m(y,n,i),a=p(o)*t.metersPerUnit,l=r.renderCoordsHelper.basisMatrixAtPosition(i,B),d=f(I,l[8],l[9],l[10]),g=b(i,d,F),u=j(g,n,k),h=m(u,u,i),v=(p(h)<1e-4?90:e(s(o,h)))*(c(d,o)<0?-1:1)+90,x=f(q,l[4],l[5],l[6]),S=e(s(x,h)),A=f(z,l[0],l[1],l[2]);return{heading:c(h,A)<0?360-S:S,tilt:v,farDistance:a}}const T=g(),V=g(),w=g(),H=g(),O=n(),y=g(),I=g(),k=g(),q=g(),z=g(),B=n(),F=u();export{P as computeParametersFromTargetPoint,C as getViewshedRotationMatrix,M as rotateBy,D as screenToCircleAngle};
