Trait for generic transformation type?

#1

This may be a general Rust question, but if I have:

pub struct Shape {
  pub transform: Affine3<f32>
  ...
}

let mut s = Shape::new();
s.transform = nalgebra::convert(Similarity3::from_scaling(2.0));
s.transform = nalgebra::convert(Translation3::new(2.0,3.0,4.0));

how can I write a set_transform function that takes either Similarity3 or Translation3 or another type of affine transform? I presume there is a trait which defines the relationship between the different transforms, but I can’t quite find it:

  pub fn set_transform<W>(&self, t:W)
    where W:SubTCategoryOf<TAffine> { /* Apparently not this */
      self.transform = nalgebra::convert(t);
  }

I’d rather not have to store a Matrix4.

#2

Hi!

If what you want is to write a function that takes any transformation convertible to an Affine3 without loss of information (using nalgebra::convert), then the W: SubsetOf<Affine3<f32>> bound should do the trick. This trait comes from the alga crate.

#3

Thank you! That’s done the trick. I saw references to this but thought that alga was something internal to the nalgebra crate.

#4

alga is actually a dependency of nalgebra and is responsible for defining abstract traits related to abstract algebra (and those traits are implemented for nalgebra types).