From 8f3f489cf0e6113b30b3499d72a018f0a22de1c6 Mon Sep 17 00:00:00 2001 From: Balthasar Teuscher Date: Thu, 2 Jan 2025 16:54:24 +0100 Subject: [PATCH] Implement `RTreeObject` for `Triangle` --- geo-types/CHANGES.md | 2 ++ geo-types/src/geometry/line.rs | 3 +-- geo-types/src/geometry/triangle.rs | 41 +++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/geo-types/CHANGES.md b/geo-types/CHANGES.md index 3937d89e2f..e7f83ac55a 100644 --- a/geo-types/CHANGES.md +++ b/geo-types/CHANGES.md @@ -2,6 +2,8 @@ ## Unreleased +- Implement `RTreeObject` for `Triangle`. + ## 0.7.14 - POSSIBLY BREAKING: Minimum supported version of Rust (MSRV) is now 1.75 diff --git a/geo-types/src/geometry/line.rs b/geo-types/src/geometry/line.rs index d9435ee342..9cbed404fa 100644 --- a/geo-types/src/geometry/line.rs +++ b/geo-types/src/geometry/line.rs @@ -237,8 +237,7 @@ macro_rules! impl_rstar_line { type Envelope = ::$rstar::AABB>; fn envelope(&self) -> Self::Envelope { - let bounding_rect = crate::private_utils::line_bounding_rect(*self); - ::$rstar::AABB::from_corners(bounding_rect.min().into(), bounding_rect.max().into()) + ::$rstar::AABB::from_corners(self.start_point(), self.end_point()) } } diff --git a/geo-types/src/geometry/triangle.rs b/geo-types/src/geometry/triangle.rs index 831e57cb1a..504919438a 100644 --- a/geo-types/src/geometry/triangle.rs +++ b/geo-types/src/geometry/triangle.rs @@ -1,4 +1,4 @@ -use crate::{polygon, Coord, CoordNum, Line, Polygon}; +use crate::{polygon, Coord, CoordNum, Line, Point, Polygon}; #[cfg(any(feature = "approx", test))] use approx::{AbsDiffEq, RelativeEq}; @@ -148,3 +148,42 @@ where true } } + +#[cfg(any( + feature = "rstar_0_8", + feature = "rstar_0_9", + feature = "rstar_0_10", + feature = "rstar_0_11", + feature = "rstar_0_12" +))] +macro_rules! impl_rstar_triangle { + ($rstar:ident) => { + impl ::$rstar::RTreeObject for Triangle + where + T: ::num_traits::Float + ::$rstar::RTreeNum, + { + type Envelope = ::$rstar::AABB>; + + fn envelope(&self) -> Self::Envelope { + let bounding_rect = + crate::private_utils::get_bounding_rect(self.to_array().into_iter()).unwrap(); + ::$rstar::AABB::from_corners(bounding_rect.min().into(), bounding_rect.max().into()) + } + } + }; +} + +#[cfg(feature = "rstar_0_8")] +impl_rstar_triangle!(rstar_0_8); + +#[cfg(feature = "rstar_0_9")] +impl_rstar_triangle!(rstar_0_9); + +#[cfg(feature = "rstar_0_10")] +impl_rstar_triangle!(rstar_0_10); + +#[cfg(feature = "rstar_0_11")] +impl_rstar_triangle!(rstar_0_11); + +#[cfg(feature = "rstar_0_12")] +impl_rstar_triangle!(rstar_0_12);