use-orbit 0.0.1

Orbital mechanics helpers for RustUse
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

//! Orbital mechanics helpers.

use core::f64::consts::{PI, TAU};

pub mod prelude;

/// Newtonian constant of gravitation, in cubic meters per kilogram second squared.
///
/// This crate keeps the value locally as a convenience for orbital helpers.
/// Broader physical constants belong in the top-level `use-constants` set.
pub const GRAVITATIONAL_CONSTANT: f64 = 6.674_30e-11;

/// Conventional standard gravity, in meters per second squared.
///
/// This crate keeps the value locally as a convenience for orbital helpers.
/// Broader physical constants belong in the top-level `use-constants` set.
pub const STANDARD_GRAVITY: f64 = 9.806_65;

fn finite(value: f64) -> Option<f64> {
    value.is_finite().then_some(value)
}

fn all_finite(values: &[f64]) -> bool {
    values.iter().all(|value| value.is_finite())
}

/// Computes the standard gravitational parameter from a source mass.
///
/// Formula: `μ = G * M`
///
/// Returns `None` when `source_mass` is negative, when the input is not finite, or when the
/// result is not finite.
///
/// # Examples
///
/// ```rust
/// use use_orbit::{GRAVITATIONAL_CONSTANT, gravitational_parameter};
///
/// assert_eq!(gravitational_parameter(1.0), Some(GRAVITATIONAL_CONSTANT));
/// ```
#[must_use]
pub fn gravitational_parameter(source_mass: f64) -> Option<f64> {
    if source_mass < 0.0 || !source_mass.is_finite() {
        return None;
    }

    finite(GRAVITATIONAL_CONSTANT * source_mass)
}

/// Computes the source mass from a standard gravitational parameter.
///
/// Formula: `M = μ / G`
///
/// Returns `None` when `mu` is negative, when the input is not finite, or when the result is not
/// finite.
#[must_use]
pub fn source_mass_from_gravitational_parameter(mu: f64) -> Option<f64> {
    if mu < 0.0 || !mu.is_finite() {
        return None;
    }

    finite(mu / GRAVITATIONAL_CONSTANT)
}

/// Computes the speed for a circular orbit at a radius around a body with gravitational parameter.
///
/// Formula: `v = sqrt(μ / r)`
///
/// Returns `None` when `mu` is negative, when `orbital_radius` is less than or equal to zero,
/// or when the input or result is not finite.
///
/// # Examples
///
/// ```rust
/// use use_orbit::circular_orbital_speed;
///
/// let speed = circular_orbital_speed(398_600_441_800_000.0, 6_371_000.0);
///
/// assert!(speed.is_some_and(|value| (value - 7_909.8).abs() < 1.0));
/// ```
#[must_use]
pub fn circular_orbital_speed(mu: f64, orbital_radius: f64) -> Option<f64> {
    if mu < 0.0 || orbital_radius <= 0.0 || !all_finite(&[mu, orbital_radius]) {
        return None;
    }

    finite((mu / orbital_radius).sqrt())
}

/// Computes the orbital period for a circular orbit.
///
/// Formula: `T = 2π * sqrt(r³ / μ)`
///
/// Returns `None` when `mu` is less than or equal to zero, when `orbital_radius` is less than or
/// equal to zero, or when the input or result is not finite.
///
/// # Examples
///
/// ```rust
/// use use_orbit::circular_orbital_period;
///
/// let period = circular_orbital_period(398_600_441_800_000.0, 6_371_000.0);
///
/// assert!(period.is_some_and(|value| value.is_finite() && value > 0.0));
/// ```
#[must_use]
pub fn circular_orbital_period(mu: f64, orbital_radius: f64) -> Option<f64> {
    if mu <= 0.0 || orbital_radius <= 0.0 || !all_finite(&[mu, orbital_radius]) {
        return None;
    }

    finite(TAU * (orbital_radius.powi(3) / mu).sqrt())
}

/// Computes the circular orbital radius from an orbital period.
///
/// Formula: `r = cbrt(μ * (T / 2π)²)`
///
/// Returns `None` when `mu` is less than or equal to zero, when `period` is less than or equal
/// to zero, or when the input or result is not finite.
#[must_use]
pub fn orbital_radius_from_period(mu: f64, period: f64) -> Option<f64> {
    if mu <= 0.0 || period <= 0.0 || !all_finite(&[mu, period]) {
        return None;
    }

    let ratio = period / TAU;

    finite((mu * ratio.powi(2)).cbrt())
}

/// Computes the circular orbital radius from circular speed.
///
/// Formula: `r = μ / v²`
///
/// Returns `None` when `mu` is negative, when `speed` is less than or equal to zero, or when the
/// input or result is not finite.
#[must_use]
pub fn orbital_radius_from_circular_speed(mu: f64, speed: f64) -> Option<f64> {
    if mu < 0.0 || speed <= 0.0 || !all_finite(&[mu, speed]) {
        return None;
    }

    finite(mu / speed.powi(2))
}

/// Computes the semi-major axis from periapsis and apoapsis radii.
///
/// Formula: `a = (r_p + r_a) / 2`
///
/// Returns `None` when either radius is less than or equal to zero, when `apoapsis_radius` is
/// less than `periapsis_radius`, or when the input or result is not finite.
///
/// # Examples
///
/// ```rust
/// use use_orbit::semi_major_axis_from_apsides;
///
/// assert_eq!(semi_major_axis_from_apsides(10.0, 20.0), Some(15.0));
/// ```
#[must_use]
pub fn semi_major_axis_from_apsides(periapsis_radius: f64, apoapsis_radius: f64) -> Option<f64> {
    if periapsis_radius <= 0.0
        || apoapsis_radius <= 0.0
        || apoapsis_radius < periapsis_radius
        || !all_finite(&[periapsis_radius, apoapsis_radius])
    {
        return None;
    }

    finite(periapsis_radius.midpoint(apoapsis_radius))
}

/// Computes eccentricity from periapsis and apoapsis radii.
///
/// Formula: `e = (r_a - r_p) / (r_a + r_p)`
///
/// Returns `None` when either radius is less than or equal to zero, when `apoapsis_radius` is
/// less than `periapsis_radius`, or when the input or result is not finite.
#[must_use]
pub fn eccentricity_from_apsides(periapsis_radius: f64, apoapsis_radius: f64) -> Option<f64> {
    if periapsis_radius <= 0.0
        || apoapsis_radius <= 0.0
        || apoapsis_radius < periapsis_radius
        || !all_finite(&[periapsis_radius, apoapsis_radius])
    {
        return None;
    }

    let eccentricity = (apoapsis_radius - periapsis_radius) / (apoapsis_radius + periapsis_radius);

    if !(0.0..1.0).contains(&eccentricity) {
        return None;
    }

    finite(eccentricity)
}

/// Computes periapsis radius from semi-major axis and eccentricity.
///
/// Formula: `r_p = a * (1 - e)`
///
/// Returns `None` when `semi_major_axis` is less than or equal to zero, when `eccentricity` is
/// outside `[0.0, 1.0)`, or when the input or result is not finite.
#[must_use]
pub fn periapsis_from_semi_major_axis_eccentricity(
    semi_major_axis: f64,
    eccentricity: f64,
) -> Option<f64> {
    if semi_major_axis <= 0.0 || !semi_major_axis.is_finite() || !(0.0..1.0).contains(&eccentricity)
    {
        return None;
    }

    finite(semi_major_axis * (1.0 - eccentricity))
}

/// Computes apoapsis radius from semi-major axis and eccentricity.
///
/// Formula: `r_a = a * (1 + e)`
///
/// Returns `None` when `semi_major_axis` is less than or equal to zero, when `eccentricity` is
/// outside `[0.0, 1.0)`, or when the input or result is not finite.
#[must_use]
pub fn apoapsis_from_semi_major_axis_eccentricity(
    semi_major_axis: f64,
    eccentricity: f64,
) -> Option<f64> {
    if semi_major_axis <= 0.0 || !semi_major_axis.is_finite() || !(0.0..1.0).contains(&eccentricity)
    {
        return None;
    }

    finite(semi_major_axis * (1.0 + eccentricity))
}

/// Computes orbital speed from the vis-viva equation.
///
/// Formula: `v = sqrt(μ * (2/r - 1/a))`
///
/// Returns `None` when `mu` is negative, when `orbital_radius` or `semi_major_axis` is less than
/// or equal to zero, when the value under the square root is negative, or when the input or
/// result is not finite.
///
/// # Examples
///
/// ```rust
/// use use_orbit::vis_viva_speed;
///
/// assert!(vis_viva_speed(100.0, 10.0, 15.0).is_some_and(|value| value > 0.0));
/// ```
#[must_use]
pub fn vis_viva_speed(mu: f64, orbital_radius: f64, semi_major_axis: f64) -> Option<f64> {
    if mu < 0.0
        || orbital_radius <= 0.0
        || semi_major_axis <= 0.0
        || !all_finite(&[mu, orbital_radius, semi_major_axis])
    {
        return None;
    }

    let radicand = mu * ((2.0 / orbital_radius) - (1.0 / semi_major_axis));

    if radicand < 0.0 || !radicand.is_finite() {
        return None;
    }

    finite(radicand.max(0.0).sqrt())
}

/// Computes speed at periapsis using the vis-viva equation.
///
/// Returns `None` when the apsides are invalid or when the derived speed is invalid.
#[must_use]
pub fn periapsis_speed(mu: f64, periapsis_radius: f64, apoapsis_radius: f64) -> Option<f64> {
    semi_major_axis_from_apsides(periapsis_radius, apoapsis_radius)
        .and_then(|semi_major_axis| vis_viva_speed(mu, periapsis_radius, semi_major_axis))
}

/// Computes speed at apoapsis using the vis-viva equation.
///
/// Returns `None` when the apsides are invalid or when the derived speed is invalid.
#[must_use]
pub fn apoapsis_speed(mu: f64, periapsis_radius: f64, apoapsis_radius: f64) -> Option<f64> {
    semi_major_axis_from_apsides(periapsis_radius, apoapsis_radius)
        .and_then(|semi_major_axis| vis_viva_speed(mu, apoapsis_radius, semi_major_axis))
}

/// Computes the orbital period for an elliptical orbit.
///
/// Formula: `T = 2π * sqrt(a³ / μ)`
///
/// Returns `None` when `mu` is less than or equal to zero, when `semi_major_axis` is less than
/// or equal to zero, or when the input or result is not finite.
#[must_use]
pub fn elliptical_orbital_period(mu: f64, semi_major_axis: f64) -> Option<f64> {
    if mu <= 0.0 || semi_major_axis <= 0.0 || !all_finite(&[mu, semi_major_axis]) {
        return None;
    }

    finite(TAU * (semi_major_axis.powi(3) / mu).sqrt())
}

/// Computes escape speed from a distance around a body with gravitational parameter.
///
/// Formula: `v_escape = sqrt(2μ / r)`
///
/// Returns `None` when `mu` is negative, when `distance` is less than or equal to zero, or when
/// the input or result is not finite.
#[must_use]
pub fn escape_speed(mu: f64, distance: f64) -> Option<f64> {
    if mu < 0.0 || distance <= 0.0 || !all_finite(&[mu, distance]) {
        return None;
    }

    finite((2.0 * mu / distance).sqrt())
}

/// Computes specific orbital energy.
///
/// Formula: `ε = v² / 2 - μ / r`
///
/// Returns `None` when `mu` is negative, when `distance` is less than or equal to zero, or when
/// the input or result is not finite.
#[must_use]
pub fn specific_orbital_energy(speed: f64, mu: f64, distance: f64) -> Option<f64> {
    if mu < 0.0 || distance <= 0.0 || !all_finite(&[speed, mu, distance]) {
        return None;
    }

    finite((speed.powi(2) / 2.0) - (mu / distance))
}

/// Computes the semi-major axis for a bound orbit from specific orbital energy.
///
/// Formula: `a = -μ / (2ε)`
///
/// Returns `None` when `mu` is less than or equal to zero, when `specific_energy` is greater than
/// or equal to zero, or when the input or result is not finite.
#[must_use]
pub fn semi_major_axis_from_specific_energy(mu: f64, specific_energy: f64) -> Option<f64> {
    if mu <= 0.0 || specific_energy >= 0.0 || !all_finite(&[mu, specific_energy]) {
        return None;
    }

    finite(-mu / (2.0 * specific_energy))
}

/// Computes orbital radius from body radius and altitude.
///
/// Formula: `r = R + h`
///
/// Returns `None` when `body_radius` is less than or equal to zero, when `altitude` is negative,
/// or when the input or result is not finite.
#[must_use]
pub fn orbital_radius_from_altitude(body_radius: f64, altitude: f64) -> Option<f64> {
    if body_radius <= 0.0 || altitude < 0.0 || !all_finite(&[body_radius, altitude]) {
        return None;
    }

    finite(body_radius + altitude)
}

/// Computes altitude from body radius and orbital radius.
///
/// Formula: `h = r - R`
///
/// Returns `None` when `body_radius` is less than or equal to zero, when `orbital_radius` is
/// less than `body_radius`, or when the input or result is not finite.
#[must_use]
pub fn altitude_from_orbital_radius(body_radius: f64, orbital_radius: f64) -> Option<f64> {
    if body_radius <= 0.0
        || orbital_radius < body_radius
        || !all_finite(&[body_radius, orbital_radius])
    {
        return None;
    }

    finite(orbital_radius - body_radius)
}

/// Computes the semi-major axis of a Hohmann transfer ellipse.
///
/// Formula: `a_transfer = (r1 + r2) / 2`
///
/// Returns `None` when either radius is less than or equal to zero, or when the input or result
/// is not finite.
#[must_use]
pub fn hohmann_transfer_semi_major_axis(radius_initial: f64, radius_final: f64) -> Option<f64> {
    if radius_initial <= 0.0 || radius_final <= 0.0 || !all_finite(&[radius_initial, radius_final])
    {
        return None;
    }

    finite(radius_initial.midpoint(radius_final))
}

/// Computes the transfer time for a Hohmann transfer.
///
/// Formula: `t_transfer = π * sqrt(a_transfer³ / μ)`
///
/// Returns `None` when `mu` is less than or equal to zero, when either radius is less than or
/// equal to zero, or when the input or result is not finite.
#[must_use]
pub fn hohmann_transfer_time(mu: f64, radius_initial: f64, radius_final: f64) -> Option<f64> {
    if mu <= 0.0 || !mu.is_finite() {
        return None;
    }

    hohmann_transfer_semi_major_axis(radius_initial, radius_final)
        .and_then(|semi_major_axis| finite(PI * (semi_major_axis.powi(3) / mu).sqrt()))
}

/// Computes the first burn for a Hohmann transfer.
///
/// Formula: `Δv1 = sqrt(μ/r1) * (sqrt(2r2 / (r1 + r2)) - 1)`
///
/// Returns `None` when `mu` is less than or equal to zero, when either radius is less than or
/// equal to zero, or when the input or result is not finite.
#[must_use]
pub fn hohmann_delta_v_1(mu: f64, radius_initial: f64, radius_final: f64) -> Option<f64> {
    if mu <= 0.0 || radius_initial <= 0.0 || radius_final <= 0.0 {
        return None;
    }
    if !all_finite(&[mu, radius_initial, radius_final]) {
        return None;
    }

    let circular_speed = (mu / radius_initial).sqrt();
    let transfer_factor = ((2.0 * radius_final) / (radius_initial + radius_final)).sqrt() - 1.0;

    finite(circular_speed * transfer_factor)
}

/// Computes the second burn for a Hohmann transfer.
///
/// Formula: `Δv2 = sqrt(μ/r2) * (1 - sqrt(2r1 / (r1 + r2)))`
///
/// Returns `None` when `mu` is less than or equal to zero, when either radius is less than or
/// equal to zero, or when the input or result is not finite.
#[must_use]
pub fn hohmann_delta_v_2(mu: f64, radius_initial: f64, radius_final: f64) -> Option<f64> {
    if mu <= 0.0 || radius_initial <= 0.0 || radius_final <= 0.0 {
        return None;
    }
    if !all_finite(&[mu, radius_initial, radius_final]) {
        return None;
    }

    let circular_speed = (mu / radius_final).sqrt();
    let transfer_factor = 1.0 - ((2.0 * radius_initial) / (radius_initial + radius_final)).sqrt();

    finite(circular_speed * transfer_factor)
}

/// Computes the total scalar delta-v magnitude for a Hohmann transfer.
///
/// Returns `None` when either component burn is invalid.
///
/// # Examples
///
/// ```rust
/// use use_orbit::hohmann_total_delta_v;
///
/// assert!(hohmann_total_delta_v(100.0, 10.0, 20.0).is_some_and(|value| value >= 0.0));
/// ```
#[must_use]
pub fn hohmann_total_delta_v(mu: f64, radius_initial: f64, radius_final: f64) -> Option<f64> {
    let delta_v_1 = hohmann_delta_v_1(mu, radius_initial, radius_final)?;
    let delta_v_2 = hohmann_delta_v_2(mu, radius_initial, radius_final)?;

    finite(delta_v_1.abs() + delta_v_2.abs())
}

/// Mass and optional radius for a central body used in orbital calculations.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CentralBody {
    /// Mass in kilograms.
    pub mass: f64,
    /// Optional radius in meters.
    pub radius: Option<f64>,
}

impl CentralBody {
    /// Creates a central body with mass and no stored radius.
    ///
    /// Returns `None` when `mass` is negative or not finite.
    #[must_use]
    pub fn new(mass: f64) -> Option<Self> {
        if mass < 0.0 || !mass.is_finite() {
            return None;
        }

        Some(Self { mass, radius: None })
    }

    /// Creates a central body with mass and radius.
    ///
    /// Returns `None` when `mass` is negative or not finite, or when `radius` is less than or
    /// equal to zero or not finite.
    #[must_use]
    pub fn with_radius(mass: f64, radius: f64) -> Option<Self> {
        if mass < 0.0 || !mass.is_finite() || radius <= 0.0 || !radius.is_finite() {
            return None;
        }

        Some(Self {
            mass,
            radius: Some(radius),
        })
    }

    /// Computes the body's standard gravitational parameter.
    #[must_use]
    pub fn gravitational_parameter(&self) -> Option<f64> {
        gravitational_parameter(self.mass)
    }

    /// Computes orbital radius from the stored body radius and an altitude.
    ///
    /// Returns `None` when the body has no stored radius or when the inputs are invalid.
    #[must_use]
    pub fn orbital_radius_from_altitude(&self, altitude: f64) -> Option<f64> {
        self.radius
            .and_then(|radius| orbital_radius_from_altitude(radius, altitude))
    }

    /// Computes circular orbital speed at a radius around this body.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use use_orbit::CentralBody;
    ///
    /// let earth = CentralBody::with_radius(5.972e24, 6.371e6);
    /// let speed = earth.and_then(|body| body.circular_orbital_speed_at_radius(6_771_000.0));
    ///
    /// assert!(speed.is_some_and(|value| value > 7_600.0));
    /// ```
    #[must_use]
    pub fn circular_orbital_speed_at_radius(&self, orbital_radius: f64) -> Option<f64> {
        self.gravitational_parameter()
            .and_then(|mu| circular_orbital_speed(mu, orbital_radius))
    }

    /// Computes circular orbital period at a radius around this body.
    #[must_use]
    pub fn circular_orbital_period_at_radius(&self, orbital_radius: f64) -> Option<f64> {
        self.gravitational_parameter()
            .and_then(|mu| circular_orbital_period(mu, orbital_radius))
    }

    /// Computes escape speed at a radius around this body.
    #[must_use]
    pub fn escape_speed_at_radius(&self, distance: f64) -> Option<f64> {
        self.gravitational_parameter()
            .and_then(|mu| escape_speed(mu, distance))
    }
}

/// Elliptical orbit state described by a gravitational parameter and apsides.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EllipticalOrbit {
    /// Central-body gravitational parameter in cubic meters per second squared.
    pub mu: f64,
    /// Periapsis radius in meters.
    pub periapsis_radius: f64,
    /// Apoapsis radius in meters.
    pub apoapsis_radius: f64,
}

impl EllipticalOrbit {
    /// Creates an elliptical orbit from a gravitational parameter and apsides.
    ///
    /// Returns `None` when `mu` is less than or equal to zero or not finite, or when the apsides
    /// are invalid.
    #[must_use]
    pub fn new(mu: f64, periapsis_radius: f64, apoapsis_radius: f64) -> Option<Self> {
        if mu <= 0.0 || !mu.is_finite() {
            return None;
        }

        semi_major_axis_from_apsides(periapsis_radius, apoapsis_radius).map(|_| Self {
            mu,
            periapsis_radius,
            apoapsis_radius,
        })
    }

    /// Computes the semi-major axis.
    #[must_use]
    pub fn semi_major_axis(&self) -> Option<f64> {
        semi_major_axis_from_apsides(self.periapsis_radius, self.apoapsis_radius)
    }

    /// Computes eccentricity.
    #[must_use]
    pub fn eccentricity(&self) -> Option<f64> {
        eccentricity_from_apsides(self.periapsis_radius, self.apoapsis_radius)
    }

    /// Computes the orbital period.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use use_orbit::EllipticalOrbit;
    ///
    /// let orbit = EllipticalOrbit::new(100.0, 10.0, 20.0);
    ///
    /// assert!(orbit.and_then(|value| value.period()).is_some_and(|period| period > 0.0));
    /// ```
    #[must_use]
    pub fn period(&self) -> Option<f64> {
        self.semi_major_axis()
            .and_then(|semi_major_axis| elliptical_orbital_period(self.mu, semi_major_axis))
    }

    /// Computes speed at periapsis.
    #[must_use]
    pub fn periapsis_speed(&self) -> Option<f64> {
        periapsis_speed(self.mu, self.periapsis_radius, self.apoapsis_radius)
    }

    /// Computes speed at apoapsis.
    #[must_use]
    pub fn apoapsis_speed(&self) -> Option<f64> {
        apoapsis_speed(self.mu, self.periapsis_radius, self.apoapsis_radius)
    }
}

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

    fn approx_eq(left: f64, right: f64, tolerance: f64) -> bool {
        (left - right).abs() <= tolerance
    }

    #[test]
    fn gravitational_parameter_matches_constant() {
        assert_eq!(gravitational_parameter(1.0), Some(GRAVITATIONAL_CONSTANT));
        assert_eq!(gravitational_parameter(-1.0), None);
    }

    #[test]
    fn source_mass_from_gravitational_parameter_round_trips() {
        assert!(
            source_mass_from_gravitational_parameter(GRAVITATIONAL_CONSTANT)
                .is_some_and(|value| approx_eq(value, 1.0, 1.0e-12))
        );
        assert_eq!(source_mass_from_gravitational_parameter(-1.0), None);
    }

    #[test]
    fn circular_orbital_speed_handles_valid_and_invalid_inputs() {
        assert!(
            circular_orbital_speed(398_600_441_800_000.0, 6_371_000.0)
                .is_some_and(|value| approx_eq(value, 7_909.8, 1.0))
        );
        assert_eq!(circular_orbital_speed(1.0, 0.0), None);
    }

    #[test]
    fn circular_orbital_period_handles_valid_and_invalid_inputs() {
        assert!(
            circular_orbital_period(398_600_441_800_000.0, 6_371_000.0)
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert_eq!(circular_orbital_period(0.0, 6_371_000.0), None);
    }

    #[test]
    fn orbital_radius_from_period_handles_valid_and_invalid_inputs() {
        assert!(
            orbital_radius_from_period(398_600_441_800_000.0, 5_400.0)
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert_eq!(orbital_radius_from_period(398_600_441_800_000.0, 0.0), None);
    }

    #[test]
    fn orbital_radius_from_circular_speed_handles_valid_and_invalid_inputs() {
        assert_eq!(orbital_radius_from_circular_speed(100.0, 10.0), Some(1.0));
        assert_eq!(orbital_radius_from_circular_speed(100.0, 0.0), None);
    }

    #[test]
    fn semi_major_axis_from_apsides_handles_valid_and_invalid_inputs() {
        assert_eq!(semi_major_axis_from_apsides(10.0, 20.0), Some(15.0));
        assert_eq!(semi_major_axis_from_apsides(20.0, 10.0), None);
    }

    #[test]
    fn eccentricity_from_apsides_handles_circular_and_elliptical_orbits() {
        assert!(
            eccentricity_from_apsides(10.0, 20.0).is_some_and(|value| approx_eq(
                value,
                1.0 / 3.0,
                1.0e-12
            ))
        );
        assert_eq!(eccentricity_from_apsides(10.0, 10.0), Some(0.0));
    }

    #[test]
    fn apsides_from_semi_major_axis_and_eccentricity_round_trip() {
        assert!(
            periapsis_from_semi_major_axis_eccentricity(15.0, 1.0 / 3.0)
                .is_some_and(|value| approx_eq(value, 10.0, 1.0e-12))
        );
        assert!(
            apoapsis_from_semi_major_axis_eccentricity(15.0, 1.0 / 3.0)
                .is_some_and(|value| approx_eq(value, 20.0, 1.0e-12))
        );
        assert_eq!(periapsis_from_semi_major_axis_eccentricity(15.0, 1.0), None);
    }

    #[test]
    fn vis_viva_speed_handles_valid_and_invalid_inputs() {
        assert!(
            vis_viva_speed(100.0, 10.0, 15.0).is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert_eq!(vis_viva_speed(100.0, 0.0, 15.0), None);
    }

    #[test]
    fn apsis_speeds_handle_valid_and_invalid_inputs() {
        assert!(
            periapsis_speed(100.0, 10.0, 20.0)
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert!(
            apoapsis_speed(100.0, 10.0, 20.0).is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert_eq!(periapsis_speed(100.0, 20.0, 10.0), None);
    }

    #[test]
    fn elliptical_orbital_period_handles_valid_and_invalid_inputs() {
        assert!(
            elliptical_orbital_period(100.0, 15.0)
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert_eq!(elliptical_orbital_period(0.0, 15.0), None);
    }

    #[test]
    fn escape_speed_handles_valid_and_invalid_inputs() {
        assert_eq!(escape_speed(100.0, 2.0), Some(10.0));
        assert_eq!(escape_speed(100.0, 0.0), None);
    }

    #[test]
    fn specific_orbital_energy_handles_valid_and_invalid_inputs() {
        assert_eq!(specific_orbital_energy(10.0, 100.0, 10.0), Some(40.0));
        assert_eq!(specific_orbital_energy(10.0, 100.0, 0.0), None);
    }

    #[test]
    fn semi_major_axis_from_specific_energy_handles_valid_and_invalid_inputs() {
        assert_eq!(
            semi_major_axis_from_specific_energy(100.0, -5.0),
            Some(10.0)
        );
        assert_eq!(semi_major_axis_from_specific_energy(100.0, 0.0), None);
    }

    #[test]
    fn altitude_and_radius_helpers_round_trip() {
        assert_eq!(
            orbital_radius_from_altitude(6_371_000.0, 400_000.0),
            Some(6_771_000.0)
        );
        assert_eq!(orbital_radius_from_altitude(6_371_000.0, -1.0), None);
        assert_eq!(
            altitude_from_orbital_radius(6_371_000.0, 6_771_000.0),
            Some(400_000.0)
        );
        assert_eq!(altitude_from_orbital_radius(6_371_000.0, 6_000_000.0), None);
    }

    #[test]
    fn hohmann_helpers_handle_valid_inputs() {
        assert_eq!(hohmann_transfer_semi_major_axis(10.0, 20.0), Some(15.0));
        assert!(
            hohmann_transfer_time(100.0, 10.0, 20.0)
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert!(hohmann_delta_v_1(100.0, 10.0, 20.0).is_some_and(f64::is_finite));
        assert!(hohmann_delta_v_2(100.0, 10.0, 20.0).is_some_and(f64::is_finite));
        assert!(
            hohmann_total_delta_v(100.0, 10.0, 20.0)
                .is_some_and(|value| value.is_finite() && value >= 0.0)
        );
    }

    #[test]
    fn central_body_delegates_to_public_helpers() {
        assert_eq!(
            CentralBody::new(1.0).and_then(|body| body.gravitational_parameter()),
            Some(GRAVITATIONAL_CONSTANT)
        );
        assert!(
            CentralBody::with_radius(5.972e24, 6.371e6)
                .and_then(|body| body.orbital_radius_from_altitude(400_000.0))
                .is_some_and(|value| approx_eq(value, 6.771e6, 1.0e-6))
        );
        assert!(
            CentralBody::with_radius(5.972e24, 6.371e6)
                .and_then(|body| body.circular_orbital_speed_at_radius(6.771e6))
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert_eq!(CentralBody::new(-1.0), None);
        assert_eq!(CentralBody::with_radius(1.0, 0.0), None);
    }

    #[test]
    fn elliptical_orbit_delegates_to_public_helpers() {
        let orbit = EllipticalOrbit::new(100.0, 10.0, 20.0);

        assert_eq!(orbit.and_then(|value| value.semi_major_axis()), Some(15.0));
        assert!(
            orbit
                .and_then(|value| value.eccentricity())
                .is_some_and(|value| approx_eq(value, 1.0 / 3.0, 1.0e-12))
        );
        assert!(
            orbit
                .and_then(|value| value.period())
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert!(
            orbit
                .and_then(|value| value.periapsis_speed())
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert!(
            orbit
                .and_then(|value| value.apoapsis_speed())
                .is_some_and(|value| value.is_finite() && value > 0.0)
        );
        assert_eq!(EllipticalOrbit::new(100.0, 20.0, 10.0), None);
    }
}