From 35a2cb9dd191eddca1fc002d0141d3c9bf5c7f9e Mon Sep 17 00:00:00 2001 From: Bodo Junglas Date: Mon, 3 Feb 2020 13:43:22 +0100 Subject: [PATCH] Fix some formatting and clippy issues --- lib/src/boolean/compare_segments.rs | 2 +- lib/src/boolean/compute_fields.rs | 22 +++++--- lib/src/boolean/connect_edges.rs | 32 ++++++------ lib/src/boolean/divide_segment.rs | 2 +- lib/src/boolean/fill_queue.rs | 11 +++- lib/src/boolean/helper.rs | 2 +- lib/src/boolean/mod.rs | 2 +- lib/src/boolean/possible_intersection.rs | 2 +- lib/src/boolean/segment_intersection.rs | 2 +- lib/src/boolean/signed_area.rs | 11 ++-- lib/src/boolean/subdivide_segments.rs | 2 +- lib/src/boolean/sweep_event.rs | 2 +- tests/src/bin/run_single_test.rs | 4 +- tests/src/compact_geojson.rs | 64 +++++++++++++----------- tests/src/generic_test_cases.rs | 9 ++-- tests/src/helper.rs | 31 +++++++----- tests/src/lib.rs | 2 +- 17 files changed, 114 insertions(+), 88 deletions(-) diff --git a/lib/src/boolean/compare_segments.rs b/lib/src/boolean/compare_segments.rs index 68c93da..b5954ef 100644 --- a/lib/src/boolean/compare_segments.rs +++ b/lib/src/boolean/compare_segments.rs @@ -1,6 +1,6 @@ +use super::helper::Float; use super::signed_area::signed_area; use super::sweep_event::SweepEvent; -use super::helper::Float; use std::cmp::Ordering; use std::rc::Rc; diff --git a/lib/src/boolean/compute_fields.rs b/lib/src/boolean/compute_fields.rs index df2f6df..f027d04 100644 --- a/lib/src/boolean/compute_fields.rs +++ b/lib/src/boolean/compute_fields.rs @@ -1,6 +1,6 @@ -use super::sweep_event::{EdgeType, SweepEvent, ResultTransition}; -use super::Operation; use super::helper::Float; +use super::sweep_event::{EdgeType, ResultTransition, SweepEvent}; +use super::Operation; use std::rc::Rc; pub fn compute_fields(event: &Rc>, maybe_prev: Option<&Rc>>, operation: Operation) @@ -66,15 +66,21 @@ where let that_in = !event.is_other_in_out(); let is_in = match operation { Operation::Intersection => this_in && that_in, - Operation::Union => this_in || that_in, - Operation::Xor => this_in ^ that_in, - Operation::Difference => - // Difference is assymmetric, so subject vs clipping matters. + Operation::Union => this_in || that_in, + Operation::Xor => this_in ^ that_in, + Operation::Difference => + // Difference is assymmetric, so subject vs clipping matters. + { if event.is_subject { this_in && !that_in } else { that_in && !this_in } + } }; - if is_in { ResultTransition::OutIn } else { ResultTransition::InOut } -} \ No newline at end of file + if is_in { + ResultTransition::OutIn + } else { + ResultTransition::InOut + } +} diff --git a/lib/src/boolean/connect_edges.rs b/lib/src/boolean/connect_edges.rs index 0c537b7..c5bb8a8 100644 --- a/lib/src/boolean/connect_edges.rs +++ b/lib/src/boolean/connect_edges.rs @@ -1,6 +1,6 @@ -use super::sweep_event::{SweepEvent, ResultTransition}; -use geo_types::{Coordinate}; use super::helper::Float; +use super::sweep_event::{ResultTransition, SweepEvent}; +use geo_types::Coordinate; use std::collections::HashSet; use std::rc::Rc; @@ -46,7 +46,6 @@ where result_events } - fn next_pos(pos: i32, result_events: &[Rc>], processed: &HashSet, orig_pos: i32) -> i32 where F: Float, @@ -79,10 +78,9 @@ where new_pos } - pub struct Contour where - F: Float + F: Float, { /// Raw coordinates of contour pub points: Vec>, @@ -98,19 +96,23 @@ where impl Contour where - F: Float + F: Float, { pub fn new(hole_of: Option, depth: i32) -> Contour { Contour { points: Vec::new(), hole_ids: Vec::new(), - hole_of: hole_of, - depth: depth, + hole_of, + depth, } } /// This logic implements the 4 cases of parent contours from Fig. 4 in the Martinez paper. - pub fn initialize_from_context(event: &Rc>, contours: &mut [Contour], contour_id: i32) -> Contour { + pub fn initialize_from_context( + event: &Rc>, + contours: &mut [Contour], + contour_id: i32, + ) -> Contour { if let Some(prev_in_result) = event.get_prev_in_result() { // Note that it is valid to query the "previous in result" for its output contour id, // because we must have already processed it (i.e., assigned an output contour id) @@ -158,7 +160,6 @@ where } } - fn mark_as_processed(processed: &mut HashSet, result_events: &[Rc>], pos: i32, contour_id: i32) where F: Float, @@ -167,7 +168,6 @@ where result_events[pos as usize].set_output_contour_id(contour_id); } - pub fn connect_edges(sorted_events: &[Rc>]) -> Vec> where F: Float, @@ -183,11 +183,7 @@ where } let contour_id = contours.len() as i32; - let mut contour = Contour::initialize_from_context( - &result_events[i as usize], - &mut contours, - contour_id, - ); + let mut contour = Contour::initialize_from_context(&result_events[i as usize], &mut contours, contour_id); let orig_pos = i; // Alias just for clarity let mut pos = i; @@ -207,12 +203,12 @@ where // terminates the loop. mark_as_processed(&mut processed, &result_events, pos, contour_id); - pos = result_events[pos as usize].get_other_pos(); // pos advancement (A) + pos = result_events[pos as usize].get_other_pos(); // pos advancement (A) mark_as_processed(&mut processed, &result_events, pos, contour_id); contour.points.push(result_events[pos as usize].point); - pos = next_pos(pos, &result_events, &processed, orig_pos); // pos advancement (B) + pos = next_pos(pos, &result_events, &processed, orig_pos); // pos advancement (B) if pos == orig_pos { break; diff --git a/lib/src/boolean/divide_segment.rs b/lib/src/boolean/divide_segment.rs index 89dc3ae..289c2ff 100644 --- a/lib/src/boolean/divide_segment.rs +++ b/lib/src/boolean/divide_segment.rs @@ -1,6 +1,6 @@ +use super::helper::Float; use super::sweep_event::SweepEvent; use geo_types::Coordinate; -use super::helper::Float; use std::collections::BinaryHeap; use std::rc::Rc; diff --git a/lib/src/boolean/fill_queue.rs b/lib/src/boolean/fill_queue.rs index fe1d2d0..b05e40f 100644 --- a/lib/src/boolean/fill_queue.rs +++ b/lib/src/boolean/fill_queue.rs @@ -1,5 +1,5 @@ -use geo_types::{LineString, Polygon, Rect}; use super::helper::Float; +use geo_types::{LineString, Polygon, Rect}; use std::collections::BinaryHeap; use std::rc::{Rc, Weak}; @@ -32,7 +32,14 @@ where if exterior { contour_id += 1; } - process_polygon(&polygon.exterior(), false, contour_id, &mut event_queue, cbbox, exterior); + process_polygon( + &polygon.exterior(), + false, + contour_id, + &mut event_queue, + cbbox, + exterior, + ); for interior in polygon.interiors() { process_polygon(interior, false, contour_id, &mut event_queue, cbbox, false); } diff --git a/lib/src/boolean/helper.rs b/lib/src/boolean/helper.rs index 9925f61..1b7930e 100644 --- a/lib/src/boolean/helper.rs +++ b/lib/src/boolean/helper.rs @@ -1,6 +1,6 @@ +use num_traits::Float as NumTraitsFloat; use std::cmp::Ordering; use std::fmt::{Debug, Display}; -use num_traits::Float as NumTraitsFloat; pub trait Float: NumTraitsFloat + Debug + Display {} impl Float for T {} diff --git a/lib/src/boolean/mod.rs b/lib/src/boolean/mod.rs index 41e641e..dd8dc0e 100644 --- a/lib/src/boolean/mod.rs +++ b/lib/src/boolean/mod.rs @@ -1,4 +1,4 @@ -use geo_types::{Coordinate, MultiPolygon, Polygon, LineString, Rect}; +use geo_types::{Coordinate, LineString, MultiPolygon, Polygon, Rect}; pub mod compare_segments; pub mod compute_fields; diff --git a/lib/src/boolean/possible_intersection.rs b/lib/src/boolean/possible_intersection.rs index 49d167c..f9e8990 100644 --- a/lib/src/boolean/possible_intersection.rs +++ b/lib/src/boolean/possible_intersection.rs @@ -1,7 +1,7 @@ use super::divide_segment::divide_segment; +use super::helper::Float; use super::segment_intersection::{intersection, LineIntersection}; use super::sweep_event::{EdgeType, SweepEvent}; -use super::helper::Float; use std::collections::BinaryHeap; use std::rc::Rc; diff --git a/lib/src/boolean/segment_intersection.rs b/lib/src/boolean/segment_intersection.rs index 0dafe58..cf61cd4 100644 --- a/lib/src/boolean/segment_intersection.rs +++ b/lib/src/boolean/segment_intersection.rs @@ -1,5 +1,5 @@ -use geo_types::Coordinate; use super::helper::Float; +use geo_types::Coordinate; #[derive(Debug, Clone, Copy, PartialEq)] pub enum LineIntersection diff --git a/lib/src/boolean/signed_area.rs b/lib/src/boolean/signed_area.rs index 0d2dc69..8e6bc49 100644 --- a/lib/src/boolean/signed_area.rs +++ b/lib/src/boolean/signed_area.rs @@ -1,13 +1,16 @@ -use geo_types::Coordinate; use super::helper::Float; -use robust::{Coord, orient2d}; +use geo_types::Coordinate; +use robust::{orient2d, Coord}; #[inline] -pub fn coordinate_to_robust(p : Coordinate) -> Coord +pub fn coordinate_to_robust(p: Coordinate) -> Coord where F: Float, { - Coord{x: p.x.to_f64().unwrap(), y: p.y.to_f64().unwrap()} + Coord { + x: p.x.to_f64().unwrap(), + y: p.y.to_f64().unwrap(), + } } #[inline] diff --git a/lib/src/boolean/subdivide_segments.rs b/lib/src/boolean/subdivide_segments.rs index cc96d7d..3cf435a 100644 --- a/lib/src/boolean/subdivide_segments.rs +++ b/lib/src/boolean/subdivide_segments.rs @@ -1,11 +1,11 @@ use super::compare_segments::compare_segments; use super::compute_fields::compute_fields; +use super::helper::Float; use super::possible_intersection::possible_intersection; use super::sweep_event::SweepEvent; use super::Operation; use crate::splay::SplaySet; use geo_types::Rect; -use super::helper::Float; use std::collections::BinaryHeap; use std::rc::Rc; diff --git a/lib/src/boolean/sweep_event.rs b/lib/src/boolean/sweep_event.rs index 2d13d69..b168573 100644 --- a/lib/src/boolean/sweep_event.rs +++ b/lib/src/boolean/sweep_event.rs @@ -1,5 +1,5 @@ -use geo_types::Coordinate; use super::helper::Float; +use geo_types::Coordinate; use std::cell::RefCell; use std::cmp::Ordering; use std::rc::{Rc, Weak}; diff --git a/tests/src/bin/run_single_test.rs b/tests/src/bin/run_single_test.rs index 163fe27..f9e88bf 100644 --- a/tests/src/bin/run_single_test.rs +++ b/tests/src/bin/run_single_test.rs @@ -1,9 +1,9 @@ extern crate geo_booleanop_tests; +use geo_booleanop_tests::helper::run_generic_test_case; use std::fs; use std::path::Path; use std::process::Command; -use geo_booleanop_tests::helper::run_generic_test_case; fn main() { let args: Vec = std::env::args().collect(); @@ -27,4 +27,4 @@ fn main() { .arg(&filename_out) .spawn() .expect("Failed to run Python plot."); -} \ No newline at end of file +} diff --git a/tests/src/compact_geojson.rs b/tests/src/compact_geojson.rs index efad2f8..3c0f9dd 100644 --- a/tests/src/compact_geojson.rs +++ b/tests/src/compact_geojson.rs @@ -12,16 +12,19 @@ use serde_json::json; use std::fs::File; use std::io::Write; - fn indent_block(indent: i32, s: &str) -> String { let indent = " ".repeat(indent as usize); - s.split("\n").enumerate().map(|(i, line)| { - if i != 0 { - indent.clone() + line - } else { - line.to_string() - } - }).collect::>().join("\n") + s.split('\n') + .enumerate() + .map(|(i, line)| { + if i != 0 { + indent.clone() + line + } else { + line.to_string() + } + }) + .collect::>() + .join("\n") } trait WriteIndented { @@ -31,20 +34,23 @@ trait WriteIndented { impl WriteIndented for &mut File { fn write_indented>(self, indent: i32, s: S) { let indent = " ".repeat(indent as usize); - self.write(indent.as_bytes()).expect("Failed to write to file."); - self.write(s.as_ref().as_bytes()).expect("Failed to write to file."); + self.write_all(indent.as_bytes()).expect("Failed to write to file."); + self.write_all(s.as_ref().as_bytes()).expect("Failed to write to file."); } } -fn write_polygon(polygon: &Vec>>, f: &mut File, indent: i32) { - +fn write_polygon(polygon: &[Vec>], f: &mut File, indent: i32) { let mut write = |s: &str| { f.write_indented(indent, s); }; let float_to_string = |x: f64| { let s = json!(x).to_string(); - if s.ends_with(".0") { s[..s.len()-2].to_string() } else { s } + if s.ends_with(".0") { + s[..s.len() - 2].to_string() + } else { + s + } }; for (i, ring) in polygon.iter().enumerate() { @@ -54,7 +60,7 @@ fn write_polygon(polygon: &Vec>>, f: &mut File, indent: i32) { " [{}, {}]{}\n", float_to_string(point[0]), float_to_string(point[1]), - if j < ring.len() - 1 { "," } else {""}, + if j < ring.len() - 1 { "," } else { "" }, )); } if i < polygon.len() - 1 { @@ -62,12 +68,10 @@ fn write_polygon(polygon: &Vec>>, f: &mut File, indent: i32) { } else { write("]\n"); } - } } -fn write_multi_polygon(polygons: &Vec>>>, f: &mut File, indent: i32) { - +fn write_multi_polygon(polygons: &[Vec>>], f: &mut File, indent: i32) { for (i, polygon) in polygons.iter().enumerate() { f.write_indented(indent, "[\n"); write_polygon(polygon, f, indent + 2); @@ -76,7 +80,6 @@ fn write_multi_polygon(polygons: &Vec>>>, f: &mut File, indent: } else { f.write_indented(indent, "]\n"); } - } } @@ -85,23 +88,30 @@ fn write_feature(feature: &Feature, f: &mut File, is_last: bool) { f.write_indented(4, " \"geometry\": {\n"); f.write_indented(4, " \"coordinates\": [\n"); - let geometry_value = feature.geometry.as_ref().expect("Feature must have 'geometry' property.").value.clone(); + let geometry_value = feature + .geometry + .as_ref() + .expect("Feature must have 'geometry' property.") + .value + .clone(); let geometry_type_name = match geometry_value { Value::Polygon(data) => { write_polygon(&data, f, 10); "Polygon" - }, + } Value::MultiPolygon(data) => { write_multi_polygon(&data, f, 10); "MultiPolygon" - }, + } _ => panic!("Feature must either be MultiPolygon or Polygon"), }; - let properties = feature.properties.as_ref().map_or( - "{}\n".to_string(), - |p| indent_block(6, &serde_json::to_string_pretty(&p).expect("Failed to convert properties to string.")), - ); + let properties = feature.properties.as_ref().map_or("{}\n".to_string(), |p| { + indent_block( + 6, + &serde_json::to_string_pretty(&p).expect("Failed to convert properties to string."), + ) + }); f.write_indented(4, " ],\n"); f.write_indented(4, " \"type\": \"".to_string() + geometry_type_name + "\"\n"); @@ -115,7 +125,6 @@ fn write_feature(feature: &Feature, f: &mut File, is_last: bool) { } } - pub fn write_compact_geojson(features: &[Feature], filename: &str) { let mut f = File::create(filename).expect("Unable to create json file."); @@ -127,5 +136,4 @@ pub fn write_compact_geojson(features: &[Feature], filename: &str) { f.write_indented(0, " ],\n"); f.write_indented(0, " \"type\": \"FeatureCollection\"\n"); f.write_indented(0, "}\n"); - -} \ No newline at end of file +} diff --git a/tests/src/generic_test_cases.rs b/tests/src/generic_test_cases.rs index 9a4a5f8..b899675 100644 --- a/tests/src/generic_test_cases.rs +++ b/tests/src/generic_test_cases.rs @@ -1,7 +1,6 @@ -use super::helper::{run_generic_test_case}; +use super::helper::run_generic_test_case; use glob::glob; - #[test] fn test_generic_test_cases() { let regenerate = std::env::var("REGEN").is_ok(); @@ -12,7 +11,9 @@ fn test_generic_test_cases() { } if regenerate { - assert!(false, - "Regenerate is set to true. Won't let tests pass in this mode, because assertions are disabled."); + assert!( + false, + "Regenerate is set to true. Won't let tests pass in this mode, because assertions are disabled." + ); } } diff --git a/tests/src/helper.rs b/tests/src/helper.rs index b713cd1..fa1d4ba 100644 --- a/tests/src/helper.rs +++ b/tests/src/helper.rs @@ -3,14 +3,13 @@ use geo_booleanop::boolean::BooleanOp; use super::compact_geojson::write_compact_geojson; use geo::{Coordinate, MultiPolygon, Polygon}; -use geojson::{GeoJson, Feature, Value, Geometry}; +use geojson::{Feature, GeoJson, Geometry, Value}; use pretty_assertions::assert_eq; -use std::fs::File; use std::convert::TryInto; +use std::fs::File; use std::io::prelude::*; - pub fn load_fixture_from_path(path: &str) -> GeoJson { let mut file = File::open(path).expect("Cannot open/find fixture"); let mut content = String::new(); @@ -95,7 +94,12 @@ struct ExpectedResult { } fn extract_multi_polygon(feature: &Feature) -> MultiPolygon { - let geometry_value = feature.geometry.as_ref().expect("Feature must have 'geometry' property").value.clone(); + let geometry_value = feature + .geometry + .as_ref() + .expect("Feature must have 'geometry' property") + .value + .clone(); let multi_polygon: MultiPolygon = match geometry_value { Value::Polygon(_) => MultiPolygon(vec![geometry_value.try_into().unwrap()]), Value::MultiPolygon(_) => geometry_value.try_into().unwrap(), @@ -107,7 +111,8 @@ fn extract_multi_polygon(feature: &Feature) -> MultiPolygon { fn extract_expected_result(feature: &Feature) -> ExpectedResult { let multi_polygon = extract_multi_polygon(feature); - let op = feature.properties + let op = feature + .properties .as_ref() .expect("Feature needs 'properties'.") .get("operation") @@ -124,9 +129,9 @@ fn extract_expected_result(feature: &Feature) -> ExpectedResult { _ => panic!(format!("Invalid operation: {}", op)), }; - ExpectedResult{ + ExpectedResult { result: multi_polygon, - op: op, + op, } } @@ -144,8 +149,8 @@ pub fn run_generic_test_case(filename: &str, regenerate: bool) { let mut output_features: Vec = vec![features[0].clone(), features[1].clone()]; - for i in 2 .. features.len() { - let expected_result = extract_expected_result(&features[i]); + for feature in features.iter().skip(2) { + let expected_result = extract_expected_result(&feature); println!("Testing operation: {:?}", expected_result.op); let result = match expected_result.op { @@ -158,13 +163,13 @@ pub fn run_generic_test_case(filename: &str, regenerate: bool) { if !regenerate { assert_eq!( - result, - expected_result.result, - "Deviation found in test case {} with operation {:?}", filename, expected_result.op, + result, expected_result.result, + "Deviation found in test case {} with operation {:?}", + filename, expected_result.op, ); } - let mut output_feature = features[i].clone(); + let mut output_feature = feature.clone(); output_feature.geometry = Some(Geometry::new(Value::from(&result))); output_features.push(output_feature); } diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 17ce95c..75cfe25 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -7,5 +7,5 @@ mod possible_intersection_test; #[cfg(test)] mod subdivide_segments_test; -pub mod helper; pub mod compact_geojson; +pub mod helper;