diff --git a/Cargo.toml b/Cargo.toml index 07462a8..5552d1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nickslinetoolsrust" -version = "1.0.1" +version = "1.2.0" authors = ["thehappycheese"] repository = "https://github.com/thehappycheese/nicks_line_tools_rust.git" readme = "README.MD" diff --git a/README.MD b/README.MD index 315169c..eaf52cf 100644 --- a/README.MD +++ b/README.MD @@ -10,6 +10,8 @@ - [2.4. `.interpolate()`](#24-interpolate) - [2.5. `.offset_basic()`](#25-offset_basic) - [2.6. Converting to `Vec`](#26-converting-to-vecvector2) + - [2.7. Converting to `Vec<(f64,f64)>`](#27-converting-to-vecf64f64) + - [2.8. Converting to `Vec<(f64,f64,f64)>`](#28-converting-to-vecf64f64f64) - [3. Struct `Vector2`](#3-struct-vector2) ## 1. Introduction @@ -99,7 +101,41 @@ assert!( Vector2::new(1f64, 0f64), Vector2::new(1f64, 1f64), ] -) +); +``` + +### 2.7. Converting to `Vec<(f64,f64)>` + +Use the `into_tuples()` function to convert a multi line string into `Vec<(f64,f64)>` objects. + +```rust +let into_tuples = ls.into_tuples(); + +assert_eq!( + into_tuples, + vec![ + (0f64, 0f64), + (1f64, 0f64), + (1f64, 1f64), + ] +); +``` + +### 2.8. Converting to `Vec<(f64,f64,f64)>` + +Use the `into_tuples_measured()` function to convert a multi line string into `Vec<(f64,f64,f64)>` objects. + +```rust +let into_tuples_measured = ls.into_tuples_measured(2.0, 10.0); + +assert_eq!( + into_tuples_measured, + vec![ + (0f64, 0f64, 2f64), + (1f64, 0f64, 6f64), + (1f64, 1f64, 10f64), + ] +); ``` ## 3. Struct `Vector2` diff --git a/src/line_string_measured.rs b/src/line_string_measured.rs index 819f81c..48c2697 100644 --- a/src/line_string_measured.rs +++ b/src/line_string_measured.rs @@ -51,10 +51,34 @@ impl Into> for &LineStringMeasured { impl LineStringMeasured { /// The into trait is difficult to call without a helper function. All this does is call the into trait. + #[deprecated(since="1.2.0", note="please use `into_tuples` or `into_tuples_measured` instead")] pub fn into_vector2(&self) -> Vec{ self.into() } + pub fn into_tuples(&self)-> Vec<(f64,f64)>{ + let mut result:Vec<(f64,f64)> = self.segments.iter().map(|seg|(&seg.a).into()).collect(); + if let Some(seg) = self.segments.last(){ + result.push((&seg.b).into()) + } + result + } + + pub fn into_tuples_measured(&self, from_measure:f64, to_measure:f64) -> Vec<(f64,f64,f64)>{ + let measure_len = to_measure-from_measure; + let scale = measure_len/self.mag; + let mut each_measure = from_measure; + let mut result:Vec<(f64,f64,f64)> = self.segments.iter().map(|seg|{ + let result = (seg.a.x, seg.a.y, each_measure); + each_measure += seg.mag*scale; + result + }).collect(); + if let Some(seg) = self.segments.last(){ + result.push((seg.b.x,seg.b.y,to_measure)) + } + result + } + pub fn magnitude(&self) -> f64 { return self.mag; } diff --git a/src/tests.rs b/src/tests.rs index d23cfe3..e1026c6 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -311,4 +311,54 @@ fn test_linestring_conversion_to_vec() { Vector2::new(1.0, 3.0), ] ); -} \ No newline at end of file +} + + +#[test] +fn test_multilinestring_conversion_to_tuples() { + let ls:LineStringMeasured = vec![ + Vector2::new(0.0, 0.0), + Vector2::new(1.0, 1.0), + Vector2::new(1.5, 2.0), + Vector2::new(1.0, 3.0), + ].into(); + assert_eq!( + ls.into_tuples(), + vec![ + (0.0, 0.0), + (1.0, 1.0), + (1.5, 2.0), + (1.0, 3.0), + ] + ); +} + +#[test] +fn test_multilinestring_conversion_to_measured_tuples() { + let ls:LineStringMeasured = vec![ + Vector2::new(0.0, 0.0), + Vector2::new(0.0, 2.0),//2 + Vector2::new(4.0, 2.0),//4 + Vector2::new(4.0, 10.0),//8 + ].into(); + assert_eq!( + ls.into_tuples_measured(0.0, 14.0), + vec![ + (0.0, 0.0, 0.0), + (0.0, 2.0, 2.0), + (4.0, 2.0, 6.0), + (4.0, 10.0, 14.0), + ] + ); + + assert_eq!( + ls.into_tuples_measured(2.0, 30.0), + vec![ + (0.0, 0.0, 2.0), + (0.0, 2.0, 6.0), + (4.0, 2.0, 14.0), + (4.0, 10.0, 30.0), + ] + ); + +} diff --git a/src/vector2.rs b/src/vector2.rs index 30c4698..e555cab 100644 --- a/src/vector2.rs +++ b/src/vector2.rs @@ -15,6 +15,17 @@ impl Serialize for Vector2 { } } +impl Into<(f64,f64)> for &Vector2 { + fn into(self)->(f64,f64){ + (self.x, self.y) + } +} +// impl From<&Vector2> for (f64,f64) { +// fn from(vec:&Vector2)->Self{ +// (vec.x, vec.y) +// } +// } + impl Vector2 { pub fn new(x: f64, y: f64) -> Vector2 { Vector2 { x, y } @@ -144,6 +155,13 @@ mod tests { assert_eq!(v.y, 2.0); } + #[test] + fn into_tuple() { + let v = Vector2::new(1.0, 2.0); + let vv:(f64,f64) = (&v).into(); + assert_eq!(vv, (1.0,2.0)); + } + #[test] fn equality_derived() { let v1 = Vector2::new(1.0, 2.0);