pub trait Contains<Rhs = Self> {
fn contains(&self, rhs: &Rhs) -> bool;
}
mod geometry;
mod geometry_collection;
mod line;
mod line_string;
mod point;
mod polygon;
mod rect;
mod triangle;
macro_rules! impl_contains_from_relate {
($for:ty, [$($target:ty),*]) => {
$(
impl<T> Contains<$target> for $for
where
T: GeoFloat
{
fn contains(&self, target: &$target) -> bool {
use $crate::algorithm::Relate;
self.relate(target).is_contains()
}
}
)*
};
}
pub(crate) use impl_contains_from_relate;
macro_rules! impl_contains_geometry_for {
($geom_type: ty) => {
impl<T> Contains<Geometry<T>> for $geom_type
where
T: GeoFloat,
{
fn contains(&self, geometry: &Geometry<T>) -> bool {
match geometry {
Geometry::Point(g) => self.contains(g),
Geometry::Line(g) => self.contains(g),
Geometry::LineString(g) => self.contains(g),
Geometry::Polygon(g) => self.contains(g),
Geometry::MultiPoint(g) => self.contains(g),
Geometry::MultiLineString(g) => self.contains(g),
Geometry::MultiPolygon(g) => self.contains(g),
Geometry::GeometryCollection(g) => self.contains(g),
Geometry::Rect(g) => self.contains(g),
Geometry::Triangle(g) => self.contains(g),
}
}
}
};
}
pub(crate) use impl_contains_geometry_for;
#[cfg(test)]
mod test {
use crate::line_string;
use crate::Contains;
use crate::{coord, Coord, Line, LineString, MultiPolygon, Point, Polygon, Rect, Triangle};
#[test]
fn linestring_contains_point() {
let line_string = LineString::from(vec![(0., 0.), (3., 3.)]);
let point_on_line = Point::new(1., 1.);
assert!(line_string.contains(&point_on_line));
}
#[test]
fn polygon_does_not_contain_polygon() {
let v = Polygon::new(
vec![
(150., 350.),
(100., 350.),
(210., 160.),
(290., 350.),
(250., 350.),
(200., 250.),
(150., 350.),
]
.into(),
vec![],
);
let rect = Polygon::new(
vec![
(250., 310.),
(150., 310.),
(150., 280.),
(250., 280.),
(250., 310.),
]
.into(),
vec![],
);
assert!(!v.contains(&rect));
}
#[test]
fn polygon_contains_polygon() {
let v = Polygon::new(
vec![
(150., 350.),
(100., 350.),
(210., 160.),
(290., 350.),
(250., 350.),
(200., 250.),
(150., 350.),
]
.into(),
vec![],
);
let rect = Polygon::new(
vec![
(185., 237.),
(220., 237.),
(220., 220.),
(185., 220.),
(185., 237.),
]
.into(),
vec![],
);
assert!(v.contains(&rect));
}
#[test]
fn linestring_fully_contained_in_polygon() {
let poly = Polygon::new(
LineString::from(vec![(0., 0.), (5., 0.), (5., 6.), (0., 6.), (0., 0.)]),
vec![],
);
let ls = LineString::from(vec![(3.0, 0.5), (3.0, 3.5)]);
assert!(poly.contains(&ls));
}
#[test]
fn empty_linestring_test() {
let linestring = LineString::new(Vec::new());
assert!(!linestring.contains(&Point::new(2., 1.)));
}
#[test]
fn linestring_point_is_vertex_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.)]);
assert!(linestring.contains(&Point::new(2., 0.)));
assert!(!linestring.contains(&Point::new(0., 0.)));
assert!(!linestring.contains(&Point::new(2., 2.)));
}
#[test]
fn linestring_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.)]);
assert!(linestring.contains(&Point::new(1., 0.)));
}
#[test]
fn empty_polygon_test() {
let linestring = LineString::new(Vec::new());
let poly = Polygon::new(linestring, Vec::new());
assert!(!poly.contains(&Point::new(2., 1.)));
}
#[test]
fn polygon_with_one_point_test() {
let linestring = LineString::from(vec![(2., 1.)]);
let poly = Polygon::new(linestring, Vec::new());
assert!(!poly.contains(&Point::new(3., 1.)));
}
#[test]
fn polygon_with_one_point_is_vertex_test() {
let linestring = LineString::from(vec![(2., 1.)]);
let poly = Polygon::new(linestring, Vec::new());
assert!(!poly.contains(&Point::new(2., 1.)));
}
#[test]
fn polygon_with_point_on_boundary_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.), (0., 2.), (0., 0.)]);
let poly = Polygon::new(linestring, Vec::new());
assert!(!poly.contains(&Point::new(1., 0.)));
assert!(!poly.contains(&Point::new(2., 1.)));
assert!(!poly.contains(&Point::new(1., 2.)));
assert!(!poly.contains(&Point::new(0., 1.)));
}
#[test]
fn point_in_polygon_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.), (0., 2.), (0., 0.)]);
let poly = Polygon::new(linestring, Vec::new());
assert!(poly.contains(&Point::new(1., 1.)));
}
#[test]
fn point_in_polygon_with_ray_passing_through_a_vertex_test() {
let linestring = LineString::from(vec![(1., 0.), (0., 1.), (-1., 0.), (0., -1.)]);
let poly = Polygon::new(linestring, Vec::new());
assert!(poly.contains(&Point::new(0., 0.)));
}
#[test]
fn point_in_polygon_with_ray_passing_through_a_vertex_and_not_crossing() {
let linestring = LineString::from(vec![
(0., 0.),
(2., 0.),
(3., 1.),
(4., 0.),
(4., 2.),
(0., 2.),
(0., 0.),
]);
let poly = Polygon::new(linestring, Vec::new());
assert!(poly.contains(&Point::new(1., 1.)));
}
#[test]
fn point_out_polygon_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.), (0., 2.), (0., 0.)]);
let poly = Polygon::new(linestring, Vec::new());
assert!(!poly.contains(&Point::new(2.1, 1.)));
assert!(!poly.contains(&Point::new(1., 2.1)));
assert!(!poly.contains(&Point::new(2.1, 2.1)));
}
#[test]
fn point_polygon_with_inner_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.), (0., 2.), (0., 0.)]);
let inner_linestring = LineString::from(vec![
[0.5, 0.5],
[1.5, 0.5],
[1.5, 1.5],
[0.0, 1.5],
[0.0, 0.0],
]);
let poly = Polygon::new(linestring, vec![inner_linestring]);
assert!(!poly.contains(&Point::new(0.25, 0.25)));
assert!(!poly.contains(&Point::new(1., 1.)));
assert!(!poly.contains(&Point::new(1.5, 1.5)));
assert!(!poly.contains(&Point::new(1.5, 1.)));
}
#[test]
fn empty_multipolygon_test() {
let multipoly = MultiPolygon::new(Vec::new());
assert!(!multipoly.contains(&Point::new(2., 1.)));
}
#[test]
fn empty_multipolygon_two_polygons_test() {
let poly1 = Polygon::new(
LineString::from(vec![(0., 0.), (1., 0.), (1., 1.), (0., 1.), (0., 0.)]),
Vec::new(),
);
let poly2 = Polygon::new(
LineString::from(vec![(2., 0.), (3., 0.), (3., 1.), (2., 1.), (2., 0.)]),
Vec::new(),
);
let multipoly = MultiPolygon::new(vec![poly1, poly2]);
assert!(multipoly.contains(&Point::new(0.5, 0.5)));
assert!(multipoly.contains(&Point::new(2.5, 0.5)));
assert!(!multipoly.contains(&Point::new(1.5, 0.5)));
}
#[test]
fn empty_multipolygon_two_polygons_and_inner_test() {
let poly1 = Polygon::new(
LineString::from(vec![(0., 0.), (5., 0.), (5., 6.), (0., 6.), (0., 0.)]),
vec![LineString::from(vec![
(1., 1.),
(4., 1.),
(4., 4.),
(1., 1.),
])],
);
let poly2 = Polygon::new(
LineString::from(vec![(9., 0.), (14., 0.), (14., 4.), (9., 4.), (9., 0.)]),
Vec::new(),
);
let multipoly = MultiPolygon::new(vec![poly1, poly2]);
assert!(multipoly.contains(&Point::new(3., 5.)));
assert!(multipoly.contains(&Point::new(12., 2.)));
assert!(!multipoly.contains(&Point::new(3., 2.)));
assert!(!multipoly.contains(&Point::new(7., 2.)));
}
#[test]
fn linestring_in_polygon_with_linestring_is_boundary_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.), (0., 2.), (0., 0.)]);
let poly = Polygon::new(linestring.clone(), Vec::new());
assert!(!poly.contains(&linestring));
assert!(!poly.contains(&LineString::from(vec![(0., 0.), (2., 0.)])));
assert!(!poly.contains(&LineString::from(vec![(2., 0.), (2., 2.)])));
assert!(!poly.contains(&LineString::from(vec![(0., 2.), (0., 0.)])));
}
#[test]
fn linestring_outside_polygon_test() {
let linestring = LineString::from(vec![(0., 0.), (2., 0.), (2., 2.), (0., 2.), (0., 0.)]);
let poly = Polygon::new(linestring, Vec::new());
assert!(!poly.contains(&LineString::from(vec![(1., 1.), (3., 0.)])));
assert!(!poly.contains(&LineString::from(vec![(3., 0.), (5., 2.)])));
}
#[test]
fn linestring_in_inner_polygon_test() {
let poly = Polygon::new(
LineString::from(vec![(0., 0.), (5., 0.), (5., 6.), (0., 6.), (0., 0.)]),
vec![LineString::from(vec![
(1., 1.),
(4., 1.),
(4., 4.),
(1., 4.),
(1., 1.),
])],
);
assert!(!poly.contains(&LineString::from(vec![(2., 2.), (3., 3.)])));
assert!(!poly.contains(&LineString::from(vec![(2., 2.), (2., 5.)])));
assert!(!poly.contains(&LineString::from(vec![(3., 0.5), (3., 5.)])));
}
#[test]
fn bounding_rect_in_inner_bounding_rect_test() {
let bounding_rect_xl =
Rect::new(coord! { x: -100., y: -200. }, coord! { x: 100., y: 200. });
let bounding_rect_sm = Rect::new(coord! { x: -10., y: -20. }, coord! { x: 10., y: 20. });
assert!(bounding_rect_xl.contains(&bounding_rect_sm));
assert!(!bounding_rect_sm.contains(&bounding_rect_xl));
}
#[test]
fn point_in_line_test() {
let c = |x, y| coord! { x: x, y: y };
let p0 = c(2., 4.);
let line1 = Line::new(c(2., 0.), c(2., 5.));
let line2 = Line::new(c(0., 6.), c(1.5, 4.5));
let line3 = Line::new(c(0., 6.), c(3., 3.));
assert!(line1.contains(&Point::from(p0)));
assert!(!line2.contains(&Point::from(p0)));
assert!(line3.contains(&Point::from(p0)));
}
#[test]
fn line_in_line_test() {
let c = |x, y| coord! { x: x, y: y };
let line0 = Line::new(c(0., 1.), c(3., 4.));
let line1 = Line::new(c(1., 2.), c(2., 2.));
let line2 = Line::new(c(1., 2.), c(4., 5.));
let line3 = Line::new(c(1., 2.), c(3., 4.));
assert!(!line0.contains(&line1));
assert!(!line0.contains(&line2));
assert!(line0.contains(&line3));
}
#[test]
fn linestring_in_line_test() {
let line = Line::from([(0, 10), (30, 40)]);
let linestring0 = LineString::from(vec![(1, 11), (10, 20), (15, 25)]);
let linestring1 = LineString::from(vec![(1, 11), (20, 20), (15, 25)]);
let linestring2 = LineString::from(vec![(1, 11), (10, 20), (40, 50)]);
let linestring3 = LineString::from(vec![(11, 11), (20, 20), (25, 25)]);
let linestring4 = LineString::from(vec![(0, 10), (0, 10), (0, 10)]);
let linestring5 = LineString::from(vec![(1, 11), (1, 11), (1, 11)]);
assert!(line.contains(&linestring0));
assert!(!line.contains(&linestring1));
assert!(!line.contains(&linestring2));
assert!(!line.contains(&linestring3));
assert!(!line.contains(&linestring4));
assert!(line.contains(&linestring5));
}
#[test]
fn line_in_polygon_test() {
let c = |x, y| coord! { x: x, y: y };
let line = Line::new(c(0.0, 10.0), c(30.0, 40.0));
let linestring0 = line_string![
c(-10.0, 0.0),
c(50.0, 0.0),
c(50.0, 50.0),
c(0.0, 50.0),
c(-10.0, 0.0)
];
let poly0 = Polygon::new(linestring0, Vec::new());
let linestring1 = line_string![
c(0.0, 0.0),
c(0.0, 20.0),
c(20.0, 20.0),
c(20.0, 0.0),
c(0.0, 0.0)
];
let poly1 = Polygon::new(linestring1, Vec::new());
assert!(poly0.contains(&line));
assert!(!poly1.contains(&line));
}
#[test]
fn line_in_polygon_edgecases_test() {
let c = |x, y| coord! { x: x, y: y };
let linestring0 = line_string![
c(0.0, 0.0),
c(1.0, 1.0),
c(1.0, -1.0),
c(-1.0, -1.0),
c(-1.0, 1.0)
];
let poly = Polygon::new(linestring0, Vec::new());
assert!(poly.contains(&Line::new(c(0.0, 0.0), c(1.0, -1.0))));
assert!(poly.contains(&Line::new(c(-1.0, 1.0), c(1.0, -1.0))));
assert!(!poly.contains(&Line::new(c(-1.0, 1.0), c(1.0, 1.0))));
}
#[test]
fn line_in_linestring_edgecases() {
let c = |x, y| coord! { x: x, y: y };
use crate::line_string;
let mut ls = line_string![c(0, 0), c(1, 0), c(0, 1), c(-1, 0)];
assert!(!ls.contains(&Line::from([(0, 0), (0, 0)])));
ls.close();
assert!(ls.contains(&Line::from([(0, 0), (0, 0)])));
assert!(ls.contains(&Line::from([(-1, 0), (1, 0)])));
}
#[test]
fn line_in_linestring_test() {
let line0 = Line::from([(1., 1.), (2., 2.)]);
let linestring0 = LineString::from(vec![(0., 0.5), (0.5, 0.5), (3., 3.)]);
let linestring1 = LineString::from(vec![
(0., 0.5),
(0.5, 0.5),
(1.2, 1.2),
(1.5, 1.5),
(3., 3.),
]);
let linestring2 = LineString::from(vec![
(0., 0.5),
(0.5, 0.5),
(1.2, 1.2),
(1.5, 0.),
(2., 2.),
(3., 3.),
]);
assert!(linestring0.contains(&line0));
assert!(linestring1.contains(&line0));
assert!(!linestring2.contains(&line0));
}
#[test]
fn integer_bounding_rects() {
let p: Point<i32> = Point::new(10, 20);
let bounding_rect: Rect<i32> = Rect::new(coord! { x: 0, y: 0 }, coord! { x: 100, y: 100 });
assert!(bounding_rect.contains(&p));
assert!(!bounding_rect.contains(&Point::new(-10, -10)));
let smaller_bounding_rect: Rect<i32> =
Rect::new(coord! { x: 10, y: 10 }, coord! { x: 20, y: 20 });
assert!(bounding_rect.contains(&smaller_bounding_rect));
}
#[test]
fn triangle_not_contains_point_on_edge() {
let t = Triangle::from([(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)]);
let p = Point::new(1.0, 0.0);
assert!(!t.contains(&p));
}
#[test]
fn triangle_not_contains_point_on_vertex() {
let t = Triangle::from([(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)]);
let p = Point::new(2.0, 0.0);
assert!(!t.contains(&p));
}
#[test]
fn triangle_contains_point_inside() {
let t = Triangle::from([(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)]);
let p = Point::new(1.0, 0.5);
assert!(t.contains(&p));
}
#[test]
fn triangle_not_contains_point_above() {
let t = Triangle::from([(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)]);
let p = Point::new(1.0, 1.5);
assert!(!t.contains(&p));
}
#[test]
fn triangle_not_contains_point_below() {
let t = Triangle::from([(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)]);
let p = Point::new(-1.0, 0.5);
assert!(!t.contains(&p));
}
#[test]
fn triangle_contains_neg_point() {
let t = Triangle::from([(0.0, 0.0), (-2.0, 0.0), (-2.0, -2.0)]);
let p = Point::new(-1.0, -0.5);
assert!(t.contains(&p));
}
#[test]
fn triangle_contains_collinear_points() {
let origin: Coord = (0., 0.).into();
let tri = Triangle::new(origin, origin, origin);
let pt: Point = (0., 1.23456).into();
assert!(!tri.contains(&pt));
let pt: Point = (0., 0.).into();
assert!(!tri.contains(&pt));
let origin: Coord = (0., 0.).into();
let tri = Triangle::new((1., 1.).into(), origin, origin);
let pt: Point = (1., 1.).into();
assert!(!tri.contains(&pt));
let pt: Point = (0.5, 0.5).into();
assert!(!tri.contains(&pt));
}
}