Distance from Spline

On 23/06/2013 at 23:55, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R14 Stud 
Platform:      Mac OSX  ; 
Language(s) :     C++  ;

I need to calculate the distance from a point to the closest point on a spline. This process must be performed repeatedly so I was wondering about the SplineHelper which, after initial setup, is quite quick I understand.

Does anyone have any suggestions how to go about this? I could iterate along the spline checking the distance to my original point but that seems somewhat inefficient as a technique. The answer needs to be reasonably accurate however some margin of error is acceptable for the planned usage.

I suspect that there are methods in the SDK which would help. I'm having trouble knowing where to look...

On 24/06/2013 at 00:41, xxxxxxxx wrote:

in the xpresso node presets is a coffee preset to calculate the closest point on a spline. i am not 
aware of any built in methods in the sdk that can do that, but i am not very comfortable with the 
cpp sdk, so i might be wrong. you can find the coffee node in the xpresso presets down in the 
advanced group.

if you really have speed up the process you could calculate the two closest control points and then 
get between those control points a predefined set of subdivision steps. like taking for example two 
subdivisions per 100 world units between these points, take the two closest points out of that list 
and calculate the closest point on that line (c4d_utils offers a method for that). then you have just 
to calculate the length of the subdivision segments to that point and get the the vector for that 
spline offset. that would decrease the number of needed iterations drastically compared to the 
coffee node approach.

On 24/06/2013 at 03:52, xxxxxxxx wrote:


On 24/06/2013 at 08:17, xxxxxxxx wrote:

Thanks for your respective responses.

In case anyone else is searching in future for similar information, here's some extra information which has since come to light...

I also located another thread on a similar theme. You can find that here:


Kuroyume0161's code sample gives a way of iterating the spline segments and testing each in turn for its distance to a point. Looking at the documentation on PointLineDistance() it would seem to be possible to replace Robert's DistanceSegmentSphere() function, however that function does not return the nearest point along the line as well as the closest distance. It therefore comes down to what information you need.

In my case the distance alone is enough but for others I suggest reading Robert's code.