// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_sg_cloud2plot
#define tools_sg_cloud2plot

#include "plottables"

#include "../words"
#include "../num2s"

#include "../histo/c2d"

#ifdef TOOLS_MEM
#include "../mem"
#endif

namespace tools {
namespace sg {

class c2d2plot : public virtual points2D {
  TOOLS_SCLASS(tools::sg::c2d2plot)
public: //plottable
  virtual plottable* copy() const {return new c2d2plot(*this);}
  virtual bool is_valid() const {return true;}
  virtual const std::string& name() const {return m_name;}
  virtual void set_name(const std::string& a_s) {m_name = a_s;}
  virtual const std::string& title() const {return m_data.title();}
  virtual const std::string& legend() const {return m_legend;}
  virtual void set_legend(const std::string& a_s) {m_legend = a_s;}

  virtual void infos(const std::string& a_opts,std::string& a_sinfos) const {
    a_sinfos.clear();
    std::string f_lf("\n");
    std::vector<std::string> ws;
    words(a_opts," ",false,ws);
    std::vector<std::string>::const_iterator it;

    for(it=ws.begin();it!=ws.end();++it) {
      if(((*it)=="name") && m_name.size()) {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "Name\n";
        a_sinfos += m_name;

      } else if((*it)=="entries") {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "Entries\n";
        if(!numas<unsigned int>(m_data.entries(),a_sinfos)){}

      } else if((*it)=="mean") {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "MeanX\n";
        if(!numas<double>(m_data.mean_x(),a_sinfos)){}
        a_sinfos += f_lf;
        a_sinfos += "MeanY\n";
        if(!numas<double>(m_data.mean_y(),a_sinfos)){}

      } else if((*it)=="rms") {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "RMS X\n";
        if(!numas<double>(m_data.rms_x(),a_sinfos)){}
        a_sinfos += f_lf;
        a_sinfos += "RMS Y\n";
        if(!numas<double>(m_data.rms_y(),a_sinfos)){}

      }
    }
  }
public: //points2D
  virtual float x_axis_min() const {return (float)m_data.lower_edge_x();}
  virtual float x_axis_max() const {return (float)m_data.upper_edge_x();}
  virtual float y_axis_min() const {return (float)m_data.lower_edge_y();}
  virtual float y_axis_max() const {return (float)m_data.upper_edge_y();}

  virtual unsigned int points() const {return m_data.entries();}
  virtual bool ith_point(unsigned int a_index,float& a_x,float& a_y) const {
    if(a_index>=m_data.entries()) {a_x = 0;a_y = 0;return false;}
    if(a_index>=m_data.entries()) {a_x = 0;a_y = 0;return false;}
    a_x = (float)m_data.value_x(a_index);
    a_y = (float)m_data.value_y(a_index);
    return true;
  }
public:
  c2d2plot(const histo::c2d& a_data)
  :m_data(a_data)
  {
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  virtual ~c2d2plot(){
#ifdef TOOLS_MEM
    mem::decrement(s_class().c_str());
#endif
  }
public:
  c2d2plot(const c2d2plot& a_from)
  :plottable(a_from)
  ,points2D(a_from)
  ,m_data(a_from.m_data)
  ,m_name(a_from.m_name)
  ,m_legend(a_from.m_legend)
  {
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  c2d2plot& operator=(const c2d2plot& a_from){
    m_name = a_from.m_name;
    m_legend = a_from.m_legend;
    return *this;
  }
protected:
  const histo::c2d& m_data;
  std::string m_name;
  std::string m_legend;
};

}}

#include "../histo/c3d"

namespace tools {
namespace sg {

class c3d2plot : public virtual points3D {
  TOOLS_SCLASS(tools::sg::c3d2plot)
public: //plottable
  virtual plottable* copy() const {return new c3d2plot(*this);}
  virtual bool is_valid() const {return true;}
  virtual const std::string& name() const {return m_name;}
  virtual void set_name(const std::string& a_s) {m_name = a_s;}
  virtual const std::string& title() const {return m_data.title();}
  virtual const std::string& legend() const {return m_legend;}
  virtual void set_legend(const std::string& a_s) {m_legend = a_s;}

  virtual void infos(const std::string& a_opts,std::string& a_sinfos) const {
    a_sinfos.clear();
    std::string f_lf("\n");
    std::vector<std::string> ws;
    words(a_opts," ",false,ws);
    std::vector<std::string>::const_iterator it;

    for(it=ws.begin();it!=ws.end();++it) {
      if(((*it)=="name") && m_name.size()) {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "Name\n";
        a_sinfos += m_name;

      } else if((*it)=="entries") {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "Entries\n";
        if(!numas<unsigned int>(m_data.entries(),a_sinfos)){}

      } else if((*it)=="mean") {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "MeanX\n";
        if(!numas<double>(m_data.mean_x(),a_sinfos)){}
        a_sinfos += f_lf;
        a_sinfos += "MeanY\n";
        if(!numas<double>(m_data.mean_y(),a_sinfos)){}
        a_sinfos += f_lf;
        a_sinfos += "MeanZ\n";
        if(!numas<double>(m_data.mean_z(),a_sinfos)){}

      } else if((*it)=="rms") {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "RMS X\n";
        if(!numas<double>(m_data.rms_x(),a_sinfos)){}
        a_sinfos += f_lf;
        a_sinfos += "RMS Y\n";
        if(!numas<double>(m_data.rms_y(),a_sinfos)){}
        a_sinfos += f_lf;
        a_sinfos += "RMS Z\n";
        if(!numas<double>(m_data.rms_z(),a_sinfos)){}

      }
    }
  }
public: //points2D
  virtual float x_axis_min() const {return (float)m_data.lower_edge_x();}
  virtual float x_axis_max() const {return (float)m_data.upper_edge_x();}
  virtual float y_axis_min() const {return (float)m_data.lower_edge_y();}
  virtual float y_axis_max() const {return (float)m_data.upper_edge_y();}
  virtual float z_axis_min() const {return (float)m_data.lower_edge_z();}
  virtual float z_axis_max() const {return (float)m_data.upper_edge_z();}

  virtual unsigned int points() const {return m_data.entries();}
  virtual bool ith_point(unsigned int a_index,float& a_x,float& a_y,float& a_z) const {
    if(a_index>=m_data.entries()) {a_x = 0;a_y = 0;a_z = 0;return false;}
    if(a_index>=m_data.entries()) {a_x = 0;a_y = 0;a_z = 0;return false;}
    a_x = (float)m_data.value_x(a_index);
    a_y = (float)m_data.value_y(a_index);
    a_z = (float)m_data.value_z(a_index);
    return true;
  }
public:
  c3d2plot(const histo::c3d& a_data)
  :m_data(a_data)
  {
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  virtual ~c3d2plot(){
#ifdef TOOLS_MEM
    mem::decrement(s_class().c_str());
#endif
  }
public:
  c3d2plot(const c3d2plot& a_from)
  :plottable(a_from)
  ,points3D(a_from)
  ,m_data(a_from.m_data)
  ,m_name(a_from.m_name)
  ,m_legend(a_from.m_legend)
  {
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  c3d2plot& operator=(const c3d2plot& a_from){
    m_name = a_from.m_name;
    m_legend = a_from.m_legend;
    return *this;
  }
protected:
  const histo::c3d& m_data;
  std::string m_name;
  std::string m_legend;
};

}}

#endif
