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
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.

4 participants