Sign in to follow this  

NURBS problem

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wonder if anyone who's grappled with NURBS before can give me a hand. I've coded a simple 2D NURBS curve evaluator, it doesn't use weights, and the knot vector is generated at runtime with values from 0.0 to 1.0 to give a uniform distribution. I've added 10 control points to the NURBS, the first 8 are the control points you see drawn below, and the last 2 are the 1st and 2nd control points added again, in an attempt to make the NURBS loop and create a closed 'blob'. The problem seems to be at the beginning and end of the curve, with some sort of overlapping, as well as some NaN values that cause those off shooting polys. After staring at the code for 2 days straight, I still can't spot the problem. Probably just need a fresh set of eyes, so any help would be much appreciated. Thanks.
class NURBS
	{
		// Private variables
		private Vector2[] p;	// NURBS control points
		private float[] t;	// NURBS knot vectors
		private int k;		// NURBS degree

		// Properties
		public Vector2[] Points
		{
			get { return p; }
			set { p = value; }
		}

		// Create the NURBS curve
		public void Create(int degree)
		{
			// Store the degree
			k = degree;

			// Create the knot vector
			t = new float[p.Length + k + 1];

			// Loop through all the knots
			for(int i = 0; i < t.Length; i++)
				// Set the knot value
				t[i] = (float)i / ((float)t.Length - 1.0f);
		}

		// Normalized B-Spline basis function
		private float N(int i, int k, float u)
		{
			// If the degree is zero
			if(k == 0)
				// Cap to the i'th knot
				if((t[i] <= u) && (u < t[i+1]))
					return 1.0f;
				else
					return 0.0f;
			// Otherwise
			else
				// Do the calculation
				return ((u - t[i]) / (t[i+k] - t[i])) * N(i, k-1, u) +
						((t[i+k+1] - u) / (t[i+k+1] - t[i+1])) * N(i+1, k-1, u);
		}

		// Evaluate the NURBS curve
		public Vector2 Evaluate(float u)
		{
			// Calculate the numerator
			Vector2 a = new Vector2();
			float b = 0;
			for(int i = 0; i < p.Length; i++)
			{
				// Add the influences to the accumulators
				float result = N(i, k, u);
				a += (p[i] * result);
				b += result;
			}

			// Calculate and return the evaluated point
			return (a / b);
		}
	}

Share this post


Link to post
Share on other sites

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this