/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{and as t,greaterThanEqual as n,Float as e,Vec3 as y,ifElse as r,or as u,greaterThan as i,lessThan as s,cond as l,min as x,not as c,Vec2 as a,max as w,equal as o,clamp as d,length as b,distance as m,Mat3 as p,abs as f,step as g,lessThanEqual as v,dot as I,normalize as h}from"../../graph/glsl.js";import{noHit as R,trueHit as j,epsilonHitTest as z,candidateHit as C}from"./constants.js";import{cross2 as S}from"./utils.js";function T(t,n){return I(t,h(n))}function V(t,n,y){const r=y.subtract(n),u=T(t.subtract(n),r),i=d(u.divide(b(r)),new e(0),new e(1));return m(t,n.add(i.multiply(y.subtract(n))))}function M(t){const n=f(t);return g(n.x.add(n.y).add(n.z),new e(1.05))}function P(t,n,r,u){const i=new p(r.x.multiply(u.y).subtract(u.x.multiply(r.y)),u.x.multiply(n.y).subtract(n.x.multiply(u.y)),n.x.multiply(r.y).subtract(r.x.multiply(n.y)),r.y.subtract(u.y),u.y.subtract(n.y),n.y.subtract(r.y),u.x.subtract(r.x),n.x.subtract(u.x),r.x.subtract(n.x)),s=n.x.multiply(r.y.subtract(u.y)),l=r.x.multiply(u.y.subtract(n.y)),x=u.x.multiply(n.y.subtract(r.y)),c=s.add(l).add(x);return new e(1).divide(c).multiply(i.multiply(new y(1,t)))}function q(t,n,y,r){return o(M(P(t,n,y,r)),new e(1))}function k(n,y,r,u){const a=r.subtract(y),w=u.subtract(y),o=S(a,w),d=t(s(o,new e(z)),i(o,new e(-z)));return l([t(c(d),q(n.xy,y,r,u)),new e(-1)],[!0,()=>{const t=V(n,y,r),e=V(n,r,u),i=V(n,u,y);return x(x(t,e),i)}])}function A(t,n,e){const{viewMat3:r,tileMat3:u}=t.view,i=r.multiply(u),s=i.multiply(new y(n.pos,1)),l=i.multiply(new y(e.nextPos1,1)),x=i.multiply(new y(e.nextPos2,1));return k(t.hittestRequest.position,s.xy,l.xy,x.xy)}function B(t,n,e){return m(t,e).subtract(n)}function D(y,u,i,s){const l=y.x,x=y.y,a=u.x,w=u.y,d=i.x,b=i.y,m=s.x,p=s.y,f=m.subtract(d),g=l.subtract(d),I=a.subtract(l),h=p.subtract(b),R=x.subtract(b),j=w.subtract(x),z=h.multiply(I).subtract(f.multiply(j)),C=f.multiply(R).subtract(h.multiply(g)).divide(z),S=I.multiply(R).subtract(j.multiply(g)).divide(z),T=t(c(o(z,new e(0))),t(t(n(C,new e(0)),v(C,new e(1))),t(n(S,new e(0)),v(S,new e(1)))));return r(T,new e(1),new e(0))}function E(e,y,r,u,i){return t(t(n(e.x,y),n(e.y,r)),t(s(e.x,u),v(e.y,i)))}function F(n,y,r,s){const l=s.xy,c=s.zw,d=new a(c.x,l.y),b=new a(l.x,c.y),m=x(l.x,c.x),p=x(l.y,c.y),f=w(l.x,c.x),g=w(l.y,c.y),v=P(new a(m,p),n,y,r),I=P(new a(f,g),n,y,r),h=P(new a(m,g),n,y,r),R=P(new a(f,p),n,y,r),j=t(o(M(v),new e(1)),t(o(M(I),new e(1)),t(o(M(h),new e(1)),o(M(R),new e(1))))),z=D(l,d,n.xy,y.xy).add(D(d,c,n.xy,y.xy)).add(D(b,c,n.xy,y.xy)).add(D(b,l,n.xy,y.xy)),C=D(l,d,n.xy,r.xy).add(D(d,c,n.xy,r.xy)).add(D(b,c,n.xy,r.xy)).add(D(b,l,n.xy,r.xy)),S=D(l,d,y.xy,r.xy).add(D(d,c,y.xy,r.xy)).add(D(b,c,y.xy,r.xy)).add(D(b,l,y.xy,r.xy)),T=E(n.xy,m,p,f,g),V=E(y.xy,m,p,f,g),q=E(r.xy,m,p,f,g),k=t(T,t(V,q));return{hasIntersectingSegments:u(i(z,new e(0)),u(i(C,new e(0)),i(S,new e(0)))),allTriangleVerticesInside:k,triangleContainsRect:j}}function G(t,n,y,i){const{hasIntersectingSegments:s,allTriangleVerticesInside:l,triangleContainsRect:x}=F(t,n,y,i);return r(s,new e(C),r(u(l,x),new e(j),new e(R)))}function H(t,n,y,i){const{hasIntersectingSegments:s,allTriangleVerticesInside:l,triangleContainsRect:x}=F(t,n,y,i);return r(u(s,u(l,x)),new e(j),new e(R))}function J(y){return t(n(y.tlbr.x,new e(0)),n(y.tlbr.y,new e(0)),n(y.tlbr.z,new e(0)),n(y.tlbr.w,new e(0)))}export{V as distPointSegment,k as distPointTriangle,A as hittestFill,B as hittestMarker,G as hittestMarkerScreenRect,H as hittestScreenRect,M as inTriangle,F as intersectsScreenRect,J as isRectHittest,E as pointInRect,T as projectScalar,D as segmentIntersects,P as xyToBarycentric};
