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

Poof of concept: geo_traits::Coord #1169

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 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
4 changes: 2 additions & 2 deletions geo-types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl fmt::Display for Error {

#[cfg(test)]
mod test {
use crate::{Geometry, Point, Rect};
use crate::{Geometry, Point, Rect, Coord};
use alloc::string::ToString;
use core::convert::TryFrom;

Expand All @@ -32,7 +32,7 @@ mod test {
let point = Point::new(1.0, 2.0);
let point_geometry = Geometry::from(point);

let rect = Rect::new(Point::new(1.0, 2.0), Point::new(3.0, 4.0));
let rect = Rect::<Coord>::new(Point::new(1.0, 2.0), Point::new(3.0, 4.0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... was this not compiling without this change?

let rect_geometry = Geometry::from(rect);

Point::try_from(point_geometry).expect("failed to unwrap inner enum Point");
Expand Down
18 changes: 17 additions & 1 deletion geo-types/src/geometry/coord.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{coord, CoordNum, Point};
use crate::{coord, geo_traits, CoordNum, Point};

#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
Expand Down Expand Up @@ -33,6 +33,22 @@ pub struct Coord<T: CoordNum = f64> {
#[deprecated(note = "Renamed to `geo_types::Coord` (or `geo::Coord`)")]
pub type Coordinate<T = f64> = Coord<T>;

impl<T: crate::CoordNum> geo_traits::Coord for Coord<T> {
type Scalar = T;

fn x(&self) -> Self::Scalar {
self.x
}

fn y(&self) -> Self::Scalar {
self.y
}

fn from_xy(x: Self::Scalar, y: Self::Scalar) -> Self {
Coord { x, y }
}
}

impl<T: CoordNum> From<(T, T)> for Coord<T> {
#[inline]
fn from(coords: (T, T)) -> Self {
Expand Down
49 changes: 25 additions & 24 deletions geo-types/src/geometry/line.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::geo_traits::{self, Coord as CoordTrait};
use crate::{Coord, CoordNum, Point};
#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq};
Expand All @@ -11,12 +12,12 @@ use approx::{AbsDiffEq, RelativeEq};
/// `LineString` with the two end points.
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Line<T: CoordNum = f64> {
pub start: Coord<T>,
pub end: Coord<T>,
pub struct Line<C: geo_traits::Coord = Coord<f64>> {
pub start: C,
pub end: C,
}

impl<T: CoordNum> Line<T> {
impl<C: geo_traits::Coord> Line<C> {
/// Creates a new line segment.
///
/// # Examples
Expand All @@ -29,18 +30,15 @@ impl<T: CoordNum> Line<T> {
/// assert_eq!(line.start, coord! { x: 0., y: 0. });
/// assert_eq!(line.end, coord! { x: 1., y: 2. });
/// ```
pub fn new<C>(start: C, end: C) -> Self
where
C: Into<Coord<T>>,
{
pub fn new(start: impl Into<C>, end: impl Into<C>) -> Self {
Self {
start: start.into(),
end: end.into(),
}
}

/// Calculate the difference in coordinates (Δx, Δy).
pub fn delta(&self) -> Coord<T> {
pub fn delta(&self) -> C {
self.end - self.start
}

Expand All @@ -59,8 +57,8 @@ impl<T: CoordNum> Line<T> {
/// line.end.x - line.start.x
/// # );
/// ```
pub fn dx(&self) -> T {
self.delta().x
pub fn dx(&self) -> C::Scalar {
self.delta().x()
}

/// Calculate the difference in ‘y’ components (Δy).
Expand All @@ -78,8 +76,8 @@ impl<T: CoordNum> Line<T> {
/// line.end.y - line.start.y
/// # );
/// ```
pub fn dy(&self) -> T {
self.delta().y
pub fn dy(&self) -> C::Scalar {
self.delta().y()
}

/// Calculate the slope (Δy/Δx).
Expand Down Expand Up @@ -108,7 +106,7 @@ impl<T: CoordNum> Line<T> {
/// Line::new(a, b).slope() == Line::new(b, a).slope()
/// # );
/// ```
pub fn slope(&self) -> T {
pub fn slope(&self) -> C::Scalar {
self.dy() / self.dx()
}

Expand Down Expand Up @@ -138,26 +136,29 @@ impl<T: CoordNum> Line<T> {
/// Line::new(a, b).determinant() == -Line::new(b, a).determinant()
/// # );
/// ```
pub fn determinant(&self) -> T {
self.start.x * self.end.y - self.start.y * self.end.x
pub fn determinant(&self) -> C::Scalar {
self.start.x() * self.end.y() - self.start.y() * self.end.x()
}

pub fn start_point(&self) -> Point<T> {
Point::from(self.start)
pub fn start_point(&self) -> Point<C::Scalar> {
Point::from_coord(self.start)
}

pub fn end_point(&self) -> Point<T> {
Point::from(self.end)
pub fn end_point(&self) -> Point<C::Scalar> {
Point::from_coord(self.end)
}

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

impl<T: CoordNum> From<[(T, T); 2]> for Line<T> {
fn from(coord: [(T, T); 2]) -> Self {
Line::new(coord[0], coord[1])
impl<C: geo_traits::Coord> From<[(C::Scalar, C::Scalar); 2]> for Line<C> {
fn from(coord: [(C::Scalar, C::Scalar); 2]) -> Self {
Line::new(
C::from_xy(coord[0].0, coord[0].1),
C::from_xy(coord[1].0, coord[1].1),
)
}
}
#[cfg(any(feature = "approx", test))]
Expand Down
Loading
Loading