Skip to content

Commit

Permalink
geometry_graph: make Send (rm Rc + RefCell)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauteh committed Jul 8, 2024
1 parent 9452543 commit 251ccb7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 40 deletions.
5 changes: 3 additions & 2 deletions geo/src/algorithm/relate/edge_end_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use super::geomgraph::{Edge, EdgeEnd, EdgeIntersection};
use crate::GeoFloat;

use std::cell::RefCell;
use std::rc::Rc;
// use std::rc::Rc;
use std::sync::Arc;

/// Computes the [`EdgeEnd`]s which arise from an [`Edge`] who has had its `edge_intersections`
/// populated with self and proper [`EdgeIntersection`]s.
Expand All @@ -19,7 +20,7 @@ impl<F: GeoFloat> EdgeEndBuilder<F> {
}
}

pub fn compute_ends_for_edges(&self, edges: &[Rc<RefCell<Edge<F>>>]) -> Vec<EdgeEnd<F>> {
pub fn compute_ends_for_edges(&self, edges: &[Arc<RefCell<Edge<F>>>]) -> Vec<EdgeEnd<F>> {
let mut list = vec![];
for edge in edges {
self.compute_ends_for_edge(&mut edge.borrow_mut(), &mut list);
Expand Down
18 changes: 9 additions & 9 deletions geo/src/algorithm/relate/geomgraph/geometry_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::HasDimensions;
use crate::{Coord, GeoFloat, GeometryCow, Line, LineString, Point, Polygon};

use rstar::{RTree, RTreeNum};
use std::cell::RefCell;
use std::rc::Rc;
// use std::cell::RefCell;
// use std::rc::Rc;
use std::sync::Arc;

/// The computation of the [`IntersectionMatrix`](crate::algorithm::relate::IntersectionMatrix) relies on the use of a
/// structure called a "topology graph". The topology graph contains nodes (CoordNode) and
Expand All @@ -35,7 +36,7 @@ where
{
arg_index: usize,
parent_geometry: GeometryCow<'a, F>,
tree: Option<Rc<RTree<Segment<F>>>>,
tree: Option<Arc<RTree<Segment<F>>>>,
use_boundary_determination_rule: bool,
has_computed_self_nodes: bool,
planar_graph: PlanarGraph<F>,
Expand All @@ -49,14 +50,14 @@ impl<F> GeometryGraph<'_, F>
where
F: GeoFloat,
{
pub(crate) fn set_tree(&mut self, tree: Rc<RTree<Segment<F>>>) {
pub(crate) fn set_tree(&mut self, tree: Arc<RTree<Segment<F>>>) {
self.tree = Some(tree);
}

pub(crate) fn get_or_build_tree(&self) -> Rc<RTree<Segment<F>>> {
pub(crate) fn get_or_build_tree(&self) -> Arc<RTree<Segment<F>>> {
self.tree
.clone()
.unwrap_or_else(|| Rc::new(self.build_tree()))
.unwrap_or_else(|| Arc::new(self.build_tree()))
}

pub(crate) fn build_tree(&self) -> RTree<Segment<F>> {
Expand All @@ -65,7 +66,7 @@ where
.iter()
.enumerate()
.flat_map(|(edge_idx, edge)| {
let edge = RefCell::borrow(edge);
// let edge = RefCell::borrow(edge);
let start_of_final_segment: usize = edge.coords().len() - 1;
(0..start_of_final_segment).map(move |segment_idx| {
let p1 = edge.coords()[segment_idx];
Expand Down Expand Up @@ -105,7 +106,7 @@ where
}
}

pub(crate) fn edges(&self) -> &[Rc<RefCell<Edge<F>>>] {
pub(crate) fn edges(&self) -> &[Edge<F>] {
self.planar_graph.edges()
}

Expand Down Expand Up @@ -404,7 +405,6 @@ where
let positions_and_intersections: Vec<(CoordPos, Vec<Coord<F>>)> = self
.edges()
.iter()
.map(|cell| cell.borrow())
.map(|edge| {
let position = edge
.label()
Expand Down
7 changes: 4 additions & 3 deletions geo/src/algorithm/relate/geomgraph/index/prepared_geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::GeometryCow;
use crate::{GeoFloat, Relate};

use std::cell::RefCell;
use std::rc::Rc;
// use std::rc::Rc;
use std::sync::Arc;

use rstar::{RTree, RTreeNum};

Expand Down Expand Up @@ -38,7 +39,7 @@ mod conversions {
Geometry, GeometryCollection, Line, LineString, MultiLineString, MultiPoint, MultiPolygon,
Point, Polygon, Rect, Triangle,
};
use std::rc::Rc;
use std::sync::Arc;

impl<'a, F: GeoFloat> From<Point<F>> for PreparedGeometry<'a, F> {
fn from(point: Point<F>) -> Self {
Expand Down Expand Up @@ -156,7 +157,7 @@ mod conversions {
impl<'a, F: GeoFloat> From<GeometryCow<'a, F>> for PreparedGeometry<'a, F> {
fn from(geometry: GeometryCow<'a, F>) -> Self {
let mut geometry_graph = GeometryGraph::new(0, geometry);
geometry_graph.set_tree(Rc::new(geometry_graph.build_tree()));
geometry_graph.set_tree(Arc::new(geometry_graph.build_tree()));

// TODO: don't pass in line intersector here - in theory we'll want pluggable line intersectors
// and the type (Robust) shouldn't be hard coded here.
Expand Down
21 changes: 10 additions & 11 deletions geo/src/algorithm/relate/geomgraph/index/segment_intersector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ where
fn is_trivial_intersection(
&self,
intersection: LineIntersection<F>,
edge0: &RefCell<Edge<F>>,
edge0: &Edge<F>,
segment_index_0: usize,
edge1: &RefCell<Edge<F>>,
edge1: &Edge<F>,
segment_index_1: usize,
) -> bool {
if edge0.as_ptr() != edge1.as_ptr() {
if !std::ptr::eq(edge0, edge1) {
return false;
}

Expand All @@ -81,7 +81,6 @@ where
return true;
}

let edge0 = edge0.borrow();
if edge0.is_closed() {
// first and last coords in a ring are adjacent
let max_segment_index = edge0.coords().len() - 1;
Expand All @@ -97,23 +96,23 @@ where

pub fn add_intersections(
&mut self,
edge0: &RefCell<Edge<F>>,
edge0: &Edge<F>,
segment_index_0: usize,
edge1: &RefCell<Edge<F>>,
edge1: &Edge<F>,
segment_index_1: usize,
) {
// avoid a segment spuriously "intersecting" with itself
if edge0.as_ptr() == edge1.as_ptr() && segment_index_0 == segment_index_1 {
if std::ptr::eq(edge0, edge1) && segment_index_0 == segment_index_1 {
return;
}

let line_0 = Line::new(
edge0.borrow().coords()[segment_index_0],
edge0.borrow().coords()[segment_index_0 + 1],
edge0.coords()[segment_index_0],
edge0.coords()[segment_index_0 + 1],
);
let line_1 = Line::new(
edge1.borrow().coords()[segment_index_1],
edge1.borrow().coords()[segment_index_1 + 1],
edge1.coords()[segment_index_1],
edge1.coords()[segment_index_1 + 1],
);

let intersection = self.line_intersector.compute_intersection(line_0, line_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use super::{EdgeSetIntersector, SegmentIntersector};
use crate::GeoFloat;

use std::cell::RefCell;
use std::rc::Rc;
// use std::rc::Rc;
use std::sync::Arc;

pub(crate) struct SimpleEdgeSetIntersector;

Expand All @@ -14,8 +15,8 @@ impl SimpleEdgeSetIntersector {

fn compute_intersects<F: GeoFloat>(
&self,
edge0: &Rc<RefCell<Edge<F>>>,
edge1: &Rc<RefCell<Edge<F>>>,
edge0: &Arc<RefCell<Edge<F>>>,
edge1: &Arc<RefCell<Edge<F>>>,
segment_intersector: &mut SegmentIntersector<F>,
) {
let edge0_coords_len = edge0.borrow().coords().len() - 1;
Expand All @@ -31,13 +32,13 @@ impl SimpleEdgeSetIntersector {
impl<F: GeoFloat> EdgeSetIntersector<F> for SimpleEdgeSetIntersector {
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();
for edge0 in edges.iter() {
for edge1 in edges.iter() {
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);
}
Expand Down
15 changes: 8 additions & 7 deletions geo/src/algorithm/relate/geomgraph/planar_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use super::{
};
use crate::{Coord, GeoFloat};

use std::cell::RefCell;
use std::rc::Rc;
// use std::cell::RefCell;
// use std::rc::Rc;
use std::sync::Arc;

#[derive(Clone, PartialEq)]
pub(crate) struct PlanarGraphNode;
Expand All @@ -24,7 +25,7 @@ where
#[derive(Clone, PartialEq)]
pub(crate) struct PlanarGraph<F: GeoFloat> {
pub(crate) nodes: NodeMap<F, PlanarGraphNode>,
edges: Vec<Rc<RefCell<Edge<F>>>>,
edges: Vec<Edge<F>>,
}

impl<F: GeoFloat> PlanarGraph<F> {
Expand All @@ -35,7 +36,7 @@ impl<F: GeoFloat> PlanarGraph<F> {
edges: self
.edges
.iter()
.map(|e| Rc::new(RefCell::new(e.borrow().clone())))
.map(|e| e.clone())
.collect(),
};
assert_eq!(from_arg_index, 0);
Expand All @@ -50,7 +51,7 @@ impl<F: GeoFloat> PlanarGraph<F> {
node.swap_label_args();
}
for edge in &mut self.edges {
edge.borrow_mut().swap_label_args();
edge.swap_label_args();
}
}

Expand All @@ -59,7 +60,7 @@ impl<F: GeoFloat> PlanarGraph<F> {
assert_eq!(self.edges, other.edges);
}

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

Expand All @@ -79,7 +80,7 @@ impl<F: GeoFloat> PlanarGraph<F> {
}

pub fn insert_edge(&mut self, edge: Edge<F>) {
self.edges.push(Rc::new(RefCell::new(edge)));
self.edges.push(edge);
}

pub fn add_node_with_coordinate(&mut self, coord: Coord<F>) -> &mut CoordNode<F> {
Expand Down
4 changes: 2 additions & 2 deletions geo/src/algorithm/relate/relate_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::CoordinatePosition;
use crate::{Coord, GeoFloat, GeometryCow};

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

/// Computes an [`IntersectionMatrix`] describing the topological relationship between two
/// Geometries.
Expand All @@ -29,7 +29,7 @@ where
graph_b: GeometryGraph<'a, F>,
nodes: NodeMap<F, RelateNodeFactory>,
line_intersector: RobustLineIntersector,
isolated_edges: Vec<Rc<RefCell<Edge<F>>>>,
isolated_edges: Vec<Arc<RefCell<Edge<F>>>>,
}

#[derive(PartialEq)]
Expand Down

0 comments on commit 251ccb7

Please sign in to comment.