I understand some about catmull-rom, but I now need to understand how to get the total length of the spline. The only approach I have come up with is to take samples at regular intervals along the spline and add up each consecutive distance. This seems a bit time consuming and clunky. I would like to know if there is a math formula that can get this more accurately.....

**0**

###
#2
Crossbones+ - Reputation: **14222**

Posted 26 June 2013 - 12:39 PM

The math formula for the length of a cubic is an integral that doesn't seem to be expressible in terms of common functions (at least Mathematica and I don't know how to do it), so you need a numerical method anyway. If your sum of distances is precise enough and fast enough for your purpose, just go with it, because it is simple.

###
#3
Members - Reputation: **725**

Posted 26 June 2013 - 12:49 PM

It's simple and easy to understand, but it ultimately is only an approximation. Looking at http://en.wikipedia.org/wiki/Arc_length , I have come to the conclusion that, without some complicated math formula, the approximation (which is the only thing I could understand in the article) is the only way to go. This poses a problem because my splines are HUGE and therefore would have to have a lot of samples to get it really close. I would have to generate a table of angles and lengths with several variations. Each of these variations could in theory be scaled for and approximately matched up with each spline I generate in-game. Perhaps I could do these calculations on-the-fly, but with hundreds of ships all potentially moving at the same time, this might be too much......

###
#4
Crossbones+ - Reputation: **14222**

Posted 26 June 2013 - 02:20 PM

It's simple and easy to understand, but it ultimately is only an approximation.

You shouldn't obsess about this. Virtually all the calculations we perform in a computer are only approximations. The art is in finding approximations that are precise enough and fast enough for what we are doing.

When you say that your splines are HUGE, what do you mean? Is it that they have MANY nodes?

###
#5
Members - Reputation: **725**

Posted 26 June 2013 - 04:22 PM

No. It only has 5 total points to define each one. HUGE means they are physically large--each vector (point) can be very far away from the next. The biggest problem with this is when you try to move along the spline in very short time increments the math gets fuzzy and there is "gitter" from point to point. This is not going to be a problem because the camera is moving so fast and I have taken that into account (for now).....

###
#6
Moderators - Reputation: **24110**

Posted 26 June 2013 - 04:58 PM

So wait...

Are you trying to find the length of the spline? That is one set of calculations for arc length of an ellipse.

Or are you trying to make something move a constant linear velocity across a spline? That is a totally different problem. There are many good interpolation-based solutions for it.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.

###
#8
Moderators - Reputation: **24110**

Posted 26 June 2013 - 08:53 PM

A cubic Hermite spline is a matrix representation of the cubic spline. Just like you can express rotations in terms of angles and also in terms of a matrix. Lots of types of curves can be expressed as cubic Hermite splines, but not all cubic Hermite splines can be represented with cubic polynomial equations. Instead of a simple polynomial it is a 4x4 matrix. You can multiply the 4x4 spline matrix against the control points and then by the distance along the spline with simple matrix multiply operations. They are really easy for mathematicians and speedy for programers.

A Catmull-Rom spline is a specific set of numbers for the cubic Hermite spline. It is one out of infinitely many potential cubic Hermite splines. Those specific numbers can be used to append splines continuously and their integrals are continuous (meaning it smoothly moves from one to the next).

Hopefully that clears up the relationships between them.

As for the arc length, there are two options. Some cubic Hermite splines can have their segments expressed as cubic polynomial, like the one at the top of this post. If you are able to convert it to such a form then simple integration will find the length; repeat for each segment and take their sum. If you cannot convert the cubic Hermite spline into a simple polynomial, it will remain an elliptic curve; that requires a numeric integration rather than symbolic integration. The best numeric integration approach to that problem is called Hermite integration. Charles Hermite explored a lot of this kind of math, so you've got Hermite functions, Hermite splines, Hermite integration, and a few others all named after him.

But again, do you need the actual computed length of a spline? Or are you actually trying to get an interpolation with approximately smooth linear velocity? Because the two are very different problems.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.

###
#9
Crossbones+ - Reputation: **5473**

Posted 27 June 2013 - 05:02 AM

I'm not sure finding the length of a cubic is a simple integration.

The length of the curve f(x) = x^{3 }is integral(sqrt(1 + (3x^{2})^{2})) = integral(sqrt(1 + 9x^{4})) which wolfram alpha tells me evaluates to (deep breath):

http://www.wolframalpha.com/input/?i=integral%28sqrt%281%2B9x^4%29%29

which also involves an elliptic integral of the first kind. (difficult I expect, and I have a degree in maths).

**Edited by Paradigm Shifter, 27 June 2013 - 05:05 AM.**

###
#10
Members - Reputation: **725**

Posted 27 June 2013 - 05:52 AM

###
#11
Crossbones+ - Reputation: **5473**

Posted 27 June 2013 - 05:59 AM

It means it isn't worth converting it to C++ ;) Use numerical methods to find the length.

The length of a curve y = f(x) between x = a and x = b is

integral(from a to b)(sqrt(1 + (dy/dx)^{2})) which is not a nice thing to integrate analytically. (EDIT: Because of the square root. It would be easy for polynomials if the sqrt wasn't there).

Wikipedia page about it is here: https://en.wikipedia.org/wiki/Arc_length

EDIT2: So, for the simple function y = x^{3} the integration involves complex numbers, inverse hyperbolic sin function, and elliptic integrals. Not nice at all ;)

The arc length for a parabola y = x^{2} is a little bit nicer, http://www.wolframalpha.com/input/?i=integral%28sqrt%281+%2B+4x^2%29%29 , it only involves inverse hyperbolic sin (asinh)... which is now standard in cmath with C++11, but it wasn't in cmath as standard before that.

EDIT3: It's easy for y = x though, here is the full working for that.

Arc length for y = x:

dy/dx = 1

integral(sqrt(1 + (dy/dx)^{2})) = integral(sqrt(1 + 1^{2})) = integral(sqrt(1 + 1)) = integral(sqrt(2)) = x * sqrt(2) + constant. (note: the integral is with respect to x)

so for arc length between a and b the answer is integrate(a to b)(sqrt(2)) = F(b) - F(a) where F(x) = integral(sqrt(2)) = x * sqrt(2) + c

= b * sqrt(2) + c - (a * sqrt(2) + c) = (b-a)sqrt(2) + (c - c) = (b-a)sqrt(2)

so arc length from 0 to 1 of y = x is (1 - 0)sqrt(2) = sqrt(2) as expected.

EDIT4: Lots of edits ;)

EDIT5: Last edit, I promise. The derivation of the arc length for a parabola is explained here http://planetmath.org/arclengthofparabola

**Edited by Paradigm Shifter, 27 June 2013 - 07:10 AM.**

###
#13
Crossbones+ - Reputation: **5473**

Posted 27 June 2013 - 09:26 AM

Yeah, I'd recommend that too. If there is a parametric way to get approx. constant speed I'd use that to find the intervals to sum the line length over. I was just pointing out how impractical and difficult an analytic approach would be (length of a parabola involves hyperbolic trig functions, and length of a cubic involves special functions i.e. elliptic integrals).

###
#14
Members - Reputation: **725**

Posted 27 June 2013 - 12:55 PM

Here is what I am currently using:

void ApproximateCatMullRomLength3D(double *Length,const D3DXVECTOR3* p0, const D3DXVECTOR3* p1, const D3DXVECTOR3* p2, const D3DXVECTOR3* p3){ D3DXVECTOR3 v0,v1; v0=*p1; *Length=0; for (int t=1;t<1000;t++){ float time=t/1000.0f; CatmullRom_Evaluate(&v1,p0,p1,p2,p3,time);//a simple CMR function *Length+=double(D3DXVec3Length(&(v1-v0))); v0=v1; } }

All it does is evaluate 1000 points along the curve and sums up the lengths between each one..... I may not need that many samples, but I'm using 1000 until I get something that works right in my AutoPilot function.

###
#15
Members - Reputation: **725**

Posted 27 June 2013 - 02:15 PM

Ok. Picture time:

This illustrates what I am trying to do (as best as I can draw it). I originally have all the "waypoints" calculated for avoidance of an object represented here as "start", "wp1", "wp2", and "end". I then calculate the CMR points by shooting vectors from the WP to its neighbors (or start/end). I'm doing the calc for WP1 in this illustration..... The inner control points (p1 and p2) are determined by some max half-way length and placed in-line between their respective WPs. The outer control points (p0 and p3) are simply shot out using the same vectors but the length is a multiple of the "max half-way length" I determined earlier. The multiple I use gives varying results of sometimes strange curves. I think I'm doing it wrong..... You may be thinking I should just use the waypoints as the control points, but that won't work in most cases for my application......

###
#16
Crossbones+ - Reputation: **5473**

Posted 27 June 2013 - 02:22 PM

If it is for avoidance definitely go with the control points not being on the spline. The convex hull of the 4 control points always contains the spline, so as long that doesn't intersect your object you are guaranteed the spline won't intersect the object. I think you need 2 control points between each waypoint rather than just one though. You can see the diagram is going to be incorrect since the convex hull formed by p0, p1, p2, p3 intersects your object.

**Edited by Paradigm Shifter, 27 June 2013 - 02:22 PM.**

###
#17
Members - Reputation: **725**

Posted 27 June 2013 - 04:02 PM

I think I didn't explain my second image correctly. There are 4 separate examples in it. Each one is a different example of the multiplier I use. Here is the code. I have since modified it and it works a little better with the mod:

(In the code "p#" is "CV#" representing the control points)

void AUTOPILOT::CalculateCurves(SHIP *Ship){ if (WP.size()<2) return;//no need to do the function for (UINT wp=0;wp<WP.size()-1;wp++){ //find the max distance. max is shortest WP away from WP[wp] D3DXVECTOR3 dir1,dir2; double dist; if (wp==0){//wp-1 is the ship's location HUGEVECTOR3 hvDir1=Ship->V_Location-WP[wp].vWP; dir1=hvDir1.Normalize(); dist=hvDir1.Length/2.50; } else { HUGEVECTOR3 hvDir1=WP[wp-1].vWP-WP[wp].vWP; dir1=hvDir1.Normalize(); dist=hvDir1.Length/2.50; } HUGEVECTOR3 hvDir2=WP[wp+1].vWP-WP[wp].vWP; dir2=hvDir2.Normalize(); double dist2=hvDir2.Length/2.0; if (dist2<dist) dist=dist2; if (dist>WP[wp].MaxAllowedVel*10.0) dist=WP[wp].MaxAllowedVel*10.0; WP[wp].CV1=dir1; WP[wp].CV1*=float(dist); WP[wp].CV0=dir1*6.0f*float(dist); WP[wp].CV2=dir2; WP[wp].CV2*=float(dist); WP[wp].CV3=dir2*6.0f*float(dist);//this one WAS *8.0 //*************** //this is part of the modification dir1=WP[wp].CV0-WP[wp].CV3; dir1/=6.0f; WP[wp].CV0-=dir1; WP[wp].CV3+=dir1; //******************** ApproximateCatMullRomLength3D(&WP[wp].CurveSize,&WP[wp].CV0,&WP[wp].CV1,&WP[wp].CV2,&WP[wp].CV3); } }

I annotated where I changed the position of CV0 & CV3 and my "multiplier" is x6. This gives me a more gradual curve between CV1 &CV2.