Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Hello,

does anyone have a function (preferably c++) to return the world space co-ordinates (x,y,z) of a single point (u) on a catmull-rom curve with any number of control points.

Thanks in advance.

does anyone have a function (preferably c++) to return the world space co-ordinates (x,y,z) of a single point (u) on a catmull-rom curve with any number of control points.

Thanks in advance.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, the catmull-rom curve is defined as:

q(t) = 0.5 * [ 2.P1 + (P2 - P1).t + (2.P0 - 5.P1 + 4.P2 - P3).t² + (3.P1 - P0 - 3.P2 + P3).t³ ]

Which, as you can see, is a cubic equation.

So, your task is merely to solve the cubic..

Secondly, the 't' quantity merely represents the distance of the spline between P1 and P2 — where 0.0 means it's at P1, 1.0 means it's at P2.

So, there's in fact multiple possible points at 't'. You need to be able to specify the 4 corresponding control points for that value, in order to solve 't'.

(As it goes, there would be at least N-2 possible values, given that 'N' is the number of control points).

You _have_ the 't' value, and also a set of control points, and you need to find out the world space coordinates at that point, 't', right?

For this, you would not need to solve a cubic, or anything like that.. you just need to specify the 4 control points to use.

Vector3 q3( Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, double t )

{

Vector3 temp ;

temp.x = q( v0.x, v1.x, v2.x, v3.x, t ) ;

temp.y = q( v0.y, v1.y, v2.y, v3.y, t ) ;

temp.z = q( v0.z, v1.z, v2.z, v3.z, t ) ;

return temp ;

}

So, if you had 6 control points, then 't' could correspond to 3 different points.

In order to construct a 3D curve given an arbitrarily sized set of control points, you merely iterate through each point, from 1 to N-1 (where points 0 and N are beyond the end points of the curve); you then iterate again, incrementing the 't' variable from 0.0 to 1.0, by an interval of 's' (which is dependent on the required smoothness of the curve; the smaller the value of 's', the more points will be constructed, and therefore the smoother the curve). You then pass X-1, X, X+1, X+2 and 't' to the above function, to get the corresponding 3D point (where X is the current control point).

So, something like this:

int N = 20 ; // the number of control points

Vector3 CP [N] ; // control points

.

double s = 1.0 / 10.0 ; // there will be 10 points between each control point

.

.

int x ;

double t ;

for ( x=1; x<N-2; x++ )

{

for ( t=0.0; t<1.0; t+=s )

{

Vector3 P = q3( CP[x-1], CP[x], CP[x+1], CP[x+2], t ) ;

// plot the point 'P', or shove it in an array/vector, or whatever

}

}

Just so you know, my code is right; it's from 1 to N-2, and not 1 to N-1.

=)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.