-
Notifications
You must be signed in to change notification settings - Fork 56
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
Detect orthogonal lines using RANSAC #1314
base: main
Are you sure you want to change the base?
Changes from all commits
d0a02fd
8b296b8
6b9ba99
78ae05b
be4e241
17d86b2
343733e
2f55f2a
67fbad7
79a8777
7ef4be1
6fe96db
ec0fbca
0e6a624
ef9e3e0
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use linear_algebra::{Point2, Vector2}; | ||
use path_serde::{PathDeserialize, PathIntrospect, PathSerialize}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
line::{Line, Line2}, | ||
Distance, | ||
}; | ||
|
||
/// Two intersecting lines given by their intersection point and directions. | ||
#[derive( | ||
Copy, | ||
Clone, | ||
Debug, | ||
Default, | ||
Deserialize, | ||
PartialEq, | ||
Serialize, | ||
PathSerialize, | ||
PathIntrospect, | ||
PathDeserialize, | ||
)] | ||
pub struct TwoLines<Frame> { | ||
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. Why isn't this called 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. Initially the geometry just matched corners (i.e. two rays starting at the same point), which is now changed to two intersecting lines. I now updated the docstring accordingly. |
||
pub intersection_point: Point2<Frame>, | ||
pub first_direction: Vector2<Frame>, | ||
pub second_direction: Vector2<Frame>, | ||
} | ||
|
||
impl<Frame> TwoLines<Frame> { | ||
/// Creates two orthogonal lines from a line and a point outside the line. | ||
pub fn from_line_and_point_orthogonal(line: &Line2<Frame>, point: Point2<Frame>) -> Self { | ||
let intersection_point = line.closest_point(point); | ||
let direction1 = line.direction; | ||
let direction2 = point - intersection_point; | ||
|
||
Self { | ||
intersection_point, | ||
first_direction: direction1, | ||
second_direction: direction2, | ||
} | ||
} | ||
} | ||
|
||
impl<Frame> Distance<Point2<Frame>> for TwoLines<Frame> { | ||
fn squared_distance_to(&self, point: Point2<Frame>) -> f32 { | ||
let first_line = Line { | ||
point: self.intersection_point, | ||
direction: self.first_direction, | ||
}; | ||
let second_line = Line { | ||
point: self.intersection_point, | ||
direction: self.second_direction, | ||
}; | ||
|
||
let squared_distance_to_first_line = first_line.squared_distance_to(point); | ||
let squared_distance_to_second_line = second_line.squared_distance_to(point); | ||
|
||
squared_distance_to_first_line.min(squared_distance_to_second_line) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use approx::assert_relative_eq; | ||
use linear_algebra::{point, vector}; | ||
|
||
use crate::line::Line; | ||
|
||
use super::*; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
struct SomeFrame; | ||
|
||
const TWO_LINES: TwoLines<SomeFrame> = TwoLines { | ||
intersection_point: point![5.0, 5.0], | ||
first_direction: vector![0.0, 3.0], | ||
second_direction: vector![-10.0, 0.0], | ||
}; | ||
|
||
#[test] | ||
fn is_orthogonal() { | ||
let line: Line2<SomeFrame> = Line { | ||
point: point![0.0, 0.0], | ||
direction: vector![10.0, 0.0], | ||
}; | ||
let point = point![15.0, 5.0]; | ||
let corner_point = point![15.0, 0.0]; | ||
|
||
let corner = TwoLines::from_line_and_point_orthogonal(&line, point); | ||
assert_relative_eq!(corner.intersection_point, corner_point); | ||
assert_relative_eq!(corner.first_direction.dot(&corner.second_direction), 0.0); | ||
} | ||
|
||
#[test] | ||
fn correct_distance_top_left() { | ||
let point = point![0.0, 15.0]; | ||
let distance = 5.0; | ||
let squared_distance = distance * distance; | ||
|
||
assert_relative_eq!(TWO_LINES.distance_to(point), distance); | ||
assert_relative_eq!(TWO_LINES.squared_distance_to(point), squared_distance); | ||
} | ||
|
||
#[test] | ||
fn correct_distance_top_right() { | ||
let point = point![15.0, 15.0]; | ||
let distance = 10.0; | ||
let squared_distance = distance * distance; | ||
|
||
assert_relative_eq!(TWO_LINES.distance_to(point), distance); | ||
assert_relative_eq!(TWO_LINES.squared_distance_to(point), squared_distance); | ||
} | ||
|
||
#[test] | ||
fn correct_distance_bottom_left() { | ||
let point = point![0.0, 0.0]; | ||
let distance = 5.0; | ||
let squared_distance = distance * distance; | ||
|
||
assert_relative_eq!(TWO_LINES.distance_to(point), distance); | ||
assert_relative_eq!(TWO_LINES.squared_distance_to(point), squared_distance); | ||
} | ||
|
||
#[test] | ||
fn correct_distance_bottom_right() { | ||
let point = point![10.0, -5.0]; | ||
let distance = 5.0; | ||
let squared_distance = distance * distance; | ||
|
||
assert_relative_eq!(TWO_LINES.distance_to(point), distance); | ||
assert_relative_eq!(TWO_LINES.squared_distance_to(point), squared_distance); | ||
} | ||
} |
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.
Not your change, but I'd expect the
rotate_vector_90_degrees
to be a function of a vector taking theDirection
as argument...Resolve when read, or maybe even open an issue for that? What do you think?
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 agree, i will open an Issue.
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.
#1637