Shortest distance from point to Catmull-Rom spline

In summary, the conversation discusses how to project a point onto a Catmull-Rom spline in C++, using a formula derived from a forumla on a webpage. The person asks for advice on which method to use, and another person suggests using a method that prioritizes speed over accuracy.
  • #1
Lantz
If I have a point P, how do I project it onto a Catmull-Rom spline (ie. get the point on the spline closest to P)?

This is how I calculate the spline, t goes from 0 to 1 (C++):
Code:
float t2 = t * t;
float t3 = t2 * t;
out.x = 0.5f * ( ( 2.0f * p1.x ) +
	( -p0.x + p2.x ) * t +
	( 2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x ) * t2 +
	( -p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x ) * t3 );
out.y = 0.5f * ( ( 2.0f * p1.y ) +
	( -p0.y + p2.y ) * t +
	( 2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y ) * t2 +
	( -p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y ) * t3 );
out.z = 0.5f * ( ( 2.0f * p1.z ) +
  	( -p0.z + p2.z ) * t +
 	( 2.0f * p0.z - 5.0f * p1.z + 4 * p2.z - p3.z ) * t2 +
 	( -p0.z + 3.0f * p1.z - 3.0f * p2.z + p3.z ) * t3 );

Which I derived from the forumla on this page. Should I use the, what's it called, the smallest square solution or something? My math skills does not go beyond one variable calculus and linear algebra so I have no clue whatsoever.

Regards,
Lantz
 
Mathematics news on Phys.org
  • #2
I tried looking into the actual solution but it quickly became bloated and even with mathematica on my side i ran into a 5th degree polynomial i needed to solve.

But I bet you need speed more than pinpoint accuracy. I suggest you draw lines that "emanate" out from the source of the point and see what the distance is for each line to intersect with the curve and then simply choose the smallest as the direction you want to head. By adjusting the number of lines you use you can trade-off speed for accuracy.
 
  • #3
Yea you're right, speed is more important than accuracy in my case. And that sounds like a way to do it. I'll give it a shot and see if I come up with anything, thanks dude.

/me scratches his head
 

FAQ: Shortest distance from point to Catmull-Rom spline

1. What is a Catmull-Rom spline and why is it used?

A Catmull-Rom spline is a type of mathematical curve that is commonly used in computer graphics and animation. It is used to create smooth and natural-looking curves between a series of points, making it ideal for creating curved paths for objects to follow in animations.

2. How is the shortest distance from a point to a Catmull-Rom spline calculated?

The shortest distance from a point to a Catmull-Rom spline is calculated by first finding the closest point on the spline to the given point. This can be done by iteratively calculating the distance between the given point and each point on the spline, and then choosing the point with the smallest distance. The distance between the given point and the closest point on the spline is then the shortest distance.

3. Is the shortest distance from a point to a Catmull-Rom spline always unique?

No, the shortest distance from a point to a Catmull-Rom spline is not always unique. Depending on the shape and complexity of the spline, there may be multiple points on the spline that are equidistant from the given point. In these cases, any of the equidistant points can be considered the shortest distance.

4. Can the shortest distance from a point to a Catmull-Rom spline be negative?

No, the shortest distance from a point to a Catmull-Rom spline cannot be negative. The distance is always calculated as a positive value, representing the distance between the given point and the closest point on the spline. If the given point is located inside the bounds of the spline, the shortest distance will be 0.

5. Are there any limitations or assumptions when using the shortest distance from a point to a Catmull-Rom spline?

One limitation of using the shortest distance from a point to a Catmull-Rom spline is that it assumes the given point is in the same plane as the spline. If the point is outside of this plane, the shortest distance may not accurately represent the actual distance between the point and the spline. Additionally, the shortest distance calculation may not be accurate for splines with sharp turns or high curvature.

Similar threads

Back
Top