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 getter methods on AffineTransform to access internal elements #1159

Merged
merged 8 commits into from
May 8, 2024
42 changes: 41 additions & 1 deletion geo/src/algorithm/affine_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use num_traits::ToPrimitive;
use approx::{AbsDiffEq, RelativeEq};

use crate::{Coord, CoordFloat, CoordNum, MapCoords, MapCoordsInPlace};
use std::{fmt, ops::Mul, ops::Neg};
use std::{fmt, ops::Index, ops::Mul, ops::Neg};

/// Apply an [`AffineTransform`] like [`scale`](AffineTransform::scale),
/// [`skew`](AffineTransform::skew), or [`rotate`](AffineTransform::rotate) to a
Expand Down Expand Up @@ -453,6 +453,34 @@ impl<U: CoordFloat> AffineTransform<U> {
}
}

impl Index<&str> for AffineTransform {
type Output = f64;

/// Get elements from the AffineTransform matrix using string-based indexing.
///
/// ```
/// let transform = AffineTransform::new(10.0, 0.0, 400_000.0, 0.0, -10.0, 500_000.0);
///
/// let a: f64 = transform["a"];
/// let b: f64 = transform["b"];
/// let c: f64 = transform["xoff"];
/// let d: f64 = transform["d"];
/// let e: f64 = transform["e"];
/// let f: f64 = transform["yoff"];
/// ```
fn index(&self, idx: &str) -> &Self::Output {
match idx {
"a" => &self.0[0][0],
"b" => &self.0[0][1],
"xoff" => &self.0[0][2],
"d" => &self.0[1][0],
"e" => &self.0[1][1],
"yoff" => &self.0[1][2],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to decide if using these names are ok, or maybe just do a, b, c, d, e, f. See discussion at #1158.

_ => &f64::NAN,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ideal to return NaN here, would prefer if the possible indexes are limited to just 6 possible values, maybe with an enum?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With getters this wouldn't be an issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out-of-bounds indexing in Rust is usually a panic, right? I'm not sure if this is strictly out of bounds, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah on second thought, getter methods might actually be better. I've refactored things in commit a0fdea5

}
}
}

#[cfg(any(feature = "approx", test))]
impl<T> RelativeEq for AffineTransform<T>
where
Expand Down Expand Up @@ -585,4 +613,16 @@ mod tests {
poly.affine_transform_mut(&identity);
assert_eq!(expected, poly);
}
#[test]
fn test_affine_transform_indexing() {
let transform = AffineTransform::new(10.0, 0.0, 400_000.0, 0.0, -10.0, 500_000.0);
assert_eq!(transform["a"], 10.0);
assert_eq!(transform["b"], 0.0);
assert_eq!(transform["xoff"], 400_000.0);
assert_eq!(transform["d"], 0.0);
assert_eq!(transform["e"], -10.0);
assert_eq!(transform["yoff"], 500_000.0);
// Invalid indexes return NaN
assert_ne!(transform["z"], f64::NAN);
}
}
Loading