Skip to content

Commit

Permalink
Merge #244
Browse files Browse the repository at this point in the history
244: Migrate Line/LineString to be a series of Coordinates (not Points). r=urschrei a=frewsxcv

Relevant conversation in #15.

This is a breaking change for `geo-types`.

I'm _not_ planning to publish a release if this merges. I have a couple other breaking changes I'd like to consider before bumping the Rust geo ecosystem.

Co-authored-by: Corey Farwell <[email protected]>
  • Loading branch information
bors[bot] and frewsxcv committed Jun 23, 2018
2 parents 57513bd + b82b3a5 commit 3816cfa
Show file tree
Hide file tree
Showing 30 changed files with 1,135 additions and 1,098 deletions.
15 changes: 8 additions & 7 deletions geo-types/src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ where
T: Float,
{
fn euclidean_distance(&self, point: &Point<T>) -> T {
line_segment_distance(*point, self.start, self.end)
let (start, end) = self.points();
line_segment_distance(*point, start, end)
}
}

Expand All @@ -63,15 +64,15 @@ where
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())
let (xmin, xmax) = if a.x <= b.x {
(a.x, b.x)
} else {
(b.x(), a.x())
(b.x, a.x)
};
let (ymin, ymax) = if a.y() <= b.y() {
(a.y(), b.y())
let (ymin, ymax) = if a.y <= b.y {
(a.y, b.y)
} else {
(b.y(), a.y())
(b.y, a.y)
};
Bbox {
xmin,
Expand Down
26 changes: 17 additions & 9 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use num_traits::{Float, ToPrimitive};
use num_traits::ToPrimitive;
use {CoordinateType, Point};

/// A primitive type which holds `x` and `y` position information
/// A lightweight struct used to store coordinates on the 2-dimensional
/// Cartesian plane.
///
/// Unlike `Point` (which in the future may contain additional information such
/// as an envelope, a precision model, and spatial reference system
/// information), a `Coordinate` only contains ordinate values and accessor
/// methods.
#[derive(PartialEq, Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Coordinate<T>
Expand Down Expand Up @@ -32,11 +38,14 @@ impl<T: CoordinateType> From<[T; 2]> for Coordinate<T> {

impl<T: CoordinateType> From<Point<T>> for Coordinate<T> {
fn from(point: Point<T>) -> Self {
point.0
Coordinate {
x: point.x(),
y: point.y(),
}
}
}

impl<T>Coordinate<T>
impl<T>Coordinate<T>
where
T: CoordinateType + ToPrimitive,
{
Expand All @@ -48,11 +57,10 @@ where
/// ```
/// use geo_types::Coordinate;
///
///
/// let c = Coordinate {
/// x: 40.02f64,
/// y: 116.34,
/// };
/// let c = Coordinate {
/// x: 40.02f64,
/// y: 116.34,
/// };
/// let (x, y) = c.x_y();
///
/// assert_eq!(y, 116.34);
Expand Down
23 changes: 13 additions & 10 deletions geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use multi_point::MultiPoint;
mod line;
pub use line::Line;

mod line_string;
pub mod line_string;
pub use line_string::LineString;

mod multi_line_string;
Expand Down Expand Up @@ -88,16 +88,16 @@ mod test {
#[test]
fn polygon_new_test() {
let exterior = LineString(vec![
Point::new(0., 0.),
Point::new(1., 1.),
Point::new(1., 0.),
Point::new(0., 0.),
Coordinate { x: 0., y: 0. },
Coordinate { x: 1., y: 1. },
Coordinate { x: 1., y: 0. },
Coordinate { x: 0., y: 0. },
]);
let interiors = vec![LineString(vec![
Point::new(0.1, 0.1),
Point::new(0.9, 0.9),
Point::new(0.9, 0.1),
Point::new(0.1, 0.1),
Coordinate { x: 0.1, y: 0.1 },
Coordinate { x: 0.9, y: 0.9 },
Coordinate { x: 0.9, y: 0.1 },
Coordinate { x: 0.1, y: 0.1 },
])];
let p = Polygon::new(exterior.clone(), interiors.clone());

Expand Down Expand Up @@ -129,7 +129,10 @@ mod test {
fn line_test() {
use spade::primitives::SimpleEdge;
let se = SimpleEdge::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
let l = Line::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
let l = Line::new(
Coordinate { x: 0.0, y: 0.0 },
Coordinate { x: 5., y: 5. },
);
assert_eq!(se.mbr(), l.mbr());
// difference in 15th decimal place
assert_eq!(26.0, se.distance2(&Point::new(4.0, 10.0)));
Expand Down
45 changes: 31 additions & 14 deletions geo-types/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct Line<T>
where
T: CoordinateType,
{
pub start: Point<T>,
pub end: Point<T>,
pub start: Coordinate<T>,
pub end: Coordinate<T>,
}

impl<T> Line<T>
Expand All @@ -30,16 +30,16 @@ where
/// Coordinate { x: 1., y: 2. },
/// );
///
/// assert_eq!(line.start.0, Coordinate { x: 0., y: 0. });
/// assert_eq!(line.end.0, Coordinate { x: 1., y: 2. });
/// assert_eq!(line.start, Coordinate { x: 0., y: 0. });
/// assert_eq!(line.end, Coordinate { x: 1., y: 2. });
/// ```
pub fn new<C>(start: C, end: C) -> Line<T>
where
C: Into<Coordinate<T>>,
{
Line {
start: Point(start.into()),
end: Point(end.into()),
start: start.into(),
end: end.into(),
}
}

Expand All @@ -55,11 +55,11 @@ where
/// # );
/// # assert_eq!(
/// # line.dx(),
/// line.end.x() - line.start.x()
/// line.end.x - line.start.x
/// # );
/// ```
pub fn dx(&self) -> T {
self.end.x() - self.start.x()
self.end.x - self.start.x
}

/// Calculate the difference in ‘y’ components (Δy).
Expand All @@ -74,11 +74,11 @@ where
/// # );
/// # assert_eq!(
/// # line.dy(),
/// line.end.y() - line.start.y()
/// line.end.y - line.start.y
/// # );
/// ```
pub fn dy(&self) -> T {
self.end.y() - self.start.y()
self.end.y - self.start.y
}

/// Calculate the slope (Δy/Δx).
Expand Down Expand Up @@ -124,8 +124,8 @@ where
/// # );
/// # assert_eq!(
/// # line.determinant(),
/// line.start.x() * line.end.y() -
/// line.start.y() * line.end.x()
/// line.start.x * line.end.y -
/// line.start.y * line.end.x
/// # );
/// ```
///
Expand All @@ -140,9 +140,26 @@ where
/// -Line::new(b, a).determinant()
/// # );
/// ```
///
pub fn determinant(&self) -> T {
self.start.x() * self.end.y() - self.start.y() * self.end.x()
self.start.x * self.end.y - self.start.y * self.end.x
}

pub fn start_point(&self) -> Point<T> {
Point(self.start)
}

pub fn end_point(&self) -> Point<T> {
Point(self.end)
}

pub fn points(&self) -> (Point<T>, Point<T>) {
(self.start_point(), self.end_point())
}
}

impl<T: CoordinateType> From<[(T, T); 2]> for Line<T> {
fn from(coord: [(T, T); 2]) -> Line<T> {
Line::new(coord[0], coord[1])
}
}

Expand Down
Loading

0 comments on commit 3816cfa

Please sign in to comment.