package Maths;

import java.util.Vector;

import Ui.*;

public class NURBS extends BSpline {


  private double weights[][];

  private double weight_temp = 0;



  public NURBS (Vector controlpoints, int degree){

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

        //adding one to the dimension of the curve, so we can reuse a lot of the parent's methods
	this.dimension = super.dimension +1 ; 

	minimal_degree = 0;

  }

  public NURBS (){

	/* constructor */
	super();

        //adding one to the dimension of the curve, so we can reuse a lot of the parent's methods
	this.dimension = super.dimension +1 ;

	minimal_degree = 0;

  }

 public Polynom Clone (){

	return  new NURBS();

 }


public int GiveDimension(){

	return dimension-1;

 }

 //following methods override the parent's methods, changes to the overriden methods are indicated !

 public void ControlPoints_vector2ControlPoints_array(Vector controlpoints){

	/* overrides the polynom class method, because we have to accommodate for the weights */

	int max = controlpoints.size();

	int max2 = max + (this.curve).additional_multiplicity;

	this.controlp = new double[dimension+1][max2]; // the last column contains the weights !

	int cummulated = 0;


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

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


		for (int j =1 ; j <= pnt.multiplicity;j++ ){

			this.controlp[0][i+j-1+cummulated] = pnt.x;

			this.controlp[1][i+j-1+cummulated] = pnt.y;

			this.controlp[2][i+j-1+cummulated] = pnt.weight; //we also need the weights !

		}

		cummulated += pnt.multiplicity-1;

	};
//pm(controlp);
 }


 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];

   weights = new double[degree+1][degree+1];//this has been added in comparison to the parent!

   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--;};

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

     System.arraycopy(controlp[dim],position-degree,dbpoints,0,degree+1);

     System.arraycopy(controlp[dimension-1],position-degree,weights[0],0,degree+1);//with each evaluation the first column has to be reinitialised

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

   };

//pm(coor);
   return coor;

 };



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

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

   double alph ;

   double alph2 ;

   alph = super.Alpha(i,j,val,knotsset);

   weights[j][i] = alph*weights[j-1][i] + (1 - alph)*weights[j-1][i-1];

   alph2 = alph*weights[j-1][i]/weights[j][i];

   return alph2;

  };


public void MoveControlPoints(int[] distance){


	int max = controlp[0].length;


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

		for (int dim = 0; dim < dimension-1; dim++ ){//the difference is just 'dimension-1' compared to
							     //'dimension' in the Polynom class !

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

		}

	}


}


public int[][] GiveDrawPoints(){

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

    double [] temp;



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

	    if ( degree >= minimal_degree && controlp[0].length >= degree+1){

                 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-1 ; dim ++ ){//the difference is just 'dimension-1' compared to
							//'dimension' in the BSpline class !

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

	}

    }

//pm(draw_points);
    return draw_points;

 }

}

