/*
	Test program for C code from An Introduction to NURBS
	by David F. Rogers. Copyright (C) 2000 David F. Rogers,
	All rights reserved.
	
	Name: tknotc.c
	Purpose: Test nonuniform chord approximation knot vector generation
	         and nonuniform basis function generation.
	Language: C
	Subroutines called: knotc.c, basisc.c
	Book reference: Section 3.6, Ex. 3.5, p 67
*/

	#include <stdio.h>

	main(){

	int i;
	int npts,c,p1;
	int nplusc;

	float b[21];
	float nbasis[21];
	float step;
	float sum;
	float t;
	float x[36];


	printf("\nTest program for nonuniform chord approximation knot vector \n\n");

	b[1]=0; /* Ex. 3.5 polygon in the z=1 plane. x=b[1], y=b[2], z=b[3], etc.*/
	b[2]=0;
	b[3]=1;
	b[4]=2;
	b[5]=6;
	b[6]=1;
	b[7]=4;
	b[8]=3;
	b[9]=1;
	b[10]=6;
	b[11]=6;
	b[12]=1;
	b[13]=8;
	b[14]=6;
	b[15]=1;


	npts = 5;
	c = 3;
	p1 = 10;
	nplusc = npts + c;

	printf("Polygon vertices are: \n\n");
	for (i = 1; i <= 3*npts; i=i+3){
		printf(" %f %f %f \n",b[i],b[i+1],b[i+2]);
	}

	printf("\nThe order is = %d \n",c);

	knotc(npts,c,b,x);

	printf("\nThe nonuniform knot vector based on chord distance is \n\n");
	for (i = 1; i <= nplusc; i++){
		printf(" %d %f \n", i,x[i]);
	}

	printf("\nCalculate the basis functions \n\n");

/*	printf("nplusc, x[nplusc] = %d %f \n",nplusc,x[nplusc]);*/

		for(t = 0.; t <= x[nplusc]; t = t + x[nplusc]/((float)(p1-1))){

		basisc(c,t,npts,x,nbasis);

		sum = 0;
		for (i = 1; i <= npts; i++){
			sum = sum + nbasis[i];
		}

		printf("For t = %f \n",t);
		printf("Basis function is \n");
		for (i = 1; i <= npts; i++){
			printf("%f ", nbasis[i]);
		}
		printf("\n");

		printf("Sum of the Basis functions is %f \n\n",sum);
	}
}

