// Copyright (c) 2014
// INRIA Saclay-Ile de France (France)
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.2-beta1/NewKernel_d/include/CGAL/NewKernel_d/Cartesian_static_filters.h $
// $Id: include/CGAL/NewKernel_d/Cartesian_static_filters.h 431cfb5357d $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s)     : Marc Glisse

#ifndef CGAL_KD_CARTESIAN_STATIC_FILTERS_H
#define CGAL_KD_CARTESIAN_STATIC_FILTERS_H
#include <CGAL/NewKernel_d/functor_tags.h>
#include <CGAL/Dimension.h>
#include <CGAL/Filtered_kernel/internal/Static_filters/tools.h> // bug, should be included by the next one
#include <CGAL/Filtered_kernel/internal/Static_filters/Orientation_2.h>
#include <CGAL/Filtered_kernel/internal/Static_filters/Side_of_oriented_circle_2.h>
#include <CGAL/Filtered_kernel/internal/Static_filters/Orientation_3.h>
#include <CGAL/Filtered_kernel/internal/Static_filters/Side_of_oriented_sphere_3.h>
#include <CGAL/Filtered_kernel/internal/Static_filters/Orientation_4.h>
#include <CGAL/Filtered_kernel/internal/Static_filters/Orientation_5.h>
#include <CGAL/Filtered_kernel/internal/Static_filters/Orientation_6.h>

namespace CGAL {
namespace SFA { // static filter adapter
// Note that this would be quite a bit simpler without stateful kernels
template <class Base_,class R_> struct Adapter_2 {
        typedef typename Get_type<R_, Orientation_tag>::type Orientation;
        typedef typename Get_type<R_, Oriented_side_tag>::type Oriented_side;
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef typename Get_functor<Base_, Orientation_of_points_tag>::type Orientation_base;
        typedef typename Get_functor<Base_, Side_of_oriented_sphere_tag>::type Side_of_oriented_circle_base;
        struct Point_2 {
                R_ const&r; CC const&c; Point const& p;
                Point_2(R_ const&r_, CC const&c_, Point const&p_):r(r_),c(c_),p(p_){}
                decltype(auto) x()const{return c(p,0);}
                decltype(auto) y()const{return c(p,1);}
        };
        struct Vector_2 {};
        struct Circle_2 {};
        struct Orientation_2 {
                typedef typename Get_type<R_, Orientation_tag>::type result_type;
                auto operator()(Point_2 const&A, Point_2 const&B, Point_2 const&C)const{
                        Point const* t[3]={&A.p,&B.p,&C.p};
                        return Orientation_base(A.r)(make_transforming_iterator<Dereference_functor>(t+0),make_transforming_iterator<Dereference_functor>(t+3));
                }
        };
        struct Side_of_oriented_circle_2 {
                typedef typename Get_type<R_, Oriented_side_tag>::type result_type;
                auto operator()(Point_2 const&A, Point_2 const&B, Point_2 const&C, Point_2 const&D)const{
                        Point const* t[3]={&A.p,&B.p,&C.p};
                        return Side_of_oriented_circle_base(A.r)(make_transforming_iterator<Dereference_functor>(t+0),make_transforming_iterator<Dereference_functor>(t+3),D.p);
                }
        };
};
template <class Base_,class R_> struct Orientation_of_points_2 : private Store_kernel<R_> {
        CGAL_FUNCTOR_INIT_STORE(Orientation_of_points_2)
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_type<R_, Orientation_tag>::type result_type;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef Adapter_2<Base_, R_> Adapter;
        template<class Iter> result_type operator()(Iter f, Iter CGAL_assertion_code(e))const{
                CC c(this->kernel());
                Point const& A=*f;
                Point const& B=*++f;
                Point const& C=*++f;
                CGAL_assertion(++f==e);
                typedef typename Adapter::Point_2 P;
                return typename internal::Static_filters_predicates::Orientation_2<Adapter>()(P(this->kernel(),c,A),P(this->kernel(),c,B),P(this->kernel(),c,C));
        }
};
template <class Base_,class R_> struct Side_of_oriented_sphere_2 : private Store_kernel<R_> {
        CGAL_FUNCTOR_INIT_STORE(Side_of_oriented_sphere_2)
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_type<R_, Oriented_side_tag>::type result_type;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef Adapter_2<Base_, R_> Adapter;
        template<class Iter> result_type operator()(Iter f, Iter CGAL_assertion_code(e), Point const& D)const{
                CC c(this->kernel());
                Point const& A=*f;
                Point const& B=*++f;
                Point const& C=*++f;
                CGAL_assertion(++f==e);
                typedef typename Adapter::Point_2 P;
                return typename internal::Static_filters_predicates::Side_of_oriented_circle_2<Adapter>()(P(this->kernel(),c,A),P(this->kernel(),c,B),P(this->kernel(),c,C),P(this->kernel(),c,D));
        }
};

template <class Base_,class R_> struct Adapter_3 {
        typedef typename Get_type<R_, Orientation_tag>::type Orientation;
        typedef typename Get_type<R_, Oriented_side_tag>::type Oriented_side;
        typedef typename Get_type<R_, Point_tag>::type Point;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef typename Get_functor<Base_, Orientation_of_points_tag>::type Orientation_base;
        typedef typename Get_functor<Base_, Side_of_oriented_sphere_tag>::type Side_of_oriented_sphere_base;
        struct Point_3 {
                R_ const&r; CC const&c; Point const& p;
                Point_3(R_ const&r_, CC const&c_, Point const&p_):r(r_),c(c_),p(p_){}
                decltype(auto) x()const{return c(p,0);}
                decltype(auto) y()const{return c(p,1);}
                decltype(auto) z()const{return c(p,2);}
        };
        struct Vector_3 {};
        struct Sphere_3 {};
        struct Tetrahedron_3 {};
        struct Orientation_3 {
                typedef typename Get_type<R_, Orientation_tag>::type result_type;
                auto operator()(Point_3 const&A, Point_3 const&B, Point_3 const&C, Point_3 const&D)const{
                        Point const* t[]={&A.p,&B.p,&C.p,&D.p};
                        return Orientation_base(A.r)(make_transforming_iterator<Dereference_functor>(t+0),make_transforming_iterator<Dereference_functor>(t+4));
                }
        };
        struct Side_of_oriented_sphere_3 {
                typedef typename Get_type<R_, Oriented_side_tag>::type result_type;
                auto operator()(Point_3 const&A, Point_3 const&B, Point_3 const&C, Point_3 const&D, Point_3 const&E)const{
                        Point const* t[]={&A.p,&B.p,&C.p,&D.p};
                        return Side_of_oriented_sphere_base(A.r)(make_transforming_iterator<Dereference_functor>(t+0),make_transforming_iterator<Dereference_functor>(t+4),E.p);
                }
        };
};
template <class Base_,class R_> struct Orientation_of_points_3 : private Store_kernel<R_> {
        CGAL_FUNCTOR_INIT_STORE(Orientation_of_points_3)
        typedef typename Get_type<R_, Point_tag>::type Point;
        typedef typename Get_type<R_, Orientation_tag>::type result_type;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef Adapter_3<Base_, R_> Adapter;
        template<class Iter> result_type operator()(Iter f, Iter CGAL_assertion_code(e))const{
                CC c(this->kernel());
                Point const& A=*f;
                Point const& B=*++f;
                Point const& C=*++f;
                Point const& D=*++f;
                CGAL_assertion(++f==e);
                typedef typename Adapter::Point_3 P;
                return typename internal::Static_filters_predicates::Orientation_3<Adapter>()(P(this->kernel(),c,A),P(this->kernel(),c,B),P(this->kernel(),c,C),P(this->kernel(),c,D));
        }
};
template <class Base_,class R_> struct Side_of_oriented_sphere_3 : private Store_kernel<R_> {
        CGAL_FUNCTOR_INIT_STORE(Side_of_oriented_sphere_3)
        typedef typename Get_type<R_, Point_tag>::type Point;
        typedef typename Get_type<R_, Oriented_side_tag>::type result_type;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef Adapter_3<Base_, R_> Adapter;
        template<class Iter> result_type operator()(Iter f, Iter CGAL_assertion_code(e), Point const& E)const{
                CC c(this->kernel());
                Point const& A=*f;
                Point const& B=*++f;
                Point const& C=*++f;
                Point const& D=*++f;
                CGAL_assertion(++f==e);
                typedef typename Adapter::Point_3 P;
                return typename internal::Static_filters_predicates::Side_of_oriented_sphere_3<Adapter>()(P(this->kernel(),c,A),P(this->kernel(),c,B),P(this->kernel(),c,C),P(this->kernel(),c,D),P(this->kernel(),c,E));
        }
};


template <class Base_,class R_> struct Adapter_4 {
        typedef typename Get_type<R_, Orientation_tag>::type Orientation;
        typedef typename Get_type<R_, Oriented_side_tag>::type Oriented_side;
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef typename Get_functor<Base_, Orientation_of_points_tag>::type Orientation_base;
        struct Point_4 {
                R_ const&r; CC const&c; Point const& p;
                Point_4(R_ const&r_, CC const&c_, Point const&p_):r(r_),c(c_),p(p_){}
                decltype(auto) c0()const{return c(p,0);}
                decltype(auto) c1()const{return c(p,1);}
                decltype(auto) c2()const{return c(p,2);}
                decltype(auto) c3()const{return c(p,3);}
        };
        struct Orientation_4 {
                typedef typename Get_type<R_, Orientation_tag>::type result_type;
                auto operator()(Point_4 const&A, Point_4 const&B, Point_4 const&C,
                                Point_4 const&D, Point_4 const&E)const{
                        Point const* t[6]={&A.p,&B.p,&C.p,&D.p,&E.p};
                        return Orientation_base(A.r)(make_transforming_iterator<Dereference_functor>(t+0),make_transforming_iterator<Dereference_functor>(t+5));
                }
        };
};


template <class Base_,class R_> struct Orientation_of_points_4 : private Store_kernel<R_> {
        CGAL_FUNCTOR_INIT_STORE(Orientation_of_points_4)
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_type<R_, Orientation_tag>::type result_type;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef Adapter_4<Base_, R_> Adapter;
        template<class Iter> result_type operator()(Iter f, Iter CGAL_assertion_code(e))const{
                CC c(this->kernel());
                Point const& A=*f;
                Point const& B=*++f;
                Point const& C=*++f;
                Point const& D=*++f;
                Point const& E=*++f;
                CGAL_assertion(++f==e);
                typedef typename Adapter::Point_4 P;
                return typename internal::Static_filters_predicates::Orientation_4<Adapter>()(P(this->kernel(),c,A),P(this->kernel(),c,B),P(this->kernel(),c,C),
                                                                                              P(this->kernel(),c,D),P(this->kernel(),c,E));
        }
};

template <class Base_,class R_> struct Adapter_5 {
        typedef typename Get_type<R_, Orientation_tag>::type Orientation;
        typedef typename Get_type<R_, Oriented_side_tag>::type Oriented_side;
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef typename Get_functor<Base_, Orientation_of_points_tag>::type Orientation_base;
        struct Point_5 {
                R_ const&r; CC const&c; Point const& p;
                Point_5(R_ const&r_, CC const&c_, Point const&p_):r(r_),c(c_),p(p_){}
                decltype(auto) c0()const{return c(p,0);}
                decltype(auto) c1()const{return c(p,1);}
                decltype(auto) c2()const{return c(p,2);}
                decltype(auto) c3()const{return c(p,3);}
                decltype(auto) c4()const{return c(p,4);}
        };
        struct Orientation_5 {
                typedef typename Get_type<R_, Orientation_tag>::type result_type;
                auto operator()(Point_5 const&A, Point_5 const&B, Point_5 const&C,
                                Point_5 const&D, Point_5 const&E, Point_5 const&F)const{
                        Point const* t[6]={&A.p,&B.p,&C.p,&D.p,&E.p,&F.p};
                        return Orientation_base(A.r)(make_transforming_iterator<Dereference_functor>(t+0),make_transforming_iterator<Dereference_functor>(t+6));
                }
        };
};


template <class Base_,class R_> struct Orientation_of_points_5 : private Store_kernel<R_> {
        CGAL_FUNCTOR_INIT_STORE(Orientation_of_points_5)
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_type<R_, Orientation_tag>::type result_type;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef Adapter_5<Base_, R_> Adapter;
        template<class Iter> result_type operator()(Iter f, Iter CGAL_assertion_code(e))const{
                CC c(this->kernel());
                Point const& A=*f;
                Point const& B=*++f;
                Point const& C=*++f;
                Point const& D=*++f;
                Point const& E=*++f;
                Point const& F=*++f;
                CGAL_assertion(++f==e);
                typedef typename Adapter::Point_5 P;
                return typename internal::Static_filters_predicates::Orientation_5<Adapter>()(P(this->kernel(),c,A),P(this->kernel(),c,B),P(this->kernel(),c,C),
                                                                                              P(this->kernel(),c,D),P(this->kernel(),c,E),P(this->kernel(),c,F));
        }
};

template <class Base_,class R_> struct Adapter_6 {
        typedef typename Get_type<R_, Orientation_tag>::type Orientation;
        typedef typename Get_type<R_, Oriented_side_tag>::type Oriented_side;
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef typename Get_functor<Base_, Orientation_of_points_tag>::type Orientation_base;
        struct Point_6 {
                R_ const&r; CC const&c; Point const& p;
                Point_6(R_ const&r_, CC const&c_, Point const&p_):r(r_),c(c_),p(p_){}
                decltype(auto) c0()const{return c(p,0);}
                decltype(auto) c1()const{return c(p,1);}
                decltype(auto) c2()const{return c(p,2);}
                decltype(auto) c3()const{return c(p,3);}
                decltype(auto) c4()const{return c(p,4);}
                decltype(auto) c5()const{return c(p,5);}
        };
        struct Orientation_6 {
                typedef typename Get_type<R_, Orientation_tag>::type result_type;
                auto operator()(Point_6 const&A, Point_6 const&B, Point_6 const&C,
                                Point_6 const&D, Point_6 const&E, Point_6 const&F, Point_6 const&G)const{
                        Point const* t[7]={&A.p,&B.p,&C.p,&D.p,&E.p,&F.p,&G.p};
                        return Orientation_base(A.r)(make_transforming_iterator<Dereference_functor>(t+0),make_transforming_iterator<Dereference_functor>(t+7));
                }
        };
};
template <class Base_,class R_> struct Orientation_of_points_6 : private Store_kernel<R_> {
        CGAL_FUNCTOR_INIT_STORE(Orientation_of_points_6)
        typedef typename Get_type<R_, Point_tag>::type        Point;
        typedef typename Get_type<R_, Orientation_tag>::type result_type;
        typedef typename Get_functor<R_, Compute_point_cartesian_coordinate_tag>::type CC;
        typedef Adapter_6<Base_, R_> Adapter;
        template<class Iter> result_type operator()(Iter f, Iter CGAL_assertion_code(e))const{
                CC c(this->kernel());
                Point const& A=*f;
                Point const& B=*++f;
                Point const& C=*++f;
                Point const& D=*++f;
                Point const& E=*++f;
                Point const& F=*++f;
                Point const& G=*++f;
                CGAL_assertion(++f==e);
                typedef typename Adapter::Point_6 P;
                return typename internal::Static_filters_predicates::Orientation_6<Adapter>()(P(this->kernel(),c,A),P(this->kernel(),c,B),P(this->kernel(),c,C),
                                                                                              P(this->kernel(),c,D),P(this->kernel(),c,E),P(this->kernel(),c,F),
                                                                                              P(this->kernel(),c,G));
        }
};



} // namespace SFA

template <class Dim_ /* should be implicit */, class R_, class Derived_=Default>
struct Cartesian_static_filters : public R_ {
  constexpr Cartesian_static_filters(){}
  constexpr Cartesian_static_filters(int d):R_(d){}
};

template <class R_, class Derived_>
struct Cartesian_static_filters<Dimension_tag<2>, R_, Derived_> : public R_ {
  constexpr Cartesian_static_filters(){}
  constexpr Cartesian_static_filters(int d):R_(d){}
        typedef Cartesian_static_filters<Dimension_tag<2>, R_, Derived_> Self;
        typedef typename Default::Get<Derived_,Self>::type Derived;
        template <class T, class=void> struct Functor : Inherit_functor<R_, T> {};
        template <class D> struct Functor <Orientation_of_points_tag,D> {
                typedef
                        //std::conditional_t <
                        //std::is_same_v<D,No_filter_tag>,
                        //typename Get_functor<R_, Orientation_of_points_tag>,
                        SFA::Orientation_of_points_2<R_,Derived>
                        //        >::type
                                type;
        };
        template <class D> struct Functor <Side_of_oriented_sphere_tag,D> {
                typedef SFA::Side_of_oriented_sphere_2<R_,Derived> type;
        };
};

template <class R_, class Derived_>
struct Cartesian_static_filters<Dimension_tag<3>, R_, Derived_> : public R_ {
  constexpr Cartesian_static_filters(){}
  constexpr Cartesian_static_filters(int d):R_(d){}
        typedef Cartesian_static_filters<Dimension_tag<3>, R_, Derived_> Self;
        typedef typename Default::Get<Derived_,Self>::type Derived;
        template <class T, class=void> struct Functor : Inherit_functor<R_, T> {};
        template <class D> struct Functor <Orientation_of_points_tag,D> {
                typedef SFA::Orientation_of_points_3<R_,Derived> type;
        };
        template <class D> struct Functor <Side_of_oriented_sphere_tag,D> {
                typedef SFA::Side_of_oriented_sphere_3<R_,Derived> type;
        };
};

#ifndef CGAL_NO_STATIC_FILTER_456

template <class R_, class Derived_>
struct Cartesian_static_filters<Dimension_tag<4>, R_, Derived_> : public R_ {
  constexpr Cartesian_static_filters(){}
  constexpr Cartesian_static_filters(int d):R_(d){}
        typedef Cartesian_static_filters<Dimension_tag<4>, R_, Derived_> Self;
        typedef typename Default::Get<Derived_,Self>::type Derived;
        template <class T, class=void> struct Functor : Inherit_functor<R_, T> {};
        template <class D> struct Functor <Orientation_of_points_tag,D> {
                typedef SFA::Orientation_of_points_4<R_,Derived> type;
        };
};

template <class R_, class Derived_>
struct Cartesian_static_filters<Dimension_tag<5>, R_, Derived_> : public R_ {
  constexpr Cartesian_static_filters(){}
  constexpr Cartesian_static_filters(int d):R_(d){}
        typedef Cartesian_static_filters<Dimension_tag<5>, R_, Derived_> Self;
        typedef typename Default::Get<Derived_,Self>::type Derived;
        template <class T, class=void> struct Functor : Inherit_functor<R_, T> {};
        template <class D> struct Functor <Orientation_of_points_tag,D> {
                typedef SFA::Orientation_of_points_5<R_,Derived> type;
        };
};

template <class R_, class Derived_>
struct Cartesian_static_filters<Dimension_tag<6>, R_, Derived_> : public R_ {
  constexpr Cartesian_static_filters(){}
  constexpr Cartesian_static_filters(int d):R_(d){}
        typedef Cartesian_static_filters<Dimension_tag<6>, R_, Derived_> Self;
        typedef typename Default::Get<Derived_,Self>::type Derived;
        template <class T, class=void> struct Functor : Inherit_functor<R_, T> {};
        template <class D> struct Functor <Orientation_of_points_tag,D> {
                typedef SFA::Orientation_of_points_6<R_,Derived> type;
        };
};
#endif
} // namespace CGAL
#endif
