views:

1088

answers:

2

I'm trying to create a class which takes in any number of points (position and control) and creates a catmull-rom spline based on the information given.

What I'm doing - and I'm really unsure if this is the right way to do it - is storing each individual point in a class like so:

class Point { public: Vector3 position; Vector3 control; }

Where obviously position is the position of the point and control is the control point.

My issue is connecting up the splines - obviously given the above class holding a point in the spline array indicates that any given position can only have one control point. So when having three or more points in a catmull rom spline the various individual catmull-rom splines which are being connected share one position and one control with another such spline.

Now with position being the same is required - since I want to create splines which are continuous between themselves. However I really wonder, should the control points also be the same between the two splines? With a bit of fiddling of the control points I can make it appear to be transitioning smoothly from one spline to another however I must emphasize that I'm not sure if the way they are transitioning is consistent with how catmull-rom splines form their shape. I'd much rather do it correctly than sit on my hands and rationalize that it's good enough.

Obviously the second part of my question is self explanatory: Given two control and position points how do I calculate the length of a catmull-rom spline?

+2  A: 

To define the spline between two control points, the Catmull-Rom spline needs the control points and the tangent vector at each control point. However, the tangent vector at internal (i.e. non-endpoint) control points is defined by the control points on either side of it: T(Pn) = (Pn+1 - Pn-1) / 2. For closed curves, the spline is completely defined by the set of control points. For non-closed curves, you need to also supply the tangent vector at the first and last control point. This is commonly done: T(P0) = P1 - P0 and T(Pn) = Pn - Pn-1.

Thus, for closed curves, your data structure is just a list of control points. For general splines, it's a list of points plus the first and last normal vector.

If you want to have a cardinal spline, then you can add a weighting factor to the tangent vector calculation as in the Wikipedia article.

To calculate the length of such a spline, one approach would be to approximate it by evaluating the spline at many points and then calculating the linear distance between each neighboring pair of points.

Eric
+1  A: 

With regards to measuring the lengths, you can do this with calculus since its a polynomial spline. You need to do integrate the distance function across the line. Its described quite well on Wikipedia...

jheriko