Skip to content

Commit

Permalink
feat: Modify the Polygon::transform method signature to improve tra…
Browse files Browse the repository at this point in the history
…nsformation handling of polygons. (#5)
miseyu authored Apr 26, 2024
1 parent f0b8fbb commit 480e86c
Showing 3 changed files with 68 additions and 6 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -27,7 +27,6 @@
## フォルダ構成

- アプリケーション:
- [`nusamai`](./nusamai/) — アプリケーションバックエンドおよびコマンドライン版の実装
- 基盤・ユーティリティ:
- [`nusamai-geometry`](./nusamai-geometry/) — ジオメトリ型
- [`nusamai-projection`](./nusamai-projection/) — 投影法変換
32 changes: 30 additions & 2 deletions nusamai-geometry/src/compact/linestring.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{Coord, Coord2d};
use std::borrow::Cow;
use std::{borrow::Cow, hash::Hash};

/// Computer-friendly LineString
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Default, PartialEq)]
#[derive(Debug, Clone, Default)]
pub struct LineString<'a, T: Coord> {
/// Coordinates of all points
///
@@ -14,6 +14,34 @@ pub struct LineString<'a, T: Coord> {
pub type LineString2<'a, C = f64> = LineString<'a, [C; 2]>;
pub type LineString3<'a, C = f64> = LineString<'a, [C; 3]>;

impl PartialEq for LineString2<'_, f64> {
fn eq(&self, other: &Self) -> bool {
self.coords == other.coords
}
}

impl PartialEq for LineString3<'_, f64> {
fn eq(&self, other: &Self) -> bool {
self.coords == other.coords
}
}

impl Hash for LineString2<'_, f64> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.coords
.iter()
.for_each(|c| c.iter().for_each(|a| a.to_bits().hash(state)));
}
}

impl Hash for LineString3<'_, f64> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.coords
.iter()
.for_each(|c| c.iter().for_each(|a| a.to_bits().hash(state)));
}
}

impl<'a, T: Coord> LineString<'a, T> {
/// Creates an empty LineString.
pub fn new() -> Self {
41 changes: 38 additions & 3 deletions nusamai-geometry/src/compact/polygon.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::borrow::Cow;
use std::{borrow::Cow, hash::Hash};

use crate::Coord2d;

use super::{linestring::LineString, Coord};

/// Computer-friendly Polygon
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Default, PartialEq)]
#[derive(Debug, Clone, Default)]
pub struct Polygon<'a, T: Coord> {
/// Coordinates
coords: Cow<'a, [T]>,
@@ -18,6 +18,41 @@ pub struct Polygon<'a, T: Coord> {
pub type Polygon3<'a, C = f64> = Polygon<'a, [C; 3]>;
pub type Polygon2<'a, C = f64> = Polygon<'a, [C; 2]>;

impl PartialEq for Polygon3<'_, f64> {
fn eq(&self, other: &Self) -> bool {
self.exterior() == other.exterior()
&& self.interiors().zip(other.interiors()).all(|(a, b)| a == b)
}
}

impl PartialEq for Polygon2<'_, f64> {
fn eq(&self, other: &Self) -> bool {
self.exterior() == other.exterior()
&& self.interiors().zip(other.interiors()).all(|(a, b)| a == b)
}
}

impl Hash for Polygon2<'_, f64> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.exterior().hash(state);
for interior in self.interiors() {
interior.hash(state);
}
}
}

impl Hash for Polygon3<'_, f64> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.exterior().hash(state);
for interior in self.interiors() {
interior.hash(state);
}
}
}

impl Eq for Polygon3<'_, f64> {}
impl Eq for Polygon2<'_, f64> {}

impl<'a, T: Coord> Polygon<'a, T> {
/// Creates an empty Polygon.
pub fn new() -> Self {
@@ -98,7 +133,7 @@ impl<'a, T: Coord> Polygon<'a, T> {
}

/// Create a new Polygon by applying the given transformation to all coordinates.
pub fn transform<T2: Coord>(&self, f: impl Fn(&T) -> T2) -> Polygon<T2> {
pub fn transform<T2: Coord>(self, f: impl Fn(&T) -> T2) -> Polygon<'a, T2> {
Polygon {
coords: self.coords.iter().map(f).collect(),
hole_indices: self.hole_indices.clone(),

0 comments on commit 480e86c

Please sign in to comment.