#include <graphmisc.h>
#include <springlineardraw.h>

springlineardraw::springlineardraw
(
  uintc N_,
  doublec springradius,
  doublec springturns,
  uintc springNspikes 
)
{
  construct(N_,springradius,springturns,springNspikes);
}

void springlineardraw::construct
(
  uintc N_,
  doublec springradius,
  doublec springturns,
  uintc springNspikes 
)
{
  springlineargeom::construct(N_);

  sph.resize(N);
  hel.resize(N+1);
  for (uint i=0; i<N; ++i)
    sph[i].quadric = & quadric;

  for (uint i=0; i<=N; ++i)
  {
    hel[i].construct(springturns,springNspikes);
    hel[i].radius = springradius;
  }

  masscolor.resize(N);
  for (uint i=0; i<N; ++i)
    masscolor[i] = point3<double>( DOUBLECOLOR(218,165,32) );

}

void springlineardraw::update()
{
  if (N==0)
    return;

/*

  assert(N+1==lengths.size());
  assert(N==xoffsets.size());
  assert(N==sph.size());
  assert(N+1==hel.size());

  // Position the mass points.
  double x(0.0);
  for (uint i=0; i<N; ++i)
  {
    x += lengths[i];
    x += xoffsets[i];
    sph[i].x = x;
  }

  // Set the helix lengths.
  hel[0].length = lengths[0]+xoffsets[0];
  if (N>1)
    hel[N].length = lengths[N]-xoffsets[N-1];
  for (uint i=1; i<N; ++i)
    hel[i].length = lengths[i]+xoffsets[i]-xoffsets[i-1];

*/
}



void springlineardraw::draw()
{
  if (N==0)
    return;

  assert(N+1==lengths.size());
  assert(N==xoffsets.size());
  assert(N==sph.size());
  assert(N+1==hel.size());
  assert(N==masscolor.size());

  //update();

  lengths_update();
  for (uint i=0; i<=N; ++i)
    hel[i].length = lengthscurrent[i];

/*
cout << SHOW(hel[0].length) << endl;
cout << SHOW(hel[1].length) << endl;
cout << SHOW(hel[2].length) << endl;
*/

  glPushAttrib(GL_CURRENT_BIT);
  glPushAttrib(GL_LIGHTING);

  glPushMatrix();

  glDisable(GL_LIGHTING);
  glColor3d(springcolor.x,springcolor.y,springcolor.z);

  hel[0].draw();

  for (uint i=0; i<N; ++i)
  {
    glTranslatef(hel[i].length,0.0,0.0);
    glEnable(GL_LIGHTING);
    glColor3d(masscolor[i].x,masscolor[i].y,masscolor[i].z);
    sph[i].draw();
    glDisable(GL_LIGHTING);
    glColor3d(springcolor.x,springcolor.y,springcolor.z);
    hel[i+1].draw();
  }

  glPopMatrix();

  glPopAttrib();
  glPopAttrib();
}



