Getting a point (x,y,z) at any position (u) on a catmull-rom curve with any number of control points

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.
aardmancgiAsked:
Who is Participating?
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.

InteractiveMindCommented:
Are you using (u) to denote the x, y [=q(t)], or t component of the catmull-rom curve?
0
aardmancgiAuthor Commented:
I am using u to denote t, sorry for the confusion. for instence I might want to know the coordinates of a point 1/4 of the way along the curve t = 0.25. Hope that makes sense!
0
InteractiveMindCommented:
Okay.

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..
0
Ultimate Tool Kit for Technology Solution Provider

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.

aardmancgiAuthor Commented:
That's very helpful thanks, but since I may have curves with any number of control points how do I know which 3 control points to use in the equation given q(t)? sorry if that is a really simple question, and thanks again.
0
InteractiveMindCommented:
Firstly, there's actually 4 points (P0 to P3).

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).
0
InteractiveMindCommented:
Sorry, I got a little muddled up there ...

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.
0
InteractiveMindCommented:
What is your actual task?

Do you need to construct a 3D curve, given a set of control points?
0
InteractiveMindCommented:
If so, then here's an example function of how you'd find the world space coordinates at point 't', given the corresponding 4 control points:

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 ;
}
0
InteractiveMindCommented:
Where q() is the catmull-rom function.
0
aardmancgiAuthor Commented:
Sorry, yes I ment 4 points. Yes I do want to find the world space point for t by constructing a 3D curve, given a set of control points and what you have written makes sense but since I could have more than 4 control points I need to solve for an arbitrary number of points not just 4. That's where I am confused.
0
InteractiveMindCommented:
Well, 't' is unique to a group of '4' points.

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
      }
   }
0
InteractiveMindCommented:
> you merely iterate through each point, from 1 to N-1

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

=)
0

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
aardmancgiAuthor Commented:
That's very helpfull, thanks for your patience :-)
0
InteractiveMindCommented:
No problem. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
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.