//#####################################################################
// Copyright 2021, Craig Schroeder.
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################

#include <Core/Implicit_Objects/INTERSECTION_HELPERS.h>
#include <Core/Arrays/ARRAY.h>
#include <Core/Basic_Geometry/RAY_HIT.h>
namespace PhysBAM{
//#####################################################################
// Function Boolean
//#####################################################################
template<class T> int
Boolean(ARRAY_VIEW<RAY_HIT<T> > ho,ARRAY_VIEW<const RAY_HIT<T> > ha,
    ARRAY_VIEW<const RAY_HIT<T> > hb,int type)
{
    int a=0,b=0,n=0;
    int a_state=type!=2;
    int b_state=type==0;
    while(a<ha.m && b<hb.m)
    {
        if(ha(a).dist<hb(b).dist)
        {
            if(b%2==b_state) ho(n++)=ha(a);
            a++;
        }
        else
        {
            if(a%2==a_state) ho(n++)=hb(b);
            b++;
        }
    }
    if(b%2==b_state)
        for(;a<ha.m;a++)
            ho(n++)=ha(a);
    if(a%2==a_state)
        for(;b<hb.m;b++)
            ho(n++)=hb(b);
    return n;
}
//#####################################################################
// Function Boolean
//#####################################################################
template<class T> void
Boolean(ARRAY<RAY_HIT<T> >& ho,const ARRAY<RAY_HIT<T> >& ha,
    const ARRAY<RAY_HIT<T> >& hb,int type)
{
    ho.Resize(ha.m+hb.m);
    ARRAY_VIEW<RAY_HIT<T> > h(ho);
    ARRAY_VIEW<const RAY_HIT<T> > a(ha),b(hb);
    ho.Resize(Boolean(h,a,b,type));
}
template void Boolean<double>(ARRAY<RAY_HIT<double>,int>&,ARRAY<RAY_HIT<double>,int> const&,ARRAY<RAY_HIT<double>,int> const&,int);
template void Boolean<float>(ARRAY<RAY_HIT<float>,int>&,ARRAY<RAY_HIT<float>,int> const&,ARRAY<RAY_HIT<float>,int> const&,int);
template int Boolean<double>(ARRAY_VIEW<RAY_HIT<double>,int>,ARRAY_VIEW<const RAY_HIT<double>,int>,ARRAY_VIEW<const RAY_HIT<double>,int>,int);
template int Boolean<float>(ARRAY_VIEW<RAY_HIT<float>,int>,ARRAY_VIEW<const RAY_HIT<float>,int>,ARRAY_VIEW<const RAY_HIT<float>,int>,int);
}
