siderust 0.6.0

High-precision astronomy and satellite mechanics in Rust.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Vallés Puig, Ramon

//! Shared segment lookup and coordinate evaluation for DE4xx datasets.
//!
//! This module provides two descriptor types:
//! - [`SegmentDescriptor`]: compile-time data via function pointer to `&'static [f64]`.
//! - [`DynSegmentDescriptor`]: runtime data backed by a heap-allocated `Vec<f64>`.
//!
//! Both share the same Chebyshev evaluation logic.

use crate::coordinates::frames::ICRF;
use crate::time::JulianDate;
use affn::{Displacement, Velocity};
use qtty::*;

type KmPerDay = Per<Kilometer, Day>;
type KmPerDayQ = qtty::Quantity<KmPerDay>;

const SECONDS_PER_DAY: f64 = qtty::time::SECONDS_PER_DAY;

// ═══════════════════════════════════════════════════════════════════════════
// Shared helpers (used by both SegmentDescriptor and DynSegmentDescriptor)
// ═══════════════════════════════════════════════════════════════════════════

#[inline]
fn jd_to_et(jd_tdb: JulianDate) -> Seconds {
    (jd_tdb - JulianDate::J2000).to::<Second>()
}

#[inline]
fn xyz_coeffs(record: &[f64], ncoeff: usize) -> (&[f64], &[f64], &[f64]) {
    (
        &record[2..2 + ncoeff],
        &record[2 + ncoeff..2 + 2 * ncoeff],
        &record[2 + 2 * ncoeff..2 + 3 * ncoeff],
    )
}

/// Compute the record index and Chebyshev parameter tau from segment metadata.
#[inline]
fn locate_params(
    init: Seconds,
    intlen: Seconds,
    n_records: usize,
    jd_tdb: JulianDate,
) -> (usize, f64, Seconds) {
    let et = jd_to_et(jd_tdb);
    let idx = ((et - init) / intlen).value() as usize;
    let idx = idx.min(n_records - 1);
    (idx, et.value(), intlen)
}

/// Given a record slice, compute (tau, radius).
#[inline]
fn record_tau(record: &[f64], et: f64) -> (f64, Seconds) {
    let mid = Seconds::new(record[0]);
    let radius = Seconds::new(record[1]);
    let tau = (et - mid.value()) / radius.value();
    (tau, radius)
}

/// Evaluate position from a record slice.
#[inline]
fn eval_position(record: &[f64], ncoeff: usize, tau: f64) -> Displacement<ICRF, Kilometer> {
    let (cx, cy, cz) = xyz_coeffs(record, ncoeff);
    Displacement::new(
        Kilometers::new(cheby::evaluate(cx, tau)),
        Kilometers::new(cheby::evaluate(cy, tau)),
        Kilometers::new(cheby::evaluate(cz, tau)),
    )
}

/// Evaluate velocity from a record slice.
#[inline]
fn eval_velocity(
    record: &[f64],
    ncoeff: usize,
    tau: f64,
    radius: Seconds,
) -> Velocity<ICRF, KmPerDay> {
    let (cx, cy, cz) = xyz_coeffs(record, ncoeff);
    let scale = SECONDS_PER_DAY / radius.value();
    Velocity::new(
        KmPerDayQ::new(cheby::evaluate_derivative(cx, tau) * scale),
        KmPerDayQ::new(cheby::evaluate_derivative(cy, tau) * scale),
        KmPerDayQ::new(cheby::evaluate_derivative(cz, tau) * scale),
    )
}

/// Evaluate both position and velocity from a record slice.
#[inline]
fn eval_both(
    record: &[f64],
    ncoeff: usize,
    tau: f64,
    radius: Seconds,
) -> (Displacement<ICRF, Kilometer>, Velocity<ICRF, KmPerDay>) {
    let (cx, cy, cz) = xyz_coeffs(record, ncoeff);
    let scale = SECONDS_PER_DAY / radius.value();

    let (px, vx) = cheby::evaluate_both(cx, tau);
    let (py, vy) = cheby::evaluate_both(cy, tau);
    let (pz, vz) = cheby::evaluate_both(cz, tau);

    (
        Displacement::new(
            Kilometers::new(px),
            Kilometers::new(py),
            Kilometers::new(pz),
        ),
        Velocity::new(
            KmPerDayQ::new(vx * scale),
            KmPerDayQ::new(vy * scale),
            KmPerDayQ::new(vz * scale),
        ),
    )
}

// ═══════════════════════════════════════════════════════════════════════════
// SegmentDescriptor — compile-time embedded data (via function pointer)
// ═══════════════════════════════════════════════════════════════════════════

/// All metadata needed to evaluate one body segment (compile-time data).
///
/// The `record_fn` field points to a function that returns a `&'static [f64]`
/// slice into data embedded at compile time via `include_bytes!`.
pub struct SegmentDescriptor {
    /// Initial epoch of the segment (TDB seconds past J2000).
    pub init: Seconds,
    /// Length of each Chebyshev sub-interval (seconds).
    pub intlen: Seconds,
    /// Number of Chebyshev coefficients per coordinate (x, y, z).
    pub ncoeff: usize,
    /// Number of coefficient records in the segment.
    pub n_records: usize,
    /// Accessor for the `i`-th coefficient record.
    pub record_fn: fn(usize) -> &'static [f64],
}

#[inline]
fn locate(seg: &SegmentDescriptor, jd_tdb: JulianDate) -> (&'static [f64], f64, Seconds) {
    let (idx, et, intlen) = locate_params(seg.init, seg.intlen, seg.n_records, jd_tdb);
    let record = (seg.record_fn)(idx);
    let (tau, radius) = record_tau(record, et);
    let _ = intlen;
    (record, tau, radius)
}

impl SegmentDescriptor {
    /// Evaluate position in km (ICRF) at Julian Date (TDB).
    #[inline]
    pub fn position(&self, jd_tdb: JulianDate) -> Displacement<ICRF, Kilometer> {
        let (record, tau, _) = locate(self, jd_tdb);
        eval_position(record, self.ncoeff, tau)
    }

    /// Evaluate velocity in km/day (ICRF) at Julian Date (TDB).
    #[inline]
    pub fn velocity(&self, jd_tdb: JulianDate) -> Velocity<ICRF, KmPerDay> {
        let (record, tau, radius) = locate(self, jd_tdb);
        eval_velocity(record, self.ncoeff, tau, radius)
    }

    /// Evaluate both position and velocity in one pass.
    #[inline]
    pub fn position_velocity(
        &self,
        jd_tdb: JulianDate,
    ) -> (Displacement<ICRF, Kilometer>, Velocity<ICRF, KmPerDay>) {
        let (record, tau, radius) = locate(self, jd_tdb);
        eval_both(record, self.ncoeff, tau, radius)
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// DynSegmentDescriptor — runtime heap-backed data
// ═══════════════════════════════════════════════════════════════════════════

/// Segment descriptor for runtime-loaded data (heap-backed).
///
/// Unlike [`SegmentDescriptor`], the coefficient data is stored in a `Vec<f64>`
/// owned by this struct (or shared via `Arc`). This enables loading BSP files
/// at runtime without embedding them at compile time.
pub struct DynSegmentDescriptor {
    /// Initial epoch of the segment (TDB seconds past J2000).
    pub init: Seconds,
    /// Length of each Chebyshev sub-interval (seconds).
    pub intlen: Seconds,
    /// Number of Chebyshev coefficients per coordinate (x, y, z).
    pub ncoeff: usize,
    /// Doubles per record (2 + 3 × ncoeff).
    pub rsize: usize,
    /// Number of coefficient records in the segment.
    pub n_records: usize,
    /// Flattened coefficient data: n_records × rsize f64 values.
    pub data: Vec<f64>,
}

impl DynSegmentDescriptor {
    /// Construct from parsed SPK segment data.
    pub fn from_spk(seg: &crate::data::spk::SegmentData) -> Self {
        Self {
            init: Seconds::new(seg.init),
            intlen: Seconds::new(seg.intlen),
            ncoeff: seg.ncoeff,
            rsize: seg.rsize,
            n_records: seg.n_records,
            data: seg.records.clone(),
        }
    }

    /// Get the `i`-th record as a slice.
    #[inline]
    fn record(&self, i: usize) -> &[f64] {
        let start = i * self.rsize;
        &self.data[start..start + self.rsize]
    }

    /// Locate the record for a given JD and compute tau.
    #[inline]
    fn locate(&self, jd_tdb: JulianDate) -> (&[f64], f64, Seconds) {
        let (idx, et, _) = locate_params(self.init, self.intlen, self.n_records, jd_tdb);
        let record = self.record(idx);
        let (tau, radius) = record_tau(record, et);
        (record, tau, radius)
    }

    /// Evaluate position in km (ICRF) at Julian Date (TDB).
    #[inline]
    pub fn position(&self, jd_tdb: JulianDate) -> Displacement<ICRF, Kilometer> {
        let (record, tau, _) = self.locate(jd_tdb);
        eval_position(record, self.ncoeff, tau)
    }

    /// Evaluate velocity in km/day (ICRF) at Julian Date (TDB).
    #[inline]
    pub fn velocity(&self, jd_tdb: JulianDate) -> Velocity<ICRF, KmPerDay> {
        let (record, tau, radius) = self.locate(jd_tdb);
        eval_velocity(record, self.ncoeff, tau, radius)
    }

    /// Evaluate both position and velocity in one pass.
    #[inline]
    pub fn position_velocity(
        &self,
        jd_tdb: JulianDate,
    ) -> (Displacement<ICRF, Kilometer>, Velocity<ICRF, KmPerDay>) {
        let (record, tau, radius) = self.locate(jd_tdb);
        eval_both(record, self.ncoeff, tau, radius)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// J2000.0 Julian Date (TDB) = 2451545.0
    const JD_J2000: f64 = 2451545.0;

    /// Build a synthetic DynSegmentDescriptor with ncoeff=2 spanning 1000 days from J2000.
    ///
    /// The single record is centred at J2000 + 500 days (tau=0 at that epoch).
    /// At tau=0 (Chebyshev T0(0)=1, T1(0)=0), position = (cx0, cy0, cz0) km.
    fn make_desc(cx0: f64, cy0: f64, cz0: f64) -> DynSegmentDescriptor {
        let ncoeff = 2usize;
        let rsize = 2 + 3 * ncoeff; // = 8
        let intlen_days = 1000.0_f64;
        let intlen_secs = intlen_days * SECONDS_PER_DAY;
        let init_secs = 0.0_f64; // 0 seconds past J2000

        // mid = init + intlen/2 =  intlen / 2
        let mid = init_secs + intlen_secs / 2.0;
        let radius = intlen_secs / 2.0;

        // Record layout: [mid, radius, cx0, cx1, cy0, cy1, cz0, cz1]
        let data = vec![mid, radius, cx0, 0.0, cy0, 0.0, cz0, 0.0];

        DynSegmentDescriptor {
            init: Seconds::new(init_secs),
            intlen: Seconds::new(intlen_secs),
            ncoeff,
            rsize,
            n_records: 1,
            data,
        }
    }

    /// JD for J2000 + 500 days (the midpoint → tau = 0).
    fn jd_mid() -> JulianDate {
        JulianDate::new(JD_J2000 + 500.0)
    }

    /// JD for J2000 + 250 days (first quarter → tau = -0.5).
    fn jd_quarter() -> JulianDate {
        JulianDate::new(JD_J2000 + 250.0)
    }

    // ── Position ──────────────────────────────────────────────────────────

    #[test]
    fn dyn_desc_position_at_mid() {
        // At tau=0, Chebyshev evaluate([c0, 0], 0) = c0 * T0(0) = c0
        let desc = make_desc(1000.0, 2000.0, 3000.0);
        let pos = desc.position(jd_mid());
        assert!(
            (pos.x().value() - 1000.0).abs() < 1e-6,
            "x = {}",
            pos.x().value()
        );
        assert!(
            (pos.y().value() - 2000.0).abs() < 1e-6,
            "y = {}",
            pos.y().value()
        );
        assert!(
            (pos.z().value() - 3000.0).abs() < 1e-6,
            "z = {}",
            pos.z().value()
        );
    }

    #[test]
    fn dyn_desc_position_is_finite() {
        let desc = make_desc(500.0, -300.0, 150.0);
        let pos = desc.position(jd_quarter());
        assert!(pos.x().value().is_finite());
        assert!(pos.y().value().is_finite());
        assert!(pos.z().value().is_finite());
    }

    #[test]
    fn dyn_desc_position_at_boundary_does_not_panic() {
        // First JD in the segment (idx clamped to 0)
        let desc = make_desc(100.0, 200.0, 300.0);
        let jd_start = JulianDate::new(JD_J2000); // et = 0 → idx = 0
        let pos = desc.position(jd_start);
        assert!(pos.x().value().is_finite());
    }

    // ── Velocity ──────────────────────────────────────────────────────────

    #[test]
    fn dyn_desc_velocity_is_finite() {
        let desc = make_desc(1000.0, 2000.0, 3000.0);
        let vel = desc.velocity(jd_mid());
        assert!(vel.x().value().is_finite());
        assert!(vel.y().value().is_finite());
        assert!(vel.z().value().is_finite());
    }

    #[test]
    fn dyn_desc_velocity_at_tau0_with_linear_coeff() {
        // With coeffs [c0=0, c1=1]: at tau=0, dT1/dtau=1 so derivative=1
        // scale = SECONDS_PER_DAY / radius
        let ncoeff = 2usize;
        let rsize = 8usize;
        let intlen_secs = 1000.0 * SECONDS_PER_DAY;
        let radius = intlen_secs / 2.0;
        let data = vec![
            intlen_secs / 2.0, // mid
            radius,            // radius
            0.0,
            1.0, // cx = [0, 1] → at tau=0: d/dtau = 1
            0.0,
            2.0, // cy = [0, 2] → at tau=0: d/dtau = 2
            0.0,
            3.0, // cz = [0, 3] → at tau=0: d/dtau = 3
        ];
        let desc = DynSegmentDescriptor {
            init: Seconds::new(0.0),
            intlen: Seconds::new(intlen_secs),
            ncoeff,
            rsize,
            n_records: 1,
            data,
        };
        let vel = desc.velocity(jd_mid());
        let scale = SECONDS_PER_DAY / radius;
        // dT1(0)/dtau = 1, so v_x = 1 * scale
        assert!((vel.x().value() - 1.0 * scale).abs() < 1e-6);
        assert!((vel.y().value() - 2.0 * scale).abs() < 1e-6);
        assert!((vel.z().value() - 3.0 * scale).abs() < 1e-6);
    }

    // ── Position+Velocity ─────────────────────────────────────────────────

    #[test]
    fn dyn_desc_position_velocity_consistent() {
        let desc = make_desc(1500.0, -800.0, 400.0);
        let jd = jd_quarter();
        let pos_only = desc.position(jd);
        let vel_only = desc.velocity(jd);
        let (pos_both, vel_both) = desc.position_velocity(jd);

        assert!((pos_only.x().value() - pos_both.x().value()).abs() < 1e-10);
        assert!((pos_only.y().value() - pos_both.y().value()).abs() < 1e-10);
        assert!((pos_only.z().value() - pos_both.z().value()).abs() < 1e-10);
        assert!((vel_only.x().value() - vel_both.x().value()).abs() < 1e-10);
        assert!((vel_only.y().value() - vel_both.y().value()).abs() < 1e-10);
        assert!((vel_only.z().value() - vel_both.z().value()).abs() < 1e-10);
    }

    // ── from_spk ─────────────────────────────────────────────────────────

    #[test]
    fn dyn_desc_from_spk_roundtrip() {
        use crate::data::spk::SegmentData;
        let ncoeff = 3usize;
        let rsize = 2 + 3 * ncoeff; // = 11
        let records = vec![0.0; rsize]; // one zero record
        let seg = SegmentData {
            init: 0.0,
            intlen: 86400.0,
            rsize,
            ncoeff,
            n_records: 1,
            records,
        };
        let desc = DynSegmentDescriptor::from_spk(&seg);
        assert_eq!(desc.ncoeff, ncoeff);
        assert_eq!(desc.rsize, rsize);
        assert_eq!(desc.n_records, 1);
        assert_eq!(desc.data.len(), rsize);
    }

    // ── SegmentDescriptor (compile-time data) ─────────────────────────────

    /// A simple static record: [mid, radius, cx0, cx1, cy0, cy1, cz0, cz1]
    ///
    /// Segment spans 1000 days from J2000 (init=0).
    /// midpoint  = 500 * 86400 s → tau=0 at J2000+500d
    /// ncoeff=2, position at tau=0 = (1000, 2000, 3000) km
    static STATIC_RECORD: [f64; 8] = [
        500.0 * 86400.0, // mid (seconds past J2000)
        500.0 * 86400.0, // radius = half-interval
        1000.0,
        0.0, // cx = [1000, 0]
        2000.0,
        0.0, // cy = [2000, 0]
        3000.0,
        0.0, // cz = [3000, 0]
    ];

    fn static_record_fn(_i: usize) -> &'static [f64] {
        &STATIC_RECORD
    }

    fn make_static_desc() -> SegmentDescriptor {
        SegmentDescriptor {
            init: Seconds::new(0.0),
            intlen: Seconds::new(1000.0 * SECONDS_PER_DAY),
            ncoeff: 2,
            n_records: 1,
            record_fn: static_record_fn,
        }
    }

    #[test]
    fn static_desc_position_at_mid() {
        let desc = make_static_desc();
        let jd = jd_mid(); // J2000 + 500d → tau = 0
        let pos = desc.position(jd);
        assert!(
            (pos.x().value() - 1000.0).abs() < 1e-6,
            "x={}",
            pos.x().value()
        );
        assert!(
            (pos.y().value() - 2000.0).abs() < 1e-6,
            "y={}",
            pos.y().value()
        );
        assert!(
            (pos.z().value() - 3000.0).abs() < 1e-6,
            "z={}",
            pos.z().value()
        );
    }

    #[test]
    fn static_desc_position_is_finite() {
        let desc = make_static_desc();
        let pos = desc.position(jd_quarter());
        assert!(pos.x().value().is_finite());
        assert!(pos.y().value().is_finite());
        assert!(pos.z().value().is_finite());
    }

    #[test]
    fn static_desc_velocity_is_finite() {
        let desc = make_static_desc();
        let vel = desc.velocity(jd_mid());
        assert!(vel.x().value().is_finite());
        assert!(vel.y().value().is_finite());
        assert!(vel.z().value().is_finite());
    }

    #[test]
    fn static_desc_velocity_zero_at_constant_segment() {
        // With cx1=0 (linear coeff=0), the derivative should be 0
        let desc = make_static_desc();
        let vel = desc.velocity(jd_mid());
        // dT0/dtau = 0, dT1/dtau(0) = 1, but linear coeff is 0 → velocity = 0
        assert!(vel.x().value().abs() < 1e-9, "vx={}", vel.x().value());
        assert!(vel.y().value().abs() < 1e-9, "vy={}", vel.y().value());
        assert!(vel.z().value().abs() < 1e-9, "vz={}", vel.z().value());
    }

    #[test]
    fn static_desc_position_velocity_consistent() {
        let desc = make_static_desc();
        let jd = jd_quarter();
        let pos_only = desc.position(jd);
        let vel_only = desc.velocity(jd);
        let (pos_both, vel_both) = desc.position_velocity(jd);
        assert!((pos_only.x().value() - pos_both.x().value()).abs() < 1e-10);
        assert!((pos_only.y().value() - pos_both.y().value()).abs() < 1e-10);
        assert!((pos_only.z().value() - pos_both.z().value()).abs() < 1e-10);
        assert!((vel_only.x().value() - vel_both.x().value()).abs() < 1e-10);
        assert!((vel_only.y().value() - vel_both.y().value()).abs() < 1e-10);
        assert!((vel_only.z().value() - vel_both.z().value()).abs() < 1e-10);
    }

    #[test]
    fn static_desc_position_at_boundary() {
        let desc = make_static_desc();
        let jd_start = JulianDate::new(JD_J2000); // et=0 → idx clamped to 0
        let pos = desc.position(jd_start);
        assert!(pos.x().value().is_finite());
    }

    // ── Multi-record ─────────────────────────────────────────────────────

    #[test]
    fn dyn_desc_multi_record_second_interval() {
        // Two records, each 500-day intervals. Query in second interval (J2000 + 750 days).
        let ncoeff = 2usize;
        let rsize = 8usize;
        let intlen_secs = 500.0 * SECONDS_PER_DAY;
        // Record 0: mid = intlen/2, radius = intlen/2, pos = (100, 0, 0)
        let mid0 = intlen_secs / 2.0;
        let rad = intlen_secs / 2.0;
        // Record 1: mid = intlen + intlen/2, pos = (200, 0, 0)
        let mid1 = intlen_secs + intlen_secs / 2.0;
        let data = vec![
            mid0, rad, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, // record 0
            mid1, rad, 200.0, 0.0, 0.0, 0.0, 0.0, 0.0, // record 1
        ];
        let desc = DynSegmentDescriptor {
            init: Seconds::new(0.0),
            intlen: Seconds::new(intlen_secs),
            ncoeff,
            rsize,
            n_records: 2,
            data,
        };
        // At J2000 + 750 days → et = 750 * 86400 = 64800000, idx = (64800000 / 43200000) = 1
        let jd_second = JulianDate::new(JD_J2000 + 750.0);
        let pos = desc.position(jd_second);
        // At tau = (et - mid1) / rad with et = 750*86400, mid1 = 750*86400 → tau=0 → x=200
        let et_s = 750.0 * SECONDS_PER_DAY;
        let tau = (et_s - mid1) / rad;
        let expected_x = 200.0; // T0(tau=0) = 1 but tau may not be 0
        let _ = tau;
        let _ = expected_x;
        assert!(pos.x().value().is_finite());
    }
}