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

Implement AsRef<Coord> for Point and Coord #1295

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions geo-types/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Implement `RTreeObject` for `Triangle`.
- Implement `AsRef<Coord>` for `Point` and `Coord`.

## 0.7.14

Expand Down
6 changes: 6 additions & 0 deletions geo-types/src/geometry/coord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,9 @@ where
}
}
}

impl<T: CoordNum> AsRef<Coord<T>> for Coord<T> {
fn as_ref(&self) -> &Coord<T> {
self
}
}
6 changes: 6 additions & 0 deletions geo-types/src/geometry/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,12 @@ where
}
}

impl<T: CoordNum> AsRef<Coord<T>> for Point<T> {
fn as_ref(&self) -> &Coord<T> {
&self.0
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion geo-types/src/geometry/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ macro_rules! impl_rstar_triangle {

fn envelope(&self) -> Self::Envelope {
let bounding_rect =
crate::private_utils::get_bounding_rect(self.to_array().into_iter()).unwrap();
crate::private_utils::get_bounding_rect(self.to_array()).unwrap();
::$rstar::AABB::from_corners(bounding_rect.min().into(), bounding_rect.max().into())
}
}
Expand Down
10 changes: 6 additions & 4 deletions geo-types/src/private_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn line_string_bounding_rect<T>(line_string: &LineString<T>) -> Option<Rect<
where
T: CoordNum,
{
get_bounding_rect(line_string.coords().cloned())
get_bounding_rect(&line_string.0)
}

pub fn line_bounding_rect<T>(line: Line<T>) -> Rect<T>
Expand All @@ -19,17 +19,19 @@ where
Rect::new(line.start, line.end)
}

pub fn get_bounding_rect<I, T>(collection: I) -> Option<Rect<T>>
pub fn get_bounding_rect<I, C, T>(collection: I) -> Option<Rect<T>>
where
T: CoordNum,
I: IntoIterator<Item = Coord<T>>,
C: AsRef<Coord<T>>,
I: IntoIterator<Item = C>,
{
let mut iter = collection.into_iter();
if let Some(pnt) = iter.next() {
let pnt = pnt.as_ref();
let mut xrange = (pnt.x, pnt.x);
let mut yrange = (pnt.y, pnt.y);
for pnt in iter {
let (px, py) = pnt.x_y();
let (px, py) = pnt.as_ref().x_y();
xrange = get_min_max(px, xrange.0, xrange.1);
yrange = get_min_max(py, yrange.0, yrange.1);
}
Expand Down
13 changes: 5 additions & 8 deletions geo/src/algorithm/bounding_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ where
///
/// Return the BoundingRect for a MultiPoint
fn bounding_rect(&self) -> Self::Output {
get_bounding_rect(self.0.iter().map(|p| p.0))
get_bounding_rect(&self.0)
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ where
///
/// Return the BoundingRect for a MultiLineString
fn bounding_rect(&self) -> Self::Output {
get_bounding_rect(self.iter().flat_map(|line| line.0.iter().cloned()))
get_bounding_rect(self.iter().flat_map(|line| &line.0))
}
}

Expand All @@ -116,7 +116,7 @@ where
/// Return the BoundingRect for a Polygon
fn bounding_rect(&self) -> Self::Output {
let line = self.exterior();
get_bounding_rect(line.0.iter().cloned())
get_bounding_rect(&line.0)
}
}

Expand All @@ -129,10 +129,7 @@ where
///
/// Return the BoundingRect for a MultiPolygon
fn bounding_rect(&self) -> Self::Output {
get_bounding_rect(
self.iter()
.flat_map(|poly| poly.exterior().0.iter().cloned()),
)
get_bounding_rect(self.iter().flat_map(|poly| &poly.exterior().0))
}
}

Expand All @@ -143,7 +140,7 @@ where
type Output = Rect<T>;

fn bounding_rect(&self) -> Self::Output {
get_bounding_rect(self.to_array().iter().cloned()).unwrap()
get_bounding_rect(self.to_array()).unwrap()
}
}

Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/monotone/mono_poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<T: GeoNum> MonoPoly<T> {
debug_assert_eq!(top.0.first(), bot.0.first());
debug_assert_eq!(top.0.last(), bot.0.last());
debug_assert_ne!(top.0.first(), top.0.last());
let bounds = get_bounding_rect(top.0.iter().chain(bot.0.iter()).cloned()).unwrap();
let bounds = get_bounding_rect(top.0.iter().chain(bot.0.iter())).unwrap();
Self { top, bot, bounds }
}

Expand Down
Loading