

#include <d3minboundary.h>
#include <d3tess.h>

typedef point2<double> pt2;
typedef point3<double> pt3;

#define SHOW(x) #x << '=' << (x)



d3minboundary::d3minboundary(d3tess & _tess)
  : d3minoperator(_tess)
{
}

bool const d3minboundary::eval(uintc a, uintc b)
{
//cout << "minb:eval(" << a << "," << b << ")" << endl;
  if (tess.isconvex(a,b)==false)
    return false;

  simplexD2linked const & A(tess.vi[a]);
  simplexD2linked const & B(tess.vi[b]);

  uint ai = A.niInverse(b);
  uint bi = B.niInverse(a);

  uint p[4];
  p[0] = A.pi[ai];
  p[3] = B.pi[bi];
  p[1] = A.pi[(ai+1)%3];
  p[2] = A.pi[(ai+2)%3];

  // d1 is the current arrangement
  
  vector<pt3> const & pt(tess.pt);
  pt2 D1(pt[p[0]]-pt[p[3]]);
  pt2 D2(pt[p[1]]-pt[p[2]]);

  double const d1=D1.distance();
  double const d2=D2.distance();
//cout << SHOW(d1) << " " << SHOW(d2) << endl;
  
  if (d1<d2)
  {
//cout << "fliped " << a << "," << b << endl;
    tess.flip(a,b);
    return true;
  }

  return false;
}



