Skip to main content

vibe/physics/
morphism.rs

1//! Frame morphisms (Galilean → Lorentz) (W5).
2
3use crate::value::Frame;
4
5/// A Galilean frame morphism — the low-velocity limit.
6///
7/// t' = t
8/// x' = x - v*t
9///
10/// No time dilation, no length contraction. Valid for v << c.
11#[derive(Debug, Clone, PartialEq)]
12pub struct GalileanMorphism {
13    /// Velocity of the target frame relative to the source frame [vx, vy, vz].
14    pub velocity: Vec<f64>,
15}
16
17impl GalileanMorphism {
18    /// Creates a new `GalileanMorphism`.
19    pub fn new(velocity: Vec<f64>) -> Self {
20        Self { velocity }
21    }
22
23    /// Transform a position [x, y, z, t] from source to target frame.
24    /// Returns [x', y', z', t'].
25    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); // t' = t
36        result
37    }
38
39    /// The inverse morphism (target → source).
40    pub fn inverse(&self) -> Self {
41        Self {
42            velocity: self.velocity.iter().map(|v| -v).collect(),
43        }
44    }
45}
46
47/// A Lorentz frame morphism — the relativistic transform.
48///
49/// Along the x-axis with velocity v:
50/// t' = gamma * (t - v*x/c²)
51/// x' = gamma * (x - v*t)
52///
53/// where gamma = 1/sqrt(1 - v²/c²).
54///
55/// Time dilation and length contraction are explicit. Valid for all
56/// v < c.
57#[derive(Debug, Clone, PartialEq)]
58pub struct LorentzMorphism {
59    /// Velocity of the target frame along the x-axis (m/s).
60    pub velocity: f64,
61    /// Speed of light (m/s). Default: 299_792_458.0.
62    pub c: f64,
63}
64
65impl LorentzMorphism {
66    pub const C: f64 = 299_792_458.0;
67
68    /// Creates a new `LorentzMorphism`.
69    pub fn new(velocity: f64) -> Self {
70        Self {
71            velocity,
72            c: Self::C,
73        }
74    }
75
76    /// Create with a custom speed of light (for testing).
77    pub fn with_c(velocity: f64, c: f64) -> Self {
78        Self { velocity, c }
79    }
80
81    /// Lorentz factor gamma = 1/sqrt(1 - v²/c²).
82    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    /// Transform [t, x, y, z] from source to target frame.
88    /// Returns [t', x', y', z'].
89    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    /// The inverse morphism (target → source).
106    pub fn inverse(&self) -> Self {
107        Self {
108            velocity: -self.velocity,
109            c: self.c,
110        }
111    }
112}
113
114/// Apply a frame morphism to a Frame, producing the transformed frame.
115pub fn transform_frame(frame: &Frame, morphism: &GalileanMorphism) -> Frame {
116    let origin = morphism.transform(&frame.origin);
117    let basis = frame.basis.clone(); // basis unchanged for Galilean
118    Frame {
119        origin,
120        basis,
121        parent: frame.parent.clone(),
122    }
123}