-
Notifications
You must be signed in to change notification settings - Fork 206
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
Changes from 1 commit
f4ca06b
a0fdea5
0bd04a5
6431033
771061c
a6138f1
7dc86fd
de84485
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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], | ||
_ => &f64::NAN, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With getters this wouldn't be an issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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); | ||
} | ||
} |
There was a problem hiding this comment.
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.