/*
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/mathUtils","../../core/ObjectStack","../../chunks/vec3","../../chunks/vec3f64","./vectorStacks"],(function(t,e,n,r,c,o){"use strict";function i(t){return t?{origin:c.clone(t.origin),vector:c.clone(t.vector)}:{origin:c.create(),vector:c.create()}}function s(t,e){const n=A.get();return n.origin=t,n.vector=e,n}function a(t,e=i()){return u(t.origin,t.vector,e)}function u(t,e,n=i()){return r.copy(n.origin,t),r.copy(n.vector,e),n}function d(t,e,n=i()){return r.copy(n.origin,t),r.subtract(n.vector,e,t),n}function g(t,n){const c=r.subtract(o.sv3d.get(),n,t.origin),i=r.dot(t.vector,c),s=r.dot(t.vector,t.vector),a=e.clamp(i/s,0,1),u=r.subtract(o.sv3d.get(),r.scale(o.sv3d.get(),t.vector,a),c);return r.dot(u,u)}function v(t,e){return Math.sqrt(g(t,e))}function l(t,e,n){return p(t,e,0,1,n)}function f(t,e,n){return r.add(n,t.origin,r.scale(n,t.vector,e))}function p(t,n,c,i,s){const{vector:a,origin:u}=t,d=r.subtract(o.sv3d.get(),n,u),g=r.dot(a,d)/r.squaredLength(a);return r.scale(s,a,e.clamp(g,c,i)),r.add(s,s,t.origin)}function b(t,e){if(M(t,s(e.origin,e.direction),!1,y)){const{tA:e,pB:n,distance2:c}=y;if(e>=0&&e<=1)return c;if(e<0)return r.squaredDistance(t.origin,n);if(e>1)return r.squaredDistance(r.add(o.sv3d.get(),t.origin,t.vector),n)}return null}function h(t,e,n){return!!M(t,e,!0,y)&&(r.copy(n,y.pA),!0)}function m(t,e){return M(t,e,!0,y)?y.distance2:null}function M(t,n,c,i){const s=1e-6,a=t.origin,u=r.add(o.sv3d.get(),a,t.vector),d=n.origin,g=r.add(o.sv3d.get(),d,n.vector),v=o.sv3d.get(),l=o.sv3d.get();if(v[0]=a[0]-d[0],v[1]=a[1]-d[1],v[2]=a[2]-d[2],l[0]=g[0]-d[0],l[1]=g[1]-d[1],l[2]=g[2]-d[2],Math.abs(l[0])<s&&Math.abs(l[1])<s&&Math.abs(l[2])<s)return!1;const f=o.sv3d.get();if(f[0]=u[0]-a[0],f[1]=u[1]-a[1],f[2]=u[2]-a[2],Math.abs(f[0])<s&&Math.abs(f[1])<s&&Math.abs(f[2])<s)return!1;const p=v[0]*l[0]+v[1]*l[1]+v[2]*l[2],b=l[0]*f[0]+l[1]*f[1]+l[2]*f[2],h=v[0]*f[0]+v[1]*f[1]+v[2]*f[2],m=l[0]*l[0]+l[1]*l[1]+l[2]*l[2],M=(f[0]*f[0]+f[1]*f[1]+f[2]*f[2])*m-b*b;if(Math.abs(M)<s)return!1;let y=(p*b-h*m)/M,A=(p+b*y)/m;c&&(y=e.clamp(y,0,1),A=e.clamp(A,0,1));const S=o.sv3d.get(),j=o.sv3d.get();return S[0]=a[0]+y*f[0],S[1]=a[1]+y*f[1],S[2]=a[2]+y*f[2],j[0]=d[0]+A*l[0],j[1]=d[1]+A*l[1],j[2]=d[2]+A*l[2],i.tA=y,i.tB=A,i.pA=S,i.pB=j,i.distance2=r.squaredDistance(S,j),!0}const y={tA:0,tB:0,pA:c.create(),pB:c.create(),distance2:0},A=new n.ObjectStack((()=>({origin:null,vector:null})));t.closestLineSegmentDistance2=m,t.closestLineSegmentPoint=h,t.closestRayDistance2=b,t.copy=a,t.create=i,t.distance=v,t.distance2=g,t.fromPoints=d,t.fromValues=u,t.pointAt=f,t.projectPoint=l,t.projectPointClamp=p,t.wrap=s,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
