lox-frames 0.1.0-alpha.21

Reference frame transformations for the Lox ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// SPDX-FileCopyrightText: 2026 Helge Eichhorn <git@helgeeichhorn.de>
//
// SPDX-License-Identifier: MPL-2.0

//! Rotations between reference frames, composed through ICRF.
//!
//! Every frame implements [`RotateToIcrf`], giving its rotation to and from
//! ICRF. The blanket [`TryRotation`] impl uses these to rotate between any two
//! frames.
//!
//! [`TryRotation`]: crate::rotations::TryRotation

use lox_bodies::TryRotationalElements;
use lox_time::{
    Time,
    offsets::TryOffset,
    time_scales::{ContinuousTimeScale, Tdb, Tt, Ut1},
};

use crate::{
    Frame,
    frames::{Cirf, Iau, Icrf, Itrf, J2000, Mod, Pef, Teme, Tirf, Tod},
    iers::{IersSystem, ReferenceSystem},
    rotations::{Rotation, RotationError, RotationProvider, TryRotation},
    traits::{FrameKey, ReferenceFrame, frame_key},
};

/// A frame that can produce its own rotation to and from ICRF from a provider's data.
pub trait RotateToIcrf<T: ContinuousTimeScale, P> {
    /// The error type returned when the rotation cannot be computed.
    type Error;

    /// Returns the rotation from this frame to ICRF at `time`.
    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error>;

    /// Returns the rotation from ICRF to this frame at `time`.
    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error>;
}

/// Rotation from `origin` to `target`, composed through ICRF.
pub fn rotation_via_icrf<T, P, O, Tg>(
    provider: &P,
    origin: O,
    target: Tg,
    time: Time<T>,
) -> Result<Rotation, O::Error>
where
    T: ContinuousTimeScale + Copy,
    O: RotateToIcrf<T, P>,
    Tg: RotateToIcrf<T, P, Error = O::Error>,
{
    let origin_to_icrf = origin.rotation_to_icrf(provider, time)?;
    let icrf_to_target = target.rotation_from_icrf(provider, time)?;
    Ok(origin_to_icrf.compose(icrf_to_target))
}

/// Blanket rotation between any two frames that know their route to ICRF.
impl<T, O, Tg, P> TryRotation<O, Tg, T> for P
where
    T: ContinuousTimeScale + Copy,
    O: ReferenceFrame + RotateToIcrf<T, P, Error = RotationError>,
    Tg: ReferenceFrame + RotateToIcrf<T, P, Error = RotationError>,
{
    type Error = RotationError;

    fn try_rotation(&self, origin: O, target: Tg, time: Time<T>) -> Result<Rotation, Self::Error> {
        // Skip work cheaply via frame keys: identical frames need no rotation,
        // and when one endpoint is ICRF a single leg suffices (no composition).
        let origin_key = frame_key(&origin);
        let target_key = frame_key(&target);
        if origin_key.is_some() && origin_key == target_key {
            Ok(Rotation::IDENTITY)
        } else if origin_key == Some(FrameKey::Icrf) {
            target.rotation_from_icrf(self, time)
        } else if target_key == Some(FrameKey::Icrf) {
            origin.rotation_to_icrf(self, time)
        } else {
            rotation_via_icrf(self, origin, target, time)
        }
    }
}

// ---- the hub ---------------------------------------------------------------

impl<T, P> RotateToIcrf<T, P> for Icrf
where
    T: ContinuousTimeScale + Copy,
    P: RotationProvider<T>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, _provider: &P, _time: Time<T>) -> Result<Rotation, Self::Error> {
        Ok(Rotation::IDENTITY)
    }

    fn rotation_from_icrf(&self, _provider: &P, _time: Time<T>) -> Result<Rotation, Self::Error> {
        Ok(Rotation::IDENTITY)
    }
}

// ---- quasi-inertial: frame bias / body-fixed -------------------------------

impl<T, P> RotateToIcrf<T, P> for J2000
where
    T: ContinuousTimeScale + Copy,
    P: RotationProvider<T>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, _time: Time<T>) -> Result<Rotation, Self::Error> {
        Ok(provider.j2000_to_icrf())
    }

    fn rotation_from_icrf(&self, provider: &P, _time: Time<T>) -> Result<Rotation, Self::Error> {
        Ok(provider.icrf_to_j2000())
    }
}

impl<T, P, R> RotateToIcrf<T, P> for Iau<R>
where
    T: ContinuousTimeScale + Copy,
    R: TryRotationalElements + Copy,
    P: RotationProvider<T> + TryOffset<T, Tdb>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.iau_to_icrf(time, *self)
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.icrf_to_iau(time, *self)
    }
}

// ---- CIO branch: ICRF ← CIRF ← TIRF ← ITRF ---------------------------------

impl<T, P> RotateToIcrf<T, P> for Cirf
where
    T: ContinuousTimeScale + Copy,
    P: RotationProvider<T> + TryOffset<T, Tdb>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.cirf_to_icrf(time)
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.icrf_to_cirf(time)
    }
}

impl<T, P> RotateToIcrf<T, P> for Tirf
where
    T: ContinuousTimeScale + Copy,
    P: RotationProvider<T> + TryOffset<T, Tdb> + TryOffset<T, Ut1>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        Ok(provider
            .tirf_to_cirf(time)?
            .compose(provider.cirf_to_icrf(time)?))
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        Ok(provider
            .icrf_to_cirf(time)?
            .compose(provider.cirf_to_tirf(time)?))
    }
}

impl<T, P> RotateToIcrf<T, P> for Itrf
where
    T: ContinuousTimeScale + Copy,
    P: RotationProvider<T> + TryOffset<T, Tt> + TryOffset<T, Tdb> + TryOffset<T, Ut1>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.itrf_to_icrf(time)
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.icrf_to_itrf(time)
    }
}

// ---- equinox branch: ICRF ← MOD ← TOD ← PEF --------------------------------

impl<T, P, C> RotateToIcrf<T, P> for Mod<C>
where
    T: ContinuousTimeScale + Copy,
    C: IersSystem + Into<ReferenceSystem> + Copy,
    P: RotationProvider<T> + TryOffset<T, Tt>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.mod_to_icrf(time, self.0.into())
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.icrf_to_mod(time, self.0.into())
    }
}

impl<T, P, C> RotateToIcrf<T, P> for Tod<C>
where
    T: ContinuousTimeScale + Copy,
    C: IersSystem + Into<ReferenceSystem> + Copy,
    P: RotationProvider<T> + TryOffset<T, Tt> + TryOffset<T, Tdb>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        // The convention (and its nutation model) comes from the frame value,
        // so `Tod(Iers2003(B))` genuinely computes the 2000B nutation.
        let sys: ReferenceSystem = self.0.into();
        Ok(provider
            .tod_to_mod(time, sys)?
            .compose(provider.mod_to_icrf(time, sys)?))
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        let sys: ReferenceSystem = self.0.into();
        Ok(provider
            .icrf_to_mod(time, sys)?
            .compose(provider.mod_to_tod(time, sys)?))
    }
}

impl<T, P, C> RotateToIcrf<T, P> for Pef<C>
where
    T: ContinuousTimeScale + Copy,
    C: IersSystem + Into<ReferenceSystem> + Copy,
    P: RotationProvider<T> + TryOffset<T, Tt> + TryOffset<T, Tdb> + TryOffset<T, Ut1>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        let sys: ReferenceSystem = self.0.into();
        Ok(provider
            .pef_to_tod(time, sys)?
            .compose(provider.tod_to_mod(time, sys)?)
            .compose(provider.mod_to_icrf(time, sys)?))
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        let sys: ReferenceSystem = self.0.into();
        Ok(provider
            .icrf_to_mod(time, sys)?
            .compose(provider.mod_to_tod(time, sys)?)
            .compose(provider.tod_to_pef(time, sys)?))
    }
}

// ---- TEME: tied to the IAU 1976/FK5 (IERS1996) equinox chain ---------------

impl<T, P> RotateToIcrf<T, P> for Teme
where
    T: ContinuousTimeScale + Copy,
    P: RotationProvider<T> + TryOffset<T, Tt> + TryOffset<T, Tdb>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.teme_to_icrf(time)
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        provider.icrf_to_teme(time)
    }
}

// ---- dynamic dispatch ------------------------------------------------------

impl<T, P> RotateToIcrf<T, P> for Frame
where
    T: ContinuousTimeScale + Copy,
    P: RotationProvider<T> + TryOffset<T, Tt> + TryOffset<T, Tdb> + TryOffset<T, Ut1>,
{
    type Error = RotationError;

    fn rotation_to_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        match *self {
            Frame::Icrf => Icrf.rotation_to_icrf(provider, time),
            Frame::J2000 => J2000.rotation_to_icrf(provider, time),
            Frame::Cirf => Cirf.rotation_to_icrf(provider, time),
            Frame::Tirf => Tirf.rotation_to_icrf(provider, time),
            Frame::Itrf => Itrf.rotation_to_icrf(provider, time),
            Frame::Iau(origin) => Iau::try_new(origin)?.rotation_to_icrf(provider, time),
            Frame::Mod(sys) => Mod(sys).rotation_to_icrf(provider, time),
            Frame::Tod(sys) => Tod(sys).rotation_to_icrf(provider, time),
            Frame::Pef(sys) => Pef(sys).rotation_to_icrf(provider, time),
            Frame::Teme => Teme.rotation_to_icrf(provider, time),
        }
    }

    fn rotation_from_icrf(&self, provider: &P, time: Time<T>) -> Result<Rotation, Self::Error> {
        match *self {
            Frame::Icrf => Icrf.rotation_from_icrf(provider, time),
            Frame::J2000 => J2000.rotation_from_icrf(provider, time),
            Frame::Cirf => Cirf.rotation_from_icrf(provider, time),
            Frame::Tirf => Tirf.rotation_from_icrf(provider, time),
            Frame::Itrf => Itrf.rotation_from_icrf(provider, time),
            Frame::Iau(origin) => Iau::try_new(origin)?.rotation_from_icrf(provider, time),
            Frame::Mod(sys) => Mod(sys).rotation_from_icrf(provider, time),
            Frame::Tod(sys) => Tod(sys).rotation_from_icrf(provider, time),
            Frame::Pef(sys) => Pef(sys).rotation_from_icrf(provider, time),
            Frame::Teme => Teme.rotation_from_icrf(provider, time),
        }
    }
}

#[cfg(test)]
mod tests {
    use lox_approx::assert_approx_eq;
    use lox_core::glam::DMat3;

    use lox_bodies::Origin;
    use lox_time::time_scales::Tai;

    use crate::iers::{Iau2000Model, Iers2003};
    use crate::providers::DefaultRotationProvider;

    use super::*;

    fn epoch() -> Time<Tt> {
        Time::from_two_part_julian_date(Tt, 2454195.5, 0.500754444444444)
    }

    fn max_abs_diff(a: DMat3, b: DMat3) -> f64 {
        let d = a - b;
        d.x_axis
            .abs()
            .max_element()
            .max(d.y_axis.abs().max_element())
            .max(d.z_axis.abs().max_element())
    }

    #[test]
    fn roundtrip_icrf_itrf() {
        let t = epoch();
        let fwd = DefaultRotationProvider.try_rotation(Icrf, Itrf, t).unwrap();
        let bwd = DefaultRotationProvider.try_rotation(Itrf, Icrf, t).unwrap();
        assert_approx_eq!(fwd.m * bwd.m, DMat3::IDENTITY, atol <= 1e-14);
    }

    #[test]
    fn rotates_between_two_non_icrf_frames() {
        // Neither endpoint is ICRF, so the composition goes through both legs of
        // `rotation_via_icrf`; the round-trip must return to identity.
        let t = epoch();
        let tod = Tod(Iers2003(Iau2000Model::A));
        let fwd = DefaultRotationProvider.try_rotation(tod, Itrf, t).unwrap();
        let bwd = DefaultRotationProvider.try_rotation(Itrf, tod, t).unwrap();
        assert_approx_eq!(fwd.m * bwd.m, DMat3::IDENTITY, atol <= 1e-14);
    }

    #[test]
    fn dynamic_rotates_between_two_non_icrf_frames() {
        let t = epoch();
        let tod = Frame::Tod(ReferenceSystem::Iers2003(Iau2000Model::A));
        let fwd = DefaultRotationProvider
            .try_rotation(tod, Frame::Itrf, t)
            .unwrap();
        let bwd = DefaultRotationProvider
            .try_rotation(Frame::Itrf, tod, t)
            .unwrap();
        assert_approx_eq!(fwd.m * bwd.m, DMat3::IDENTITY, atol <= 1e-14);
    }

    #[test]
    fn roundtrip_icrf_j2000() {
        let t = epoch();
        let fwd = DefaultRotationProvider
            .try_rotation(Icrf, J2000, t)
            .unwrap();
        let bwd = DefaultRotationProvider
            .try_rotation(J2000, Icrf, t)
            .unwrap();
        assert_approx_eq!(fwd.m * bwd.m, DMat3::IDENTITY, atol <= 1e-15);
        // J2000 differs from ICRF only by the small frame bias.
        assert!(!fwd.m.abs_diff_eq(DMat3::IDENTITY, 1e-9));
        assert!(fwd.m.abs_diff_eq(DMat3::IDENTITY, 1e-6));
    }

    #[test]
    fn threads_2000b_model() {
        // The hub route reads the nutation model from the frame value, so 2000A
        // and 2000B genuinely differ (mas-level) instead of collapsing to 2000A.
        let t = epoch();
        let tod_a = DefaultRotationProvider
            .try_rotation(Icrf, Tod(Iers2003(Iau2000Model::A)), t)
            .unwrap();
        let tod_b = DefaultRotationProvider
            .try_rotation(Icrf, Tod(Iers2003(Iau2000Model::B)), t)
            .unwrap();
        assert!(max_abs_diff(tod_a.m, tod_b.m) > 1e-9);
    }

    // ---- mixed concrete <-> Frame, served by the blanket impl -----------

    fn tai_j2000() -> Time<Tai> {
        Time::j2000(Tai)
    }

    #[test]
    fn mixed_icrf_to_dynframe() {
        let rot = DefaultRotationProvider
            .try_rotation(Icrf, Frame::Icrf, tai_j2000())
            .unwrap();
        assert!(rot.m.abs_diff_eq(DMat3::IDENTITY, 1e-14));
    }

    #[test]
    fn mixed_dynframe_to_icrf() {
        let rot = DefaultRotationProvider
            .try_rotation(Frame::Icrf, Icrf, tai_j2000())
            .unwrap();
        assert!(rot.m.abs_diff_eq(DMat3::IDENTITY, 1e-14));
    }

    #[test]
    fn mixed_iau_dynorigin_and_dynframe() {
        let iau_earth = Iau::try_new(Origin::Earth).unwrap();
        let fwd = DefaultRotationProvider
            .try_rotation(Icrf, Frame::Iau(Origin::Earth), tai_j2000())
            .unwrap();
        let bwd = DefaultRotationProvider
            .try_rotation(iau_earth, Frame::Icrf, tai_j2000())
            .unwrap();
        // Non-trivial body-fixed rotation, with a clean round-trip across the
        // concrete↔dynamic boundary.
        assert!(!fwd.m.abs_diff_eq(DMat3::IDENTITY, 1e-6));
        assert!((fwd.m * bwd.m).abs_diff_eq(DMat3::IDENTITY, 1e-14));
    }
}