Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round up Intersects implementations #516

Merged
merged 5 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Complete rest of Intersects
+ add blanket impl for Geometry, Geom.Coll.
  • Loading branch information
rmanoka committed Oct 5, 2020
commit f7b6527ea224847bbe9cac422f72990df9477a0d
49 changes: 49 additions & 0 deletions geo/src/algorithm/intersects/collections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use super::Intersects;
use crate::*;

impl<T, G> Intersects<G> for Geometry<T>
where
T: CoordinateType,
Point<T>: Intersects<G>,
MultiPoint<T>: Intersects<G>,
Line<T>: Intersects<G>,
LineString<T>: Intersects<G>,
MultiLineString<T>: Intersects<G>,
Triangle<T>: Intersects<G>,
Rect<T>: Intersects<G>,
Polygon<T>: Intersects<G>,
MultiPolygon<T>: Intersects<G>,
{
fn intersects(&self, rhs: &G) -> bool {
match self {
Geometry::Point(geom) => geom.intersects(rhs),
Geometry::MultiPoint(geom) => geom.intersects(rhs),
Geometry::Line(geom) => geom.intersects(rhs),
Geometry::LineString(geom) => geom.intersects(rhs),
Geometry::MultiLineString(geom) => geom.intersects(rhs),
Geometry::Triangle(geom) => geom.intersects(rhs),
Geometry::Rect(geom) => geom.intersects(rhs),
Geometry::Polygon(geom) => geom.intersects(rhs),
Geometry::MultiPolygon(geom) => geom.intersects(rhs),
Geometry::GeometryCollection(geom) => geom.intersects(rhs),
}
}
}
symmetric_intersects_impl!(Coordinate<T>, Geometry<T>);
symmetric_intersects_impl!(Line<T>, Geometry<T>);
symmetric_intersects_impl!(Rect<T>, Geometry<T>);
symmetric_intersects_impl!(Polygon<T>, Geometry<T>);

impl<T, G> Intersects<G> for GeometryCollection<T>
where
T: CoordinateType,
Geometry<T>: Intersects<G>,
{
fn intersects(&self, rhs: &G) -> bool {
self.0.iter().any(|geom| geom.intersects(rhs))
}
}
symmetric_intersects_impl!(Coordinate<T>, GeometryCollection<T>);
symmetric_intersects_impl!(Line<T>, GeometryCollection<T>);
symmetric_intersects_impl!(Rect<T>, GeometryCollection<T>);
symmetric_intersects_impl!(Polygon<T>, GeometryCollection<T>);
9 changes: 8 additions & 1 deletion geo/src/algorithm/intersects/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ where
}

// The other side of this is handled via a blanket impl.
rhs_pt_from_coord_intersects_impl!(Coordinate<T>);
impl<T> Intersects<Point<T>> for Coordinate<T>
where
T: CoordinateType,
{
fn intersects(&self, rhs: &Point<T>) -> bool {
self == &rhs.0
}
}
7 changes: 1 addition & 6 deletions geo/src/algorithm/intersects/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ where
Line<T>: Intersects<G>,
{
fn intersects(&self, geom: &G) -> bool {
// No need to loop if either self or rhs is empty.
if self.0.is_empty() {
false
} else {
self.lines().any(|l| l.intersects(geom))
}
self.lines().any(|l| l.intersects(geom))
}
}
symmetric_intersects_impl!(Coordinate<T>, LineString<T>);
Expand Down
27 changes: 2 additions & 25 deletions geo/src/algorithm/intersects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,15 @@ macro_rules! symmetric_intersects_impl {
};
}

// Macro to delegate implementation to field `0` of rhs.
// Used for `Intersects<Point<T>>` from corresponding
// `Coordinate<T>` impl. This cannot be a blanket impl.
// because it would conflict with blanket impl. of Point<T>.
#[macro_use]
macro_rules! rhs_pt_from_coord_intersects_impl {
($t:ty) => {
impl<T> $crate::algorithm::intersects::Intersects<Point<T>> for $t
where
T: CoordinateType,
$t: $crate::algorithm::intersects::Intersects<Coordinate<T>>
{
fn intersects(&self, rhs: &Point<T>) -> bool {
self.intersects(&rhs.0)
}

}
};
}

// Macro to delegate implementation to any() of an iterator.
// Used for `LineString`, `Intersects<Point<T>>` from corresponding
// `Coordinate<T>` impl. This cannot be a blanket impl.
// because it would conflict with blanket impl. of Point<T>.

mod coordinate;
mod line;
mod line_string;
mod point;
mod polygon;
mod rect;
mod triangle;
mod collections;


// Helper function to check value lies between min and max.
// Only makes sense if min <= max (or always false)
Expand Down