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

Add associated return type for BoundingBox algorithm trait. #156

Merged
merged 1 commit into from
Sep 4, 2017
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
38 changes: 25 additions & 13 deletions src/algorithm/boundingbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use types::{Bbox, Point, MultiPoint, Line, LineString, MultiLineString, Polygon,
/// Calculation of the bounding box of a geometry.
pub trait BoundingBox<T: Float> {
type Output;

/// Return the Bounding Box of a geometry
///
/// ```
Expand All @@ -24,7 +26,7 @@ pub trait BoundingBox<T: Float> {
/// assert_eq!(118.34, bbox.ymax);
/// ```
///
fn bbox(&self) -> Option<Bbox<T>>;
fn bbox(&self) -> Self::Output;
}


Expand Down Expand Up @@ -57,56 +59,66 @@ fn get_bbox<'a, I, T>(collection: I) -> Option<Bbox<T>>
impl<T> BoundingBox<T> for MultiPoint<T>
where T: Float
{
type Output = Option<Bbox<T>>;

///
/// Return the BoundingBox for a MultiPoint
///
fn bbox(&self) -> Option<Bbox<T>> {
fn bbox(&self) -> Self::Output {
get_bbox(&self.0)
}
}

impl<T> BoundingBox<T> for Line<T>
where T: Float
{
fn bbox(&self) -> Option<Bbox<T>> {
type Output = Bbox<T>;

fn bbox(&self) -> Self::Output {
let a = self.start;
let b = self.end;
let (xmin, xmax) = if a.x() <= b.x() {(a.x(), b.x())} else {(b.x(), a.x())};
let (ymin, ymax) = if a.y() <= b.y() {(a.y(), b.y())} else {(b.y(), a.y())};
Some(Bbox {xmin: xmin, xmax: xmax,
ymin: ymin, ymax: ymax})
Bbox {xmin: xmin, xmax: xmax,
ymin: ymin, ymax: ymax}
}
}

impl<T> BoundingBox<T> for LineString<T>
where T: Float
{
type Output = Option<Bbox<T>>;

///
/// Return the BoundingBox for a LineString
///
fn bbox(&self) -> Option<Bbox<T>> {
fn bbox(&self) -> Self::Output {
get_bbox(&self.0)
}
}

impl<T> BoundingBox<T> for MultiLineString<T>
where T: Float
{
type Output = Option<Bbox<T>>;

///
/// Return the BoundingBox for a MultiLineString
///
fn bbox(&self) -> Option<Bbox<T>> {
fn bbox(&self) -> Self::Output {
get_bbox(self.0.iter().flat_map(|line| line.0.iter()))
}
}

impl<T> BoundingBox<T> for Polygon<T>
where T: Float
{
type Output = Option<Bbox<T>>;

///
/// Return the BoundingBox for a Polygon
///
fn bbox(&self) -> Option<Bbox<T>> {
fn bbox(&self) -> Self::Output {
let line = &self.exterior;
get_bbox(&line.0)
}
Expand All @@ -115,10 +127,12 @@ impl<T> BoundingBox<T> for Polygon<T>
impl<T> BoundingBox<T> for MultiPolygon<T>
where T: Float
{
type Output = Option<Bbox<T>>;

///
/// Return the BoundingBox for a MultiPolygon
///
fn bbox(&self) -> Option<Bbox<T>> {
fn bbox(&self) -> Self::Output {
get_bbox(self.0.iter().flat_map(|poly| (poly.exterior).0.iter()))
}
}
Expand Down Expand Up @@ -193,9 +207,7 @@ mod test {
let p = |x, y| Point(Coordinate { x: x, y: y });
let line1 = Line::new(p(0., 1.), p(2., 3.));
let line2 = Line::new(p(2., 3.), p(0., 1.));
assert_eq!(line1.bbox().unwrap(),
Bbox {xmin: 0., xmax: 2., ymin: 1., ymax: 3.});
assert_eq!(line2.bbox().unwrap(),
Bbox {xmin: 0., xmax: 2., ymin: 1., ymax: 3.});
assert_eq!(line1.bbox(), Bbox {xmin: 0., xmax: 2., ymin: 1., ymax: 3.});
assert_eq!(line2.bbox(), Bbox {xmin: 0., xmax: 2., ymin: 1., ymax: 3.});
}
}