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

Solved

Posted on 2006-04-13

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.

14 Comments

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

}

}

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

fizzArray challenge | 1 | 28 | |

java ^ examples | 8 | 48 | |

thread-safe code in c++ | 2 | 58 | |

countPairs challenge | 7 | 36 |

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

Connect with top rated Experts

**11** Experts available now in Live!