chrisveness / geodesy

Libraries of geodesy functions implemented in JavaScript
http://www.movable-type.co.uk/scripts/geodesy-library.html
MIT License
1.17k stars 203 forks source link

Point-Line Distance 3-Dimensional #112

Open emanuelef opened 1 year ago

emanuelef commented 1 year ago

Hi, Not an issue but a question. I'm trying to understand if with geodesy I can get the minimum distance between one 3D point (lat, lon, alt) and the line segment generated by two other 3D points. This is better explained here: Point-Line Distance--3-Dimensional

In my case I'm trying to find the minimum distance in meters from a point on the ground (but considering elevation) and a flying object passing by two points.

Thanks

SkybuckFlying commented 1 year ago

Yes, I tried the same thing.

So far I found two solutions:

The first solution is for one both paths are "straight" and not great circles: https://www.fundza.com/vectors/point2line/index.html

The second solution I tried using geodesy, but it has some strange issues,I already reported this as an issue, my delphi code is in there, it might give you some ideas how to do it:

https://github.com/chrisveness/geodesy/issues/111

I highly suggest you use the code from solution 1 as a starting point and checking point/code.

And then try and develop a solution 2, which tries and use geodesy to account for the bending of earth...

If you find a solution that works across the globe, let me know, my solution so far has some issues, not sure what is causing them... maybe porting issue to Delphi, or maybe it's a more general math issue...

SkybuckFlying commented 1 year ago

I just found a good solution:

https://www.ffi.no/en/research/n-vector/

Example 10:

When implemented correctly the CrossTrackDistance is all you need.

Make sure to normalize the input point though.

Also normalize the normal after computing it for the great circle.

It was well worth it to convert this code from C++ to Delphi...

The final solution/code is actually quite simple and works pretty fast.

SkybuckFlying commented 1 year ago

There is a problem with that solution though, it seems to not fully respect the line segment... and goes beyond it somehow... strange...

emanuelef commented 1 year ago

Yes, for what I need the point with minimum distance should be on the line segment. @SkybuckFlying in the link you provided the example seems to be using only lat,lon and not altitude. What did you use for the "3D points" ?