Skip to content

Commit

Permalink
geom enum impl
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkirk committed Jan 26, 2021
1 parent 5ac0b48 commit 56c6a6a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 125 deletions.
31 changes: 3 additions & 28 deletions geo/src/algorithm/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,34 +236,9 @@ impl<T> Area<T> for Geometry<T>
where
T: CoordFloat,
{
fn signed_area(&self) -> T {
match self {
Geometry::Point(g) => g.signed_area(),
Geometry::Line(g) => g.signed_area(),
Geometry::LineString(g) => g.signed_area(),
Geometry::Polygon(g) => g.signed_area(),
Geometry::MultiPoint(g) => g.signed_area(),
Geometry::MultiLineString(g) => g.signed_area(),
Geometry::MultiPolygon(g) => g.signed_area(),
Geometry::GeometryCollection(g) => g.signed_area(),
Geometry::Rect(g) => g.signed_area(),
Geometry::Triangle(g) => g.signed_area(),
}
}

fn unsigned_area(&self) -> T {
match self {
Geometry::Point(g) => g.unsigned_area(),
Geometry::Line(g) => g.unsigned_area(),
Geometry::LineString(g) => g.unsigned_area(),
Geometry::Polygon(g) => g.unsigned_area(),
Geometry::MultiPoint(g) => g.unsigned_area(),
Geometry::MultiLineString(g) => g.unsigned_area(),
Geometry::MultiPolygon(g) => g.unsigned_area(),
Geometry::GeometryCollection(g) => g.unsigned_area(),
Geometry::Rect(g) => g.unsigned_area(),
Geometry::Triangle(g) => g.unsigned_area(),
}
crate::geometry_enum_impl! {
fn signed_area(&self) -> T;
fn unsigned_area(&self) -> T;
}
}

Expand Down
15 changes: 2 additions & 13 deletions geo/src/algorithm/bounding_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,8 @@ where
{
type Output = Option<Rect<T>>;

fn bounding_rect(&self) -> Self::Output {
match self {
Geometry::Point(g) => Some(g.bounding_rect()),
Geometry::Line(g) => Some(g.bounding_rect()),
Geometry::LineString(g) => g.bounding_rect(),
Geometry::Polygon(g) => g.bounding_rect(),
Geometry::MultiPoint(g) => g.bounding_rect(),
Geometry::MultiLineString(g) => g.bounding_rect(),
Geometry::MultiPolygon(g) => g.bounding_rect(),
Geometry::GeometryCollection(g) => g.bounding_rect(),
Geometry::Rect(g) => Some(g.bounding_rect()),
Geometry::Triangle(g) => Some(g.bounding_rect()),
}
crate::geometry_enum_impl! {
fn bounding_rect(&self) -> Self::Output;
}
}

Expand Down
15 changes: 2 additions & 13 deletions geo/src/algorithm/contains/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,8 @@ impl<T> Contains<Coordinate<T>> for Geometry<T>
where
T: GeoNum,
{
fn contains(&self, coord: &Coordinate<T>) -> bool {
match self {
Geometry::Point(g) => g.contains(coord),
Geometry::Line(g) => g.contains(coord),
Geometry::LineString(g) => g.contains(coord),
Geometry::Polygon(g) => g.contains(coord),
Geometry::MultiPoint(g) => g.contains(coord),
Geometry::MultiLineString(g) => g.contains(coord),
Geometry::MultiPolygon(g) => g.contains(coord),
Geometry::GeometryCollection(g) => g.contains(coord),
Geometry::Rect(g) => g.contains(coord),
Geometry::Triangle(g) => g.contains(coord),
}
geometry_enum_impl! {
fn contains(&self, coord: &Coordinate<T>) -> bool;
}
}

Expand Down
18 changes: 3 additions & 15 deletions geo/src/algorithm/coords_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,21 +395,9 @@ impl<'a, T: CoordNum + 'a> CoordsIter<'a> for Geometry<T> {
Geometry::Triangle(g) => GeometryCoordsIter::Triangle(g.coords_iter()),
}
}

/// Return the number of coordinates in the `Geometry`.
fn coords_count(&'a self) -> usize {
match self {
Geometry::Point(g) => g.coords_count(),
Geometry::Line(g) => g.coords_count(),
Geometry::LineString(g) => g.coords_count(),
Geometry::Polygon(g) => g.coords_count(),
Geometry::MultiPoint(g) => g.coords_count(),
Geometry::MultiLineString(g) => g.coords_count(),
Geometry::MultiPolygon(g) => g.coords_count(),
Geometry::GeometryCollection(g) => g.coords_count(),
Geometry::Rect(g) => g.coords_count(),
Geometry::Triangle(g) => g.coords_count(),
}
crate::geometry_enum_impl! {
/// Return the number of coordinates in the `Geometry`.
fn coords_count(&'a self) -> usize;
}

fn exterior_coords_iter(&'a self) -> Self::ExteriorIter {
Expand Down
47 changes: 4 additions & 43 deletions geo/src/algorithm/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,49 +132,10 @@ pub trait HasDimensions {
}

impl<C: CoordNum> HasDimensions for Geometry<C> {
fn is_empty(&self) -> bool {
match self {
Geometry::Point(g) => g.is_empty(),
Geometry::Line(g) => g.is_empty(),
Geometry::LineString(g) => g.is_empty(),
Geometry::Polygon(g) => g.is_empty(),
Geometry::MultiPoint(g) => g.is_empty(),
Geometry::MultiLineString(g) => g.is_empty(),
Geometry::MultiPolygon(g) => g.is_empty(),
Geometry::GeometryCollection(g) => g.is_empty(),
Geometry::Rect(g) => g.is_empty(),
Geometry::Triangle(g) => g.is_empty(),
}
}

fn dimensions(&self) -> Dimensions {
match self {
Geometry::Point(g) => g.dimensions(),
Geometry::Line(g) => g.dimensions(),
Geometry::LineString(g) => g.dimensions(),
Geometry::Polygon(g) => g.dimensions(),
Geometry::MultiPoint(g) => g.dimensions(),
Geometry::MultiLineString(g) => g.dimensions(),
Geometry::MultiPolygon(g) => g.dimensions(),
Geometry::GeometryCollection(g) => g.dimensions(),
Geometry::Rect(g) => g.dimensions(),
Geometry::Triangle(g) => g.dimensions(),
}
}

fn boundary_dimensions(&self) -> Dimensions {
match self {
Geometry::Point(g) => g.boundary_dimensions(),
Geometry::Line(g) => g.boundary_dimensions(),
Geometry::LineString(g) => g.boundary_dimensions(),
Geometry::Polygon(g) => g.boundary_dimensions(),
Geometry::MultiPoint(g) => g.boundary_dimensions(),
Geometry::MultiLineString(g) => g.boundary_dimensions(),
Geometry::MultiPolygon(g) => g.boundary_dimensions(),
Geometry::GeometryCollection(g) => g.boundary_dimensions(),
Geometry::Rect(g) => g.boundary_dimensions(),
Geometry::Triangle(g) => g.boundary_dimensions(),
}
crate::geometry_enum_impl! {
fn is_empty(&self) -> bool;
fn dimensions(&self) -> Dimensions;
fn boundary_dimensions(&self) -> Dimensions;
}
}

Expand Down
15 changes: 2 additions & 13 deletions geo/src/algorithm/intersects/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,8 @@ where
Polygon<T>: Intersects<G>,
MultiPolygon<T>: Intersects<G>,
{
fn intersects(&self, rhs: &G) -> bool {
match self {
Geometry::Point(geom) => geom.intersects(rhs),
Geometry::MultiPoint(geom) => geom.intersects(rhs),
Geometry::Line(geom) => geom.intersects(rhs),
Geometry::LineString(geom) => geom.intersects(rhs),
Geometry::MultiLineString(geom) => geom.intersects(rhs),
Geometry::Triangle(geom) => geom.intersects(rhs),
Geometry::Rect(geom) => geom.intersects(rhs),
Geometry::Polygon(geom) => geom.intersects(rhs),
Geometry::MultiPolygon(geom) => geom.intersects(rhs),
Geometry::GeometryCollection(geom) => geom.intersects(rhs),
}
geometry_enum_impl! {
fn intersects(&self, rhs: &G) -> bool;
}
}
symmetric_intersects_impl!(Coordinate<T>, Geometry<T>);
Expand Down
28 changes: 28 additions & 0 deletions geo/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,31 @@ impl<F: GeoFloat> Closest<F> {
}
}
}

#[macro_export]
macro_rules! geometry_enum_impl {
(
$(
$(#[$outer:meta])*
fn $func_name: ident(&$($self_life:lifetime)?self $(, $arg_name: ident: $arg_type: ty)*) -> $return: ty;
)+
) => {
$(
$(#[$outer])*
fn $func_name(&$($self_life)? self, $($arg_name: $arg_type),*) -> $return {
match self {
Geometry::Point(g) => g.$func_name($($arg_name),*).into(),
Geometry::Line(g) => g.$func_name($($arg_name),*).into(),
Geometry::LineString(g) => g.$func_name($($arg_name),*).into(),
Geometry::Polygon(g) => g.$func_name($($arg_name),*).into(),
Geometry::MultiPoint(g) => g.$func_name($($arg_name),*).into(),
Geometry::MultiLineString(g) => g.$func_name($($arg_name),*).into(),
Geometry::MultiPolygon(g) => g.$func_name($($arg_name),*).into(),
Geometry::GeometryCollection(g) => g.$func_name($($arg_name),*).into(),
Geometry::Rect(g) => g.$func_name($($arg_name),*).into(),
Geometry::Triangle(g) => g.$func_name($($arg_name),*).into(),
}
}
)+
};
}

0 comments on commit 56c6a6a

Please sign in to comment.