Sign in to follow this  

Cubic Splines

This topic is 4237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

A long time ago I found a great site that had a C++ function for cubic splines, that took four coords as points and a ratio of 0 to 1 that determined at which point along the spline it returned. Does anyone know where I can find this or anything similar? Any good tutorials on the net for splines that are explained in code instead of maths?(I'm a much better coder than I am mathematician :) ) Or has anyone wrote a 4 point cubic spline function they don't mind sharing?

Share this post


Link to post
Share on other sites
Is a cubic spline the same as a cubic Bézier curve? In which case it's just three parametric equations determined by the control points and the code should be trivial.

*Googles subject*

Okay, so it seems not. Cubic splines go through all of their points, right? This document shows how to transform such a spline into a normal Bezier curve, and it's even in code ;) (PostScript code! Muahaha!).

Edit: Beaten. And also, why is this in Multiplayer?

Share this post


Link to post
Share on other sites
Splines are used for interpolation, so it could be multiplayer. It could also be Math and Physics.

There are many kinds of cubic splines. Bezier splines are "cubic" in complexity, but don't go through all control points. Meanwhile, Hermite splines are also "cubic" but go through the points. In general, any cubic interpolation function will take the format:

output_pos = interpolate(float t, pos p0, pos p1, pos p2, pos p3);

Share this post


Link to post
Share on other sites
Yes, precisely.
I've been messing about with udp streams and I've found the sweet spot
for update intervals, but really need cubic splines to smooth the movement.


H, your function prototype is exactly like the function i found before,
except it worked on one dimension (I.e you had to call it for each dimension, no biggie)

I don't suppose you or anyone has a matching function body with the code do they? I really suck at complicated 3d maths so most of the tutorials go over my head. Can't find any that demonstrate it in code.

Share this post


Link to post
Share on other sites
Using three separate interpolators is easy, but might be not as good as a custom three-space interpolator. The reason is that constant velocity in 3-space is different from constant velocity in 1-space (if you want a constant velocity interpolator).

One good single-variable interpolator is the cubic hermite, found for example on http://www.musicdsp.com/.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob Janova
Is a cubic spline the same as a cubic Bézier curve?


If you mean a Parametric Free (or Natural) Cubic Spline, then indeed they are the same. In fact, the Cubic Bézier Curve is defined in terms of Parametric Cubic Splines, where you interpolate the dependent variables x, y on t.

Generally, a free cubic spline is defined as
y = S(x) = ax^3 + bx^2 + cx + d

However, when you parameterize S(x) with respect to [wrt] time, x = x(t), y = y(t), you get the Cubic Bézier curve.

x(t) = at^3 + bt^2 + ct + d
y(t) = et^3 + ft^2 + gt + h
(let t = 0, and then d,h simply become d = x_0 and h = y_0 )

A really nice explanation of the derivation of a-f is presented at:
http://www.tinaja.com/glib/fastbez.pdf

Since general cubic splines always go through their end-points, the Cubic Bézier curve must also go through all of the end-points wrt to time t. This is where in the confusion lies as to whether or not the spline goes through all the points. It definately goes through all the end-points, but whether or not you define your "control points" to equal your end-points is up to you.

Since you generally use cubic splines to piece-wise approximate an extended curve between multiple fixed/known/measured points, I call all these piece-wise fixed end-points the control points. However, when defining the Cubic Bézier, you must approximate (or choose from your control points) two inner points between each of the end-points. The Cubic Bézier curve will not pass through these inner points. Some people call these inner points also "control points", in which case the curve will not pass through all the control points. Thus, it's a matter of perspective.

I personally believe that if the two inner points are an approximation (e.g. non-end-points), it's unfair to call them control points because they are not guaranteed accurate (e.g. not measured). But if you select some of your end-points as your inner points (e.g now guaranteed accurate), beware the curve will not pass through them, even though you know it should! A real catch-22.


A little bit more explanation can be found at http://groups.google.com/group/comp.graphics.algorithms/msg/7276de840df9614c?hl=en&fwc=1


A sample implementation in C is on Wikipedia: http://en.wikipedia.org/wiki/B%C3%A9zier_curve

An alternate approach is to use the standard 2D Cubic Spline interpolation on an XY plane, fixing x and y wrt to t. If you do this, you can use the example in "Numerical recipies in C" at http://www.library.cornell.edu/nr/bookcpdf/c3-3.pdf to create a Cubic Spline in each dimension (x(t), y(t)). E.g. Instead of fixing x and interpolating for y, fix t and interpolate for x, then keeping the same fixed t interpolate for y.

[Edited by - osterman on May 14, 2006 2:00:48 AM]

Share this post


Link to post
Share on other sites

This topic is 4237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this