
#include <GL/glut.h>
#include <GL/gl.h>

#include <d3marchdisp.h>

typedef point2<double> pt2;

pt3c d3marchdisp::interpolate(pt3c & A, pt3c & B) const
{ 
  double const t = (cvalue-A.z)/(B.z-A.z); 
  pt3 X(B-A); 
  X*=t; 
  X+=A; 
  return X; 
}

void d3marchdisp::drawline( pt3c & A, pt3c & B ) const 
{
  glBegin(GL_LINES);

    glVertex2f(A.x,A.y);
    glVertex2f(B.x,B.y);

  glEnd();  
}


void d3marchdisp::eval( pt3c & P0, pt3c & P1, pt3c & P2 ) const
{
  unsigned int res(0);

  if (P0.z<cvalue)
    res += 1;
  if (P1.z<cvalue)
    res += 2;
  if (P2.z<cvalue)
    res += 4;

  if (res==0)
    return;

  if (res==7)
    return;

  switch(res)
  {
    case 1: case 6:
      drawline( interpolate(P0,P1), interpolate(P0,P2) );
      break;

    case 2: case 5:
      drawline( interpolate(P1,P0), interpolate(P1,P2) );
      break;

    case 4: case 3:
      drawline( interpolate(P2,P0), interpolate(P2,P1) );
      break;

  }

}


