#include "total.h"








#include <iostream.h>  

//***************abstract base class DifferentTriangulation***************************
List<Polygon*> * DifferentTriangulation::triangulateMonoPolygon(Polygon &p)
{ }




/*********************derived class Triangulation_1**********************/
enum {UPPER,LOWER};
enum {LEFT,RIGHT,BEYOND,BEHIND,BETWEEN,ORIGIN,DESTINATION};


//static function leastVertex
//The function is passed a polygon p and a comparison function cmp, 
//and then finds the least vertex in p. The definition of the least
//vertex is whichever vertex that is less than the others under the 
//linear ordering of points given by cmp. Function leastVertex moves
//p's windows over the least vertex and returns the vertex.
  
static Vertex *leastVertex(Polygon &p, int(*cmp)(Point*,Point*))
{
	Vertex *bestV=p.currentVertex();
	p.advance(CLOCKWISE);
	for(int i=1; i<p.size();p.advance(CLOCKWISE),i++)
		if((*cmp)(p.currentVertex(),bestV)<0)
			bestV=p.currentVertex();
	p.setV(bestV);
	return bestV;
}



//static function leftToRightCmp
//I call leastVertex with this comparison function to find 
//the leftmost vertex in a polygon.

static int leftToRightCmp(Point *a, Point *b)
{
	if (*a<*b) return -1;
	if (*a>*b) return 1;
	return 0;
}


//static function adjacent
static bool adjacent(Vertex *v,Vertex *w)
{ return ((w==v->cw()) || (w==v->ccw()));}


//static function advancePtr
//This function marchs the pointers vu and vl, and updates pointerv 
//to point to whichever is moved.

static int advancePtr(Vertex * &vu, Vertex* &vl, Vertex* &v)
{
	Vertex *vun=vu->cw();
	Vertex *vln=vl->ccw();
	if (vun->point() < vln->point())
	{
		v=vu=vun;
		return UPPER;
	}
	else
	{
		v=vl=vln;
		return LOWER;
	}
}


//static function triangulateFanPolygon
//This fuction destructively decomposes a fan shaped polygon 
//with n vertices into (n-2) triangles, and append these triangles
//to list triangles.

static void triangulateFanPolygon(Polygon &p, List<Polygon*> *triangles)
{
	Vertex *w=p.currentVertex()->cw()->cw();
	int size=p.size();
	for(int i=3;i<size;i++)
	{
		triangles->append(p.split(w));
		
                w=w->cw();
	}
	triangles->append(&p);
        
}


//kernel member function Triangulation_1::triangulateMonoPolygon


List<Polygon*> * Triangulation_1::triangulateMonoPolygon(Polygon &p)
{
	Stack<Vertex*> s;
	List<Polygon*>*triangles=new List<Polygon*>;
	Vertex *v,*vu,*vl;
	leastVertex(p,leftToRightCmp);	//call external function
	v=vu=vl=p.currentVertex();
	s.push(v);
	int chain=advancePtr(vl,vu,v);	//call external function
	s.push(v);
	while(1)	//outer while loop
	{	
		chain=advancePtr(vl,vu,v);
		if(adjacent(v,s.top()) && !adjacent(v,s.bottom()))	//case 1
		{
		   int side =(chain==UPPER) ? LEFT:RIGHT;
		  
		   Vertex *a=s.top();
		   Vertex *b=s.nextToTop();
		   while((s.size()>1)&&(b->classify(v->point(),a->point())==side))
		   {
			if(chain==UPPER)
			{
			  cout<<"step 1"<<endl;
                           p.setV(b);
			   triangles->append(p.split(v));
			}
			else
			{
			   p.setV(v);
			   triangles->append(p.split(b));
		 	}
			s.pop();
			a=b;
			b=s.nextToTop();
		   }
		   s.push(v);
		}
		else if (!adjacent(v,s.top()))	//case 2
		{
		  
                   Polygon *q;
		   Vertex *t=s.pop();
		   if (chain==UPPER)
		   {
			p.setV(t);
  			q=p.split(v);
  		   }			
		   else
		   {
			p.setV(v);
  			q=p.split(t);
			q->advance(CLOCKWISE);
		   }
		   triangulateFanPolygon(*q,triangles);		//call external function
		   while (!s.empty())
			s.pop();
		   s.push(t);
		   s.push(v);
		}
		else						//case 3
		  {
		   p.setV(v);
		   triangulateFanPolygon(p,triangles);   	//call external function
		   

        int numOfTriangles = triangles->length();
	cout<<endl<<endl<<"After triangulation, the number of triangles is:";
	  cout<<"   "<<numOfTriangles<<endl<<endl; 

        cout<<"These triangles are:"<<endl;
        for (int i=0;i<numOfTriangles;i++)
	{
	        cout<<"Triangle No."<<i+1<<":  ";
	
		Polygon *tri = triangles->next();
		for (int j=0;j<3;j++)
		{
			double tmp_x=((tri->advance(CLOCKWISE))->point()).x;
			double tmp_y=((tri->currentVertex())->point()).y;
			cout<<"("<<tmp_x<<","<<tmp_y<<")"<<"     ";
		}
		cout<<endl;
	}


      return triangles;
     }
   }		//end of outer while
}  	




List<Polygon*> *  extfunc_of_triangulation(DifferentTriangulation &d, Polygon &p)
{ d.triangulateMonoPolygon(p); }
