Skip to main content

sidereon_core/astro/
iod.rs

1//! Initial orbit determination (IOD) from position or angle observations.
2//!
3//! Authoritative implementations of the classical IOD methods; language
4//! bindings are thin marshaling layers over these functions.
5//!
6//! - [`gibbs`] - velocity at the middle of three coplanar position vectors
7//!   (Algorithm 54, Vallado 2022, pp. 460-467).
8//! - [`hgibbs`] - Herrick-Gibbs velocity from three closely-spaced timed
9//!   positions (Algorithm 55, Vallado 2022, pp. 467-472).
10//! - [`gauss_angles`] - angles-only orbit from three optical sightings
11//!   (Algorithm 52, Vallado 2022, pp. 448-459).
12//!
13//! ## Reference constants
14//!
15//! The constants prefixed `VALLADO_` below are reference-suite values, NOT the
16//! WGS84/EGM datum. They match the Vallado worked examples and the `valladopy`
17//! reference suite the unit tests validate against (`VALLADO_MU = 398600.4415`,
18//! `VALLADO_RE = 6378.1363`), and are kept local so the methods stay bit-exact
19//! with that published reference rather than drifting to the WGS84/GM values in
20//! [`crate::astro::constants`]. Callers needing the WGS84/GM datum must use the
21//! constants module, not these.
22
23/// Earth gravitational parameter (km^3/s^2), Vallado reference suite value (not
24/// the WGS84/GM datum in [`crate::astro::constants`]).
25const VALLADO_MU: f64 = 398600.4415;
26/// Earth equatorial radius (km), Vallado reference suite value (not the WGS84
27/// value in [`crate::astro::constants`]).
28const VALLADO_RE: f64 = 6378.1363;
29/// Canonical time unit (seconds) for the Gauss canonical-unit formulation,
30/// Vallado reference suite value.
31const VALLADO_TUSEC: f64 = 806.8109913067327;
32// Seconds per day; the canonical core value (bit-identical to the Vallado 86400)
33// under the local `DAY2SEC` name the epoch-difference factors below read.
34use crate::astro::math::linear::invert_3x3_adjugate;
35use crate::astro::math::mat3::mul_vec3;
36use crate::astro::math::vec3;
37use crate::constants::SECONDS_PER_DAY as DAY2SEC;
38const SMALL: f64 = 1e-10;
39/// Maximum coplanarity deviation (radians) tolerated by the Gibbs methods. The
40/// three position vectors must lie in a common plane to define an orbit; this
41/// is the standard few-degree IOD acceptance bound (here 5 degrees).
42const COPLANAR_TOL_RAD: f64 = 5.0 * std::f64::consts::PI / 180.0;
43
44/// Error returned by the initial-orbit-determination methods.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
46pub enum IodError {
47    /// The line-of-sight matrix determinant is too small to invert (degenerate
48    /// geometry).
49    #[error("line-of-sight determinant too small")]
50    DeterminantTooSmall,
51    /// The position vectors do not admit an orbit solution (degenerate D or N
52    /// vector in the Gibbs construction).
53    #[error("orbit determination not possible from the given geometry")]
54    OrbitNotPossible,
55    /// A supplied position vector has (near) zero magnitude, so it cannot be
56    /// normalized.
57    #[error("position vector has near-zero magnitude")]
58    ZeroVector,
59    /// The two position vectors whose cross product is normalized for the
60    /// coplanarity check are collinear, leaving the coplanarity angle undefined.
61    #[error("position vectors are collinear")]
62    CollinearVectors,
63    /// The three position vectors are not sufficiently coplanar to define a
64    /// single orbit.
65    #[error("position vectors are not coplanar")]
66    NotCoplanar,
67    /// The observation times are equal or near-equal, so the time geometry is
68    /// degenerate (zero denominators).
69    #[error("observation times are equal or near-equal")]
70    InvalidTimeGeometry,
71    /// The Gauss radius polynomial's root is non-positive or outside the
72    /// supported geocentric-radius range (see [`gauss_angles`]).
73    #[error("no positive real root for the slant-range polynomial")]
74    NoPositiveRoot,
75    /// The Gauss radius root solver failed numerically (degenerate Halley
76    /// denominator, non-finite iterate, or no convergence within the iteration
77    /// cap), so no trustworthy root is available.
78    #[error("slant-range root solver did not converge")]
79    RootSolveFailed,
80    /// An intermediate or output value was not finite (NaN or infinity).
81    #[error("non-finite value encountered")]
82    NonFiniteValue,
83}
84
85/// True when every component of a 3-vector is finite.
86fn all_finite(a: &[f64; 3]) -> bool {
87    a.iter().all(|x| x.is_finite())
88}
89
90fn cross(a: &[f64; 3], b: &[f64; 3]) -> [f64; 3] {
91    vec3::cross3_ref(a, b)
92}
93
94fn dot(a: &[f64; 3], b: &[f64; 3]) -> f64 {
95    vec3::dot3_ref(a, b)
96}
97
98fn mag(a: &[f64; 3]) -> f64 {
99    vec3::norm3_ref(a)
100}
101
102fn unit(a: &[f64; 3]) -> [f64; 3] {
103    vec3::unit3_ref_unchecked(a)
104}
105
106// Kept local: the shared `vec3::add3` debug-asserts finiteness, but the
107// Gibbs/Herrick-Gibbs overflow guards intentionally let a non-finite
108// intermediate sum form and then reject it (returning `NonFiniteValue`), so a
109// finiteness assertion here would change that behavior.
110fn vadd(a: &[f64; 3], b: &[f64; 3]) -> [f64; 3] {
111    [a[0] + b[0], a[1] + b[1], a[2] + b[2]]
112}
113
114// `s * a[i]` and `a[i] * s` are bitwise identical (IEEE multiplication is
115// commutative), so the shared `scale3` preserves the prior operation order.
116fn smul(s: f64, a: &[f64; 3]) -> [f64; 3] {
117    vec3::scale3(*a, s)
118}
119
120/// 3x3 matrix determinant.
121fn det3(m: &[[f64; 3]; 3]) -> f64 {
122    m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
123        - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
124        + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])
125}
126
127// IOD-local 3x3 product. Kept distinct from `astro::math::mat3::inline_rxr`
128// (which accumulates from 0.0): this evaluates each entry as the single
129// `t0 + t1 + t2` expression the IOD goldens were captured against, so it stays
130// bit-identical (the two differ only on signed-zero / NaN intermediates).
131fn mat3_mat3(a: &[[f64; 3]; 3], b: &[[f64; 3]; 3]) -> [[f64; 3]; 3] {
132    let mut r = [[0.0; 3]; 3];
133    for i in 0..3 {
134        for j in 0..3 {
135            r[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
136        }
137    }
138    r
139}
140
141/// Line-of-sight unit vector from right ascension and declination (radians).
142fn los(ra: f64, dec: f64) -> [f64; 3] {
143    [dec.cos() * ra.cos(), dec.cos() * ra.sin(), dec.sin()]
144}
145
146/// Gibbs method: determine the velocity at `r2` from three coplanar position
147/// vectors (km).
148///
149/// Algorithm 54, Vallado 2022, pp. 460-467.
150///
151/// Returns `(v2, theta12_rad, theta23_rad, copa_rad)`: the velocity at `r2`
152/// (km/s), the angles between successive position vectors, and the coplanarity
153/// angle.
154pub fn gibbs(
155    r1: &[f64; 3],
156    r2: &[f64; 3],
157    r3: &[f64; 3],
158) -> Result<([f64; 3], f64, f64, f64), IodError> {
159    // Validate inputs before any normalization or division so a degenerate
160    // input surfaces as a typed error instead of a NaN inside `Ok`.
161    if !all_finite(r1) || !all_finite(r2) || !all_finite(r3) {
162        return Err(IodError::NonFiniteValue);
163    }
164
165    let magr1 = mag(r1);
166    let magr2 = mag(r2);
167    let magr3 = mag(r3);
168
169    // A finite input whose magnitude overflows to infinity would silently
170    // collapse later normalizations to zero and fabricate a finite result, so
171    // reject non-finite magnitudes outright.
172    if !magr1.is_finite() || !magr2.is_finite() || !magr3.is_finite() {
173        return Err(IodError::NonFiniteValue);
174    }
175
176    if magr1 < SMALL || magr2 < SMALL || magr3 < SMALL {
177        return Err(IodError::ZeroVector);
178    }
179
180    // Cross products
181    let p = cross(r2, r3);
182    let q = cross(r3, r1);
183    let w = cross(r1, r2);
184
185    // Only `p` is normalized (for the coplanarity angle), so only r2/r3
186    // collinearity is fatal here. `q` or `w` vanishing on its own (e.g.
187    // anti-parallel r1/r2 at the apsides) is a legitimate geometry; the
188    // degenerate-orbit case is caught by the `D`/`N` magnitude check below.
189    let magp = mag(&p);
190    if !magp.is_finite() {
191        return Err(IodError::NonFiniteValue);
192    }
193    if magp < SMALL {
194        return Err(IodError::CollinearVectors);
195    }
196
197    // Coplanarity angle (now safe: `p` and `r1` are nonzero). Clamp the dot
198    // product into asin's domain so floating-point spill past +-1 cannot yield
199    // a NaN that slips through the `> tol` test below.
200    let copa = dot(&unit(&p), &unit(r1)).clamp(-1.0, 1.0).asin();
201    if copa.abs() > COPLANAR_TOL_RAD {
202        return Err(IodError::NotCoplanar);
203    }
204
205    // D = P + Q + W
206    let d = vadd(&vadd(&p, &q), &w);
207    let magd = mag(&d);
208
209    // N = |r1|*P + |r2|*Q + |r3|*W
210    let n = vadd(&vadd(&smul(magr1, &p), &smul(magr2, &q)), &smul(magr3, &w));
211    let magn = mag(&n);
212
213    if !magd.is_finite() || !magn.is_finite() {
214        return Err(IodError::NonFiniteValue);
215    }
216    if magd < 1e-6 || magn < 1e-6 {
217        return Err(IodError::OrbitNotPossible);
218    }
219
220    // Angles between position vectors
221    let theta12 = (dot(r1, r2) / (magr1 * magr2)).clamp(-1.0, 1.0).acos();
222    let theta23 = (dot(r2, r3) / (magr2 * magr3)).clamp(-1.0, 1.0).acos();
223
224    // S vector
225    let r1mr2 = magr1 - magr2;
226    let r3mr1 = magr3 - magr1;
227    let r2mr3 = magr2 - magr3;
228    let s = vadd(&vadd(&smul(r1mr2, r3), &smul(r3mr1, r2)), &smul(r2mr3, r1));
229
230    // B = D x r2
231    let b = cross(&d, r2);
232
233    // Scaling factor
234    let lg = (VALLADO_MU / (magd * magn)).sqrt();
235
236    // v2 = (lg / |r2|) * B + lg * S
237    let v2 = vadd(&smul(lg / magr2, &b), &smul(lg, &s));
238
239    // Guard against non-finite results that can arise from finite-but-extreme
240    // inputs (e.g. magnitudes that overflow to infinity).
241    if !all_finite(&v2) || !theta12.is_finite() || !theta23.is_finite() || !copa.is_finite() {
242        return Err(IodError::NonFiniteValue);
243    }
244
245    Ok((v2, theta12, theta23, copa))
246}
247
248/// Herrick-Gibbs method: determine the velocity at `r2` from three
249/// closely-spaced position vectors (km) with timestamps.
250///
251/// Algorithm 55, Vallado 2022, pp. 467-472.
252///
253/// `jd1`, `jd2`, `jd3` are the observation epochs in Julian days. The method
254/// converts epoch differences to seconds via the `* DAY2SEC` factor below, so
255/// the inputs must be Julian days (not seconds or an arbitrary unit); the
256/// epochs must be distinct.
257///
258/// Returns `(v2, theta12_rad, theta23_rad, copa_rad)`.
259pub fn hgibbs(
260    r1: &[f64; 3],
261    r2: &[f64; 3],
262    r3: &[f64; 3],
263    jd1: f64,
264    jd2: f64,
265    jd3: f64,
266) -> Result<([f64; 3], f64, f64, f64), IodError> {
267    // Validate inputs before any normalization or division so a degenerate
268    // input surfaces as a typed error instead of a NaN inside `Ok`.
269    if !all_finite(r1)
270        || !all_finite(r2)
271        || !all_finite(r3)
272        || !jd1.is_finite()
273        || !jd2.is_finite()
274        || !jd3.is_finite()
275    {
276        return Err(IodError::NonFiniteValue);
277    }
278
279    let magr1 = mag(r1);
280    let magr2 = mag(r2);
281    let magr3 = mag(r3);
282
283    // Reject magnitudes that overflowed to infinity (finite but huge inputs),
284    // which would otherwise collapse the normalization below to a fake result.
285    if !magr1.is_finite() || !magr2.is_finite() || !magr3.is_finite() {
286        return Err(IodError::NonFiniteValue);
287    }
288
289    if magr1 < SMALL || magr2 < SMALL || magr3 < SMALL {
290        return Err(IodError::ZeroVector);
291    }
292
293    // Time differences (seconds; inputs are Julian days).
294    let dt21 = (jd2 - jd1) * DAY2SEC;
295    let dt31 = (jd3 - jd1) * DAY2SEC;
296    let dt32 = (jd3 - jd2) * DAY2SEC;
297
298    // Equal or near-equal epochs make the divisors below blow up.
299    if dt21.abs() < SMALL || dt31.abs() < SMALL || dt32.abs() < SMALL {
300        return Err(IodError::InvalidTimeGeometry);
301    }
302
303    // Cross product for coplanarity check; must be finite and nonzero to
304    // normalize.
305    let p = cross(r2, r3);
306    let magp = mag(&p);
307    if !magp.is_finite() {
308        return Err(IodError::NonFiniteValue);
309    }
310    if magp < SMALL {
311        return Err(IodError::CollinearVectors);
312    }
313
314    // Coplanarity angle (now safe: `p` and `r1` are nonzero). Clamp the dot
315    // product into asin's domain so floating-point spill past +-1 cannot yield
316    // a NaN that slips through the `> tol` test below.
317    let copa = dot(&unit(&p), &unit(r1)).clamp(-1.0, 1.0).asin();
318    if copa.abs() > COPLANAR_TOL_RAD {
319        return Err(IodError::NotCoplanar);
320    }
321
322    // Angles between position vectors
323    let theta12 = (dot(r1, r2) / (magr1 * magr2)).clamp(-1.0, 1.0).acos();
324    let theta23 = (dot(r2, r3) / (magr2 * magr3)).clamp(-1.0, 1.0).acos();
325
326    // Herrick-Gibbs velocity approximation
327    let term1 = smul(
328        -dt32 * (1.0 / (dt21 * dt31) + VALLADO_MU / (12.0 * magr1.powi(3))),
329        r1,
330    );
331    let term2 = smul(
332        (dt32 - dt21) * (1.0 / (dt21 * dt32) + VALLADO_MU / (12.0 * magr2.powi(3))),
333        r2,
334    );
335    let term3 = smul(
336        dt21 * (1.0 / (dt32 * dt31) + VALLADO_MU / (12.0 * magr3.powi(3))),
337        r3,
338    );
339
340    let v2 = vadd(&vadd(&term1, &term2), &term3);
341
342    // Guard against non-finite results from finite-but-extreme inputs (e.g.
343    // epoch differences large enough to overflow the second-scale divisors).
344    if !all_finite(&v2) || !theta12.is_finite() || !theta23.is_finite() || !copa.is_finite() {
345        return Err(IodError::NonFiniteValue);
346    }
347
348    Ok((v2, theta12, theta23, copa))
349}
350
351/// Halley iteration to refine the 8th-order Gauss polynomial root.
352///
353/// Returns `None` if the Halley denominator vanishes, the iterate becomes
354/// non-finite, or the iteration does not converge within the cap, so the caller
355/// can surface a typed error rather than propagate a NaN/Inf or non-root value.
356fn halley_iteration(poly: &[f64; 9]) -> Option<f64> {
357    // Initial guess at roughly GPS altitude in canonical (Earth-radii) units.
358    let mut bigr2c = 20000.0 / VALLADO_RE;
359    let mut bigr2 = 100.0;
360    let mut converged = false;
361
362    for _ in 0..15 {
363        if (bigr2 - bigr2c).abs() < 8e-5 {
364            converged = true;
365            break;
366        }
367        bigr2 = bigr2c;
368        let x = bigr2;
369        let f = x.powi(8) + poly[2] * x.powi(6) + poly[5] * x.powi(3) + poly[8];
370        let f1 = 8.0 * x.powi(7) + 6.0 * poly[2] * x.powi(5) + 3.0 * poly[5] * x.powi(2);
371        let f2 = 56.0 * x.powi(6) + 30.0 * poly[2] * x.powi(4) + 6.0 * poly[5] * x;
372        let denom = 2.0 * f1 * f1 - f * f2;
373        if denom.abs() < SMALL {
374            return None;
375        }
376        bigr2c = bigr2 - (2.0 * f * f1) / denom;
377        if !bigr2c.is_finite() {
378            return None;
379        }
380    }
381
382    // Convergence is decided by the final step size, not the loop count, so an
383    // iterate that converges on the last allowed pass still counts; an iterate
384    // that merely ran out of iterations without settling is rejected rather than
385    // returned as a fabricated root.
386    if !converged {
387        converged = (bigr2 - bigr2c).abs() < 8e-5;
388    }
389
390    if !converged || !bigr2c.is_finite() {
391        return None;
392    }
393
394    // A zero Halley step also occurs at a stationary point of f (f1 == 0) that
395    // is not a root, which the step-size test alone would accept. Confirm the
396    // polynomial actually vanishes there via a scale-relative residual.
397    let x = bigr2c;
398    let t0 = x.powi(8);
399    let t2 = poly[2] * x.powi(6);
400    let t5 = poly[5] * x.powi(3);
401    let t8 = poly[8];
402    let residual = t0 + t2 + t5 + t8;
403    let scale = t0.abs() + t2.abs() + t5.abs() + t8.abs();
404    if !residual.is_finite() || !scale.is_finite() || residual.abs() > 1e-9 * scale {
405        return None;
406    }
407
408    Some(bigr2c)
409}
410
411/// Gauss angles-only orbit determination.
412///
413/// Given three angular observations (right ascension / declination, radians)
414/// with split Julian dates (`jd` whole part, `jdf` fraction) and the observer
415/// site ECI positions (km), determine the orbit at the middle observation.
416///
417/// Algorithm 52, Vallado 2022, pp. 448-459. Returns `(r2, v2)`: the position
418/// (km) and velocity (km/s) at the middle epoch.
419///
420/// Domain: the radius root solver is seeded near GPS altitude and accepts a
421/// geocentric radius in the near-Earth-through-GEO regime (positive and up to
422/// ~50,000 km). A converged root outside that range yields
423/// [`IodError::NoPositiveRoot`]; a numerical failure of the solver yields
424/// [`IodError::RootSolveFailed`].
425pub fn gauss_angles(
426    decl: &[f64; 3],
427    rtasc: &[f64; 3],
428    jd: &[f64; 3],
429    jdf: &[f64; 3],
430    rseci: &[[f64; 3]; 3],
431) -> Result<([f64; 3], [f64; 3]), IodError> {
432    // Reject non-finite inputs up front: a NaN angle would slip past the
433    // determinant guard below (NaN comparisons are always false).
434    if !decl.iter().all(|x| x.is_finite())
435        || !rtasc.iter().all(|x| x.is_finite())
436        || !jd.iter().all(|x| x.is_finite())
437        || !jdf.iter().all(|x| x.is_finite())
438        || !rseci.iter().all(all_finite)
439    {
440        return Err(IodError::NonFiniteValue);
441    }
442
443    // Time intervals (seconds)
444    let tau12 = ((jd[0] - jd[1]) + (jdf[0] - jdf[1])) * DAY2SEC;
445    let _tau13 = ((jd[0] - jd[2]) + (jdf[0] - jdf[2])) * DAY2SEC;
446    let tau32 = ((jd[2] - jd[1]) + (jdf[2] - jdf[1])) * DAY2SEC;
447
448    // Equal or near-equal observation times make the polynomial-coefficient
449    // divisors below degenerate.
450    if tau12.abs() < SMALL || tau32.abs() < SMALL || (tau32 - tau12).abs() < SMALL {
451        return Err(IodError::InvalidTimeGeometry);
452    }
453
454    // Line-of-sight vectors
455    let l1 = los(rtasc[0], decl[0]);
456    let l2 = los(rtasc[1], decl[1]);
457    let l3 = los(rtasc[2], decl[2]);
458
459    // Canonical units
460    let tau12c = tau12 / VALLADO_TUSEC;
461    let tau32c = tau32 / VALLADO_TUSEC;
462    let rseci1c = smul(1.0 / VALLADO_RE, &rseci[0]);
463    let rseci2c = smul(1.0 / VALLADO_RE, &rseci[1]);
464    let rseci3c = smul(1.0 / VALLADO_RE, &rseci[2]);
465
466    // L-matrix (columns = LOS vectors)
467    let lmat = [
468        [l1[0], l2[0], l3[0]],
469        [l1[1], l2[1], l3[1]],
470        [l1[2], l2[2], l3[2]],
471    ];
472
473    let d = det3(&lmat);
474    if d.abs() < SMALL {
475        return Err(IodError::DeterminantTooSmall);
476    }
477
478    // The determinant guard above keeps `|d| >= SMALL`, which exceeds the
479    // adjugate inverter's `PIVOT_EPSILON` floor, so this never yields `None` on
480    // reachable geometry; the `?` simply re-maps the degenerate case to the same
481    // typed error.
482    let lmati = invert_3x3_adjugate(&lmat).ok_or(IodError::DeterminantTooSmall)?;
483
484    // Range-site matrix (columns = site vectors in canonical units)
485    let rsmatc = [
486        [rseci1c[0], rseci2c[0], rseci3c[0]],
487        [rseci1c[1], rseci2c[1], rseci3c[1]],
488        [rseci1c[2], rseci2c[2], rseci3c[2]],
489    ];
490
491    let lir = mat3_mat3(&lmati, &rsmatc);
492
493    // Polynomial coefficients
494    let a1 = tau32c / (tau32c - tau12c);
495    let a1u = (tau32c * ((tau32c - tau12c).powi(2) - tau32c.powi(2))) / (6.0 * (tau32c - tau12c));
496    let a3 = -tau12c / (tau32c - tau12c);
497    let a3u = -(tau12c * ((tau32c - tau12c).powi(2) - tau12c.powi(2))) / (6.0 * (tau32c - tau12c));
498
499    let d1c = lir[1][0] * a1 - lir[1][1] + lir[1][2] * a3;
500    let d2c = lir[1][0] * a1u + lir[1][2] * a3u;
501    let magrs2 = mag(&rseci2c);
502    let l2dotrs = dot(&l2, &rseci2c);
503
504    // 8th-order polynomial
505    let mut poly = [0.0; 9];
506    poly[0] = 1.0;
507    poly[2] = -(d1c.powi(2) + 2.0 * d1c * l2dotrs + magrs2.powi(2));
508    poly[5] = -2.0 * (l2dotrs * d2c + d1c * d2c);
509    poly[8] = -(d2c.powi(2));
510
511    // Solve for radius. Accept only a converged root in the supported physical
512    // range; surface a typed error rather than substitute a fabricated orbit.
513    // Distinguish a solver failure (`None`) from a converged-but-out-of-range
514    // root so callers can tell the two apart.
515    let bigr2c = match halley_iteration(&poly) {
516        Some(r) if r > 0.0 && r * VALLADO_RE <= 50000.0 => r,
517        Some(_) => return Err(IodError::NoPositiveRoot),
518        None => return Err(IodError::RootSolveFailed),
519    };
520
521    let bigr2 = bigr2c * VALLADO_RE;
522    let a1u_sec = a1u * VALLADO_TUSEC.powi(2);
523    let a3u_sec = a3u * VALLADO_TUSEC.powi(2);
524
525    // Solve for f and g series
526    let u = VALLADO_MU / bigr2.powi(3);
527    let c1 = a1 + a1u_sec * u;
528    let c2 = -1.0;
529    let c3 = a3 + a3u_sec * u;
530
531    // The reconstructed positions divide by c1 and c3; a vanishing coefficient
532    // means the geometry does not yield an orbit.
533    if c1.abs() < SMALL || c3.abs() < SMALL {
534        return Err(IodError::OrbitNotPossible);
535    }
536
537    // Range-site matrix (non-canonical)
538    let rsmat = [
539        [rseci[0][0], rseci[1][0], rseci[2][0]],
540        [rseci[0][1], rseci[1][1], rseci[2][1]],
541        [rseci[0][2], rseci[1][2], rseci[2][2]],
542    ];
543    let lir_full = mat3_mat3(&lmati, &rsmat);
544    let cmat = [-c1, -c2, -c3];
545    let rhomat = mul_vec3(&lir_full, cmat);
546
547    // Form position vectors
548    let r1 = vadd(&smul(rhomat[0] / c1, &l1), &rseci[0]);
549    let r2 = vadd(&smul(rhomat[1] / c2, &l2), &rseci[1]);
550    let r3 = vadd(&smul(rhomat[2] / c3, &l3), &rseci[2]);
551
552    // Use Gibbs to recover the velocity at the middle epoch.
553    let (v2, _, _, _) = gibbs(&r1, &r2, &r3)?;
554
555    if !all_finite(&r2) || !all_finite(&v2) {
556        return Err(IodError::NonFiniteValue);
557    }
558
559    Ok((r2, v2))
560}
561
562#[cfg(test)]
563mod tests {
564    use super::*;
565    use std::f64::consts::PI;
566
567    fn assert_rel(actual: f64, expected: f64, label: &str) {
568        let rel = ((actual - expected) / expected).abs();
569        assert!(rel < 1e-12, "{label}: relative error {rel:e} exceeds 1e-12");
570    }
571
572    // Bit-for-bit reference: valladopy. Gibbs/Herrick-Gibbs velocities are
573    // exact; the reported angles match to a few ULP.
574    fn assert_ulp(actual: f64, expected: f64, max_ulps: i64, label: &str) {
575        if actual == expected {
576            return;
577        }
578        let a = actual.to_bits() as i64;
579        let b = expected.to_bits() as i64;
580        let ulps = (a - b).abs();
581        assert!(
582            ulps <= max_ulps,
583            "{label}: {ulps} ulps exceeds {max_ulps} (got {actual}, expected {expected})"
584        );
585    }
586
587    #[test]
588    fn gibbs_example_7_3() {
589        let r1 = [0.0, 0.0, 6378.1363];
590        let r2 = [0.0, -4464.696, -5102.509];
591        let r3 = [0.0, 5740.323, 3189.068];
592
593        let (v2, theta12, theta23, copa) = gibbs(&r1, &r2, &r3).unwrap();
594        assert_ulp(v2[0], 0.0, 0, "v2_x");
595        assert_ulp(v2[1], 5.5311472050176125, 0, "v2_y");
596        assert_ulp(v2[2], -5.191806413494606, 0, "v2_z");
597        assert_ulp(theta12 * 180.0 / PI, 138.81407085944375, 2, "theta12");
598        assert_ulp(theta23 * 180.0 / PI, 160.24053069723146, 2, "theta23");
599        assert_ulp(copa, 0.0, 0, "copa");
600    }
601
602    #[test]
603    fn hgibbs_example_7_4() {
604        let r1 = [3419.85564, 6019.82602, 2784.60022];
605        let r2 = [2935.91195, 6326.18324, 2660.59584];
606        let r3 = [2434.95202, 6597.38674, 2521.52311];
607        let jd1 = 0.0;
608        let jd2 = (60.0 + 16.48) / crate::constants::SECONDS_PER_DAY;
609        let jd3 = (120.0 + 33.04) / crate::constants::SECONDS_PER_DAY;
610
611        let (v2, theta12, theta23, _copa) = hgibbs(&r1, &r2, &r3, jd1, jd2, jd3).unwrap();
612        assert_ulp(v2[0], -6.441557227511062, 0, "v2_x");
613        assert_ulp(v2[1], 3.777559606719521, 0, "v2_y");
614        assert_ulp(v2[2], -1.7205675602414345, 0, "v2_z");
615        assert_ulp(theta12 * 180.0 / PI, 4.499996147374992, 2, "theta12");
616        assert_ulp(theta23 * 180.0 / PI, 4.499998402168982, 2, "theta23");
617    }
618
619    #[test]
620    fn gauss_example_7_2() {
621        let d2r = |d: f64| d * PI / 180.0;
622        let decl = [d2r(18.667717), d2r(35.664741), d2r(36.996583)];
623        let rtasc = [d2r(0.939913), d2r(45.025748), d2r(67.886655)];
624        let jd = [2_456_159.5, 2_456_159.5, 2_456_159.5];
625        let jdf = [0.4864351851851852, 0.49199074074074073, 0.4947685185185185];
626        let rseci = [
627            [4054.881, 2748.195, 4074.237],
628            [3956.224, 2888.232, 4074.364],
629            [3905.073, 2956.935, 4074.430],
630        ];
631
632        let (r2, v2) = gauss_angles(&decl, &rtasc, &jd, &jdf, &rseci).unwrap();
633        assert_rel(r2[0], 6313.378130210396, "r2_x");
634        assert_rel(r2[1], 5247.50563344895, "r2_y");
635        assert_rel(r2[2], 6467.707164431651, "r2_z");
636        assert_rel(v2[0], -4.185488280436629, "v2_x");
637        assert_rel(v2[1], 4.7884929168898145, "v2_y");
638        assert_rel(v2[2], 1.721714659663034, "v2_z");
639    }
640
641    // --- Degenerate-input rejection (no NaN/Inf or fabricated value in Ok). ---
642
643    #[test]
644    fn gibbs_rejects_zero_vector() {
645        let r2 = [0.0, -4464.696, -5102.509];
646        let r3 = [0.0, 5740.323, 3189.068];
647        assert_eq!(
648            gibbs(&[0.0, 0.0, 0.0], &r2, &r3).unwrap_err(),
649            IodError::ZeroVector
650        );
651    }
652
653    #[test]
654    fn gibbs_rejects_collinear_vectors() {
655        // r2 and r3 are parallel: cross(r2, r3) vanishes.
656        let r1 = [0.0, 0.0, 6378.1363];
657        let r2 = [0.0, 1000.0, 0.0];
658        let r3 = [0.0, 2000.0, 0.0];
659        assert_eq!(
660            gibbs(&r1, &r2, &r3).unwrap_err(),
661            IodError::CollinearVectors
662        );
663    }
664
665    #[test]
666    fn gibbs_rejects_noncoplanar_vectors() {
667        // Push r1 well out of the r2/r3 plane.
668        let r1 = [6378.1363, 6378.1363, 6378.1363];
669        let r2 = [0.0, -4464.696, -5102.509];
670        let r3 = [0.0, 5740.323, 3189.068];
671        assert_eq!(gibbs(&r1, &r2, &r3).unwrap_err(), IodError::NotCoplanar);
672    }
673
674    #[test]
675    fn gibbs_rejects_nonfinite_input() {
676        let r2 = [0.0, -4464.696, -5102.509];
677        let r3 = [0.0, 5740.323, 3189.068];
678        assert_eq!(
679            gibbs(&[f64::NAN, 0.0, 6378.1363], &r2, &r3).unwrap_err(),
680            IodError::NonFiniteValue
681        );
682    }
683
684    #[test]
685    fn gibbs_accepts_antiparallel_endpoints() {
686        // r1 and r3 are anti-parallel (q = r3 x r1 = 0), but this is a valid
687        // coplanar geometry: three points 90 degrees apart on a circular orbit.
688        // The solver must not reject it as collinear; the middle velocity is the
689        // circular speed, tangent to r2.
690        let r1 = [7000.0, 0.0, 0.0];
691        let r2 = [0.0, 7000.0, 0.0];
692        let r3 = [-7000.0, 0.0, 0.0];
693        let (v2, _, _, copa) = gibbs(&r1, &r2, &r3).unwrap();
694        let vcirc = (VALLADO_MU / 7000.0).sqrt();
695        assert!((v2[0] + vcirc).abs() < 1e-9, "v2_x {} vs {}", v2[0], -vcirc);
696        assert!(v2[1].abs() < 1e-9 && v2[2].abs() < 1e-9);
697        assert!(copa.abs() < 1e-12);
698    }
699
700    #[test]
701    fn gibbs_rejects_overflowing_magnitude() {
702        // Finite but astronomically large inputs whose magnitudes/cross products
703        // overflow to infinity must not collapse into a fabricated finite Ok.
704        let r1 = [1e160, 0.0, 0.0];
705        let r2 = [0.0, 1e160, 0.0];
706        let r3 = [0.0, 0.0, 1e160];
707        assert_eq!(gibbs(&r1, &r2, &r3).unwrap_err(), IodError::NonFiniteValue);
708    }
709
710    #[test]
711    fn halley_iteration_reports_stationary_nonroot() {
712        // Coefficients chosen so f'(x0) == 0 at the seed x0 = 20000/RE while
713        // f(x0) != 0: the Halley step is zero, which the step-size test alone
714        // would accept. The residual check must reject this stationary non-root.
715        let x0 = 20000.0 / VALLADO_RE;
716        let mut poly = [0.0; 9];
717        poly[0] = 1.0;
718        poly[2] = -x0.powi(2);
719        poly[5] = -(2.0 / 3.0) * x0.powi(5);
720        poly[8] = -x0.powi(8) / 9.0;
721        assert_eq!(halley_iteration(&poly), None);
722    }
723
724    #[test]
725    fn halley_iteration_reports_nonconvergence() {
726        // f(x) = x^8 has its only root at 0; from the GPS-altitude seed the
727        // Halley step shrinks geometrically and does not reach the tolerance
728        // within the iteration cap, so the solver reports failure (None) rather
729        // than returning a non-root iterate.
730        let poly = [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
731        assert_eq!(halley_iteration(&poly), None);
732    }
733
734    #[test]
735    fn hgibbs_rejects_equal_times() {
736        let r1 = [3419.85564, 6019.82602, 2784.60022];
737        let r2 = [2935.91195, 6326.18324, 2660.59584];
738        let r3 = [2434.95202, 6597.38674, 2521.52311];
739        // jd1 == jd2 -> zero time difference.
740        let jd = 0.0;
741        assert_eq!(
742            hgibbs(
743                &r1,
744                &r2,
745                &r3,
746                jd,
747                jd,
748                (120.0 + 33.04) / crate::constants::SECONDS_PER_DAY
749            )
750            .unwrap_err(),
751            IodError::InvalidTimeGeometry
752        );
753    }
754
755    #[test]
756    fn hgibbs_rejects_zero_vector() {
757        let r2 = [2935.91195, 6326.18324, 2660.59584];
758        let r3 = [2434.95202, 6597.38674, 2521.52311];
759        let jd1 = 0.0;
760        let jd2 = (60.0 + 16.48) / crate::constants::SECONDS_PER_DAY;
761        let jd3 = (120.0 + 33.04) / crate::constants::SECONDS_PER_DAY;
762        assert_eq!(
763            hgibbs(&[0.0, 0.0, 0.0], &r2, &r3, jd1, jd2, jd3).unwrap_err(),
764            IodError::ZeroVector
765        );
766    }
767
768    #[test]
769    fn hgibbs_rejects_nonfinite_output() {
770        // Epoch differences large enough to overflow the second-scale divisors
771        // would yield a non-finite velocity; surface it instead of returning
772        // NaN/Inf inside Ok.
773        let r1 = [3419.85564, 6019.82602, 2784.60022];
774        let r2 = [2935.91195, 6326.18324, 2660.59584];
775        let r3 = [2434.95202, 6597.38674, 2521.52311];
776        let err = hgibbs(&r1, &r2, &r3, 0.0, 1e306, 2e306).unwrap_err();
777        assert_eq!(err, IodError::NonFiniteValue);
778    }
779
780    #[test]
781    fn gauss_rejects_equal_times() {
782        let d2r = |d: f64| d * PI / 180.0;
783        let decl = [d2r(18.667717), d2r(35.664741), d2r(36.996583)];
784        let rtasc = [d2r(0.939913), d2r(45.025748), d2r(67.886655)];
785        let jd = [2_456_159.5, 2_456_159.5, 2_456_159.5];
786        // First two epochs identical -> tau12 == 0.
787        let jdf = [0.49199074074074073, 0.49199074074074073, 0.4947685185185185];
788        let rseci = [
789            [4054.881, 2748.195, 4074.237],
790            [3956.224, 2888.232, 4074.364],
791            [3905.073, 2956.935, 4074.430],
792        ];
793        assert_eq!(
794            gauss_angles(&decl, &rtasc, &jd, &jdf, &rseci).unwrap_err(),
795            IodError::InvalidTimeGeometry
796        );
797    }
798
799    #[test]
800    fn gauss_rejects_out_of_range_root() {
801        let d2r = |d: f64| d * PI / 180.0;
802        let decl = [d2r(18.667717), d2r(35.664741), d2r(36.996583)];
803        let rtasc = [d2r(0.939913), d2r(45.025748), d2r(67.886655)];
804        let jd = [2_456_159.5, 2_456_159.5, 2_456_159.5];
805        let rseci = [
806            [4054.881, 2748.195, 4074.237],
807            [3956.224, 2888.232, 4074.364],
808            [3905.073, 2956.935, 4074.430],
809        ];
810        // Stretch the time gaps 100x past the method's validity so the implied
811        // middle range leaves the physical bracket: no positive real root.
812        let base = [0.4864351851851852, 0.49199074074074073, 0.4947685185185185];
813        let mid = base[1];
814        let jdf = [
815            mid + (base[0] - mid) * 100.0,
816            mid,
817            mid + (base[2] - mid) * 100.0,
818        ];
819        assert_eq!(
820            gauss_angles(&decl, &rtasc, &jd, &jdf, &rseci).unwrap_err(),
821            IodError::NoPositiveRoot
822        );
823    }
824
825    #[test]
826    fn gauss_rejects_nonfinite_input() {
827        let d2r = |d: f64| d * PI / 180.0;
828        let decl = [f64::NAN, d2r(35.664741), d2r(36.996583)];
829        let rtasc = [d2r(0.939913), d2r(45.025748), d2r(67.886655)];
830        let jd = [2_456_159.5, 2_456_159.5, 2_456_159.5];
831        let jdf = [0.4864351851851852, 0.49199074074074073, 0.4947685185185185];
832        let rseci = [
833            [4054.881, 2748.195, 4074.237],
834            [3956.224, 2888.232, 4074.364],
835            [3905.073, 2956.935, 4074.430],
836        ];
837        assert_eq!(
838            gauss_angles(&decl, &rtasc, &jd, &jdf, &rseci).unwrap_err(),
839            IodError::NonFiniteValue
840        );
841    }
842}