-
-
Notifications
You must be signed in to change notification settings - Fork 840
Use poly-cool as the root finder in bezier-rs #3031
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,23 +250,23 @@ impl Bezier { | |
/// Returns the parametric `t`-value that corresponds to the closest point on the curve to the provided point. | ||
/// <iframe frameBorder="0" width="100%" height="300px" src="https://graphite.rs/libraries/bezier-rs#bezier/project/solo" title="Project Demo"></iframe> | ||
pub fn project(&self, point: DVec2) -> f64 { | ||
let sbasis = crate::symmetrical_basis::to_symmetrical_basis_pair(*self); | ||
let derivative = sbasis.derivative(); | ||
let dd = (sbasis - point).dot(&derivative); | ||
let roots = dd.roots(); | ||
// The points at which the line from us to `point` is perpendicular | ||
// to our curve are the critical points of the distance function. | ||
let critical = self.normals_to_point(point); | ||
|
||
let mut closest = 0.; | ||
let mut min_dist_squared = self.evaluate(TValue::Parametric(0.)).distance_squared(point); | ||
|
||
for time in roots { | ||
for time in critical { | ||
let distance = self.evaluate(TValue::Parametric(time)).distance_squared(point); | ||
dbg!(distance, time); | ||
if distance < min_dist_squared { | ||
closest = time; | ||
min_dist_squared = distance; | ||
} | ||
} | ||
|
||
if self.evaluate(TValue::Parametric(1.)).distance_squared(point) < min_dist_squared { | ||
if dbg!(self.evaluate(TValue::Parametric(1.)).distance_squared(point)) < min_dist_squared { | ||
closest = 1.; | ||
} | ||
closest | ||
|
@@ -354,7 +354,8 @@ mod tests { | |
assert_eq!(bezier1.project(DVec2::new(100., 100.)), 1.); | ||
|
||
let bezier2 = Bezier::from_quadratic_coordinates(0., 0., 0., 100., 100., 100.); | ||
assert_eq!(bezier2.project(DVec2::new(100., 0.)), 0.); | ||
assert_eq!(bezier2.project(DVec2::new(99.99, 0.)), 0.); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to change this test slightly, because the old point had two nearest points (at t=0 and t=1), and the new root-finder was flagging t=0.999999999 as the minimizer. |
||
assert!((bezier2.project(DVec2::new(-50., 150.)) - 0.5).abs() <= 1e-8); | ||
|
||
let bezier3 = Bezier::from_cubic_coordinates(-50., -50., -50., -50., 50., -50., 50., -50.); | ||
assert_eq!(DVec2::new(0., -50.), bezier3.evaluate(TValue::Parametric(bezier3.project(DVec2::new(0., -50.))))); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed this to run just the bezier-rs tests (with
cargo test -p bezier-rs --features=std
), because without it there were lots of undefined references to math functions.