1use crate::value::Frame;
4
5#[derive(Debug, Clone, PartialEq)]
12pub struct GalileanMorphism {
13 pub velocity: Vec<f64>,
15}
16
17impl GalileanMorphism {
18 pub fn new(velocity: Vec<f64>) -> Self {
20 Self { velocity }
21 }
22
23 pub fn transform(&self, position: &[f64]) -> Vec<f64> {
26 if position.len() < 4 {
27 return position.to_vec();
28 }
29 let t = position[3];
30 let mut result = Vec::with_capacity(4);
31 for i in 0..3 {
32 let v = self.velocity.get(i).copied().unwrap_or(0.0);
33 result.push(position[i] - v * t);
34 }
35 result.push(t); result
37 }
38
39 pub fn inverse(&self) -> Self {
41 Self {
42 velocity: self.velocity.iter().map(|v| -v).collect(),
43 }
44 }
45}
46
47#[derive(Debug, Clone, PartialEq)]
58pub struct LorentzMorphism {
59 pub velocity: f64,
61 pub c: f64,
63}
64
65impl LorentzMorphism {
66 pub const C: f64 = 299_792_458.0;
67
68 pub fn new(velocity: f64) -> Self {
70 Self {
71 velocity,
72 c: Self::C,
73 }
74 }
75
76 pub fn with_c(velocity: f64, c: f64) -> Self {
78 Self { velocity, c }
79 }
80
81 pub fn gamma(&self) -> f64 {
83 let beta_sq = (self.velocity * self.velocity) / (self.c * self.c);
84 1.0 / (1.0 - beta_sq).sqrt()
85 }
86
87 pub fn transform(&self, coords: &[f64]) -> Vec<f64> {
90 if coords.len() < 4 {
91 return coords.to_vec();
92 }
93 let t = coords[0];
94 let x = coords[1];
95 let y = coords[2];
96 let z = coords[3];
97 let g = self.gamma();
98 let v = self.velocity;
99 let c = self.c;
100 let t_prime = g * (t - v * x / (c * c));
101 let x_prime = g * (x - v * t);
102 vec![t_prime, x_prime, y, z]
103 }
104
105 pub fn inverse(&self) -> Self {
107 Self {
108 velocity: -self.velocity,
109 c: self.c,
110 }
111 }
112}
113
114pub fn transform_frame(frame: &Frame, morphism: &GalileanMorphism) -> Frame {
116 let origin = morphism.transform(&frame.origin);
117 let basis = frame.basis.clone(); Frame {
119 origin,
120 basis,
121 parent: frame.parent.clone(),
122 }
123}