#include <cmath>
using namespace std;

#include <d3tesstransform.h>


d3tesstransform::d3tesstransform(d3tess & _tess)
  : tess(_tess)
{
}


void d3tesstransform::multiply( pt2c & row1, pt2c & row2 )
{
  vector<pt3> & pt(tess.pt);
  uintc sz = pt.size();
  for ( uint i=1; i<sz; ++i )
  {
    pt2c w(pt[i]);
    pt[i].x = row1.dot(w);
    pt[i].y = row2.dot(w);
  }
}

void d3tesstransform::translate( pt2c & x )
{
  vector<pt3> & pt(tess.pt);
  uintc sz = pt.size();
  for ( uint i=1; i<sz; ++i )
  {
    pt[i].x += x.x;
    pt[i].y += x.y;
  }
}

void d3tesstransform::rotate(double const theta)
{
  double const cos_t = cos(theta);
  double const sin_t = sin(theta);
  pt2c r1(cos_t,-sin_t);
  pt2c r2(sin_t,cos_t);

  multiply(r1,r2);
}


void d3tesstransform::transEval()
{
  vector<pt3> & pt(tess.pt);
  uintc sz = pt.size();
  point3<double> t;
  point3<double> *p;
  double c;
  for ( uint i=1; i<sz; ++i )
  {
    p = & pt[i]; 
    // Preserve the c value of the point
    c = p->z;
    t.x = p->x;
    t.y = p->y;
    // Homogeneous points have 1 as their z value
    t.z = 1.0;
    
    trans.matrixMultiply(*p,t);
   
    // Restore the c value of the point
    p->z = c;
  }
}





