/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{clamp as t}from"../../core/mathUtils.js";import{ObjectStack as r}from"../../core/ObjectStack.js";import{copy as n,subtract as o,dot as i,squaredLength as e,scale as c,add as g,squaredDistance as s}from"../../core/libs/gl-matrix-2/math/vec3.js";import{create as u,clone as a}from"../../core/libs/gl-matrix-2/factories/vec3f64.js";import{sv3d as f}from"./vectorStacks.js";function v(t){return t?{origin:a(t.origin),vector:a(t.vector)}:{origin:u(),vector:u()}}function m(t,r){const n=S.get();return n.origin=t,n.vector=r,n}function p(t,r=v()){return b(t.origin,t.vector,r)}function b(t,r,o=v()){return n(o.origin,t),n(o.vector,r),o}function h(t,r,i=v()){return n(i.origin,t),o(i.vector,r,t),i}function l(r,n){const e=o(f.get(),n,r.origin),g=i(r.vector,e),s=i(r.vector,r.vector),u=t(g/s,0,1),a=o(f.get(),c(f.get(),r.vector,u),e);return i(a,a)}function M(t,r,n){return A(t,r,0,1,n)}function j(t,r,n){return g(n,t.origin,c(n,t.vector,r))}function A(r,n,s,u,a){const{vector:v,origin:m}=r,p=o(f.get(),n,m),b=i(v,p)/e(v);return c(a,v,t(b,s,u)),g(a,a,r.origin)}function B(t,r){if(x(t,m(r.origin,r.direction),!1,k)){const{tA:r,pB:n,distance2:o}=k;if(r>=0&&r<=1)return o;if(r<0)return s(t.origin,n);if(r>1)return s(g(f.get(),t.origin,t.vector),n)}return null}function d(t,r,o){return!!x(t,r,!0,k)&&(n(o,k.pA),!0)}function x(r,n,o,i){const e=1e-6,c=r.origin,u=g(f.get(),c,r.vector),a=n.origin,v=g(f.get(),a,n.vector),m=f.get(),p=f.get();if(m[0]=c[0]-a[0],m[1]=c[1]-a[1],m[2]=c[2]-a[2],p[0]=v[0]-a[0],p[1]=v[1]-a[1],p[2]=v[2]-a[2],Math.abs(p[0])<e&&Math.abs(p[1])<e&&Math.abs(p[2])<e)return!1;const b=f.get();if(b[0]=u[0]-c[0],b[1]=u[1]-c[1],b[2]=u[2]-c[2],Math.abs(b[0])<e&&Math.abs(b[1])<e&&Math.abs(b[2])<e)return!1;const h=m[0]*p[0]+m[1]*p[1]+m[2]*p[2],l=p[0]*b[0]+p[1]*b[1]+p[2]*b[2],M=m[0]*b[0]+m[1]*b[1]+m[2]*b[2],j=p[0]*p[0]+p[1]*p[1]+p[2]*p[2],A=(b[0]*b[0]+b[1]*b[1]+b[2]*b[2])*j-l*l;if(Math.abs(A)<e)return!1;let B=(h*l-M*j)/A,d=(h+l*B)/j;o&&(B=t(B,0,1),d=t(d,0,1));const x=f.get(),k=f.get();return x[0]=c[0]+B*b[0],x[1]=c[1]+B*b[1],x[2]=c[2]+B*b[2],k[0]=a[0]+d*p[0],k[1]=a[1]+d*p[1],k[2]=a[2]+d*p[2],i.tA=B,i.tB=d,i.pA=x,i.pB=k,i.distance2=s(x,k),!0}const k={tA:0,tB:0,pA:u(),pB:u(),distance2:0},S=new r(()=>v());export{d as closestLineSegmentPoint,B as closestRayDistance2,p as copy,v as create,l as distance2,h as fromPoints,b as fromValues,j as pointAt,M as projectPoint,A as projectPointClamp,m as wrap};
