package Maths;

import java.util.Vector;

import Ui.*;


public class BSpline extends Polynom {


 /** The Spline class*/ 

	
 protected int position; /*this indicates the position of a val in the knotsequence*/


  public BSpline (Vector controlpoints, int degree){

	/* constructor */
	super(controlpoints, degree);

	minimal_degree = 0;

  }

  public BSpline (){

	/* constructor */
	super();

	minimal_degree = 0;

  }

 public Polynom Clone (){

	return  new BSpline();

 }



 protected void setDegree(){

	this.degree = curve.getDegree();

 }



 public  void CreateNewControlPoints(){

  /* creates the periodic controlpoints necessary for closed splines


	Last change:  TMC  20 Jan 98    2:25 pm
   */

		int g = controlp[0].length -2;

		double new_controlp[][] = new double[dimension][g+degree+1];


  		for ( int i = 0; i <= g+1 ; i++ ){// copying the old controlpoints

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

				new_controlp[dim][i] = controlp[dim][i];

			};

		};

  		for ( int i = 1; i < degree ; i++ ){// creating periodic controlpoints

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

				new_controlp[dim][i+g+1] = controlp[dim][i];

			};

		};

		controlp = new_controlp;
//pm(controlp);
  }


  public void Parameterise(){

       /* Searches a parameterisation for the curve

	  input: the controlpoints in array form

	  output: the knots, the first knot has value 0, the last value 1
	Last change:  TMC  20 Jan 98   11:14 am

	*/

	double total_distance = 0.0;

	h = Calculate_h(controlp);//calculate the distances between the nodes


	if ( (this.curve).closed ){ //then we need first to create new controlpoints

		CreateNewControlPoints();

		int n = controlp[0].length-1;

		int g = n - degree;

		knots = new double[2][g+2*degree+2];


		knots[0][degree] = 0; //the first knot has value 0, and multiplicity 1

		knots[1][degree] = 1;


		for ( int i = 1; i <= g+1 ; i++ ){//the internal knots

		       knots[0][i+degree] = knots[0][i-1+degree] + h[i-1];

		       knots[1][i+degree] = 1;

		       total_distance+=h[i-1];

		}

	     	for ( int i = 1; i <= degree  ; i++){//the boundary knots (periodic)

			knots[0][degree-i] = knots[0][degree+g+1-i] - 1;

			knots[1][degree-i] = 1;

			knots[0][i+degree+g+1] = knots[0][degree+i] + 1;

			knots[1][i+degree+g+1] = 1;

		}

	}

	else{

	double distance = 0.0;

	int n = controlp[0].length-1;

	int g = n - degree;

	knots = new double[2][g+2*degree+2];



	knots[0][degree] = 0.0; //the first knot has value 0, and multiplicity 1

	knots[1][degree] = 1;


	for ( int i = 1; i <= g+1 ; i++ ){ // calculating the internal knots, see article by Hartley and Judd

	       for ( int j = i; j <= degree+i-1 ; j++ ){

			distance += h[j-1];

	       }

	       knots[0][i+degree] = knots[0][i-1+degree] + distance;

	       knots[1][i+degree] = 1;

	       total_distance += distance;

	       distance = 0.0;

	}


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

		knots[0][i+degree] /= (total_distance);

	};


		for ( int i = 0; i <= degree ; i++ ){//the boundary knots coincide

		       knots[0][i] = 0.0;

		       knots[1][i] = degree+1;

		}

		for (int i = g+degree+2; i <= g+2*degree+1; i++ ){//the boundary knots coincide

		       knots[0][i] = 1.0;

		       knots[1][i] = degree+1;


		};

	};
// pm(knots);
 }



 public int[][] GiveDrawPoints(){

	/* calculates the coordinates of the points that will constitute the curve

	   output: the drawpoints*/

    int draw_points[][] = new int[dimension][number_drawing_points+1];

    double [] temp;



    for ( int i = 0 ; i <= number_drawing_points ; i++ ){//to calculate the drawpoints we evaluate the curve
							// for different values in the [0,1] interval

	    if ( degree >= minimal_degree && controlp[0].length >= degree+1){//degree OK & sufficient #controlpoints

                 double b= 1.0;

		 double getal = ((double)i)*b/number_drawing_points;

		 temp = Evaluate(getal);

	    }

	    else{

	        temp = new double[2];

		temp[0] = 0;
		temp[1] = 0;


	    };

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

		draw_points[dim][i] = (int)temp[dim];

	}

    }

    return draw_points;

 }



 protected double Alpha(int i, int j,double val, double[] knotsset) {

   /*defines the alpha used in the deBoor scheme*/

   double alf ;

    if (knotsset[(1+i-j+position)]!=knotsset[position-degree+i]  ){

	   alf = (val-knotsset[(i+position-degree)])/(knotsset[(1+i-j+position)]-knotsset[position-degree+i]);

    }

    else{alf = 0;};

	   return alf;


  };



 public int Position(double val) {

   /**returns the index of the knot that is immediately smaller then the value,assumes the val 

      lies between the extremal values found in set

      input : the parameter value

      ouput: the index number of the subinterval that contains the value
      **/

   int t = 0;

   while(t < knots[0].length && val >= knots[0][t]) t++;

   if (t <= knots[0].length) {return --t;}

   else {return -1;};


 };


 public double[]  Evaluate(double value){

   /**evaluates the Spline using the deBoor scheme

      input: value of the parameter

      output: the 2-D coordinates of the point**/

   double dbpoints[] = new double[degree+1];

   double coor[] = new double[2];


   try{

	   position = Position(value);

	   if( position >= knots[0].length-1-degree && !curve.closed){ position-=(degree+1) ;}/*evaluating in the endpoint needs a small correction */

	   if( position >= knots[0].length-1-degree && curve.closed){ position--;};

//System.out.println(position);

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

	     System.arraycopy(controlp[dim],position-degree,dbpoints,0,degree+1);//setting up the first column for usage in deBoor

	     coor[dim] = deBoor(dbpoints,value,knots[0])[degree][degree];

	   };

  }

  catch(Exception ex){

	//System.out.println(ex.toString());
       
  };

   return coor;

 }



 public double[][] InsertKnot( double value){

	/* inserts a new knot and a new controlpoint to the curve

	input: the parametervalue of the new knot
	output: the list of new controlpoints

	 */



   double dbpoints[] = new double[degree+1];

   int max = controlp[0].length+1;

   double newcontrolp[][] = new  double[dimension][max];

   double newknots[][] = new double[2][knots[0].length+1];

   position = Position(value);//searching the interval to which the new value belongs

   if( position > knots[0].length-1-degree && !curve.closed){ position-=degree+1 ;}

   if( position >= knots[0].length-1-degree && curve.closed){ position--;};

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

     System.arraycopy(controlp[dim],position-degree,dbpoints,0,degree+1);//setting up the first column for usage in deBoor


     System.arraycopy(controlp[dim],0,newcontrolp[dim],0,position-degree+1); //previous points are the same

     System.arraycopy(Transpose(deBoor(dbpoints,value,knots[0]))[1],1,newcontrolp[dim],position-degree+1,degree);//inserting the new controlpoints

     System.arraycopy(controlp[dim],position,newcontrolp[dim],position+1,(max-1-position));//the following points also

//pm(newcontrolp);

   }


   if ( curve.closed ){//inserting the new knot for closed curves and setting the controlp to the new controlpoints

//pm(controlp);

	  controlp = new double[dimension][newcontrolp[0].length-degree+1];

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

     	  	System.arraycopy(newcontrolp[dim],0,controlp[dim],0,controlp[0].length);

          }


	  int n = controlp[0].length+degree-2;

	  int g = n - degree;



	//inserting the new knot

        System.arraycopy(knots[0],degree,newknots[0],degree,position+1);

        System.arraycopy(knots[0],position+1,newknots[0],position+2,(knots[0].length-position-1));

        newknots[0][position+1] = value;

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

			newknots[0][degree-i] = newknots[0][degree+g+1-i] - 1;

			newknots[1][degree-i] = 1;

			newknots[0][i+degree+g+1] = newknots[0][degree+i] + 1;

			newknots[1][i+degree+g+1] = 1;

		}

   }

   else{//inserting the new knot for open curves and setting the controlp to the new controlpoints

        System.arraycopy(knots[0],0,newknots[0],0,position+1);

        System.arraycopy(knots[0],position+1,newknots[0],position+2,(knots[0].length-position-1));

        newknots[0][position+1] = value;

        controlp = newcontrolp;

   };
//pm(knots);
   knots = newknots;
//pm(knots);

   return controlp;



 }



 public int[][] CalculateCurvesegments(){

	/* calculates the coordinates of the boundaries of the
           curvesegments, this is achieved by evaluating the curve at the knots

	   output :the coordinates of the boundaries of the curvesegments */

	int n = controlp[0].length-1;

	int max = n+1;

 	double segm[][] = new double[n-degree+2][dimension];


	for (int i = degree; i <= max; i++){
//System.out.println(knots[0][i]);
		segm[i-degree] = Evaluate(knots[0][i]);

	}

        return Double2Int(Transpose(segm));

 }

}

