Jump to content
  • Advertisement
Sign in to follow this  
Scruffytuff

NURBS problem

This topic is 4079 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 = (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 <= u) && (u < t[i+1]))
					return 1.0f;
				else
					return 0.0f;
			// Otherwise
			else
				// Do the calculation
				return ((u - t) / (t[i+k] - t)) * 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 * result);
				b += result;
			}

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

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!