# How can I calculate the derivative of a Catmull-Rom spline with nonuniform parameterization?

Allow me to preface this by saying I am not a trained mathematician in any sense, so it's entirely possible I'm missing something rather fundamental. That said, I'm trying to take the derivative of a centripetal or chordal Catmull-Rom spline. Using the uniform equation calculating the tangents for the control points as well as the derivative is very simple. But when you want to make a spline with nonuniform $$t$$ values, you have to go through this: Arrows indicate that you multiply by the coefficient and add to the adjacent to form the next coefficient up in the pyramid. When you get up to $$C$$ you have your coordinate on the curve. $$P$$ values are your control points and $$t$$ values are calculated with the following: If I'm just plugging values into $$t$$ then I can get the curve coordinates, but I need the derivative to do some other calculations. I tried to expand the entire thing manually and take the derivative at $$t_1$$ and $$t_2$$, but it was a mess, and I think I did it completely wrong, so I ask here. Looking at the answer here, this person claims to have done it and got these very simple formulas, but the only methodology listed is "mathematica."

How can I get a usable, general derivative? Is it necessary to choose an alpha value beforehand, or can it be done easily without choosing a parameterization type?

Images in this question were borrowed from an answer here.

The linked answer from eriatarka84 essentially gives you the equation of the curve. Then you just have to differentiate this equation to get the first derivative vector as a function of $t$.

His (or her) answer gives you the position $P_1$ and the derivative vector $Q_1=C(t_1)$ at the start of the curve segment. And, similarly, his formulas give you the position $P_2$ and the derivative vector $Q_2=C(t_2)$ at the end of the curve segment. As he points out, these four pieces of data make it easy to construct a fairly simple equation for the segment, which you can then differentiate.

The easiest (and most familiar) approach would be to use the Bézier form of the curve. So, let $$R_1 = P_1 + \frac13 (t_2 - t_1)Q_1 \quad ; \quad R_2 = P_2 - \frac13 (t_2 - t_1)Q_2$$

Then the Bézier control points of the segment are $P_1$, $R_1$, $R_2$, $P_2$. Let's introduce a new normalized parameter $u=(t-t_1)/(t_2-t_1)$, so that $u$ ranges over $[0,1]$ as $t$ goes from $t _1$ to $t_2$. Then the equation of the segment is $$C(u) = (1-u)^3 P_1 + 3t (1-u)^2 R_1 + 3u^2(1-u) R_2 + u^3 P_2 \quad (0 \le u \le 1)$$ If you already have a software function that calculates the derivative at a point on a Bézier curve, you're done. If not, then we have to differentiate this formula to get $C'(u)$. We get $$\frac{dC}{du}(u) = 3\big( (1-u)^2(R_1 - P_1) +2u (1-u)(R_2 - R_1) + u^2(P_2 - R_2) \big )$$ Then, finally, by the chain rule for differentiation $$\frac{dC}{dt}(t) = \frac{1}{t_2-t_1}\frac{dC}{du}(u)$$ If you plug in values for $R_1$ and $R_2$, then you'll get a big mess, but I don't see any point in doing this.

If you really want to simplify the formulae, and you're not adept at algebraic manipulation, you ought to use a computer algebra system. There are some free ones available.

• That's kind of where I'm having trouble, though. The equation produced by the expanded pyramid is so unwieldy that I don't really know how to get it into a form that I can actually use. I don't have mathematica or anything and when I tried to do it by hand it was a horrible mess. I know that I might be missing something obvious in the linked answer, so if you can point it out and make it clear for someone dull like me then I'll happily accept the answer. – ssb Jun 26 '14 at 12:51
• Do you need an explicit formula for the derivative, or do you just need a procedure (I.e. A few lines of code) that will give you the derivative? – bubba Jun 26 '14 at 13:17
• Ideally I'd like to see the formula. The answer I linked to shows the derivative if you solve for t1 and t2, but I need to be able to find the tangent of the curve at an arbitrary t value, so I need something I can interpolate over to get tangents at each t. I'm not sure if what I was doing was right or wrong because it ended up so complicated – ssb Jun 26 '14 at 13:28
• Thank you for your update. I think now looking at your post that I did indeed have a very fundamental misunderstanding/mixup regarding the original data. – ssb Jun 26 '14 at 15:55

Denis Kovacs wrote about finding the derivatives here: http://denkovacs.com/2016/02/catmull-rom-spline-derivatives/. He even provides a Matlab code to compute the spline and its derivatives.

Please see his site, here I just quote the solution:

You can use the same pyramid structure to evaluate the tangent C′(t) as well:

\begin{align} A_1' &= \frac{1}{t_1 - t_0}(P_1 - P_0)\\ A_2' &= \frac{1}{t_2 - t_1}(P_2 - P_1)\\ A_3' &= \frac{1}{t_3 - t_2}(P_3 - P_2)\\ \\ B_1' &= \frac{1}{t_2 - t_0}(A_2 - A_1) + \frac{t_2 - t}{t_2 - t_0} A_1' + \frac{t - t_0}{t_2 - t_0} A_2'\\ B_2' &= \frac{1}{t_3 - t_1}(A_3 - A_2) + \frac{t_3 - t}{t_3 - t_1} A_2' + \frac{t - t_1}{t_3 - t_1} A_3'\\ \\ C' &= \frac{1}{t_2 - t_1}(B_2 - B_1) + \frac{t_2 - t}{t_2 - t_1} B_1'(t) + \frac{t - t_1}{t_2 - t_1} B_2'(t) \end{align}

P.S. A and B are the same as your L's were A is the 2nd level and B the 3rd level of the pyramid, see the wiki.