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

Rename bounding ‘box’ to ‘rect’; move structure to geo-types. #295

Merged
merged 1 commit into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
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
57 changes: 24 additions & 33 deletions geo-types/src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// wouldn't have this duplication

use num_traits::{Float, ToPrimitive};
use {Coordinate, CoordinateType, Line, LineString, Point};
use {Coordinate, CoordinateType, Line, LineString, Point, Rect};

pub static COORD_PRECISION: f32 = 1e-1; // 0.1m

Expand Down Expand Up @@ -125,10 +125,10 @@ where
}
}

pub trait BoundingBox<T: CoordinateType> {
pub trait BoundingRect<T: CoordinateType> {
type Output;

fn bbox(&self) -> Self::Output;
fn bounding_rect(&self) -> Self::Output;
}

fn get_min_max<T>(p: T, min: T, max: T) -> (T, T)
Expand All @@ -144,7 +144,7 @@ where
}
}

fn get_bbox<I, T>(collection: I) -> Option<Bbox<T>>
fn get_bounding_rect<I, T>(collection: I) -> Option<Rect<T>>
where
T: CoordinateType,
I: IntoIterator<Item = Coordinate<T>>,
Expand All @@ -158,57 +158,48 @@ where
xrange = get_min_max(px, xrange.0, xrange.1);
yrange = get_min_max(py, yrange.0, yrange.1);
}
return Some(Bbox {
xmin: xrange.0,
xmax: xrange.1,
ymin: yrange.0,
ymax: yrange.1,
return Some(Rect {
min: Coordinate {
x: xrange.0,
y: yrange.0,
},
max: Coordinate {
x: xrange.1,
y: yrange.1,
},
});
}
None
}

impl<T> BoundingBox<T> for Line<T>
impl<T> BoundingRect<T> for Line<T>
where
T: CoordinateType,
{
type Output = Bbox<T>;
type Output = Rect<T>;

fn bbox(&self) -> Self::Output {
fn bounding_rect(&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) };
Bbox {
xmin,
xmax,
ymin,
ymax,
Rect {
min: Coordinate { x: xmin, y: ymin },
max: Coordinate { x: xmax, y: ymax },
}
}
}

impl<T> BoundingBox<T> for LineString<T>
impl<T> BoundingRect<T> for LineString<T>
where
T: CoordinateType,
{
type Output = Option<Bbox<T>>;
type Output = Option<Rect<T>>;

///
/// Return the BoundingBox for a LineString
/// Return the bounding rectangle for a LineString
///
fn bbox(&self) -> Self::Output {
get_bbox(self.0.iter().cloned())
fn bounding_rect(&self) -> Self::Output {
get_bounding_rect(self.0.iter().cloned())
}
}

#[derive(PartialEq, Clone, Copy, Debug)]
pub struct Bbox<T>
where
T: CoordinateType,
{
pub xmin: T,
pub xmax: T,
pub ymin: T,
pub ymax: T,
}
3 changes: 3 additions & 0 deletions geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub use geometry_collection::GeometryCollection;
mod triangle;
pub use triangle::Triangle;

mod rect;
pub use rect::Rect;

#[cfg(test)]
mod test {
use super::*;
Expand Down
8 changes: 4 additions & 4 deletions geo-types/src/line.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {Coordinate, CoordinateType, Point};

#[cfg(feature = "spade")]
use algorithms::{BoundingBox, EuclideanDistance};
use algorithms::{BoundingRect, EuclideanDistance};

/// A line segment made up of exactly two [`Point`s](struct.Point.html)
#[derive(PartialEq, Clone, Copy, Debug)]
Expand Down Expand Up @@ -171,10 +171,10 @@ where
type Point = Point<T>;

fn mbr(&self) -> ::spade::BoundingRect<Self::Point> {
let bbox = self.bbox();
let bounding_rect = self.bounding_rect();
::spade::BoundingRect::from_corners(
&Point::new(bbox.xmin, bbox.ymin),
&Point::new(bbox.xmax, bbox.ymax),
&Point::new(bounding_rect.min.x, bounding_rect.min.y),
&Point::new(bounding_rect.max.x, bounding_rect.max.y),
)
}

Expand Down
26 changes: 11 additions & 15 deletions geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::iter::FromIterator;
use {Coordinate, CoordinateType, Line, Point, Triangle};

#[cfg(feature = "spade")]
use algorithms::{BoundingBox, EuclideanDistance};
use algorithms::{BoundingRect, EuclideanDistance};

/// An ordered collection of two or more [`Coordinate`s](struct.Coordinate.html), representing a
/// path between locations.
Expand Down Expand Up @@ -174,20 +174,16 @@ where
type Point = Point<T>;

fn mbr(&self) -> ::spade::BoundingRect<Self::Point> {
let bbox = self.bbox();
match bbox {
None => {
::spade::BoundingRect::from_corners(
&Point::new(T::min_value(), T::min_value()),
&Point::new(T::max_value(), T::max_value()),
)
},
Some(b) => {
::spade::BoundingRect::from_corners(
&Point::new(b.xmin, b.ymin),
&Point::new(b.xmax, b.ymax),
)
},
let bounding_rect = self.bounding_rect();
match bounding_rect {
None => ::spade::BoundingRect::from_corners(
&Point::new(T::min_value(), T::min_value()),
&Point::new(T::max_value(), T::max_value()),
),
Some(b) => ::spade::BoundingRect::from_corners(
&Point::new(b.min.x, b.min.y),
&Point::new(b.max.x, b.max.y),
),
}
}

Expand Down
11 changes: 11 additions & 0 deletions geo-types/src/rect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use {Coordinate, CoordinateType};

#[derive(PartialEq, Clone, Copy, Debug)]
#[cfg_attr(feature = "use-serde", derive(Serialize, Deserialize))]
pub struct Rect<T>
where
T: CoordinateType,
{
pub min: Coordinate<T>,
pub max: Coordinate<T>,
}
20 changes: 9 additions & 11 deletions geo/src/algorithm/area.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use num_traits::Float;
use {Bbox, Line, LineString, MultiPolygon, Polygon, Triangle};
use {Line, LineString, MultiPolygon, Polygon, Rect, Triangle};

use algorithm::winding_order::twice_signed_ring_area;

Expand Down Expand Up @@ -74,12 +74,12 @@ where
}
}

impl<T> Area<T> for Bbox<T>
impl<T> Area<T> for Rect<T>
where
T: Float,
{
fn area(&self) -> T {
(self.xmax - self.xmin) * (self.ymax - self.ymin)
(self.max.x - self.min.x) * (self.max.y - self.min.y)
}
}

Expand All @@ -97,7 +97,7 @@ where
#[cfg(test)]
mod test {
use algorithm::area::Area;
use {Bbox, Coordinate, Line, LineString, MultiPolygon, Polygon, Triangle};
use {Coordinate, Line, LineString, MultiPolygon, Polygon, Rect, Triangle};

// Area of the polygon
#[test]
Expand All @@ -118,14 +118,12 @@ mod test {
assert_relative_eq!(poly.area(), 30.);
}
#[test]
fn bbox_test() {
let bbox = Bbox {
xmin: 10.,
xmax: 20.,
ymin: 30.,
ymax: 40.,
fn rectangle_test() {
let rect = Rect {
min: Coordinate { x: 10., y: 30. },
max: Coordinate { x: 20., y: 40. },
};
assert_relative_eq!(bbox.area(), 100.);
assert_relative_eq!(rect.area(), 100.);
}
#[test]
fn area_polygon_inner_test() {
Expand Down
Loading