sguaba 0.10.3

Hard to misuse rigid body transforms (aka "spatial math") for engineers with other things to worry about than linear algebra.
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
use crate::Vector;
use crate::util::BoundedAngle;
use core::f64::consts::FRAC_PI_2;
use core::fmt::{Display, Formatter};
use core::marker::PhantomData;
use uom::si::f64::{Angle, Length};
use uom::si::{angle::degree, length::meter};

#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(doc)]
use crate::{
    CoordinateSystem,
    systems::{BearingDefined, FrdLike, NedLike},
};
use uom::ConstZero;

/// A direction (conceptually represented as a unit vector) in the [`CoordinateSystem`] `In`.
///
/// A `Bearing` has an azimuth and elevation, whose meanings differ for different kinds of
/// coordinate systems (although there [are conventions][bearing]). The meaning of azimuth and
/// elevation for a given coordinate system is defined by its implementation of [`BearingDefined`].
/// For example, a bearing azimuth in an FRD system is relative to the forward direction of the
/// system, while in both NED _and_ ENU (somewhat counter-intuitively) it is relative to North. In
/// general, azimuth _tends to_ align with yaw and elevation with pitch (as defined by Tait-Bryan
/// angles).
///
/// See also [horizontal coordinate systems][azel].
///
/// To construct a bearing, you can either use [a builder] via [`Bearing::builder`] or provide a
/// [`Components`] to [`Bearing::build`]. The following are equivalent:
///
/// ```rust
/// use sguaba::{Bearing, system};
/// use uom::si::f64::Angle;
/// use uom::si::angle::degree;
///
/// system!(struct PlaneFrd using FRD);
///
/// Bearing::<PlaneFrd>::builder()
///     // clockwise from forward
///     .azimuth(Angle::new::<degree>(20.))
///     // upwards from straight-ahead
///     .elevation(Angle::new::<degree>(10.))
///     .expect("elevation is in [-90°, 90°]")
///     .build();
/// ```
///
/// ```rust
/// use sguaba::{Bearing, system, builder::bearing::Components};
/// use uom::si::f64::Angle;
/// use uom::si::angle::degree;
///
/// system!(struct PlaneFrd using FRD);
///
/// Bearing::<PlaneFrd>::build(Components {
///   // clockwise from forward
///   azimuth: Angle::new::<degree>(20.),
///   // upwards from straight-ahead
///   elevation: Angle::new::<degree>(10.),
/// }).expect("elevation is in [-90°, 90°]");
/// ```
///
/// [bearing]: https://en.wikipedia.org/wiki/Bearing_%28navigation%29
/// [azel]: https://en.wikipedia.org/wiki/Horizontal_coordinate_system
/// [a builder]: https://rust-unofficial.github.io/patterns/patterns/creational/builder.html
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// don't require In: Serialize/Deserialize since we skip it anyway
#[cfg_attr(feature = "serde", serde(bound = ""))]
pub struct Bearing<In> {
    azimuth: Angle,
    elevation: Angle,

    #[cfg_attr(feature = "serde", serde(skip))]
    system: PhantomData<In>,
}

// manual impls of Clone and Copy to avoid requiring In: Copy + Clone
impl<In> Clone for Bearing<In> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<In> Copy for Bearing<In> {}

impl<In> Bearing<In> {
    /// Constructs a bearing towards the given azimuth and elevation in the [`CoordinateSystem`]
    /// `In`.
    ///
    /// The elevation must be in [-90°,90°] % 360°. If it is not, this function returns `None`.
    #[must_use]
    pub fn build(Components { azimuth, elevation }: Components) -> Option<Self> {
        Some(
            Self::builder()
                .azimuth(azimuth)
                .elevation(elevation)?
                .build(),
        )
    }

    /// Provides a constructor for a bearing in the [`CoordinateSystem`] `In`.
    pub fn builder() -> Builder<In, MissingAzimuth, MissingElevation> {
        Builder {
            under_construction: Self {
                azimuth: Angle::ZERO,
                elevation: Angle::ZERO,
                system: PhantomData,
            },
            has: (PhantomData, PhantomData),
        }
    }

    /// Turns a [`Bearing`] back into a builder so that its components can be modified.
    pub fn to_builder(self) -> Builder<In, HasAzimuth, HasElevation> {
        Builder {
            under_construction: Self {
                azimuth: self.azimuth,
                elevation: self.elevation,
                system: PhantomData,
            },
            has: (PhantomData, PhantomData),
        }
    }

    /// Constructs a bearing towards the given azimuth and elevation in the [`CoordinateSystem`]
    /// `In`.
    ///
    /// Prefer [`Bearing::build`] or [`Bearing::builder`] to avoid risk of argument order
    /// confusion. This function will be removed in a future version of Sguaba in favor of those.
    ///
    /// The elevation must be in [-90°,90°] % 360°. If it is not, this function returns `None`.
    #[must_use]
    #[deprecated = "prefer `Bearing::build` or `Bearing::builder` to avoid risk of argument order confusion"]
    pub fn new(azimuth: impl Into<Angle>, elevation: impl Into<Angle>) -> Option<Self> {
        Self::build(Components {
            azimuth: azimuth.into(),
            elevation: elevation.into(),
        })
    }

    /// Returns the azimuthal angle of this bearing.
    ///
    /// The definition of the azimuthal angle depends on the implementation of [`BearingDefined`]
    /// for `In`.
    ///
    /// There is no guarantee on the numeric range of the azimuthal angle.
    #[must_use]
    pub fn azimuth(&self) -> Angle {
        self.azimuth
    }

    /// Returns the elevation angle of this bearing.
    ///
    /// The definition of the azimuthal angle depends on the implementation of [`BearingDefined`]
    /// for `In`.
    ///
    /// The returned value is always in [-90°, 90°] ± N × 360°.
    #[must_use]
    pub fn elevation(&self) -> Angle {
        self.elevation
    }

    /// Returns a unit vector pointing away from origin at this bearing.
    #[must_use]
    pub fn to_unit_vector(&self) -> Vector<In>
    where
        In: crate::systems::BearingDefined,
    {
        Vector::from_bearing(*self, Length::new::<meter>(1.))
    }

    /// Constructs a bearing in coordinate system `In` with azimuth and elevation set to 0.
    ///
    /// The definition of the azimuthal angle depends on the implementation of [`BearingDefined`]
    /// for `In`.
    #[must_use]
    pub fn zero() -> Self {
        Self {
            azimuth: Angle::ZERO,
            elevation: Angle::ZERO,
            system: PhantomData,
        }
    }
}

impl<In> Default for Bearing<In> {
    fn default() -> Self {
        Self::zero()
    }
}

impl<In> Display for Bearing<In> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "bearing {:?}° at elevation {:?}°",
            self.azimuth().get::<degree>(),
            self.elevation().get::<degree>(),
        )
    }
}

impl<In> PartialEq<Self> for Bearing<In> {
    fn eq(&self, other: &Self) -> bool {
        self.elevation.eq(&other.elevation) && self.azimuth.eq(&other.azimuth)
    }
}

#[cfg(any(feature = "approx", test))]
impl<In> AbsDiffEq<Self> for Bearing<In> {
    type Epsilon = <f64 as AbsDiffEq>::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        BoundedAngle::default_epsilon()
    }

    fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
        BoundedAngle::abs_diff_eq(
            &BoundedAngle::new(self.azimuth()),
            &BoundedAngle::new(other.azimuth()),
            epsilon,
        ) && BoundedAngle::abs_diff_eq(
            &BoundedAngle::new(self.elevation()),
            &BoundedAngle::new(other.elevation()),
            epsilon,
        )
    }
}

#[cfg(any(feature = "approx", test))]
impl<In> RelativeEq for Bearing<In> {
    fn default_max_relative() -> Self::Epsilon {
        BoundedAngle::default_max_relative()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        BoundedAngle::relative_eq(
            &BoundedAngle::new(self.azimuth()),
            &BoundedAngle::new(other.azimuth()),
            epsilon,
            max_relative,
        ) && BoundedAngle::relative_eq(
            &BoundedAngle::new(self.elevation()),
            &BoundedAngle::new(other.elevation()),
            epsilon,
            max_relative,
        )
    }
}

/// Argument type for [`Bearing::build`].
#[derive(Debug, Default)]
#[must_use]
pub struct Components {
    /// The azimuthal angle of the proposed [`Bearing`].
    pub azimuth: Angle,

    /// The elevation angle of the proposed [`Bearing`].
    ///
    /// The elevation must be in [-90°,90°] % 360° to form a valid [`Bearing`].
    pub elevation: Angle,
}

/// Used to indicate that a partially-constructed [`Bearing`] is missing the azimuthal component.
pub struct MissingAzimuth;
/// Used to indicate that a partially-constructed [`Bearing`] has the azimuthal component set.
pub struct HasAzimuth;
/// Used to indicate that a partially-constructed [`Bearing`] is missing the elevation component.
pub struct MissingElevation;
/// Used to indicate that a partially-constructed [`Bearing`] has the elevation component set.
pub struct HasElevation;

/// [Builder] for a [`Bearing`].
///
/// Construct one through [`Bearing::builder`] or [`Bearing::to_builder`], and finalize with
/// [`Builder::build`].
///
/// [Builder]: https://rust-unofficial.github.io/patterns/patterns/creational/builder.html
#[derive(Debug)]
#[must_use]
pub struct Builder<In, Azimuth, Elevation> {
    under_construction: Bearing<In>,
    has: (PhantomData<Azimuth>, PhantomData<Elevation>),
}

// manual impls of Clone and Copy to avoid requiring In: Copy + Clone
impl<In, H1, H2> Clone for Builder<In, H1, H2> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<In, H1, H2> Copy for Builder<In, H1, H2> {}

impl<In, H1, H2> Builder<In, H1, H2> {
    /// Sets the azimuthal angle of the [`Bearing`]-to-be.
    pub fn azimuth(mut self, angle: impl Into<Angle>) -> Builder<In, HasAzimuth, H2> {
        self.under_construction.azimuth = angle.into();
        Builder {
            under_construction: self.under_construction,
            has: (PhantomData::<HasAzimuth>, self.has.1),
        }
    }

    /// Sets the elevation angle of the [`Bearing`]-to-be.
    ///
    /// The elevation must be in [-90°,90°] % 360°. If it is not, this function returns `None`.
    pub fn elevation(mut self, angle: impl Into<Angle>) -> Option<Builder<In, H1, HasElevation>> {
        let elevation = angle.into();
        let elevation_signed = BoundedAngle::new(elevation).to_signed_range();
        if !(-FRAC_PI_2..=FRAC_PI_2).contains(&elevation_signed) {
            None
        } else {
            self.under_construction.elevation = elevation;
            Some(Builder {
                under_construction: self.under_construction,
                has: (self.has.0, PhantomData::<HasElevation>),
            })
        }
    }
}

impl<In> Builder<In, HasAzimuth, HasElevation> {
    #[must_use]
    pub fn build(self) -> Bearing<In> {
        self.under_construction
    }
}

/// Constructs a [`Bearing`] with compile-time validated angles using unit suffixes.
///
/// This macro provides a safe way to construct bearings with compile-time known angles,
/// eliminating the need for `.expect()` calls on the elevation validation.
///
/// # Supported Units
/// - `deg` - degrees
/// - `rad` - radians
///
/// # Examples
/// ```rust
/// use sguaba::{bearing, system};
///
/// system!(struct PlaneFrd using FRD);
///
/// // Using degrees with explicit coordinate system
/// let bearing1 = bearing!(azimuth = deg(20.0), elevation = deg(10.0); in PlaneFrd);
///
/// // Using degrees with inferred coordinate system (requires type annotation)
/// let bearing2: sguaba::Bearing<PlaneFrd> = bearing!(azimuth = deg(20.0), elevation = deg(10.0));
///
/// // Using radians
/// let bearing3 = bearing!(azimuth = rad(0.349), elevation = rad(0.175); in PlaneFrd);
/// ```
///
/// # Compile-time validation
///
/// The following examples should fail to compile because elevation is out of range:
///
/// ```compile_fail
/// # use sguaba::{bearing, system};
/// # system!(struct PlaneFrd using FRD);
/// // Elevation > 90° should fail
/// let bearing = bearing!(azimuth = deg(0.0), elevation = deg(91.0); in PlaneFrd);
/// ```
///
/// ```compile_fail
/// # use sguaba::{bearing, system};
/// # system!(struct PlaneFrd using FRD);
/// // Elevation < -90° should fail
/// let bearing = bearing!(azimuth = deg(0.0), elevation = deg(-91.0); in PlaneFrd);
/// ```
///
/// ```compile_fail
/// # use sguaba::{bearing, system};
/// # system!(struct PlaneFrd using FRD);
/// // Elevation > π/2 radians should fail
/// let bearing = bearing!(azimuth = rad(0.0), elevation = rad(1.58); in PlaneFrd);
/// ```
///
/// ```compile_fail
/// # use sguaba::{bearing, system};
/// # system!(struct PlaneFrd using FRD);
/// // Elevation < -π/2 radians should fail
/// let bearing = bearing!(azimuth = rad(0.0), elevation = rad(-1.58); in PlaneFrd);
/// ```
#[macro_export]
macro_rules! bearing {
    (azimuth = deg($az:expr), elevation = deg($el:expr) $(,)?) => {
        bearing!(azimuth = deg($az), elevation = deg($el); in _)
    };
    (azimuth = rad($az:expr), elevation = rad($el:expr) $(,)?) => {
        bearing!(azimuth = rad($az), elevation = rad($el); in _)
    };
    (azimuth = deg($az:expr), elevation = deg($el:expr); in $system:ty) => {{
        const _: () = assert!(
            $el >= -90.0 && $el <= 90.0,
            "elevation must be in [-90°, 90°]"
        );
        $crate::Bearing::<$system>::builder()
            .azimuth(::uom::si::f64::Angle::new::<::uom::si::angle::degree>($az))
            .elevation(::uom::si::f64::Angle::new::<::uom::si::angle::degree>($el))
            .expect("elevation is valid because it was checked at compile time")
            .build()
    }};
    (azimuth = rad($az:expr), elevation = rad($el:expr); in $system:ty) => {{
        const _: () = assert!(
            $el >= -::core::f64::consts::FRAC_PI_2 && $el <= ::core::f64::consts::FRAC_PI_2,
            "elevation must be in [-π/2, π/2] radians"
        );
        $crate::Bearing::<$system>::builder()
            .azimuth(::uom::si::f64::Angle::new::<::uom::si::angle::radian>($az))
            .elevation(::uom::si::f64::Angle::new::<::uom::si::angle::radian>($el))
            .expect("elevation is valid because it was checked at compile time")
            .build()
    }};
}

#[cfg(test)]
mod tests {
    use crate::coordinate_systems::Frd;
    use crate::coordinates::Coordinate;
    use crate::directions::{Bearing, Components};
    use crate::util::BoundedAngle;
    use crate::{coordinate, vector};
    use approx::{assert_abs_diff_eq, assert_relative_eq};
    use quickcheck::quickcheck;
    use rstest::rstest;
    use std::boxed::Box;
    use std::f64::consts::{FRAC_PI_2, PI, TAU};
    use uom::si::f64::{Angle, Length};
    use uom::si::{
        angle::{degree, radian},
        length::meter,
    };

    fn m(meters: f64) -> Length {
        Length::new::<meter>(meters)
    }
    fn d(degrees: f64) -> Angle {
        Angle::new::<degree>(degrees)
    }

    // recall that in Frd (which we're using here), positive Z is _down_
    #[rstest]
    #[case(0.0, 90.0, [0.0, 0.0, -1.0])]
    #[case(90.0, 90.0, [0.0, 0.0, -1.0])]
    #[case(180.0, 90.0, [0.0, 0.0, -1.0])]
    #[case(270.0, 90.0, [0.0, 0.0, -1.0])]
    #[case(0.0, 0.0, [1.0, 0.0, 0.0])]
    #[case(90.0, 0.0, [0.0, 1.0, 0.0])]
    #[case(180.0, 0.0, [-1.0, 0.0, 0.0])]
    #[case(270.0, 0.0, [0.0, -1.0, 0.0])]
    #[case(0.0, -90.0, [0.0, 0.0, 1.0])]
    #[case(90.0, -90.0, [0.0, 0.0, 1.0])]
    #[case(180.0, -90.0, [0.0, 0.0, 1.0])]
    #[case(270.0, -90.0, [0.0, 0.0, 1.0])]
    fn to_unit_vector(#[case] azimuth: f64, #[case] elevation: f64, #[case] expected: [f64; 3]) {
        assert_relative_eq!(
            Bearing::<Frd>::build(Components {
                azimuth: d(azimuth),
                elevation: d(elevation)
            })
            .unwrap()
            .to_unit_vector(),
            vector!(f = m(expected[0]), r = m(expected[1]), d = m(expected[2]))
        );
    }

    #[test]
    fn bearing_macro() {
        // Test degrees with explicit coordinate system
        let bearing1 = bearing!(azimuth = deg(20.0), elevation = deg(10.0); in Frd);
        assert_eq!(bearing1.azimuth(), d(20.0));
        assert_eq!(bearing1.elevation(), d(10.0));

        // Test degrees with inferred coordinate system
        let bearing2: Bearing<Frd> = bearing!(azimuth = deg(30.0), elevation = deg(5.0));
        assert_eq!(bearing2.azimuth(), d(30.0));
        assert_eq!(bearing2.elevation(), d(5.0));

        // Test radians with explicit coordinate system
        let bearing3 = bearing!(azimuth = rad(0.349), elevation = rad(0.175); in Frd);
        assert_relative_eq!(bearing3.azimuth().get::<radian>(), 0.349);
        assert_relative_eq!(bearing3.elevation().get::<radian>(), 0.175);

        // Test radians with inferred coordinate system
        let bearing4: Bearing<Frd> = bearing!(azimuth = rad(0.5), elevation = rad(0.1));
        assert_relative_eq!(bearing4.azimuth().get::<radian>(), 0.5);
        assert_relative_eq!(bearing4.elevation().get::<radian>(), 0.1);

        // Test boundary values
        let bearing5 = bearing!(azimuth = deg(0.0), elevation = deg(90.0); in Frd);
        assert_eq!(bearing5.azimuth(), d(0.0));
        assert_eq!(bearing5.elevation(), d(90.0));

        let bearing6 = bearing!(azimuth = deg(0.0), elevation = deg(-90.0); in Frd);
        assert_eq!(bearing6.azimuth(), d(0.0));
        assert_eq!(bearing6.elevation(), d(-90.0));

        // Test with radians at boundaries
        let bearing7 = bearing!(azimuth = rad(0.0), elevation = rad(FRAC_PI_2); in Frd);
        assert_relative_eq!(bearing7.elevation().get::<radian>(), FRAC_PI_2);

        let bearing8 = bearing!(azimuth = rad(0.0), elevation = rad(-FRAC_PI_2); in Frd);
        assert_relative_eq!(bearing8.elevation().get::<radian>(), -FRAC_PI_2);
    }

    #[test]
    fn bearing_builder() {
        let bearing = Bearing::<Frd>::build(Components {
            azimuth: d(1.0),
            elevation: d(2.0),
        })
        .unwrap();
        assert_eq!(bearing.azimuth(), d(1.0));
        assert_eq!(bearing.elevation(), d(2.0));

        let bearing = bearing.to_builder().azimuth(d(10.0)).build();

        assert_eq!(bearing.azimuth(), d(10.0));
        assert_eq!(bearing.elevation(), d(2.0));

        let bearing = bearing.to_builder().elevation(d(20.0)).unwrap().build();

        assert_eq!(bearing.azimuth(), d(10.0));
        assert_eq!(bearing.elevation(), d(20.0));
    }

    impl<In> quickcheck::Arbitrary for Bearing<In>
    where
        In: 'static,
    {
        fn arbitrary(g: &mut quickcheck::Gen) -> Self {
            // quickcheck will give us awkward f64 values -- we ignore those
            let azimuth = loop {
                match f64::arbitrary(g) {
                    0. => break 0.,
                    f if f.is_normal() => break f,
                    _ => {}
                }
            };
            let elevation = loop {
                match f64::arbitrary(g) {
                    0. => break 0.,
                    f if f.is_normal() => break f,
                    _ => {}
                }
            };
            Self {
                elevation: Angle::new::<radian>(elevation.rem_euclid(PI) - FRAC_PI_2),
                azimuth: Angle::new::<radian>(azimuth.rem_euclid(TAU)),
                system: core::marker::PhantomData,
            }
        }

        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
            let Self {
                azimuth,
                elevation,
                system: phantom_data,
            } = *self;
            if azimuth.get::<radian>() == 0. {
                Box::new(elevation.get::<radian>().shrink().map(move |el| Self {
                    elevation: Angle::new::<radian>(el),
                    azimuth,
                    system: phantom_data,
                }))
            } else {
                Box::new(azimuth.get::<radian>().shrink().map(move |az| Self {
                    elevation,
                    azimuth: Angle::new::<radian>(az),
                    system: phantom_data,
                }))
            }
        }
    }

    quickcheck! {
        fn bearing_vector_roundtrip(bearing: Bearing<Frd>) -> () {
            // azimuth won't be preserved if the bearing is along the Z axis
            let mut bearing = bearing;
            if approx::relative_eq!(bearing.elevation().get::<radian>(), FRAC_PI_2)
                || approx::relative_eq!(bearing.elevation().get::<radian>(), -FRAC_PI_2) {
                bearing.azimuth = Angle::new::<radian>(0.);
            }

            assert_relative_eq!(
                bearing,
                bearing
                  .to_unit_vector()
                  .bearing_at_origin()
                  .expect("it was a bearing, so it can be again")
            );
        }
    }

    /// Asserts that the angles are within the correct ranges
    fn _assert_correct_angles(point: (i16, i16, i16), direction: &Bearing<Frd>) {
        let positive_map = (point.0 >= 0, point.1 >= 0, point.2 >= 0);

        // Consider the z-axis pointing down. Then points with positive z-value are "below" the xy-plane.
        // These are the 8 sections spanned by the coordinate system.

        let error_checker = |(az_start, az_end, el_start, el_end): (f64, f64, f64, f64)| {
            if !(direction.elevation() >= d(el_start) && direction.elevation() <= d(el_end)) {
                if BoundedAngle::new(d(el_start) - direction.elevation()).to_signed_range()
                    < f64::EPSILON
                {
                    // boundary conditions that are also acceptable
                } else {
                    panic!(
                        "{point:?} elevation is not in [{el_start:?}, {el_end:?}] (was {:?})",
                        direction.elevation().get::<degree>()
                    );
                }
            }
            if !(direction.azimuth() >= d(az_start) && direction.azimuth() <= d(az_end)) {
                if BoundedAngle::new(d(az_start) - direction.azimuth()).to_signed_range()
                    < f64::EPSILON
                {
                    // boundary conditions that are also acceptable
                } else {
                    panic!(
                        "{point:?} azimuth is not in [{az_start:?}, {az_end:?}] (was {:?})",
                        direction.azimuth().get::<degree>()
                    );
                }
            }
        };

        let (azimuth_start, azimuth_stop, elevation_start, elevation_stop) = match positive_map {
            // x, y, z
            (true, true, true) => {
                // Point is below the xy-plane and to the top-right
                // Azimuth between 0 and 90
                // Elevation between 270 and 360  [-90, 0]
                (0., 90., -90., 0.)
            }
            (true, true, false) => {
                // Point is above the xy-plane and to the top-right
                // Azimuth between 0 and 90
                // Elevation between 0 and 90
                (0., 90., 0., 90.)
            }
            (true, false, true) => {
                // Point is below the xy-plane and to the top-left
                // Azimuth between 270 and 360
                // Elevation between 270 and 360 [-90, 0]
                (-90., 0., -90., 0.)
            }
            (true, false, false) => {
                // Point is above the xy-plane and to the top-left
                // Azimuth between 270 and 360
                // Elevation between 0 and 90
                (-90., 0., 0., 90.)
            }
            (false, true, true) => {
                // Point is below the xy-plane and to the bottom-right
                // Azimuth between 90 and 180
                // Elevation between 270 and 360 [-90, 0]
                (90., 180., -90., 0.)
            }
            (false, true, false) => {
                // Point is above the xy-plane and to the bottom-right
                // Azimuth between 90 and 180
                // Elevation between 0 and 90
                (90., 180., 0., 90.)
            }
            (false, false, true) => {
                // Point is below the xy-plane and to the bottom-left
                // Azimuth between 180 and 270
                // Elevation between 270 and 360  [-90, 0]
                (-180., -90., -90., 0.)
            }
            (false, false, false) => {
                // Point is above the xy-plane and to the bottom-left
                // Azimuth between 180 and 270
                // Elevation between 0 and 90
                (-180., -90., 0., 90.)
            }
        };

        error_checker((azimuth_start, azimuth_stop, elevation_start, elevation_stop));
    }

    // Test the cartesian -> spherical -> cartesian conversion around the origin in a [-100, 100) grid.
    quickcheck! {
        fn azimuth_elevation_range_conversion_works(x: i16, y: i16, z: i16) -> () {
            let frd_coordinate = coordinate! {
                f = m(x as f64),
                r = m(y as f64),
                d = m(z as f64);
                in Frd
            };
            let frd_direction = frd_coordinate.bearing_from_origin();

            let Some(frd_direction) = frd_direction else {
                assert!(
                    x == 0 && y == 0,
                    "only zero vectors or Z-aligned vectors have no bearing"
                );
                return;
            };

            let range = frd_coordinate.distance_from_origin();

            let frd_again = Coordinate::<Frd>::from_bearing(frd_direction, range);

            assert_relative_eq!(frd_coordinate, frd_again);

            assert_abs_diff_eq!(
                frd_direction,
                frd_again
                    .bearing_from_origin()
                    .expect("if it could be a bearing once, it can again")
            );

            _assert_correct_angles((x, y, z), &frd_direction);
        }
    }
}