From 254ceccd659d39f281f29c227b8ef3646e4e8806 Mon Sep 17 00:00:00 2001 From: Gaute Hope Date: Mon, 8 Jul 2024 19:25:43 +0200 Subject: [PATCH] more &mut's rather than Rc. --- geo/src/algorithm/relate/edge_end_builder.rs | 4 +-- .../relate/geomgraph/geometry_graph.rs | 8 +++-- .../geomgraph/index/edge_set_intersector.rs | 6 ++-- .../index/rstar_edge_set_intersector.rs | 26 +++++++------- .../geomgraph/index/segment_intersector.rs | 16 ++++----- .../index/simple_edge_set_intersector.rs | 34 +++++++++++-------- .../relate/geomgraph/planar_graph.rs | 4 +++ geo/src/algorithm/relate/relate_operation.rs | 20 +++++------ 8 files changed, 63 insertions(+), 55 deletions(-) diff --git a/geo/src/algorithm/relate/edge_end_builder.rs b/geo/src/algorithm/relate/edge_end_builder.rs index fdaca8e8a..4542639c6 100644 --- a/geo/src/algorithm/relate/edge_end_builder.rs +++ b/geo/src/algorithm/relate/edge_end_builder.rs @@ -20,10 +20,10 @@ impl EdgeEndBuilder { } } - pub fn compute_ends_for_edges(&self, edges: &[Arc>>]) -> Vec> { + pub fn compute_ends_for_edges(&self, edges: &mut [Edge]) -> Vec> { let mut list = vec![]; for edge in edges { - self.compute_ends_for_edge(&mut edge.borrow_mut(), &mut list); + self.compute_ends_for_edge(edge, &mut list); } list } diff --git a/geo/src/algorithm/relate/geomgraph/geometry_graph.rs b/geo/src/algorithm/relate/geomgraph/geometry_graph.rs index 44f925cdf..606b4880c 100644 --- a/geo/src/algorithm/relate/geomgraph/geometry_graph.rs +++ b/geo/src/algorithm/relate/geomgraph/geometry_graph.rs @@ -110,6 +110,10 @@ where self.planar_graph.edges() } + pub(crate) fn edges_mut(&mut self) -> &mut [Edge] { + self.planar_graph.edges_mut() + } + pub(crate) fn insert_edge(&mut self, edge: Edge) { self.planar_graph.insert_edge(edge) } @@ -353,8 +357,8 @@ where } pub(crate) fn compute_edge_intersections( - &self, - other: &GeometryGraph, + &'a mut self, + other: &mut GeometryGraph<'a, F>, line_intersector: Box>, ) -> SegmentIntersector { let mut segment_intersector = SegmentIntersector::new(line_intersector, false); diff --git a/geo/src/algorithm/relate/geomgraph/index/edge_set_intersector.rs b/geo/src/algorithm/relate/geomgraph/index/edge_set_intersector.rs index a1f81af66..f5c89db6d 100644 --- a/geo/src/algorithm/relate/geomgraph/index/edge_set_intersector.rs +++ b/geo/src/algorithm/relate/geomgraph/index/edge_set_intersector.rs @@ -14,7 +14,7 @@ pub(crate) trait EdgeSetIntersector { /// `segment_intersector`: the SegmentIntersector to use fn compute_intersections_within_set( &self, - graph: &GeometryGraph, + graph: &mut GeometryGraph, check_for_self_intersecting_edges: bool, segment_intersector: &mut SegmentIntersector, ); @@ -23,8 +23,8 @@ pub(crate) trait EdgeSetIntersector { /// the intersecting edges. fn compute_intersections_between_sets<'a>( &self, - graph_0: &GeometryGraph<'a, F>, - graph_1: &GeometryGraph<'a, F>, + graph_0: &mut GeometryGraph<'a, F>, + graph_1: &mut GeometryGraph<'a, F>, segment_intersector: &mut SegmentIntersector, ); } diff --git a/geo/src/algorithm/relate/geomgraph/index/rstar_edge_set_intersector.rs b/geo/src/algorithm/relate/geomgraph/index/rstar_edge_set_intersector.rs index 5c0190d33..da8e3b062 100644 --- a/geo/src/algorithm/relate/geomgraph/index/rstar_edge_set_intersector.rs +++ b/geo/src/algorithm/relate/geomgraph/index/rstar_edge_set_intersector.rs @@ -15,17 +15,19 @@ where { fn compute_intersections_within_set( &self, - graph: &GeometryGraph, + graph: &mut GeometryGraph, check_for_self_intersecting_edges: bool, segment_intersector: &mut SegmentIntersector, ) { - let edges = graph.edges(); - let tree = graph.get_or_build_tree(); + let mut edges = graph.edges_mut(); for (segment_0, segment_1) in tree.intersection_candidates_with_other_tree(&tree) { if check_for_self_intersecting_edges || segment_0.edge_idx != segment_1.edge_idx { - let edge_0 = &edges[segment_0.edge_idx]; - let edge_1 = &edges[segment_1.edge_idx]; + // use get_many_mut when available. + assert!(segment_1.edge_idx > segment_0.edge_idx); + let (e0, e1) = edges.split_at_mut(segment_0.edge_idx+1); + let edge_0 = &mut e0[segment_0.edge_idx]; + let edge_1 = &mut e1[segment_1.edge_idx-segment_0.edge_idx+1]; segment_intersector.add_intersections( edge_0, segment_0.segment_idx, @@ -38,19 +40,19 @@ where fn compute_intersections_between_sets<'a>( &self, - graph_0: &GeometryGraph<'a, F>, - graph_1: &GeometryGraph<'a, F>, + graph_0: &mut GeometryGraph<'a, F>, + graph_1: &mut GeometryGraph<'a, F>, segment_intersector: &mut SegmentIntersector, ) { - let edges_0 = graph_0.edges(); - let edges_1 = graph_1.edges(); - let tree_0 = graph_0.get_or_build_tree(); let tree_1 = graph_1.get_or_build_tree(); + let edges_0 = graph_0.edges_mut(); + let edges_1 = graph_1.edges_mut(); + for (segment_0, segment_1) in tree_0.intersection_candidates_with_other_tree(&tree_1) { - let edge_0 = &edges_0[segment_0.edge_idx]; - let edge_1 = &edges_1[segment_1.edge_idx]; + let edge_0 = &mut edges_0[segment_0.edge_idx]; + let edge_1 = &mut edges_1[segment_1.edge_idx]; segment_intersector.add_intersections( edge_0, segment_0.segment_idx, diff --git a/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs b/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs index 868b5915a..2ce7d4d53 100644 --- a/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs +++ b/geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs @@ -96,9 +96,9 @@ where pub fn add_intersections( &mut self, - edge0: &Edge, + edge0: &mut Edge, segment_index_0: usize, - edge1: &Edge, + edge1: &mut Edge, segment_index_1: usize, ) { // avoid a segment spuriously "intersecting" with itself @@ -123,8 +123,8 @@ where let intersection = intersection.unwrap(); if !self.edges_are_from_same_geometry { - edge0.borrow_mut().mark_as_unisolated(); - edge1.borrow_mut().mark_as_unisolated(); + edge0.mark_as_unisolated(); + edge1.mark_as_unisolated(); } if !self.is_trivial_intersection( intersection, @@ -136,13 +136,9 @@ where if self.edges_are_from_same_geometry || !intersection.is_proper() { // In the case of self-noding, `edge0` might alias `edge1`, so it's imperative that // the mutable borrows are short lived and do not overlap. - edge0 - .borrow_mut() - .add_intersections(intersection, line_0, segment_index_0); + edge0.add_intersections(intersection, line_0, segment_index_0); - edge1 - .borrow_mut() - .add_intersections(intersection, line_1, segment_index_1); + edge1.add_intersections(intersection, line_1, segment_index_1); } if let LineIntersection::SinglePoint { is_proper: true, diff --git a/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs b/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs index 15beefd8e..ca2867bdb 100644 --- a/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs +++ b/geo/src/algorithm/relate/geomgraph/index/simple_edge_set_intersector.rs @@ -15,12 +15,12 @@ impl SimpleEdgeSetIntersector { fn compute_intersects( &self, - edge0: &Arc>>, - edge1: &Arc>>, + edge0: &mut Edge, + edge1: &mut Edge, segment_intersector: &mut SegmentIntersector, ) { - let edge0_coords_len = edge0.borrow().coords().len() - 1; - let edge1_coords_len = edge1.borrow().coords().len() - 1; + let edge0_coords_len = edge0.coords().len() - 1; + let edge1_coords_len = edge1.coords().len() - 1; for i0 in 0..edge0_coords_len { for i1 in 0..edge1_coords_len { segment_intersector.add_intersections(edge0, i0, edge1, i1); @@ -36,24 +36,30 @@ impl EdgeSetIntersector for SimpleEdgeSetIntersector { check_for_self_intersecting_edges: bool, segment_intersector: &mut SegmentIntersector, ) { - let edges = graph.edges(); - for edge0 in edges.iter_mut() { - for edge1 in edges.iter_mut() { - if check_for_self_intersecting_edges || edge0.as_ptr() != edge1.as_ptr() { - self.compute_intersects(edge0, edge1, segment_intersector); - } + let edges = graph.edges_mut(); + for i in 0..edges.len() { + let (e0, e1) = edges.split_at_mut(i+1); + let (e0, edge0) = e0.split_at_mut(i); + let edge0 = &mut edge0[0]; + + if check_for_self_intersecting_edges { + self.compute_intersects(edge0, edge0, segment_intersector); + } + + for edge1 in e0.iter_mut().chain(e1) { + self.compute_intersects(edge0, edge1, segment_intersector); } } } fn compute_intersections_between_sets<'a>( &self, - graph_0: &GeometryGraph<'a, F>, - graph_1: &GeometryGraph<'a, F>, + graph_0: &mut GeometryGraph<'a, F>, + graph_1: &mut GeometryGraph<'a, F>, segment_intersector: &mut SegmentIntersector, ) { - let edges_0 = graph_0.edges(); - let edges_1 = graph_1.edges(); + let edges_0 = graph_0.edges_mut(); + let edges_1 = graph_1.edges_mut(); for edge0 in edges_0 { for edge1 in edges_1 { diff --git a/geo/src/algorithm/relate/geomgraph/planar_graph.rs b/geo/src/algorithm/relate/geomgraph/planar_graph.rs index 7d1c18dd7..1b4fc7ebc 100644 --- a/geo/src/algorithm/relate/geomgraph/planar_graph.rs +++ b/geo/src/algorithm/relate/geomgraph/planar_graph.rs @@ -64,6 +64,10 @@ impl PlanarGraph { &self.edges } + pub fn edges_mut(&mut self) -> &mut [Edge] { + &mut self.edges + } + pub fn new() -> Self { PlanarGraph { nodes: NodeMap::new(), diff --git a/geo/src/algorithm/relate/relate_operation.rs b/geo/src/algorithm/relate/relate_operation.rs index badce014a..feecff92d 100644 --- a/geo/src/algorithm/relate/relate_operation.rs +++ b/geo/src/algorithm/relate/relate_operation.rs @@ -29,7 +29,7 @@ where graph_b: GeometryGraph<'a, F>, nodes: NodeMap, line_intersector: RobustLineIntersector, - isolated_edges: Vec>>>, + isolated_edges: Vec>, } #[derive(PartialEq)] @@ -87,7 +87,7 @@ where // compute intersections between edges of the two input geometries let segment_intersector = self .graph_a - .compute_edge_intersections(&self.graph_b, Box::new(self.line_intersector.clone())); + .compute_edge_intersections(&mut self.graph_b, Box::new(self.line_intersector.clone())); self.compute_intersection_nodes(0); self.compute_intersection_nodes(1); @@ -103,9 +103,9 @@ where // (eg where one or other of the geometries has a vertex at the intersection point) // We need to compute the edge graph at all nodes to determine the IM. let edge_end_builder = EdgeEndBuilder::new(); - let edge_ends_a: Vec<_> = edge_end_builder.compute_ends_for_edges(self.graph_a.edges()); + let edge_ends_a: Vec<_> = edge_end_builder.compute_ends_for_edges(self.graph_a.edges_mut()); self.insert_edge_ends(edge_ends_a); - let edge_ends_b: Vec<_> = edge_end_builder.compute_ends_for_edges(self.graph_b.edges()); + let edge_ends_b: Vec<_> = edge_end_builder.compute_ends_for_edges(self.graph_b.edges_mut()); self.insert_edge_ends(edge_ends_b); let mut nodes = NodeMap::new(); @@ -268,8 +268,6 @@ where }; for edge in graph.edges() { - let edge = edge.borrow(); - let edge_position = edge.label().on_position(geom_index); for edge_intersection in edge.edge_intersections() { let (new_node, _edges) = self @@ -294,8 +292,7 @@ where "before updated_intersection_matrix(isolated_edges): {:?}", intersection_matrix ); - for isolated_edge in &self.isolated_edges { - let edge = isolated_edge.borrow(); + for edge in &self.isolated_edges { Edge::::update_intersection_matrix(edge.label(), intersection_matrix); debug!( "after isolated_edge update_intersection_matrix: {:?}, (isolated_edge: {:?}, label: {:?})", @@ -324,10 +321,9 @@ where (&self.graph_b, &self.graph_a) }; - for edge in this_graph.edges() { - let mut mut_edge = edge.borrow_mut(); - if mut_edge.is_isolated() { - Self::label_isolated_edge(&mut mut_edge, target_index, target_graph.geometry()); + for edge in this_graph.edges_mut() { + if edge.is_isolated() { + Self::label_isolated_edge(&mut edge, target_index, target_graph.geometry()); self.isolated_edges.push(edge.clone()); } }