Skip to content

Commit

Permalink
Improve documentation and variable naming
Browse files Browse the repository at this point in the history
  • Loading branch information
julianschuler committed Feb 18, 2025
1 parent 7ef4be1 commit 6fe96db
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 47 deletions.
47 changes: 24 additions & 23 deletions crates/geometry/src/two_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
Distance,
};

/// A corner given by a point and the directions of two outgoing rays.
/// Two intersecting lines given by their intersection point and directions.
#[derive(
Copy,
Clone,
Expand All @@ -21,40 +21,41 @@ use crate::{
PathDeserialize,
)]
pub struct TwoLines<Frame> {
pub point: Point2<Frame>,
pub direction1: Vector2<Frame>,
pub direction2: Vector2<Frame>,
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 corner_point = line.closest_point(point);
let intersection_point = line.closest_point(point);
let direction1 = line.direction;
let direction2 = point - corner_point;
let direction2 = point - intersection_point;

Self {
point: corner_point,
direction1,
direction2,
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 line1 = Line {
point: self.point,
direction: self.direction1,
let first_line = Line {
point: self.intersection_point,
direction: self.first_direction,
};
let line2 = Line {
point: self.point,
direction: self.direction2,
let second_line = Line {
point: self.intersection_point,
direction: self.second_direction,
};

line1
.squared_distance_to(point)
.min(line2.squared_distance_to(point))
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)
}
}

Expand All @@ -71,9 +72,9 @@ mod tests {
struct SomeFrame;

const TWO_LINES: TwoLines<SomeFrame> = TwoLines {
point: point![5.0, 5.0],
direction1: vector![0.0, 3.0],
direction2: vector![-10.0, 0.0],
intersection_point: point![5.0, 5.0],
first_direction: vector![0.0, 3.0],
second_direction: vector![-10.0, 0.0],
};

#[test]
Expand All @@ -86,8 +87,8 @@ mod tests {
let corner_point = point![15.0, 0.0];

let corner = TwoLines::from_line_and_point_orthogonal(&line, point);
assert_relative_eq!(corner.point, corner_point);
assert_relative_eq!(corner.direction1.dot(corner.direction2), 0.0);
assert_relative_eq!(corner.intersection_point, corner_point);
assert_relative_eq!(corner.first_direction.dot(corner.second_direction), 0.0);
}

#[test]
Expand Down
20 changes: 11 additions & 9 deletions crates/ransac/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<Frame> RansacFeature<Frame> {
let line = Line::from_points(point1, point2);
let two_lines = TwoLines::from_line_and_point_orthogonal(&line, point3);

if two_lines.direction2.norm() > f32::EPSILON {
if two_lines.second_direction.norm() > f32::EPSILON {
Self::TwoLines(two_lines)
} else {
Self::Line(line)
Expand Down Expand Up @@ -73,25 +73,27 @@ impl<Frame> IntoIterator for RansacResult<Frame> {
}
RansacFeature::TwoLines(two_lines) => {
let dividing_line1 = Line {
point: two_lines.point,
direction: two_lines.direction1.normalize() + two_lines.direction2.normalize(),
point: two_lines.intersection_point,
direction: two_lines.first_direction.normalize()
+ two_lines.second_direction.normalize(),
};
let dividing_line2 = Line {
point: two_lines.point,
direction: two_lines.direction1.normalize() - two_lines.direction2.normalize(),
point: two_lines.intersection_point,
direction: two_lines.first_direction.normalize()
- two_lines.second_direction.normalize(),
};

let (used_points1, used_points2): (Vec<_>, Vec<_>) =
self.used_points.iter().partition(|&point| {
dividing_line1.is_above(*point) != dividing_line2.is_above(*point)
});
let line1 = Line {
point: two_lines.point,
direction: two_lines.direction1,
point: two_lines.intersection_point,
direction: two_lines.first_direction,
};
let line2 = Line {
point: two_lines.point,
direction: two_lines.direction2,
point: two_lines.intersection_point,
direction: two_lines.second_direction,
};

[
Expand Down
28 changes: 17 additions & 11 deletions crates/vision/src/line_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,28 @@ impl LineDetection {
.unwrap(),
)),
RansacFeature::TwoLines(two_lines) => {
let point = context
let intersection_point = context
.camera_matrix
.ground_to_pixel(two_lines.point)
.ground_to_pixel(two_lines.intersection_point)
.unwrap();
RansacFeature::TwoLines(TwoLines {
point,
direction1: context
intersection_point,
first_direction: context
.camera_matrix
.ground_to_pixel(two_lines.point + two_lines.direction1.normalize())
.unwrap_or(point)
- point,
direction2: context
.ground_to_pixel(
two_lines.intersection_point
+ two_lines.first_direction.normalize(),
)
.unwrap_or(intersection_point)
- intersection_point,
second_direction: context
.camera_matrix
.ground_to_pixel(two_lines.point + two_lines.direction2.normalize())
.unwrap_or(point)
- point,
.ground_to_pixel(
two_lines.intersection_point
+ two_lines.second_direction.normalize(),
)
.unwrap_or(intersection_point)
- intersection_point,
})
}
})
Expand Down
8 changes: 4 additions & 4 deletions tools/twix/src/panels/image/overlays/line_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ impl Overlay for LineDetection {
}
RansacFeature::TwoLines(two_lines) => {
painter.line(
two_lines.point,
two_lines.direction1,
two_lines.intersection_point,
two_lines.first_direction,
Stroke::new(2.0, Color32::GREEN),
);
painter.line(
two_lines.point,
two_lines.direction2,
two_lines.intersection_point,
two_lines.second_direction,
Stroke::new(2.0, Color32::GREEN),
);
}
Expand Down

0 comments on commit 6fe96db

Please sign in to comment.