/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../core/has","../core/Logger","../core/mathUtils","../core/maybe","./mat4","./vec3","./vec3f64","./vec4","./vec4f64","../geometry/support/Axis","../geometry/support/ray","../geometry/support/vector","../geometry/support/vectorStacks"],(function(t,e,n,r,o,s,c,i,u,a,l,f,d,g){"use strict";const h=n.getLogger("esri.geometry.support.sphere");function p(){return a.create()}function m(t,e=p()){return u.copy(e,t)}function y(t,e){return a.fromValues(t[0],t[1],t[2],e)}function v(t){return t}function A(t){t[0]=t[1]=t[2]=t[3]=0}function S(t,e){return t[0]=t[1]=t[2]=0,t[3]=e,t}function R(t){return t[3]}function M(t){return t}function b(t,e,n,r){return a.fromValues(t,e,n,r)}function x(t,e,n){return t!==n&&c.copy(n,t),n[3]=t[3]+e,n}function P(t,e,n){return h.error("sphere.setExtent is not yet supported"),t===n?n:m(t,n)}function C(t,e,n){if(o.isNone(e))return!1;const{origin:r,direction:s}=e,c=T;c[0]=r[0]-t[0],c[1]=r[1]-t[1],c[2]=r[2]-t[2];const i=s[0]*s[0]+s[1]*s[1]+s[2]*s[2],u=2*(s[0]*c[0]+s[1]*c[1]+s[2]*c[2]),a=u*u-4*i*(c[0]*c[0]+c[1]*c[1]+c[2]*c[2]-t[3]*t[3]);if(a<0)return!1;const l=Math.sqrt(a);let f=(-u-l)/(2*i);const d=(-u+l)/(2*i);return(f<0||d<f&&d>0)&&(f=d),!(f<0)&&(n&&(n[0]=r[0]+s[0]*f,n[1]=r[1]+s[1]*f,n[2]=r[2]+s[2]*f),!0)}const T=i.create();function j(t,e){return C(t,e,null)}function q(t,e,n){if(C(t,e,n))return n;const r=O(t,e,g.sv3d.get());return c.add(n,e.origin,c.scale(g.sv3d.get(),e.direction,c.distance(e.origin,r)/c.length(e.direction))),n}function O(t,e,n){const r=g.sv3d.get(),o=g.sm4d.get();c.cross(r,e.origin,e.direction);const i=R(t);c.cross(n,r,e.origin),c.scale(n,n,1/c.length(n)*i);const u=E(t,e.origin),a=d.angle(e.origin,n);return s.fromRotation(o,a+u,r),c.transformMat4(n,n,o),n}function V(t,e,n){return C(t,e,n)?n:(f.closestPoint(e,M(t),n),_(t,n,n))}function _(t,e,n){const r=c.subtract(g.sv3d.get(),e,M(t)),o=c.scale(g.sv3d.get(),r,t[3]/c.length(r));return c.add(n,o,M(t))}function w(t,e){const n=c.subtract(g.sv3d.get(),e,M(t)),r=c.squaredLength(n),o=t[3]*t[3];return Math.sqrt(Math.abs(r-o))}function E(t,e){const n=c.subtract(g.sv3d.get(),e,M(t)),o=c.length(n),s=R(t),i=s+Math.abs(s-o);return r.acosClamped(s/i)}const L=i.create();function z(t,e,n,o){const s=c.subtract(L,e,M(t));switch(n){case l.Axis.X:{const t=r.cartesianToSpherical(s,L)[2];return c.set(o,-Math.sin(t),Math.cos(t),0)}case l.Axis.Y:{const t=r.cartesianToSpherical(s,L),e=t[1],n=t[2],i=Math.sin(e);return c.set(o,-i*Math.cos(n),-i*Math.sin(n),Math.cos(e))}case l.Axis.Z:return c.normalize(o,s);default:return}}function Z(t,e){const n=c.subtract(N,e,M(t));return c.length(n)-t[3]}function k(t,e,n,r){const o=Z(t,e),s=z(t,e,l.Axis.Z,N),i=c.scale(N,s,n-o);return c.add(r,e,i)}function D(t,e){const n=c.squaredDistance(M(t),e),r=R(t);return n<=r*r}const N=i.create(),U=p(),X=Object.freeze(Object.defineProperty({__proto__:null,create:p,copy:m,fromCenterAndRadius:y,wrap:v,clear:A,fromRadius:S,getRadius:R,getCenter:M,fromValues:b,elevate:x,setExtent:P,intersectRay:C,intersectsRay:j,intersectRayClosestSilhouette:q,closestPointOnSilhouette:O,closestPoint:V,projectPoint:_,distanceToSilhouette:w,angleToSilhouette:E,axisAt:z,altitudeAt:Z,setAltitudeAt:k,containsPoint:D,tmpSphere:U},Symbol.toStringTag,{value:"Module"}));t.altitudeAt=Z,t.angleToSilhouette=E,t.axisAt=z,t.clear=A,t.closestPoint=V,t.closestPointOnSilhouette=O,t.containsPoint=D,t.copy=m,t.create=p,t.distanceToSilhouette=w,t.elevate=x,t.fromCenterAndRadius=y,t.fromRadius=S,t.fromValues=b,t.getCenter=M,t.getRadius=R,t.intersectRay=C,t.intersectRayClosestSilhouette=q,t.intersectsRay=j,t.projectPoint=_,t.setAltitudeAt=k,t.setExtent=P,t.sphere=X,t.tmpSphere=U,t.wrap=v}));
