Skip to content

Commit

Permalink
Traitification of geometries and support geo-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximkaaa authored Jan 12, 2024
1 parent b972a4a commit 3e8d73c
Show file tree
Hide file tree
Showing 44 changed files with 1,257 additions and 426 deletions.
6 changes: 5 additions & 1 deletion galileo-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ edition.workspace = true
authors.workspace = true
repository.workspace = true

[features]
default = ["geo-types"]

[dependencies]
num-traits = "0.2.17"
nalgebra = "0.32"
serde = { version = "1.0", features = ["derive"] }
geodesy = "0.11.1"
geodesy = "0.11.1"
geo-types = { version = "0.7", optional = true }
57 changes: 15 additions & 42 deletions galileo-types/src/cartesian/impls/contour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::cartesian::rect::Rect;
use crate::cartesian::traits::cartesian_point::CartesianPoint2d;
use crate::cartesian::traits::contour::CartesianContour;
use crate::geo::traits::projection::Projection;
use crate::geometry::{CartesianGeometry2d, Geom, Geometry};
use crate::geometry::CartesianGeometry2d;
use crate::geometry_type::{ContourGeometryType, GeometryType};
use num_traits::Float;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -89,11 +90,11 @@ impl<P> From<ClosedContour<P>> for Contour<P> {
}
}

impl<P> crate::cartesian::traits::contour::ClosedContour for ClosedContour<P> {
impl<P> crate::contour::ClosedContour for ClosedContour<P> {
type Point = P;

fn iter_points(&self) -> Box<dyn Iterator<Item = &'_ P> + '_> {
Box::new(self.points.iter())
fn iter_points(&self) -> impl Iterator<Item = &'_ P> {
self.points.iter()
}
}

Expand All @@ -103,15 +104,15 @@ impl<P> crate::cartesian::traits::contour::ClosedContour for ClosedContour<P> {
// }
// }

impl<P> crate::cartesian::traits::contour::Contour for Contour<P> {
impl<P> crate::contour::Contour for Contour<P> {
type Point = P;

fn is_closed(&self) -> bool {
self.is_closed
}

fn iter_points(&self) -> Box<dyn Iterator<Item = &P> + '_> {
Box::new(self.points.iter())
fn iter_points(&self) -> impl Iterator<Item = &P> {
self.points.iter()
}
}

Expand All @@ -120,45 +121,17 @@ impl<P> crate::cartesian::traits::contour::Contour for Contour<P> {
// {
// }

impl<P> Geometry for ClosedContour<P> {
type Point = P;

fn project<Proj: Projection<InPoint = Self::Point> + ?Sized>(
&self,
projection: &Proj,
) -> Option<Geom<Proj::OutPoint>> {
let points = self
.points
.iter()
.map(|p| projection.project(p))
.collect::<Option<Vec<Proj::OutPoint>>>()?;
Some(Geom::Line(Contour {
points,
is_closed: true,
}))
}
impl<P: GeometryType> GeometryType for Contour<P> {
type Type = ContourGeometryType;
type Space = P::Space;
}

impl<P> Geometry for Contour<P> {
type Point = P;

fn project<Proj: Projection<InPoint = Self::Point> + ?Sized>(
&self,
projection: &Proj,
) -> Option<Geom<Proj::OutPoint>> {
let points = self
.points
.iter()
.map(|p| projection.project(p))
.collect::<Option<Vec<Proj::OutPoint>>>()?;
Some(Geom::Line(Contour {
points,
is_closed: true,
}))
}
impl<P: GeometryType> GeometryType for ClosedContour<P> {
type Type = ContourGeometryType;
type Space = P::Space;
}

impl<N, P> CartesianGeometry2d<P> for Contour<P>
impl<N, P: GeometryType> CartesianGeometry2d<P> for Contour<P>
where
N: Float,
P: CartesianPoint2d<Num = N>,
Expand Down
33 changes: 12 additions & 21 deletions galileo-types/src/cartesian/impls/multipolygon.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cartesian::impls::polygon::Polygon;
use crate::cartesian::rect::Rect;
use crate::cartesian::traits::cartesian_point::CartesianPoint2d;
use crate::geo::traits::projection::Projection;
use crate::geometry::{CartesianGeometry2d, Geom, Geometry};
use crate::geometry::CartesianGeometry2d;
use crate::geometry_type::{GeometryType, MultiPolygonGeometryType};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -22,29 +22,20 @@ impl<P> MultiPolygon<P> {
}
}

impl<P> Geometry for MultiPolygon<P> {
type Point = P;
impl<P> crate::multi_polygon::MultiPolygon for MultiPolygon<P> {
type Polygon = Polygon<P>;

fn project<Proj: Projection<InPoint = Self::Point> + ?Sized>(
&self,
projection: &Proj,
) -> Option<Geom<Proj::OutPoint>> {
Some(Geom::MultiPolygon(MultiPolygon {
parts: self
.parts
.iter()
.map(|p| {
p.project(projection).and_then(|p| match p {
Geom::Polygon(v) => Some(v),
_ => None,
})
})
.collect::<Option<Vec<Polygon<Proj::OutPoint>>>>()?,
}))
fn polygons(&self) -> impl Iterator<Item = &Self::Polygon> {
self.parts.iter()
}
}

impl<P> CartesianGeometry2d<P> for MultiPolygon<P>
impl<P: GeometryType> GeometryType for MultiPolygon<P> {
type Type = MultiPolygonGeometryType;
type Space = P::Space;
}

impl<P: GeometryType> CartesianGeometry2d<P> for MultiPolygon<P>
where
P: CartesianPoint2d,
{
Expand Down
13 changes: 4 additions & 9 deletions galileo-types/src/cartesian/impls/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::cartesian::traits::cartesian_point::{
};
use crate::geo::traits::projection::Projection;
use crate::geometry::{Geom, Geometry};
use crate::geometry_type::{CartesianSpace2d, GeometryType, PointGeometryType};
use crate::point::{CartesianPointType, Point};
pub use nalgebra::Point2;
use nalgebra::{Point3, Scalar};
Expand Down Expand Up @@ -60,15 +61,9 @@ impl<Num: Scalar + Copy> NewCartesianPoint3d<Num> for Point3<Num> {
}
}

impl<Num: Scalar> Geometry for Point2<Num> {
type Point = Point2<Num>;

fn project<P: Projection<InPoint = Self::Point> + ?Sized>(
&self,
projection: &P,
) -> Option<Geom<P::OutPoint>> {
Some(Geom::Point(projection.project(self)?))
}
impl<Num: Scalar> GeometryType for Point2<Num> {
type Type = PointGeometryType;
type Space = CartesianSpace2d;
}

impl<Num: Scalar> Geometry for Point3<Num> {
Expand Down
38 changes: 8 additions & 30 deletions galileo-types/src/cartesian/impls/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::cartesian::impls::contour::ClosedContour;
use crate::cartesian::rect::Rect;
use crate::cartesian::traits::cartesian_point::CartesianPoint2d;
use crate::cartesian::traits::polygon::CartesianPolygon;
use crate::geo::traits::projection::Projection;
use crate::geometry::{CartesianGeometry2d, Geom, Geometry};
use crate::geometry::CartesianGeometry2d;
use crate::geometry_type::{GeometryType, PolygonGeometryType};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -31,14 +31,14 @@ impl<P> Polygon<P> {
}
}

impl<P> crate::cartesian::traits::polygon::Polygon for Polygon<P> {
impl<P> crate::polygon::Polygon for Polygon<P> {
type Contour = ClosedContour<P>;

fn outer_contour(&self) -> &Self::Contour {
&self.outer_contour
}

fn inner_contours(&self) -> Box<dyn Iterator<Item = &'_ Self::Contour> + '_> {
fn inner_contours(&self) -> impl Iterator<Item = &'_ Self::Contour> {
Box::new(self.inner_contours.iter())
}
}
Expand All @@ -52,34 +52,12 @@ impl<P> From<ClosedContour<P>> for Polygon<P> {
}
}

impl<P> Geometry for Polygon<P> {
type Point = P;

fn project<Proj: Projection<InPoint = Self::Point> + ?Sized>(
&self,
projection: &Proj,
) -> Option<Geom<Proj::OutPoint>> {
let Geom::Line(outer_contour) = self.outer_contour.project(projection)? else {
return None;
};
let inner_contours = self
.inner_contours
.iter()
.map(|c| {
c.project(projection).and_then(|c| match c {
Geom::Line(contour) => contour.into_closed(),
_ => None,
})
})
.collect::<Option<Vec<ClosedContour<Proj::OutPoint>>>>()?;
Some(Geom::Polygon(Polygon {
outer_contour: outer_contour.into_closed()?,
inner_contours,
}))
}
impl<P: GeometryType> GeometryType for Polygon<P> {
type Type = PolygonGeometryType;
type Space = P::Space;
}

impl<P> CartesianGeometry2d<P> for Polygon<P>
impl<P: GeometryType> CartesianGeometry2d<P> for Polygon<P>
where
P: CartesianPoint2d,
{
Expand Down
17 changes: 17 additions & 0 deletions galileo-types/src/cartesian/traits/cartesian_point.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::geo::traits::projection::Projection;
use crate::geometry::{Geom, GeometrySpecialization};
use crate::geometry_type::{CartesianSpace2d, GeometryType, PointGeometryType};
use crate::point::{CartesianPointType, Point, PointHelper};
use nalgebra::{Point2, Scalar, Vector2};
use num_traits::{Bounded, Float, FromPrimitive, Num};
Expand Down Expand Up @@ -75,3 +78,17 @@ pub trait CartesianPoint2dFloat<N: Float = f64>: CartesianPoint2d<Num = N> {
}

impl<N: Float, T: CartesianPoint2d<Num = N>> CartesianPoint2dFloat<N> for T {}

impl<P> GeometrySpecialization<PointGeometryType, CartesianSpace2d> for P
where
P: CartesianPoint2d + GeometryType<Type = PointGeometryType, Space = CartesianSpace2d>,
{
type Point = P;

fn project<Proj>(&self, projection: &Proj) -> Option<Geom<Proj::OutPoint>>
where
Proj: Projection<InPoint = Self::Point> + ?Sized,
{
Some(Geom::Point(projection.project(self)?))
}
}
Loading

0 comments on commit 3e8d73c

Please sign in to comment.