1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
use crate::algorithm::geo::{AffineOps, Center, Centroid};
use crate::array::MultiPointArray;
use crate::array::*;
use arrow_array::{Float64Array, OffsetSizeTrait};
use geo::AffineTransform;
/// Rotate geometries around a point by an angle, in degrees.
///
/// Positive angles are counter-clockwise, and negative angles are clockwise rotations.
///
/// ## Performance
///
/// If you will be performing multiple transformations, like
/// [`Scale`](crate::algorithm::geo::Scale), [`Skew`](crate::algorithm::geo::Skew),
/// [`Translate`](crate::algorithm::geo::Translate), or [`Rotate`](crate::algorithm::geo::Rotate),
/// it is more efficient to compose the transformations and apply them as a single operation using
/// the [`AffineOps`](crate::algorithm::geo::AffineOps) trait.
pub trait Rotate<DegreesT> {
/// Rotate a geometry around its [centroid](Centroid) by an angle, in degrees
///
/// Positive angles are counter-clockwise, and negative angles are clockwise rotations.
///
/// # Examples
///
/// ```
/// use geo::Rotate;
/// use geo::line_string;
/// use approx::assert_relative_eq;
///
/// let line_string = line_string![
/// (x: 0.0, y: 0.0),
/// (x: 5.0, y: 5.0),
/// (x: 10.0, y: 10.0),
/// ];
///
/// let rotated = line_string.rotate_around_centroid(-45.0);
///
/// let expected = line_string![
/// (x: -2.071067811865475, y: 5.0),
/// (x: 5.0, y: 5.0),
/// (x: 12.071067811865476, y: 5.0),
/// ];
///
/// assert_relative_eq!(expected, rotated);
/// ```
#[must_use]
fn rotate_around_centroid(&self, degrees: &DegreesT) -> Self;
// /// Mutable version of [`Self::rotate_around_centroid`]
// fn rotate_around_centroid_mut(&mut self, degrees: f64);
/// Rotate a geometry around the center of its [bounding
/// box](crate::algorithm::geo::BoundingRect) by an angle, in degrees.
///
/// Positive angles are counter-clockwise, and negative angles are clockwise rotations.
///
#[must_use]
fn rotate_around_center(&self, degrees: &DegreesT) -> Self;
// /// Mutable version of [`Self::rotate_around_center`]
// fn rotate_around_center_mut(&mut self, degrees: f64);
/// Rotate a Geometry around an arbitrary point by an angle, given in degrees
///
/// Positive angles are counter-clockwise, and negative angles are clockwise rotations.
///
/// # Examples
///
/// ```
/// use geo::Rotate;
/// use geo::{line_string, point};
///
/// let ls = line_string![
/// (x: 0.0, y: 0.0),
/// (x: 5.0, y: 5.0),
/// (x: 10.0, y: 10.0)
/// ];
///
/// let rotated = ls.rotate_around_point(
/// -45.0,
/// point!(x: 10.0, y: 0.0),
/// );
///
/// assert_eq!(rotated, line_string![
/// (x: 2.9289321881345245, y: 7.071067811865475),
/// (x: 10.0, y: 7.0710678118654755),
/// (x: 17.071067811865476, y: 7.0710678118654755)
/// ]);
/// ```
#[must_use]
fn rotate_around_point(&self, degrees: &DegreesT, point: geo::Point) -> Self;
// /// Mutable version of [`Self::rotate_around_point`]
// fn rotate_around_point_mut(&mut self, degrees: f64, point: Point<f64>);
}
// ┌────────────────────────────────┐
// │ Implementations for RHS arrays │
// └────────────────────────────────┘
// Note: this can't (easily) be parameterized in the macro because PointArray is not generic over O
impl Rotate<Float64Array> for PointArray {
fn rotate_around_centroid(&self, degrees: &Float64Array) -> Self {
let centroids = self.centroid();
let transforms: Vec<AffineTransform> = centroids
.iter_geo_values()
.zip(degrees.values().iter())
.map(|(point, angle)| AffineTransform::rotate(*angle, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_center(&self, degrees: &Float64Array) -> Self {
let centers = self.center();
let transforms: Vec<AffineTransform> = centers
.iter_geo_values()
.zip(degrees.values().iter())
.map(|(point, angle)| AffineTransform::rotate(*angle, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_point(&self, degrees: &Float64Array, point: geo::Point) -> Self {
let transforms: Vec<AffineTransform> = degrees
.values()
.iter()
.map(|degrees| AffineTransform::rotate(*degrees, point))
.collect();
self.affine_transform(&transforms)
}
}
/// Implementation that iterates over geo objects
macro_rules! iter_geo_impl {
($type:ty) => {
impl<O: OffsetSizeTrait> Rotate<Float64Array> for $type {
fn rotate_around_centroid(&self, degrees: &Float64Array) -> $type {
let centroids = self.centroid();
let transforms: Vec<AffineTransform> = centroids
.iter_geo_values()
.zip(degrees.values().iter())
.map(|(point, angle)| AffineTransform::rotate(*angle, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_center(&self, degrees: &Float64Array) -> Self {
let centers = self.center();
let transforms: Vec<AffineTransform> = centers
.iter_geo_values()
.zip(degrees.values().iter())
.map(|(point, angle)| AffineTransform::rotate(*angle, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_point(&self, degrees: &Float64Array, point: geo::Point) -> Self {
let transforms: Vec<AffineTransform> = degrees
.values()
.iter()
.map(|degrees| AffineTransform::rotate(*degrees, point))
.collect();
self.affine_transform(&transforms)
}
}
};
}
iter_geo_impl!(LineStringArray<O>);
iter_geo_impl!(PolygonArray<O>);
iter_geo_impl!(MultiPointArray<O>);
iter_geo_impl!(MultiLineStringArray<O>);
iter_geo_impl!(MultiPolygonArray<O>);
iter_geo_impl!(WKBArray<O>);
impl<O: OffsetSizeTrait> Rotate<Float64Array> for GeometryArray<O> {
crate::geometry_array_delegate_impl! {
fn rotate_around_centroid(&self, degrees: &Float64Array) -> Self;
fn rotate_around_center(&self, degrees: &Float64Array) -> Self;
fn rotate_around_point(&self, degrees: &Float64Array, point: geo::Point) -> Self;
}
}
// ┌─────────────────────────────────┐
// │ Implementations for RHS scalars │
// └─────────────────────────────────┘
// Note: this can't (easily) be parameterized in the macro because PointArray is not generic over O
impl Rotate<f64> for PointArray {
fn rotate_around_centroid(&self, degrees: &f64) -> Self {
let centroids = self.centroid();
let transforms: Vec<AffineTransform> = centroids
.iter_geo_values()
.map(|point| AffineTransform::rotate(*degrees, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_center(&self, degrees: &f64) -> Self {
let centers = self.center();
let transforms: Vec<AffineTransform> = centers
.iter_geo_values()
.map(|point| AffineTransform::rotate(*degrees, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_point(&self, degrees: &f64, point: geo::Point) -> Self {
let transform = AffineTransform::rotate(*degrees, point);
self.affine_transform(&transform)
}
}
/// Implementation that iterates over geo objects
macro_rules! iter_geo_impl_scalar {
($type:ty) => {
impl<O: OffsetSizeTrait> Rotate<f64> for $type {
fn rotate_around_centroid(&self, degrees: &f64) -> $type {
let centroids = self.centroid();
let transforms: Vec<AffineTransform> = centroids
.iter_geo_values()
.map(|point| AffineTransform::rotate(*degrees, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_center(&self, degrees: &f64) -> Self {
let centers = self.center();
let transforms: Vec<AffineTransform> = centers
.iter_geo_values()
.map(|point| AffineTransform::rotate(*degrees, point))
.collect();
self.affine_transform(&transforms)
}
fn rotate_around_point(&self, degrees: &f64, point: geo::Point) -> Self {
let transform = AffineTransform::rotate(*degrees, point);
self.affine_transform(&transform)
}
}
};
}
iter_geo_impl_scalar!(LineStringArray<O>);
iter_geo_impl_scalar!(PolygonArray<O>);
iter_geo_impl_scalar!(MultiPointArray<O>);
iter_geo_impl_scalar!(MultiLineStringArray<O>);
iter_geo_impl_scalar!(MultiPolygonArray<O>);
iter_geo_impl_scalar!(WKBArray<O>);
impl<O: OffsetSizeTrait> Rotate<f64> for GeometryArray<O> {
crate::geometry_array_delegate_impl! {
fn rotate_around_centroid(&self, degrees: &f64) -> Self;
fn rotate_around_center(&self, degrees: &f64) -> Self;
fn rotate_around_point(&self, degrees: &f64, point: geo::Point) -> Self;
}
}