Skip to content

Commit

Permalink
Derive common traits for structs (Maximkaaa#67)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: Maxim <[email protected]>
  • Loading branch information
crumplecup and Maximkaaa authored Apr 27, 2024
1 parent bb2206b commit 7b0e358
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 16 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Cargo.lock
# Ignore large sample files
**/*.laz

#Npm
# Npm
**/package-lock.json
**/node_modules
**/dist
**/dist

# ctags
tags
3 changes: 2 additions & 1 deletion galileo-types/src/cartesian/orient.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::cartesian::CartesianPoint2d;
use serde::{Deserialize, Serialize};

/// Orientation of a triplet of points.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Orientation {
/// Clockwise
Clockwise,
Expand Down
2 changes: 1 addition & 1 deletion galileo-types/src/cartesian/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use std::ops::Deref;

/// Rectangle in 2d cartesian coordinate space.
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Hash, Deserialize, Serialize)]
pub struct Rect<N = f64> {
x_min: N,
y_min: N,
Expand Down
3 changes: 2 additions & 1 deletion galileo-types/src/cartesian/size.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use num_traits::{FromPrimitive, NumCast};
use serde::{Deserialize, Serialize};

/// Generic size type. Size is not guaranteed to be non-negative.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Hash, Deserialize, Serialize)]
pub struct Size<Num: num_traits::Num + PartialOrd + Copy + PartialEq = f64> {
width: Num,
height: Num,
Expand Down
3 changes: 2 additions & 1 deletion galileo-types/src/cartesian/traits/contour.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::cartesian::traits::cartesian_point::CartesianPoint2d;
use crate::contour::{ClosedContour, Contour};
use num_traits::{One, Zero};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt::Debug;

Expand Down Expand Up @@ -63,7 +64,7 @@ where
}

/// [Winding](https://en.wikipedia.org/wiki/Winding_number) direction of the contour.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Hash, Deserialize, Serialize)]
pub enum Winding {
/// Positive winding.
Clockwise,
Expand Down
2 changes: 2 additions & 0 deletions galileo-types/src/contour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl<P, T: ClosedContour<Point = P>> Contour for T {
}

/// Iterator of contour points.
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct ContourPointsIterator<'a, P, Iter>
where
Iter: Iterator<Item = &'a P>,
Expand Down Expand Up @@ -146,6 +147,7 @@ where
}

/// Iterator of contour segements.
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct ContourSegmentIterator<'a, P: 'a, Iter>
where
Iter: Iterator<Item = &'a P>,
Expand Down
4 changes: 2 additions & 2 deletions galileo-types/src/geo/crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::geo::traits::projection::Projection;
use serde::{Deserialize, Serialize};

/// Coordinate reference system.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Crs {
datum: Datum,
projection_type: ProjectionType,
}

/// Method used for projecting coordinates.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ProjectionType {
/// Some method.
Expand Down
2 changes: 1 addition & 1 deletion galileo-types/src/geo/datum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

/// Reference ellipsoid used to do calculations with geographic coordinates.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Datum {
semimajor: f64,
inv_flattening: f64,
Expand Down
3 changes: 2 additions & 1 deletion galileo-types/src/geo/impls/point.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::geo::traits::point::{GeoPoint, NewGeoPoint};
use crate::geo::traits::projection::Projection;
use crate::geometry::{Geom, Geometry};
use serde::{Deserialize, Serialize};

/// 2d point on the surface of a celestial body.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct GeoPoint2d {
lat: f64,
lon: f64,
Expand Down
2 changes: 2 additions & 0 deletions galileo-types/src/geo/impls/projection/dimensions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::cartesian::{NewCartesianPoint2d, NewCartesianPoint3d};
use crate::geo::traits::projection::Projection;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

/// Projection that adds a default z-value to a 2d point. Reversed projecting drops the z-value.
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Hash, Deserialize, Serialize)]
pub struct AddDimensionProjection<Num, In, Out> {
z: Num,
phantom_in: PhantomData<In>,
Expand Down
1 change: 1 addition & 0 deletions galileo-types/src/geo/impls/projection/geodesy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use geodesy::prelude::*;
use std::marker::PhantomData;

/// A projection constructed by `geodesy` crate.
#[derive(Debug, Default)]
pub struct GeodesyProjection<In, Out> {
context: Minimal,
op: OpHandle,
Expand Down
3 changes: 2 additions & 1 deletion galileo-types/src/geo/impls/projection/web_mercator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use crate::cartesian::NewCartesianPoint2d;
use crate::geo::datum::Datum;
use crate::geo::traits::point::NewGeoPoint;
use crate::geo::traits::projection::Projection;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

/// Web Mercator projection.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct WebMercator<In, Out> {
datum: Datum,
phantom_in: PhantomData<In>,
Expand Down
4 changes: 4 additions & 0 deletions galileo-types/src/geojson/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use crate::error::GalileoTypesError;
use crate::geo::{GeoPoint, NewGeoPoint};
use crate::geometry_type::{GeoSpace2d, GeometryType, PointGeometryType};
use geojson::Position;
use serde::{Deserialize, Serialize};

#[derive(
Debug, Default, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct GeoJsonPoint(Position);

impl TryFrom<Position> for GeoJsonPoint {
Expand Down
2 changes: 2 additions & 0 deletions galileo-types/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use crate::cartesian::{CartesianPoint2d, Rect};
use crate::geo::Projection;
use crate::geometry_type::{CartesianSpace2d, GeometryType, PointGeometryType};
use crate::impls::{Contour, MultiContour, MultiPoint, MultiPolygon, Polygon};
use serde::{Deserialize, Serialize};

/// Enum of different geometry types. This enum implements the [`Geometry`] trait so you can use any generic geometry
/// method without knowing a specific geometry type you are working with.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize)]
pub enum Geom<P> {
/// Point geometry.
Point(P),
Expand Down
31 changes: 31 additions & 0 deletions galileo-types/src/geometry_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! See documentation for [`GeometryType`] trait.
use serde::{Deserialize, Serialize};

/// This trait allows automatically implement [`Geometry`](crate::Geometry) trait for types that implement specific
/// geometry traits (e.g. [`Polygon`](crate::Polygon) etc).
Expand All @@ -25,31 +26,61 @@ pub trait GeometryType {
}

/// Point geometry marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct PointGeometryType;

/// Multipoint geometry marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct MultiPointGeometryType;

/// Contour geometry marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct ContourGeometryType;

/// MultiContour geometry marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct MultiContourGeometryType;

/// Polygon geometry marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct PolygonGeometryType;

/// MultiPolygon geometry marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct MultiPolygonGeometryType;

/// Geographic coordinate space marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct GeoSpace2d;

/// 2d cartesian coordinate space marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct CartesianSpace2d;

/// 3d cartesian coordinate space marker.
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct CartesianSpace3d;

/// See [`Disambiguate`](super::disambig::Disambiguate).
#[derive(
Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize,
)]
pub struct AmbiguousSpace;
4 changes: 2 additions & 2 deletions galileo-types/src/impls/contour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::geometry_type::{ContourGeometryType, GeometryType};
use serde::{Deserialize, Serialize};

/// Simple [`crate::Contour`] implementation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize)]
pub struct Contour<Point> {
points: Vec<Point>,
is_closed: bool,
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<Point> Contour<Point> {
}

/// Closed contour implementation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize)]
pub struct ClosedContour<Point> {
/// Points of the contour.
pub points: Vec<Point>,
Expand Down
2 changes: 2 additions & 0 deletions galileo-types/src/impls/multi_contour.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::geometry_type::{GeometryType, MultiContourGeometryType};
use crate::impls::contour::Contour;
use serde::{Deserialize, Serialize};

/// A set of contours.
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize)]
pub struct MultiContour<P>(Vec<Contour<P>>);

impl<P> crate::multi_contour::MultiContour for MultiContour<P> {
Expand Down
2 changes: 2 additions & 0 deletions galileo-types/src/impls/multi_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::geometry_type::{GeometryType, MultiPointGeometryType};
use serde::{Deserialize, Serialize};

/// A set of points.
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize)]
pub struct MultiPoint<P>(Vec<P>);

impl<P> crate::multi_point::MultiPoint for MultiPoint<P> {
Expand Down
2 changes: 1 addition & 1 deletion galileo-types/src/impls/multi_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::impls::polygon::Polygon;
use serde::{Deserialize, Serialize};

/// A set of polygons.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize)]
pub struct MultiPolygon<P> {
/// Inner polygons.
pub parts: Vec<Polygon<P>>,
Expand Down
2 changes: 1 addition & 1 deletion galileo-types/src/impls/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::impls::contour::ClosedContour;
use serde::{Deserialize, Serialize};

/// Simple implementation of the [`Polygon`](crate::Polygon) trait.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize, Serialize)]
pub struct Polygon<P> {
/// Outer contour.
pub outer_contour: ClosedContour<P>,
Expand Down
2 changes: 1 addition & 1 deletion galileo-types/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::cartesian::{CartesianPoint2d, Orientation};
use num_traits::{One, Zero};

/// A strait line segment between two points.
#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct Segment<'a, Point>(pub &'a Point, pub &'a Point);

impl<'a, P: CartesianPoint2d> Segment<'a, P> {
Expand Down

0 comments on commit 7b0e358

Please sign in to comment.