lib_jsl_electromagnetics/coordinates/
transforms.rs1use super::systems::Position;
4use crate::conventions::C64;
5use lib_jsl_geometry::algorithms::rotations::Rotation3;
6
7#[derive(Debug, Clone, Copy, PartialEq)]
10pub struct RigidTransform {
11 pub rotation: Rotation3,
12 pub translation: [f64; 3],
13}
14
15impl Default for RigidTransform {
16 fn default() -> Self {
17 Self::identity()
18 }
19}
20
21impl RigidTransform {
22 pub fn identity() -> Self {
23 RigidTransform { rotation: Rotation3::identity(), translation: [0.0; 3] }
24 }
25
26 pub fn translation(t: [f64; 3]) -> Self {
27 RigidTransform { rotation: Rotation3::identity(), translation: t }
28 }
29
30 pub fn rotation(r: Rotation3) -> Self {
31 RigidTransform { rotation: r, translation: [0.0; 3] }
32 }
33
34 pub fn rotation_about(axis: [f64; 3], angle: f64) -> Self {
36 Self::rotation(Rotation3::from_axis_angle(axis, angle))
37 }
38
39 pub fn apply_point(&self, p: [f64; 3]) -> [f64; 3] {
41 let r = self.rotation.rotate(p);
42 [r[0] + self.translation[0], r[1] + self.translation[1], r[2] + self.translation[2]]
43 }
44
45 pub fn apply_position(&self, p: &Position) -> Position {
47 let c = self.apply_point(p.to_cartesian());
48 Position::cartesian(c[0], c[1], c[2]).in_system(p.system)
49 }
50
51 pub fn apply_vector(&self, v: [f64; 3]) -> [f64; 3] {
53 self.rotation.rotate(v)
54 }
55
56 pub fn apply_complex_vector(&self, v: [C64; 3]) -> [C64; 3] {
58 let re = self.rotation.rotate(v.map(|c| c.re));
59 let im = self.rotation.rotate(v.map(|c| c.im));
60 std::array::from_fn(|k| C64::new(re[k], im[k]))
61 }
62
63 pub fn then_after(&self, other: &RigidTransform) -> RigidTransform {
65 RigidTransform { rotation: self.rotation * other.rotation, translation: self.apply_point(other.translation) }
66 }
67
68 pub fn inverse(&self) -> RigidTransform {
70 let inv = self.rotation.inverse();
71 let t = inv.rotate(self.translation);
72 RigidTransform { rotation: inv, translation: [-t[0], -t[1], -t[2]] }
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use std::f64::consts::FRAC_PI_2;
80
81 fn close(a: [f64; 3], b: [f64; 3]) -> bool {
82 (0..3).all(|i| (a[i] - b[i]).abs() < 1e-12)
83 }
84
85 #[test]
86 fn points_move_vectors_only_rotate() {
87 let t = RigidTransform { rotation: Rotation3::from_axis_angle([0.0, 0.0, 1.0], FRAC_PI_2), translation: [1.0, 2.0, 3.0] };
88 assert!(close(t.apply_point([1.0, 0.0, 0.0]), [1.0, 3.0, 3.0]));
89 assert!(close(t.apply_vector([1.0, 0.0, 0.0]), [0.0, 1.0, 0.0]));
90 let cv = t.apply_complex_vector([C64::new(1.0, 2.0), C64::new(0.0, 0.0), C64::new(0.0, -1.0)]);
91 assert!((cv[1] - C64::new(1.0, 2.0)).norm() < 1e-12 && (cv[2] - C64::new(0.0, -1.0)).norm() < 1e-12);
92 let p = t.apply_position(&Position::spherical(1.0, FRAC_PI_2, 0.0));
94 assert_eq!(p.system, crate::coordinates::systems::CoordinateSystem::Spherical);
95 assert!(close(p.to_cartesian(), [1.0, 3.0, 3.0]));
96 }
97
98 #[test]
99 fn composition_and_inverse() {
100 let a = RigidTransform { rotation: Rotation3::from_axis_angle([1.0, 1.0, 0.0], 0.7), translation: [0.5, -1.0, 2.0] };
101 let b = RigidTransform { rotation: Rotation3::from_axis_angle([0.0, 1.0, 1.0], -1.2), translation: [3.0, 0.0, -0.5] };
102 let p = [0.3, -0.8, 1.5];
103 assert!(close(a.then_after(&b).apply_point(p), a.apply_point(b.apply_point(p))));
104 assert!(close(a.inverse().apply_point(a.apply_point(p)), p));
105 assert!(close(a.then_after(&a.inverse()).apply_point(p), p));
106 assert!(close(RigidTransform::identity().apply_point(p), p));
107 assert!(close(RigidTransform::translation([1.0, 1.0, 1.0]).apply_vector(p), p));
108 let r = RigidTransform::rotation_about([0.0, 0.0, 1.0], 0.3);
109 let v = r.apply_vector(p);
110 assert!(((v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt() - (p[0] * p[0] + p[1] * p[1] + p[2] * p[2]).sqrt()).abs() < 1e-12);
111 }
112}