egml_core/model/common/apply_transform.rs
1use nalgebra::{Isometry3, Rotation3, Scale3, Transform3, Translation3, Vector3};
2
3/// Applies a transform to `self` in place.
4///
5/// Only [`apply_transform`](ApplyTransform::apply_transform) is required. The other four
6/// methods have default implementations that convert to `Transform3<f64>` and delegate —
7/// correct everywhere, but not the fastest path: `apply_translation`, for instance, ends up
8/// doing a full homogeneous matrix multiply per point instead of a plain component-wise add.
9///
10/// A default can only reach `self` through other trait methods, so it can never be faster
11/// than `apply_transform` on its own. Implementors that care about that cost (in particular
12/// leaf position types, and container types that recurse into many of them) should override
13/// the specific methods with dedicated math all the way down the recursion — falling back to
14/// the default at any layer converts to a general transform there, and every layer below pays
15/// the full matrix cost regardless of what leaves further down are capable of.
16pub trait ApplyTransform {
17 fn apply_transform(&mut self, transform: Transform3<f64>);
18
19 fn apply_isometry(&mut self, isometry: Isometry3<f64>) {
20 self.apply_transform(nalgebra::convert(isometry));
21 }
22
23 fn apply_translation(&mut self, vector: Vector3<f64>) {
24 self.apply_transform(nalgebra::convert(Translation3::from(vector)));
25 }
26
27 fn apply_rotation(&mut self, rotation: Rotation3<f64>) {
28 self.apply_transform(nalgebra::convert(rotation));
29 }
30
31 fn apply_scale(&mut self, scale: Scale3<f64>) {
32 self.apply_transform(nalgebra::convert(scale));
33 }
34}