Skip to content

Commit

Permalink
Implement AsRef<Coord> for Point and Coord
Browse files Browse the repository at this point in the history
  • Loading branch information
b4l committed Jan 5, 2025
1 parent 161cbd0 commit 24acc12
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
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
3 changes: 2 additions & 1 deletion geo-types/src/geometry/triangle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{polygon, Coord, CoordNum, Line, Point, Polygon};
use crate::{polygon, Coord, CoordNum, Line, Point, Polygon};

#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq};
Expand Down Expand Up @@ -166,7 +167,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

0 comments on commit 24acc12

Please sign in to comment.