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

Implement Intersects<Bbox<T>> for Polygon #76

Merged
merged 3 commits into from
Oct 16, 2016
Merged
Changes from all commits
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
66 changes: 65 additions & 1 deletion src/algorithm/intersects.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use num::Float;
use types::{LineString, Polygon, Bbox};
use types::{LineString, Polygon, Bbox, Point};
use algorithm::contains::Contains;

/// Checks if the geometry A intersects the geometry B.
Expand Down Expand Up @@ -82,6 +82,28 @@ impl<T> Intersects<Bbox<T>> for Bbox<T>
}
}

impl<T> Intersects<Polygon<T>> for Bbox<T>
where T: Float
{
fn intersects(&self, polygon: &Polygon<T>) -> bool {
polygon.intersects(self)
}
}

impl<T> Intersects<Bbox<T>> for Polygon<T>
where T: Float
{
fn intersects(&self, bbox: &Bbox<T>) -> bool {
let p = Polygon(LineString(vec![Point::new(bbox.xmin, bbox.ymin),
Point::new(bbox.xmin, bbox.ymax),
Point::new(bbox.xmax, bbox.ymax),
Point::new(bbox.xmax, bbox.ymin),
Point::new(bbox.xmin, bbox.ymin)]),
vec![]);
self.intersects(&p)
}
}

impl<T> Intersects<Polygon<T>> for Polygon<T>
where T: Float
{
Expand Down Expand Up @@ -258,6 +280,48 @@ mod test {
assert!(p2.intersects(&p1));
}
#[test]
fn polygon_intersects_bbox_test() {
// Polygon poly =
//
// (0,8) (12,8)
// ┌──────────────────────┐
// │ (7,7) (11,7) │
// │ ┌──────┐ │
// │ │ │ │
// │ │(hole)│ │
// │ │ │ │
// │ │ │ │
// │ └──────┘ │
// │ (7,4) (11,4) │
// │ │
// │ │
// │ │
// │ │
// │ │
// └──────────────────────┘
// (0,0) (12,0)
let p = |x, y| Point(Coordinate { x: x, y: y });
let poly = Polygon(LineString(vec![p(0., 0.), p(12., 0.), p(12., 8.), p(0., 8.), p(0., 0.)]),
vec![LineString(vec![p(7., 4.), p(11., 4.), p(11., 7.), p(7., 7.), p(7., 4.)])]);
let b1 = Bbox { xmin: 11.0, xmax: 13.0, ymin: 1.0, ymax: 2.0 };
let b2 = Bbox { xmin: 2.0, xmax: 8.0, ymin: 2.0, ymax: 5.0 };
let b3 = Bbox { xmin: 8.0, xmax: 10.0, ymin: 5.0, ymax: 6.0 };
let b4 = Bbox { xmin: 1.0, xmax: 3.0, ymin: 1.0, ymax: 3.0 };
// overlaps
assert!(poly.intersects(&b1));
// contained in exterior, overlaps with hole
assert!(poly.intersects(&b2));
// completely contained in the hole
assert!(!poly.intersects(&b3));
// completely contained in the polygon
assert!(poly.intersects(&b4));
// conversely,
assert!(b1.intersects(&poly));
assert!(b2.intersects(&poly));
assert!(!b3.intersects(&poly));
assert!(b4.intersects(&poly));
}
#[test]
fn bbox_test() {
let bbox_xl = Bbox { xmin: -100., xmax: 100., ymin: -200., ymax: 200.};
let bbox_sm = Bbox { xmin: -10., xmax: 10., ymin: -20., ymax: 20.};
Expand Down