Skip to content

Commit

Permalink
more &mut's rather than Rc<RefCells>.
Browse files Browse the repository at this point in the history
  • Loading branch information
gauteh committed Jul 8, 2024
1 parent 251ccb7 commit d4cbd32
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 55 deletions.
4 changes: 2 additions & 2 deletions geo/src/algorithm/relate/edge_end_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ impl<F: GeoFloat> EdgeEndBuilder<F> {
}
}

pub fn compute_ends_for_edges(&self, edges: &[Arc<RefCell<Edge<F>>>]) -> Vec<EdgeEnd<F>> {
pub fn compute_ends_for_edges(&self, edges: &mut [Edge<F>]) -> Vec<EdgeEnd<F>> {
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
}
Expand Down
8 changes: 6 additions & 2 deletions geo/src/algorithm/relate/geomgraph/geometry_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ where
self.planar_graph.edges()
}

pub(crate) fn edges_mut(&mut self) -> &mut [Edge<F>] {
self.planar_graph.edges_mut()
}

pub(crate) fn insert_edge(&mut self, edge: Edge<F>) {
self.planar_graph.insert_edge(edge)
}
Expand Down Expand Up @@ -353,8 +357,8 @@ where
}

pub(crate) fn compute_edge_intersections(
&self,
other: &GeometryGraph<F>,
&'a mut self,
other: &mut GeometryGraph<'a, F>,
line_intersector: Box<dyn LineIntersector<F>>,
) -> SegmentIntersector<F> {
let mut segment_intersector = SegmentIntersector::new(line_intersector, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) trait EdgeSetIntersector<F: GeoFloat> {
/// `segment_intersector`: the SegmentIntersector to use
fn compute_intersections_within_set(
&self,
graph: &GeometryGraph<F>,
graph: &mut GeometryGraph<F>,
check_for_self_intersecting_edges: bool,
segment_intersector: &mut SegmentIntersector<F>,
);
Expand All @@ -23,8 +23,8 @@ pub(crate) trait EdgeSetIntersector<F: GeoFloat> {
/// 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<F>,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ where
{
fn compute_intersections_within_set(
&self,
graph: &GeometryGraph<F>,
graph: &mut GeometryGraph<F>,
check_for_self_intersecting_edges: bool,
segment_intersector: &mut SegmentIntersector<F>,
) {
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,
Expand All @@ -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<F>,
) {
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,
Expand Down
16 changes: 6 additions & 10 deletions geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ where

pub fn add_intersections(
&mut self,
edge0: &Edge<F>,
edge0: &mut Edge<F>,
segment_index_0: usize,
edge1: &Edge<F>,
edge1: &mut Edge<F>,
segment_index_1: usize,
) {
// avoid a segment spuriously "intersecting" with itself
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ impl SimpleEdgeSetIntersector {

fn compute_intersects<F: GeoFloat>(
&self,
edge0: &Arc<RefCell<Edge<F>>>,
edge1: &Arc<RefCell<Edge<F>>>,
edge0: &mut Edge<F>,
edge1: &mut Edge<F>,
segment_intersector: &mut SegmentIntersector<F>,
) {
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);
Expand All @@ -36,24 +36,30 @@ impl<F: GeoFloat> EdgeSetIntersector<F> for SimpleEdgeSetIntersector {
check_for_self_intersecting_edges: bool,
segment_intersector: &mut SegmentIntersector<F>,
) {
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<F>,
) {
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 {
Expand Down
4 changes: 4 additions & 0 deletions geo/src/algorithm/relate/geomgraph/planar_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl<F: GeoFloat> PlanarGraph<F> {
&self.edges
}

pub fn edges_mut(&mut self) -> &mut [Edge<F>] {
&mut self.edges
}

pub fn new() -> Self {
PlanarGraph {
nodes: NodeMap::new(),
Expand Down
20 changes: 8 additions & 12 deletions geo/src/algorithm/relate/relate_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
graph_b: GeometryGraph<'a, F>,
nodes: NodeMap<F, RelateNodeFactory>,
line_intersector: RobustLineIntersector,
isolated_edges: Vec<Arc<RefCell<Edge<F>>>>,
isolated_edges: Vec<Edge<F>>,
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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::<F>::update_intersection_matrix(edge.label(), intersection_matrix);
debug!(
"after isolated_edge update_intersection_matrix: {:?}, (isolated_edge: {:?}, label: {:?})",
Expand Down Expand Up @@ -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());
}
}
Expand Down

0 comments on commit d4cbd32

Please sign in to comment.