component sphereprobev2 "Probe a pretend hemisphere";
author "Jeff Epler";
license "GPL";

pin_ptr in s32 px;
pin_ptr in s32 py;
pin_ptr in s32 pz "\\fBrawcounts\\fR position from software encoder";

pin_ptr in s32 cx;
pin_ptr in s32 cy;
pin_ptr in s32 cz "Center of sphere in counts";
pin_ptr in s32 r "Radius of hemisphere in counts";

pin_ptr out bit probe-out;

function _ nofp "update probe-out based on inputs";
;;
#undef abs
int abs(int x) { if(x < 0) return -x; else return x; }

FUNCTION(_)
{
    __u64 dx = abs(gs(px)-gs(cx)), dy = abs(gs(py)-gs(cy)), dz = abs(gs(pz) - gs(cz));
    __u64 d2 = dx * dx + dy*dy;
    __u64 r2 = (__s64)gs(r) * (__s64)gs(r);
    if(d2 > r2) {
        sb(probe_out, gs(pz) < gs(cz));
    } else {
        d2 += dz * dz;
        sb(probe_out, d2 <= r2);
    }

return 0;
}
