/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{isArrayLike as e}from"../../../core/arrayUtils.js";import{deg2rad as t}from"../../../core/mathUtils.js";import{clone as r,zeros as a}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import i from"../../../geometry/Point.js";import{isElevationSource as n}from"../core/ElevationSourceDefinitions.js";import{rectifyMapPointPanoramic as o,rectifyMapPoint as c}from"./rectifyMapPoint.js";import{getWebMercatorScalingFactor as s,hasAngles as f,convertPixelToHeadingPitch as l,adjustHorizonAngles as m,isElevationSampler as p,reducerFn as h,validatePixelsToTransform as u,validateRotationMatrix as y,computeFarplaneVertices as d,computeHFOVAndVFOV as w,vecToPoint as g,or as v,isUpdateElevationWithElevationSource as x,scaleAndAddWithFactor as R,computeNewReferenceCoordinates as M,projectiveTransform as P}from"./utils.js";import{worldToImage as A}from"./worldToImage.js";import{viewingLimit as V}from"../../../widgets/OrientedImageryViewer/constants.js";async function j(e,t,r){const{verticalFieldOfView:a,imageBoundaries:i,scalingFactor:n,farPlaneVertices:o,cameraLocation:c,pixelsToTransform:s,vecToPoint:f,pixelDepths:l}=S(e,t),m=new Array;return await F(s,o,i,f,t,m,c,n,a,r,l),Array.isArray(e)?m:m[0]}async function F(e,t,r,a,n,o,s,f,l,m,p){let h=n.averageElevation;const u=e.length;for(let y=0;y<u;y+=1){const u=e.at(y),d=O(t,r,u,a,n),w=p?.at(y);if("number"==typeof w){const e=b(d,s,f),t=w*f,r=[s[0]+e[0]*t,s[1]+e[1]*t,s[2]+e[2]*w];o.push(new i(r,n.cameraLocation.spatialReference));continue}if(m&&v("elevationSample"in m,x(m))){h=(await c(a(d),{x:u[0],y:u[1]},{...n,...m,farPlaneVertices:t.map(a)}))[2]}o.push(a(T(d,s,f,h,n.cameraPitch,l)))}}function b(e,t,r){const i=e[0]-t[0],n=e[1]-t[1],o=e[2]-t[2],c=Math.hypot(i,n)/r,s=Math.hypot(o,c)*r,f=a();return f[0]=i/s,f[1]=n/s,f[2]=o/s,f[2]=f[2]*r,f}function T(e,t,a,i,n,o){let c=r(e);const s=b(e,t,a);if(L(e[2],i,n,o)){const e=Math.abs((t[2]-i)/-s[2])*a;c=R(t,s,e,a)}else c[2]=i;return c}function L(e,t,r,a){return e<t||r+a/2<V}function O(e,t,r,a,i){let n=null;const o=9;let c,s=0,f=e,l=t;for(;s<=o;){const e=D(r,l,f,i);if(c=e.error,n=e.transformedPoint,v(c<=1,s===o))break;f=M(f,c,r,t),l=z(f,a,i),s++}return n}function z(e,t,r){return A(e.map(t),r).map(({x:e,y:t})=>[e,t,1])}function D(e,t,r,a){const{cameraLocation:n}=a,o=P(e,t,r),{x:c,y:s}=A(new i(o,n.spatialReference),a);return{transformedPoint:o,error:E(e,[c,s,1])}}function E(e,t){return Math.abs(e[0]-t[0])+Math.abs(e[1]-t[1])}function S(t,r){const a=e(t)||"items"in t?t:[t],{cameraLocation:n,rotationMatrix:o}=r;if(u(a,n),y(o),9!==o?.length)throw new Error("Rotation matrix is not provided or is not a valid 3x3 matrix");const c=s(n.y,n.spatialReference),f=d({...r,scalingFactor:c}),l=A(f.map(e=>new i(e,n.spatialReference)),r),{vfov:m}=w(r.horizontalFieldOfView,r.verticalFieldOfView,r.cameraRoll);return{cameraLocation:n.toArray(),imageBoundaries:l.map(({x:e,y:t})=>[e,t,1]),verticalFieldOfView:m,farPlaneVertices:f,scalingFactor:c,pixelsToTransform:a.map(e=>[e.x,e.y,1]),vecToPoint:g(n.spatialReference),pixelDepths:a.map(e=>B(e)?e.depth:void 0)}}function B(e){return"depth"in e&&"number"==typeof e.depth}async function I(e,r,a){const{cameraLocation:c,farDistance:u,imageHeight:y,imageWidth:d,verticalFieldOfView:w,horizonPitch:g,horizonRoll:v,horizonYaw:x}=r,R=s(c.y,c.spatialReference),M=u*R,P=new Array,[A,V,j]=c.toArray(),F=Array.isArray(e)?e:[e];for(const s of F){let e,F;const b=B(s),L=b?s.depth*R:M;if(f(s))e=s.heading,F=s.pitch;else{const t=l({x:s.x,y:s.y},d,y),r=m(t.heading,t.pitch,[g,v,x],!0);e=r[0],F=r[1]}const O=[A+Math.sin(t(e))*Math.sin(t(F))*L,V+Math.cos(t(e))*Math.sin(t(F))*L,j+-Math.cos(t(F))*(b?s.depth:u)];if(b){P.push(new i(O,c.spatialReference));continue}let z=r.averageElevation;if(a&&(n(a)||p(a.elevationSample))){z=(await o(new i(O,c.spatialReference),{heading:e,pitch:F},{...r,...a,farPlaneVertices:h(u,u).map(([e,t])=>new i([c.x+e,c.y+t],c.spatialReference))}))[2]}P.push(new i(T(O,c.toArray(),R,z,F,w),c.spatialReference))}return Array.isArray(e)?P:P[0]}export{j as imageToWorld,I as imageToWorldPanoramic};
