?
Solved

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

Posted on 2006-04-13
14
Medium Priority
?
296 Views
Last Modified: 2010-04-17
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.
0
Comment
Question by:aardmancgi
  • 10
  • 4
14 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16449259
Are you using (u) to denote the x, y [=q(t)], or t component of the catmull-rom curve?
0
 

Author Comment

by:aardmancgi
ID: 16449338
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16449401
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aardmancgi
ID: 16449461
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16449494
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16449521
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16449529
What is your actual task?

Do you need to construct a 3D curve, given a set of control points?
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16449743
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16449751
Where q() is the catmull-rom function.
0
 

Author Comment

by:aardmancgi
ID: 16449988
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16450144
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
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 16450147
> 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
 

Author Comment

by:aardmancgi
ID: 16452877
That's very helpfull, thanks for your patience :-)
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16452885
No problem. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question