Skip to content

Commit

Permalink
cargo fmt + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappycheese committed Aug 5, 2023
1 parent fe2a811 commit c4ba6be
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 87 deletions.
18 changes: 10 additions & 8 deletions geo/src/algorithm/line_split/line_split_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ use geo_types::CoordFloat;

use super::{LineSplitResult, LineSplitTwiceResult};


pub trait LineSplit<Scalar> where Self:Sized, Scalar: CoordFloat {

pub trait LineSplit<Scalar>
where
Self: Sized,
Scalar: CoordFloat,
{
/// Note on choice of return type:
///
///
/// You may wonder why this does not return `Option<(Option<Line>, Option<Line>)>`?
/// It is because then the return type causes uncertainty; The user may expect to possibly
/// receive `Some((None, None))` which is never possible, this would lead to clutter in match
/// statements.
///
///
/// To make it easier to 'just get the first' or 'just get the second' you can use
/// `LineSplitResult::first()` and `LineSplitResult::second()` which return `Option<T>`
///
///
///
///
fn line_split(&self, fraction: Scalar) -> Option<LineSplitResult<Self>>;

/// Note on choice of return type:
Expand Down Expand Up @@ -57,7 +59,7 @@ pub trait LineSplit<Scalar> where Self:Sized, Scalar: CoordFloat {
},
Some(LineSplitResult::First (line1)) => Some(First(line1)),
Some(LineSplitResult::Second(line2)) => match line2.line_split(second_fraction) {
Some(LineSplitResult::FirstSecond(line2, line3)) => Some(SecondThird ( line2, line3)),
Some(LineSplitResult::FirstSecond(line2, line3)) => Some(SecondThird ( line2, line3)),
Some(LineSplitResult::First (line2 )) => Some(Second ( line2 )),
Some(LineSplitResult::Second ( line3)) => Some(Third ( line3)),
None => None,
Expand Down
127 changes: 66 additions & 61 deletions geo/src/algorithm/line_split/line_split_trait_impl_for_line.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use geo_types::{Line, CoordFloat};
use geo_types::{CoordFloat, Line};

use super::{
LineSplitResult,
LineSplit,
};
use super::{LineSplit, LineSplitResult};

impl<Scalar> LineSplit<Scalar> for Line<Scalar> where Scalar: CoordFloat {
impl<Scalar> LineSplit<Scalar> for Line<Scalar>
where
Scalar: CoordFloat,
{
fn line_split(&self, fraction: Scalar) -> Option<LineSplitResult<Self>> {
if fraction.is_nan() {
return None
return None;
}
if fraction <= Scalar::zero() {
Some(LineSplitResult::Second(self.clone()))
Some(LineSplitResult::Second(*self))
} else if fraction >= Scalar::one() {
Some(LineSplitResult::First(self.clone()))
Some(LineSplitResult::First(*self))
} else {
let new_midpoint = self.start + self.delta() * fraction;
Some(LineSplitResult::FirstSecond(
Expand All @@ -25,10 +25,10 @@ impl<Scalar> LineSplit<Scalar> for Line<Scalar> where Scalar: CoordFloat {
}

#[cfg(test)]
mod test{
use geo_types::coord;
mod test {
use super::super::LineSplitTwiceResult;
use super::*;
use geo_types::coord;

// =============================================================================================
// Line::line_split()
Expand All @@ -38,71 +38,71 @@ mod test{
fn test_line_split_first_second() {
// simple x-axis aligned check
let line = Line::new(
coord!{x: 0.0_f32, y:0.0_f32},
coord!{x:10.0_f32, y:0.0_f32},
coord! {x: 0.0_f32, y:0.0_f32},
coord! {x:10.0_f32, y:0.0_f32},
);
let result = line.line_split(0.6);
assert_eq!(result, Some(LineSplitResult::FirstSecond(
Line::new(
coord!{x: 0.0_f32, y:0.0_f32},
coord!{x: 6.0_f32, y:0.0_f32},
),
Line::new(
coord!{x: 6.0_f32, y:0.0_f32},
coord!{x:10.0_f32, y:0.0_f32},
)
)));
assert_eq!(
result,
Some(LineSplitResult::FirstSecond(
Line::new(
coord! {x: 0.0_f32, y:0.0_f32},
coord! {x: 6.0_f32, y:0.0_f32},
),
Line::new(
coord! {x: 6.0_f32, y:0.0_f32},
coord! {x:10.0_f32, y:0.0_f32},
)
))
);

// simple y-axis aligned check
let line = Line::new(
coord!{x:0.0_f32, y: 0.0_f32},
coord!{x:0.0_f32, y:10.0_f32},
coord! {x:0.0_f32, y: 0.0_f32},
coord! {x:0.0_f32, y:10.0_f32},
);
let result = line.line_split(0.3);
assert_eq!(result, Some(LineSplitResult::FirstSecond(
Line::new(
coord!{x:0.0_f32, y:0.0_f32},
coord!{x:0.0_f32, y:3.0_f32},
),
Line::new(
coord!{x:0.0_f32, y:3.0_f32},
coord!{x:0.0_f32, y:10.0_f32},
)
)));
assert_eq!(
result,
Some(LineSplitResult::FirstSecond(
Line::new(coord! {x:0.0_f32, y:0.0_f32}, coord! {x:0.0_f32, y:3.0_f32},),
Line::new(
coord! {x:0.0_f32, y:3.0_f32},
coord! {x:0.0_f32, y:10.0_f32},
)
))
);

// non_trivial check
let line = Line::new(
coord!{x: 1.0_f32, y: 1.0_f32},
coord!{x:10.0_f32, y:-10.0_f32},
coord! {x: 1.0_f32, y: 1.0_f32},
coord! {x:10.0_f32, y:-10.0_f32},
);
let split_point = line.start + line.delta() * 0.7;
let result = line.line_split(0.7);
assert_eq!(result, Some(LineSplitResult::FirstSecond(
Line::new(
line.start,
split_point,
),
Line::new(
split_point,
line.end,
)
)));
assert_eq!(
result,
Some(LineSplitResult::FirstSecond(
Line::new(line.start, split_point,),
Line::new(split_point, line.end,)
))
);
}

#[test]
fn test_line_split_first() {
// test one
let line = Line::new(
coord!{x: 0.0_f32, y:0.0_f32},
coord!{x:10.0_f32, y:0.0_f32},
coord! {x: 0.0_f32, y:0.0_f32},
coord! {x:10.0_f32, y:0.0_f32},
);
let result = line.line_split(1.0);
assert_eq!(result, Some(LineSplitResult::First(line)));

// Test numbers larger than one
let line = Line::new(
coord!{x: 0.0_f32, y:0.0_f32},
coord!{x:10.0_f32, y:0.0_f32},
coord! {x: 0.0_f32, y:0.0_f32},
coord! {x:10.0_f32, y:0.0_f32},
);
let result = line.line_split(2.0);
assert_eq!(result, Some(LineSplitResult::First(line)));
Expand All @@ -111,26 +111,25 @@ mod test{
fn test_line_split_second() {
// test zero
let line = Line::new(
coord!{x: 0.0_f32, y:0.0_f32},
coord!{x:10.0_f32, y:0.0_f32},
coord! {x: 0.0_f32, y:0.0_f32},
coord! {x:10.0_f32, y:0.0_f32},
);
let result = line.line_split(0.0);
assert_eq!(result, Some(LineSplitResult::Second(line)));

// Test negative numbers
let line = Line::new(
coord!{x: 0.0_f32, y:0.0_f32},
coord!{x:10.0_f32, y:0.0_f32},
coord! {x: 0.0_f32, y:0.0_f32},
coord! {x:10.0_f32, y:0.0_f32},
);
let result = line.line_split(-2.0);
assert_eq!(result, Some(LineSplitResult::Second(line)));
}


// =============================================================================================
// Line::line_split_twice()
// =============================================================================================

macro_rules! test_line_split_twice_helper{
($a:expr, $b:expr, $enum_variant:ident, $(($x1:expr, $x2:expr)),*)=>{{
let line = Line::new(
Expand All @@ -154,14 +153,20 @@ mod test{
}

#[test]
fn test_line_split_twice(){
test_line_split_twice_helper!(0.6, 0.8, FirstSecondThird, (0.0, 6.0), (6.0, 8.0), (8.0, 10.0));
fn test_line_split_twice() {
test_line_split_twice_helper!(
0.6,
0.8,
FirstSecondThird,
(0.0, 6.0),
(6.0, 8.0),
(8.0, 10.0)
);
test_line_split_twice_helper!(0.6, 1.0, FirstSecond, (0.0, 6.0), (6.0, 10.0));
test_line_split_twice_helper!(0.6, 0.6, FirstThird, (0.0, 6.0), (6.0, 10.0));
test_line_split_twice_helper!(0.0, 0.6, SecondThird, (0.0, 6.0), (6.0, 10.0));
test_line_split_twice_helper!(1.0, 1.0, First, (0.0, 10.0));
test_line_split_twice_helper!(0.0, 1.0, Second, (0.0, 10.0));
test_line_split_twice_helper!(0.0, 0.0, Third, (0.0, 10.0));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ where
let LineStringMeasurements {
length_total,
length_segments,
} = match measure_line_string(&self) {
} = match measure_line_string(self) {
Some(x) => x,
None => return None,
};
Expand Down Expand Up @@ -88,7 +88,7 @@ where

#[cfg(test)]
mod test {
use geo_types::{line_string, coord};
use geo_types::{coord, line_string};

use super::super::LineSplitTwiceResult;

Expand All @@ -99,7 +99,7 @@ mod test {

#[test]
fn split() {
let line_string:LineString<f32> = line_string![
let line_string: LineString<f32> = line_string![
(x:0.0, y:0.0),
(x:1.0, y:0.0),
(x:1.0, y:1.0),
Expand All @@ -110,15 +110,15 @@ mod test {
assert_eq!(
line_string.line_split(0.5),
Some(LineSplitResult::FirstSecond(
LineString::new(vec![line_string.0[0],line_string.0[1], slice_point]),
LineString::new(vec![slice_point, line_string.0[2],line_string.0[3]])
LineString::new(vec![line_string.0[0], line_string.0[1], slice_point]),
LineString::new(vec![slice_point, line_string.0[2], line_string.0[3]])
))
);
}

#[test]
fn split_on_point() {
let line_string:LineString<f32> = line_string![
let line_string: LineString<f32> = line_string![
(x:0.0, y:0.0),
(x:1.0, y:0.0),
(x:1.0, y:1.0),
Expand All @@ -129,15 +129,15 @@ mod test {
assert_eq!(
line_string.line_split(0.5),
Some(LineSplitResult::FirstSecond(
LineString::new(vec![line_string.0[0],line_string.0[1], slice_point]),
LineString::new(vec![line_string.0[0], line_string.0[1], slice_point]),
LineString::new(vec![slice_point, line_string.0[3], line_string.0[4]])
))
);
}

#[test]
fn split_half_way_through_last_segment() {
let line_string:LineString<f32> = line_string![
let line_string: LineString<f32> = line_string![
(x:0.0, y:0.0),
(x:1.0, y:0.0),
(x:1.0, y:1.0),
Expand All @@ -154,7 +154,7 @@ mod test {

#[test]
fn split_half_way_through_first_segment() {
let line_string:LineString<f32> = line_string![
let line_string: LineString<f32> = line_string![
(x:0.0, y:0.0),
(x:1.0, y:0.0),
(x:1.0, y:1.0),
Expand All @@ -171,7 +171,7 @@ mod test {

#[test]
fn split_first() {
let line_string:LineString<f32> = line_string![
let line_string: LineString<f32> = line_string![
(x:0.0, y:0.0),
(x:1.0, y:0.0),
];
Expand All @@ -183,7 +183,7 @@ mod test {

#[test]
fn split_second() {
let line_string:LineString<f32> = line_string![
let line_string: LineString<f32> = line_string![
(x:0.0, y:0.0),
(x:1.0, y:0.0),
];
Expand All @@ -193,21 +193,19 @@ mod test {
);
}



// =============================================================================================
// LineString::line_split_twice()
// =============================================================================================
#[test]
fn split_twice_typical(){
fn split_twice_typical() {
// I think if we exhaustively check
// - `Line::line_split_twice()` and
// - `LineString::line_split()`
// then because the implementation for `line_split_twice` is shared
// we don't need an exhaustive check for `LineString::line_split_twice()`
// So I will just do a spot check for a typical case

let line_string:LineString<f32> = line_string![
let line_string: LineString<f32> = line_string![
(x:0.0, y:0.0),
(x:1.0, y:0.0),
(x:1.0, y:1.0),
Expand All @@ -234,4 +232,4 @@ mod test {
)
);
}
}
}
2 changes: 1 addition & 1 deletion geo/src/algorithm/line_split/line_split_twice_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ impl<T> LineSplitTwiceResult<T> {
Self::FirstSecondThird(a, b, c) => (Some(a), Some(b), Some(c)),
}
}
}
}

0 comments on commit c4ba6be

Please sign in to comment.