Skip to content

Commit

Permalink
Merge georust#727
Browse files Browse the repository at this point in the history
727: Add get() to IntersectionMatrix r=michaelkirk a=michaelkirk

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

Just georust#714 with my typo fixes rebased and squashed.



Co-authored-by: Patrick Chieppe <patrick.chieppe@hotmail.com>
  • Loading branch information
bors[bot] and Palladinium authored Feb 14, 2022
2 parents 53faa66 + 6772a8b commit cdd780a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions geo/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* <https://github.com/georust/geo/pull/725>
* Note: The MSRV when installing latest dependencies has increased to 1.55
* <https://github.com/georust/geo/pull/726>
* Add `get()` to `IntersectionMatrix` for directly querying DE-9IM matrices
* <https://github.com/georust/geo/pull/714>

## 0.18.0

Expand Down
45 changes: 43 additions & 2 deletions geo/src/algorithm/relate/geomgraph/intersection_matrix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::algorithm::coordinate_position::CoordPos;
use crate::algorithm::dimensions::Dimensions;
use crate::algorithm::{coordinate_position::CoordPos, dimensions::Dimensions};

/// Models a *Dimensionally Extended Nine-Intersection Model (DE-9IM)* matrix.
///
Expand Down Expand Up @@ -220,6 +219,48 @@ impl IntersectionMatrix {
&& self.0[CoordPos::Outside][CoordPos::Inside] == Dimensions::Empty
&& self.0[CoordPos::Outside][CoordPos::OnBoundary] == Dimensions::Empty
}

/// Directly accesses this matrix
///
/// ```
/// use geo_types::{LineString, Rect, line_string};
/// use geo::algorithm::{coordinate_position::CoordPos, dimensions::Dimensions, relate::Relate};
///
/// let line_string: LineString<f64> = line_string![(x: 0.0, y: 0.0), (x: 10.0, y: 0.0), (x: 5.0, y: 5.0)];
/// let rect = Rect::new((0.0, 0.0), (5.0, 5.0));
///
/// let intersection = line_string.relate(&rect);
///
/// // The intersection of the two interiors is empty, because no part of the string is inside the rect
/// assert_eq!(intersection.get(CoordPos::Inside, CoordPos::Inside), Dimensions::Empty);
///
/// // The intersection of the line string's interior with the rect's boundary is one-dimensoinal, because part of the first line overlaps one of the rect's edges
/// assert_eq!(intersection.get(CoordPos::Inside, CoordPos::OnBoundary), Dimensions::OneDimensional);
///
/// // The intersection of the line string's interior with the rect's exterior is one-dimensional, because part of the string is outside the rect
/// assert_eq!(intersection.get(CoordPos::Inside, CoordPos::Outside), Dimensions::OneDimensional);
///
/// // The intersection of the line string's boundary with the rect's interior is empty, because neither of its end points are inside the rect
/// assert_eq!(intersection.get(CoordPos::OnBoundary, CoordPos::Inside), Dimensions::Empty);
///
/// // The intersection of the line string's boundary with the rect's boundary is zero-dimensional, because the string's start and end points are on the rect's edges
/// assert_eq!(intersection.get(CoordPos::OnBoundary, CoordPos::OnBoundary), Dimensions::ZeroDimensional);
///
/// // The intersection of the line string's boundary with the rect's exterior is empty, because neither of its end points are outside the rect
/// assert_eq!(intersection.get(CoordPos::OnBoundary, CoordPos::Outside), Dimensions::Empty);
///
/// // The intersection of the the line's exterior with the rect's interior is two-dimensional, because it's simply the rect's interior
/// assert_eq!(intersection.get(CoordPos::Outside, CoordPos::Inside), Dimensions::TwoDimensional);
///
/// // The intersection of the line's exterior with the rect's boundary is one-dimensional, because it's the rect's edges (minus where the string overlaps it)
/// assert_eq!(intersection.get(CoordPos::Outside, CoordPos::OnBoundary), Dimensions::OneDimensional);
///
/// // The intersection of the two exteriors is two-dimensional, because it's the whole plane around the two shapes
/// assert_eq!(intersection.get(CoordPos::Outside, CoordPos::Outside), Dimensions::TwoDimensional);
/// ```
pub fn get(&self, lhs: CoordPos, rhs: CoordPos) -> Dimensions {
self.0[lhs][rhs]
}
}

impl std::str::FromStr for IntersectionMatrix {
Expand Down

0 comments on commit cdd780a

Please sign in to comment.