/*! This example demonstrates the technique used to extend the point
 * kernel-object.
 */
#include <CGAL/Cartesian.h>
#include <CGAL/MP_Float.h>
#include <CGAL/Quotient.h>
#include <CGAL/Pm_segment_traits_2.h>
#include <CGAL/Pm_default_dcel.h>
#include <CGAL/Planar_map_2.h>
#include <iostream>
#include <iterator>
#include <algorithm>

// Kernel is the new kernel, and Kernel_base is the old kernel
template <typename Kernel, typename Kernel_base>
class My_cartesian_base : public Kernel_base::template Base<Kernel>::Type {
public:
  typedef typename Kernel_base::template Base<Kernel>::Type     Old_kernel;
  typedef typename Old_kernel::Point_2                          Old_point_2;
  typedef typename Old_kernel::FT                               FT;
  typedef typename Old_kernel::RT                               RT;
  typedef typename Old_kernel::Segment_2                        Segment_2;  

public:
  /*! The new point type */
  class New_point_2 : public Old_point_2 {
  private:
    /*! A field extending the point */
    int m_data;

  public:
    /*! Constructors */
    New_point_2(int data = 0) : m_data(data) {}
    New_point_2(const CGAL::Origin & origin, int data = 0) :
      Old_point_2(origin), m_data(data) {}

    New_point_2(const RT & hx, const RT & hy, const RT & hw, int data = 0) :
      Old_point_2(hx, hy, hw), m_data(data) {}
    New_point_2(const RT & hx, const RT & hy, int data = 0) :
      Old_point_2(hx, hy), m_data(data) {}

    /*! \brief obtains the data of the extended point */
    int get_data() const { return m_data; }
  };
  
  typedef New_point_2                 Point_2;

  /*! */
  template <typename Kernel2>
  struct Base { typedef My_cartesian_base<Kernel2, Kernel_base> Type; };
};

/*! The extended Kernel type */
template <typename FT_>
struct Ext_seg_kernel
  : public My_cartesian_base<Ext_seg_kernel<FT_>, CGAL::Cartesian<FT_> >
{};

// The remaining types:
typedef CGAL::Quotient<CGAL::MP_Float>          NT;
typedef Ext_seg_kernel<NT>                      Kernel;
typedef CGAL::Pm_segment_traits_2<Kernel>       Traits;
typedef Traits::Point_2                         Point_2;
typedef Traits::X_monotone_curve_2              X_monotone_curve_2;
typedef CGAL::Pm_default_dcel<Traits>           Dcel;
typedef CGAL::Planar_map_2<Dcel,Traits>         Planar_map;

/*! Exporter of the extended-point type */
inline
std::ostream & operator<<(std::ostream & o, const Point_2 & p)
{
  o << p.x() << ", " << p.y() << ", " << p.get_data();
  return o;
}

/*! main */
int main()
{
  // Create an instance of a Planar_map:
  Planar_map pm;
  X_monotone_curve_2 cv[5];

  Point_2 p0(1, 4, 0), p1(5, 7, 1), p2(9, 4, 2), p3(5, 1, 3);

  // Create the curves:
  cv[0] = X_monotone_curve_2(p0, p1);
  cv[1] = X_monotone_curve_2(p1, p2);
  cv[2] = X_monotone_curve_2(p2, p3);
  cv[3] = X_monotone_curve_2(p3, p0);
  cv[4] = X_monotone_curve_2(p0, p2);

  std::cout << "The curves of the map :" << std::endl;
  std::copy(&cv[0], &cv[5],
            std::ostream_iterator<X_monotone_curve_2>(std::cout, "\n"));
  std::cout << std::endl;

  // Insert the curves into the Planar_map:
  std::cout << "Inserting the curves to the map ... ";
  pm.insert(&cv[0], &cv[5]);
  std::cout << ((pm.is_valid()) ? "map valid!" : "map invalid!") << std::endl
            << std::endl;

  // Print the curves:
  Planar_map::Halfedge_const_iterator eit;
  for (eit = pm.halfedges_begin(); eit != pm.halfedges_end(); ++eit, ++eit) {
    const X_monotone_curve_2 & cv = eit->curve();
    std::cout << cv << std::endl;
  }
  
  return 0;
}
