Search code examples
rustgeo

How to retrieve scale factors from geo::AffineTransform in Rust?


I'm using the geo crate (version 0.28.0) in a Rust project and I need to retrieve the amount of scaling in the x and y directions from a geo::AffineTransform instance.

However, after going through the documentation and source code, I couldn't find a direct way to access the scale factors. The internal matrix representation is private, and the public API doesn't seem to expose methods to retrieve the scale factors.

I have tried the following approach:

let [a, b, c, d, tx, ty] = transform.into();
let sx = (a * a + c * c).sqrt()

But this approach assumes knowledge of the internal matrix structure and doesn't work with the public API.

Is there a way to retrieve the scale factors using the existing public API of geo::AffineTransform? Or would I need to modify the crate and submit a pull request to add this functionality?

I'm open to alternative approaches or workarounds if directly retrieving the scale factors is not possible with the current API.


Solution

  • These methods are now public.

    https://docs.rs/geo/latest/geo/algorithm/affine_ops/struct.AffineTransform.html#method.new

    /// Create a new custom transform matrix
    ///
    /// The argument order matches that of the affine transform matrix:
    ///```ignore
    /// [[a, b, xoff],
    ///  [d, e, yoff],
    ///  [0, 0, 1]] <-- not part of the input arguments
    /// ```
    pub fn new(a: T, b: T, xoff: T, d: T, e: T, yoff: T) -> Self {
        Self([[a, b, xoff], [d, e, yoff], [T::zero(), T::zero(), T::one()]])
    }
    pub fn a(&self) -> T {
        self.0[0][0]
    }
    pub fn b(&self) -> T {
        self.0[0][1]
    }
    pub fn xoff(&self) -> T {
        self.0[0][2]
    }
    pub fn d(&self) -> T {
        self.0[1][0]
    }
    pub fn e(&self) -> T {
        self.0[1][1]
    }
    pub fn yoff(&self) -> T {
        self.0[1][2]
    }