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
use crate::{AffineOps, AffineTransform, BoundingRect, Coord, CoordFloat, CoordNum, Rect};
/// An affine transformation which skews a geometry, sheared by angles along x and y dimensions.
///
/// ## Performance
///
/// If you will be performing multiple transformations, like [`Scale`](crate::Scale),
/// [`Skew`](crate::Skew), [`Translate`](crate::Translate), or [`Rotate`](crate::Rotate), it is more
/// efficient to compose the transformations and apply them as a single operation using the
/// [`AffineOps`](crate::AffineOps) trait.
///
pub trait Skew<T: CoordNum> {
/// An affine transformation which skews a geometry, sheared by a uniform angle along the x and
/// y dimensions.
///
/// # Examples
///
/// ```
/// use geo::Skew;
/// use geo::{Polygon, polygon};
///
/// let square: Polygon = polygon![
/// (x: 0., y: 0.),
/// (x: 10., y: 0.),
/// (x: 10., y: 10.),
/// (x: 0., y: 10.)
/// ];
///
/// let skewed = square.skew(30.);
///
/// let expected_output: Polygon = polygon![
/// (x: -2.89, y: -2.89),
/// (x: 7.11, y: 2.89),
/// (x: 12.89, y: 12.89),
/// (x: 2.89, y: 7.11)
/// ];
/// approx::assert_relative_eq!(skewed, expected_output, epsilon = 1e-2);
/// ```
#[must_use]
fn skew(&self, degrees: T) -> Self;
/// Mutable version of [`skew`](Self::skew).
fn skew_mut(&mut self, degrees: T);
/// An affine transformation which skews a geometry, sheared by an angle along the x and y dimensions.
///
/// # Examples
///
/// ```
/// use geo::Skew;
/// use geo::{Polygon, polygon};
///
/// let square: Polygon = polygon![
/// (x: 0., y: 0.),
/// (x: 10., y: 0.),
/// (x: 10., y: 10.),
/// (x: 0., y: 10.)
/// ];
///
/// let skewed = square.skew_xy(30., 12.);
///
/// let expected_output: Polygon = polygon![
/// (x: -2.89, y: -1.06),
/// (x: 7.11, y: 1.06),
/// (x: 12.89, y: 11.06),
/// (x: 2.89, y: 8.94)
/// ];
/// approx::assert_relative_eq!(skewed, expected_output, epsilon = 1e-2);
/// ```
#[must_use]
fn skew_xy(&self, degrees_x: T, degrees_y: T) -> Self;
/// Mutable version of [`skew_xy`](Self::skew_xy).
fn skew_xy_mut(&mut self, degrees_x: T, degrees_y: T);
/// An affine transformation which skews a geometry around a point of `origin`, sheared by an
/// angle along the x and y dimensions.
///
/// The point of origin is *usually* given as the 2D bounding box centre of the geometry, in
/// which case you can just use [`skew`](Self::skew) or [`skew_xy`](Self::skew_xy), but this method allows you
/// to specify any point.
///
/// # Examples
///
/// ```
/// use geo::Skew;
/// use geo::{Polygon, polygon, point};
///
/// let square: Polygon = polygon![
/// (x: 0., y: 0.),
/// (x: 10., y: 0.),
/// (x: 10., y: 10.),
/// (x: 0., y: 10.)
/// ];
///
/// let origin = point! { x: 2., y: 2. };
/// let skewed = square.skew_around_point(45.0, 10.0, origin);
///
/// let expected_output: Polygon = polygon![
/// (x: -2., y: -0.353),
/// (x: 8., y: 1.410),
/// (x: 18., y: 11.41),
/// (x: 8., y: 9.647)
/// ];
/// approx::assert_relative_eq!(skewed, expected_output, epsilon = 1e-2);
/// ```
#[must_use]
fn skew_around_point(&self, degrees_x: T, degrees_y: T, origin: impl Into<Coord<T>>) -> Self;
/// Mutable version of [`skew_around_point`](Self::skew_around_point).
fn skew_around_point_mut(&mut self, degrees_x: T, degrees_y: T, origin: impl Into<Coord<T>>);
}
impl<T, IR, G> Skew<T> for G
where
T: CoordFloat,
IR: Into<Option<Rect<T>>>,
G: Clone + AffineOps<T> + BoundingRect<T, Output = IR>,
{
fn skew(&self, degrees: T) -> Self {
self.skew_xy(degrees, degrees)
}
fn skew_mut(&mut self, degrees: T) {
self.skew_xy_mut(degrees, degrees);
}
fn skew_xy(&self, degrees_x: T, degrees_y: T) -> Self {
let origin = match self.bounding_rect().into() {
Some(rect) => rect.center(),
// Empty geometries have no bounding rect, but in that case
// transforming is a no-op anyway.
None => return self.clone(),
};
self.skew_around_point(degrees_x, degrees_y, origin)
}
fn skew_xy_mut(&mut self, degrees_x: T, degrees_y: T) {
let origin = match self.bounding_rect().into() {
Some(rect) => rect.center(),
// Empty geometries have no bounding rect, but in that case
// transforming is a no-op anyway.
None => return,
};
self.skew_around_point_mut(degrees_x, degrees_y, origin);
}
fn skew_around_point(&self, xs: T, ys: T, origin: impl Into<Coord<T>>) -> Self {
let transform = AffineTransform::skew(xs, ys, origin);
self.affine_transform(&transform)
}
fn skew_around_point_mut(&mut self, xs: T, ys: T, origin: impl Into<Coord<T>>) {
let transform = AffineTransform::skew(xs, ys, origin);
self.affine_transform_mut(&transform);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{line_string, BoundingRect, Centroid, LineString};
#[test]
fn skew_linestring() {
let ls: LineString<f64> = line_string![
(x: 3.0, y: 0.0),
(x: 3.0, y: 10.0),
];
let origin = ls.bounding_rect().unwrap().centroid();
let sheared = ls.skew_around_point(45.0, 45.0, origin);
assert_eq!(
sheared,
line_string![
(x: -1.9999999999999991, y: 0.0),
(x: 7.999999999999999, y: 10.0)
]
);
}
}