package Ui;



import java.awt.*;

import java.util.*;

import Maths.*;



public class Curve extends Object implements java.io.Serializable {

	Vector controlpoints; //holds the list of controlpoints in vector format

	public int degree ;//holds the degree

	Polynom polynom;//holds a link the associated polynomial

	Generationmethod generation_mode;//holds the definition of which kind of generation method we use to render the curve

	int parameterisation_mode;

	int drawingpoints[][];//holds the drawing points

	public int curvesegmentpoints[][];//holds the coordinates of the points that indicate the curve segments

	public boolean closed; //

	Color color;

	public int additional_multiplicity = 0;//since multiple controlpoints are treated as one from the user's point of view
					       // we need to know how much extra space we need to stock the controlpoints in array format !


	public Curve (Vector controlpoints,Polynom polynom, Generationmethod generation_mode, int parameterisation_mode, int degree, Color color){

		/* constructor */

		this.controlpoints = controlpoints;

		this.polynom = polynom;

		this.generation_mode = generation_mode;

		this.parameterisation_mode = parameterisation_mode;

		(this.polynom).curve = this;//Create a link from the polynom to the curve

		this.degree = degree;

		this.closed = false;

		this.color = color;


	};

	public Vector GiveControlPoints(){

		return controlpoints;

	}


	public Curve Clone(){

	    /* creates a copy of a curve

	Last change:  TMC  20 Jan 98   12:52 pm
	    */


	   int max = controlpoints.size();

	   Vector new_list = new Vector(max);


	   for (int i = 0; i < max; i++){

	      	ControlPoint temppnt = ((ControlPoint)((this.controlpoints).elementAt(i)));

	        ControlPoint point = new ControlPoint (temppnt.x, temppnt.y,temppnt.weight,temppnt.multiplicity);

	      	new_list.addElement(point);

	   }

	    Curve newcurve = new Curve(new_list,(this.polynom).Clone(), this.generation_mode, this.parameterisation_mode, this.degree, this.color);

	    newcurve.closed = this.closed;

	    newcurve.additional_multiplicity = this.additional_multiplicity;

	    newcurve.CalculateDrawPoints();

	    return newcurve;

	}


	public Curve Clone(int[] distance){

	    /* creates a copy of a curve at a certain distance away from the original curve

	       input: the distance between the new curve and the original

	    */


	   int max = controlpoints.size();

	   Vector new_list = new Vector(max);


	   for (int i = 0; i < max; i++){

	   	ControlPoint temppnt = ((ControlPoint)((this.controlpoints).elementAt(i)));

	       	ControlPoint point = new ControlPoint (temppnt.x, temppnt.y,temppnt.weight,temppnt.multiplicity);

        	point.x +=distance[0];

 	        point.y += distance[1];

		new_list.addElement(point);

	   }

	    Curve newcurve = new Curve(new_list,(this.polynom).Clone(), this.generation_mode, this.parameterisation_mode, this.degree, this.color);

	    newcurve.closed = this.closed;

	    newcurve.additional_multiplicity = this.additional_multiplicity;

	    newcurve.CalculateDrawPoints();

	    return newcurve;

	}



	public void CalculateDrawPoints(){

	      /* calls on the associated polynom object to calculate the drawingpoints*/

              this.drawingpoints = polynom.GiveDrawPoints(controlpoints);

        }

	public void AddMultiplicity (int change){

		additional_multiplicity = Math.max(additional_multiplicity+change,0);

	}


	public void CheckMultiplicity( int deg){

		/* checks all controlpoints to see if their multiplicity doesn't excede the degree of the curve
		   and adjusts the multiplicity if necessary
		   (the user may have lowered the degree of the curve, and some controlpoints may now end up with
		    to high a multiplicity)

		 */

		int max = controlpoints.size();

		for (int i = 0 ;i < max ;i++ ){

			ControlPoint pnt = (ControlPoint)controlpoints.elementAt(i);

			if ( pnt.multiplicity > deg ){

			     AddMultiplicity(deg - pnt.multiplicity);

                             pnt.multiplicity = deg;


			};
		}

                (Curvegeneration.pp).repaint();//adjust the multiplicity indication in the UI

	}



	public void setAdditionalMultiplicity( ){

		/* updates the additional_multiplicity variable, by recalculating it */

		int max ;

		if ( closed ) {max = controlpoints.size()-1;}

		else max = controlpoints.size();

		additional_multiplicity = 0;

		for (int i = 0 ;i < max ;i++ ){

			ControlPoint pnt = (ControlPoint)controlpoints.elementAt(i);


			additional_multiplicity += pnt.multiplicity-1;

		}

   	}



	public void setDegree( int degree){

		this.degree  = degree;

	}



	 public int getDegree(){

		/* gets the desired degree from the UI */

		return CurvePanel.degree;

	 }



	 public boolean deg_change(int ch){

		return polynom.deg_change(ch);

	 }


	public int GiveParamMode(){

		return this.parameterisation_mode;

	}



	public void MoveDrawPoints(int[] distance){

		/* moves the controlpoints in vector format over a certain distance, and calls on the Polynom method
		   that does the same for the controlpoints in arrayformat

		   input: the distance(N-Dimensional) over which we must move

		 */

		int max = drawingpoints[0].length;

		int maxdimension = polynom.GiveDimension();//THIS IS DEPENDENT OF THE DIMENSION OF THE PROBLEM AND IS NOT UPDATED AUTOM.!!!!!

		for ( int i = 0; i < max; i++ ){//moving the drawing points

			for (int dim = 0; dim < maxdimension; dim++ ){

				drawingpoints[dim][i] =drawingpoints[dim][i] + distance[dim];

			}

		}

		max = curvesegmentpoints[0].length;//moving the points that indidcate the curvesegments

		for ( int i = 0; i < max; i++ ){

			for (int dim = 0; dim < maxdimension; dim++ ){

				curvesegmentpoints[dim][i] = curvesegmentpoints[dim][i] + distance[dim];

			}

		}

 		polynom.MoveControlPoints(distance);//the array format of the controlpoints must also move

	      //	return drawpoints;

	}


	public void VectoriseControlPoints(double [][] controlp){

	/* vectorises the controlpoints, WITH CASTING from double to int

	   input: the controlpoints in array format, stemming from some operation within the Polynom class

	   results : the variable 'controlpoints' gets replaced

	*/

	    int max = controlp[0].length ;

	    int x;

	    int y;

	    double weight;

            ControlPoint point ;

	    Vector tempvector = new Vector(max);

	    additional_multiplicity = 0;

	    x=(int)controlp[0][0];

	    y=(int)controlp[1][0];

	    if ( controlp.length > polynom.dimension ){//weights are functional

			 weight = controlp[polynom.dimension][0];

	    }

	    else weight = 1;

            point = new ControlPoint(x,y,weight,1);
	       
            tempvector.addElement(point);

	    for ( int i = 1; i < max ; i++ ){

	       x=(int)controlp[0][i];

	       y=(int)controlp[1][i];

	       if ( x == point.x && y == point.y ){

			point.multiplicity++;

			additional_multiplicity++;

	       }

	       else{

		    if ( controlp.length > polynom.dimension ){//weights are functional

				 weight = controlp[polynom.dimension][i];

		    }

		    else{ weight = 1; };

       		    point = new ControlPoint(x,y,weight,1);
	       
	            tempvector.addElement(point);

	       }


	    };

	    controlpoints = tempvector;

	}

}


