Skip to content

Implement closest_point for Segment[23]d. #20130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

IQuick143
Copy link
Contributor

Objective

  • Getting the closest point to a line segment is a fairly simple and useful but nontrivial operation.
  • @janhohenheim recently asked if we had a built-in for a similar problem. (Finding the distance, which afaik is best done by finding the closest point first, from there on it's a trivial problem given our API.)

Solution

  • So I did it.

Testing

  • Added 2 tests

@IQuick143 IQuick143 added C-Feature A new feature, making something new possible A-Math Fundamental domain-agnostic mathematical operations D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 14, 2025
let closest = segment.closest_point(*point);
assert!(
point.distance_squared(closest) <= point.distance_squared(segment.point1()),
"Closest point must always at least as close as either vertex."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this sentence is missing a "be"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, grammr.

@@ -2349,15 +2349,15 @@ mod tests {
let closest = segment.closest_point(*point);
assert!(
point.distance_squared(closest) <= point.distance_squared(segment.point1()),
"Closest point must always at least as close as either vertex."
"Closest point must always at be least as close as either vertex."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum, it's "must always be at least" x) sorry for pestering :p

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brain not working today, oops.

Copy link
Contributor

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Pretty much exactly the same implementation as what I have in Peck.

This uses a more intuitive and simple approach in terms of the naming and comments, whereas I (and Parry, Box2D, etc.) explicitly describe the barycentric coordinates and Voronoi regions (see the version I linked on Discord). Both are fine for this line segment case, but for the more complicated simplices like triangles and tetrahedra, IMO you really need to use Voronoi regions to make sense of things.

I'm kind of biased towards using barycentric coordinates and Voronoi regions for all simplices to have them be more consistent and kind of build on each other, but I'm fine with this for now :)

@Jondolf
Copy link
Contributor

Jondolf commented Jul 14, 2025

Also I expect these to be moved under a trait like PointQuery2d/PointQuery3d once I or someone else implements point queries more generally for all the shapes (I have most of them done in Peck), but I'm fine having a method for it on the shapes themselves in the meanwhile

@IQuick143 IQuick143 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 14, 2025
Copy link
Contributor

@djeedai djeedai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably fine as is but see remark on potential precision issue.

return self.vertices[0];
}

let length_squared = segment_vector.length_squared();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering about precision here. Since the value is scaled by the segment length it's potentially quite large already, and squaring it could lead to precision issues. On the other hand normalizing first wastes cycles for the early out case. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really cannot get a better precision than this. Because the actual length is computed as the square root of length_squared (which is computed through the pythagorean theorem), in fact one of the reason I'm using the length squared here is to skip on the square root operation that would end up being redundant here.

Due to how f32 numbers work, they have roughly the same amount of precision (as in, how many digits are correct) regardless of scale. So the number just being large is not a problem, problems arise when one tries adding together large numbers and small numbers, because then the small number gets lost in the rounding error of the large number. Or a better example: When subtracting two large numbers, and the result is a small number: The small number is going to be largely influenced by rounding on the large ones.

However if the operations are only multiplication or division, or addition of numbers of similar magnitude, then this is not a concern. (Ofc other concerns such as f32 error accumulation apply.)
As an example: The t parameter at the end will always* have around 5 digits of precision, since it's computed as a quotient of two numbers which have a similar* error.

There's practically no additions in this algorithm, except:
Vec3::dot, Vec3::length_squared and the vector operations at the end.

As I've said Vec3::length_squared is really the best measurement of length we can obtain.
The vector operations at the end are fundamentally constrained by the precision of the input points, if your points have coordinates on the order of 1megaunit, then they will have an error on the order of units, so the output of this method will obviously have a similar error. If you scale the whole thing down, the error also scales down.

The potentially most problematic component is Vec3::dot, where the situation of "subtracting two very close large numbers" might occur, if you have a point 1Mu far away from your line segment, which is a bunch shorter, and the point is roughly orthogonal to the line segment, then projection_scaled, might have a surprisingly large error, compared to the magnitude of length_scaled.

But I consider the limitations of Vec3::dot when dealing with extremely long nearly orthogonal vectors to be beyond my scope.

Nonetheless, you do make a good point, I appreciate that people are thinking about the precision issues during review.

);

let segments = [
Segment2d::new(Vec2::new(0.0, 0.0), Vec2::new(0.0, 0.0)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add one very long segment?

@IQuick143 IQuick143 added this to the 0.17 milestone Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants