#ifndef POLYTOPED2TESSDISP01_H
#define POLYTOPED2TESSDISP01_H

#include <pointsdisplay.h>
#include <graphmisc.h>
#include <gobj.h>
#include <point.h>
#include <random.h>
#include <typedefs.h>

template< typename VPTS, typename VPOLY >
class polytopeD2tessdisp01
{
public:

  /** Global points. */
  VPTS const & pts;
  /** Vector of polytopes indexes to the points. */
  VPOLY const & vi;

  /** References to a tessellation. */
  polytopeD2tessdisp01
  (
    VPTS const & pts_,
    VPOLY const & vi_
  )
    : pts(pts_), vi(vi_) {}

  bool lines;
  bool multicolored;
  bool labelpoints;
  bool labelpolytopes;

  /** Write the graphics as unprocessed graphics. */
  void eval( gobjContainer & gr );

  vector< point3<float> > multicoloredvec;
  vector< point2<float> > vicenter;
};


//---------------------------------------------------------
// Implementation

template< typename VPTS, typename VPOLY >
void polytopeD2tessdisp01<VPTS,VPOLY>::eval
( 
  gobjContainer & gr 
)
{
  random11<double> r;

/*
  if (labelpoints)
  {

  }
*/
  uintc imax = vi.size();
  assert(imax>1);

  if (multicolored)
  {
    // Dummy to start index at 1.
    multicoloredvec.push_back( point3<float>() );
    for (uint i=1; i<imax; ++i)
    {
      if (vi[i].isnull())
      {
        multicoloredvec.push_back( point3<float>() );
        continue;
      }

      multicoloredvec.push_back( point3<float>(r(),r(),r()) );
    }
  }

  if (lines)
  {
    gr.push( new gobjglEnable(GL_LINE_STIPPLE) );
    gr.push( new gobjglLineStipple(1,0x1C47) );

    uint kmax;
    gr.push( new gobjglBegin(GL_LINES) );
    for (uint i=1; i<imax; ++i)
    {
      if (vi[i].isnull())
        continue;

    //gr.push_back( new gobjglLineStipple(1,(GLushort)rand()) );

      if (multicolored)
        gr.push( new gobjglColor3f(
          multicoloredvec[i].x,multicoloredvec[i].y,multicoloredvec[i].z) );

      kmax = vi[i].pi.size();
      for (uint k=0; k<kmax; ++k)
      {
        gr.push( new gobjglVertex2f(pts[ vi[i].pi[k]]) ); 
        gr.push( new gobjglVertex2f(pts[ vi[i].pi[(k+1)%kmax]]) ); 
      }
    }

    gr.push( new gobjglEnd() );

    gr.push(new gobjglDisable(GL_LINE_STIPPLE));
  }

  if (labelpolytopes)
  {
    uint kmax;
    vicenter.push_back( point2<float>() );
    for (uint i=1; i<imax; ++i)
    {
      if (vi[i].isnull())
      {
        vicenter.push_back( point2<float>() );
        continue;
      }

      kmax = vi[i].pi.size();
      assert(kmax!=0);

      point2<float> c(0.0,0.0);
      for (uint k=0; k<kmax; ++k)
      {
        c.x += pts[vi[i].pi[k]].x;
        c.y += pts[vi[i].pi[k]].y;
      }

      c.x /= (double)kmax;
      c.y /= (double)kmax;

      vicenter.push_back(c);
    }

    gr.push(new gobjglColor3f(1.0,0.0,0.0));

    pointsdisplay2D< point2<float> > * dp = 
      new pointsdisplay2D< point2<float> >(gr,vicenter,false,true);
    gr.push(dp);
  }

  if (labelpoints)
  {
    gr.push(new gobjglColor3f(0.0,1.0,0.0));
    gr.push( new pointsdisplay2D< point2<double> >(gr,pts,false,true));
  }

}


#endif



