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
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
use crate::builder::{Set, Unset};
use crate::coordinate_systems::HasComponents;
use crate::directions::Bearing;
use crate::engineering::Orientation;
use crate::float_math::FloatMath;
use crate::{
    Coordinate, CoordinateSystem,
    systems::{EnuLike, EquivalentTo, FrdLike, NedLike, RightHandedXyzLike},
};
use crate::{LengthPossiblyPer, Vector3};
use core::fmt::{Display, Formatter};
use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use core::{fmt, iter::Sum};
use typenum::{Integer, N1, N2, Z0};
use uom::ConstZero;
use uom::si::f64::{Acceleration, Angle, Length, Velocity};
use uom::si::{
    acceleration::meter_per_second_squared, angle::radian, length::meter,
    velocity::meter_per_second,
};

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

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

#[cfg(doc)]
use crate::{
    math::RigidBodyTransform,
    systems::BearingDefined,
    vector::{AccelerationVector, LengthVector, VelocityVector},
};

/// Defines a vector (ie, direction with magnitude) in the coordinate system specified by `In`.
///
/// You can construct one using [Cartesian](Vector::build) or
/// [spherical](Vector::from_spherical) components, or using [bearing +
/// range](Vector::from_bearing). You can also use the [`vector!`](crate::vector!) macro
/// for a concise constructor with named arguments.
///
/// Depending on the convention of the coordinate system (eg, [`NedLike`], [`FrdLike`], or
/// [`RightHandedXyzLike`]), you'll have different appropriately-named accessors for the vector's
/// cartesian components like [`Vector::ned_north`] or [`Vector::frd_front`].
///
/// # Working with different `Vector` units
///
/// sguaba supports vectors representing different physical quantities:
///
/// - **Length vectors** (`Vector<In>`, `Vector<In, Z0>`, or [`LengthVector`]) for positions and displacements
/// - **Velocity vectors** (`Vector<In, N1>` or [`VelocityVector`]) for velocities
/// - **Acceleration vectors** (`Vector<In, N2>` or [`AccelerationVector`]) for accelerations
///
/// All vector operations (addition, scaling, transforms) are supported for all units.
///
/// ## Length `Vector`
/// ```rust
/// # use sguaba::{system, vector, Vector};
/// # use uom::si::f64::Length;
/// # use uom::si::length::meter;
/// # system!(struct PlaneFrd using FRD);
///
/// let displacement = vector!(
///     f = Length::new::<meter>(10.0),
///     r = Length::new::<meter>(5.0),
///     d = Length::new::<meter>(0.0);
///     in PlaneFrd
/// );
/// ```
///
/// ## Velocity `Vector`
/// ```rust
/// # use sguaba::{system, vector, Vector, vector::VelocityVector};
/// # use uom::si::f64::Velocity;
/// # use uom::si::velocity::meter_per_second;
/// # system!(struct PlaneFrd using FRD);
///
/// let velocity = vector!(
///     f = Velocity::new::<meter_per_second>(100.0), // forward
///     r = Velocity::new::<meter_per_second>(0.0),   // right
///     d = Velocity::new::<meter_per_second>(5.0);   // down
///     in PlaneFrd
/// );
/// ```
///
/// ## Acceleration `Vector`
/// ```rust
/// # use sguaba::{system, vector, Vector, vector::AccelerationVector};
/// # use uom::si::f64::Acceleration;
/// # use uom::si::acceleration::meter_per_second_squared;
/// # system!(struct PlaneFrd using FRD);
/// # system!(struct PlaneNed using NED);
///
/// // Commanded acceleration (climb has negative d in FRD)
/// let acceleration = vector!(
///     f = Acceleration::new::<meter_per_second_squared>(2.0),  // speed up
///     r = Acceleration::new::<meter_per_second_squared>(0.0),
///     d = Acceleration::new::<meter_per_second_squared>(-1.0); // climb
///     in PlaneFrd
/// );
///
/// // Can use other acceleration units, such as standard gravity
/// let gravity = vector!(
///     n = Acceleration::new::<meter_per_second_squared>(0.0),
///     e = Acceleration::new::<meter_per_second_squared>(0.0),
///     d = Acceleration::new::<uom::si::acceleration::standard_gravity>(1.);
///     in PlaneNed
/// );
/// ```
///
/// ## Type safety with units
///
/// The type system prevents mixing incompatible units:
///
/// ```compile_fail
/// # use sguaba::{system, vector};
/// # use uom::si::f64::{Length, Velocity};
/// # use uom::si::{length::meter, velocity::meter_per_second};
/// # system!(struct PlaneFrd using FRD);
///
/// // Cannot put meters in a velocity vector - this will not compile
/// let bad_velocity = vector!(
///     f = Length::new::<meter>(10.0), // Length, not Velocity!
///     r = Velocity::new::<meter_per_second>(5.0),
///     d = Velocity::new::<meter_per_second>(0.0);
///     in PlaneFrd
/// );
/// ```
///
/// ```compile_fail
/// # use sguaba::{system, vector, Vector, vector::{VelocityVector, AccelerationVector}};
/// # use uom::si::f64::{Velocity, Acceleration};
/// # use uom::si::{velocity::meter_per_second, acceleration::meter_per_second_squared};
/// # system!(struct PlaneFrd using FRD);
///
/// // Cannot add length and acceleration vectors - this will not compile
/// let velocity = vector!(
///     f = Velocity::new::<meter_per_second>(10.0),
///     r = Velocity::new::<meter_per_second>(0.0),
///     d = Velocity::new::<meter_per_second>(0.0);
///     in PlaneFrd
/// );
/// let acceleration = vector!(
///     f = Acceleration::new::<meter_per_second_squared>(2.0),
///     r = Acceleration::new::<meter_per_second_squared>(0.0),
///     d = Acceleration::new::<meter_per_second_squared>(0.0);
///     in PlaneFrd
/// );
/// let bad_sum = velocity + acceleration; // Different Time parameters!
/// ```
///
/// Only [`LengthVector`] can be created from [`Coordinate`], as coordinates are inherently just
/// describing positions in a coordinate system, which are displacements (ie, lengths) from origin:
///
/// ```compile_fail
/// # use sguaba::{system, coordinate, vector, vector::{LengthVector, VelocityVector}};
/// # use uom::si::f64::{Length, Velocity};
/// # use uom::si::{length::meter, velocity::meter_per_second};
/// # system!(struct PlaneFrd using FRD);
///
/// let coordinate = coordinate!(
///     f = Length::new::<meter>(10.0),
///     r = Length::new::<meter>(5.0),
///     d = Length::new::<meter>(0.0);
///     in PlaneFrd
/// );
///
/// // This compiles fine
/// let _: LengthVector<PlaneFrd> = coordinate.into();
///
/// // This will not compile as that `impl From` does not exist
/// let _: VelocityVector<PlaneFrd> = coordinate.into();
/// ```
///
/// <div class="warning">
///
/// Note that this type implements `Deserialize` despite having `unsafe` constructors -- this is
/// because doing otherwise would be extremely unergonomic. However, when deserializing, the
/// coordinate system of the deserialized value is _not_ checked, so this is a foot-gun to be
/// mindful of.
///
/// </div>
#[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 = ""))]
// no need for the "inner": indirection
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Vector<In, Time = Z0>
where
    Time: Integer,
{
    /// X, Y, Z in meters
    pub(crate) inner: Vector3,
    #[cfg_attr(feature = "serde", serde(skip))]
    system: PhantomData<In>,
    #[cfg_attr(feature = "serde", serde(skip))]
    unit: PhantomData<LengthPossiblyPer<Time>>,
}

/// Trait used to access a [`Vector`]'s Cartesian components in its native units.
///
/// Only used to abstract over `Time`; should never be used in user code.
///
/// This trait is sealed, and so also cannot be implemented by user code.
#[doc(hidden)]
pub trait LengthBasedComponents<In, Time>: private::Sealed
where
    Time: Integer,
{
    /// Directly construct from constituent components.
    fn from_cartesian(xyz: [LengthPossiblyPer<Time>; 3]) -> Self;
    /// Deconstruct into constituent components.
    fn to_cartesian(&self) -> [LengthPossiblyPer<Time>; 3];

    /// Cast constituent component unit to [`Length`].
    ///
    /// This is entirely intended for _internal_ use, such as to allow conversion to and from
    /// [`Coordinate`] (which is only ever [`Length`]-unit). You should strongly avoid using this
    /// directly, as it allows units to be confused.
    // NOTE(jon): while it's tempting to just re-assign the .value of the various uom types that
    // involve length, we're not _guaranteed_ that uom's inner representation for length, velocity,
    // and acceleration is the same, so we have to do the whole dance and expect it to be elided by
    // the compiler.
    fn recast_to_length(v: LengthPossiblyPer<Time>) -> Length;
}

impl<In> LengthBasedComponents<In, Z0> for Vector<In, Z0> {
    fn from_cartesian([x, y, z]: [Length; 3]) -> Self {
        Self::from_nalgebra_vector(Vector3::new(
            x.get::<meter>(),
            y.get::<meter>(),
            z.get::<meter>(),
        ))
    }
    fn to_cartesian(&self) -> [Length; 3] {
        [
            Length::new::<meter>(self.inner.x),
            Length::new::<meter>(self.inner.y),
            Length::new::<meter>(self.inner.z),
        ]
    }
    fn recast_to_length(v: LengthPossiblyPer<Z0>) -> Length {
        core::convert::identity(v)
    }
}

impl<In> LengthBasedComponents<In, N1> for Vector<In, N1> {
    fn from_cartesian([x, y, z]: [Velocity; 3]) -> Self {
        Self::from_nalgebra_vector(Vector3::new(
            x.get::<meter_per_second>(),
            y.get::<meter_per_second>(),
            z.get::<meter_per_second>(),
        ))
    }
    fn to_cartesian(&self) -> [Velocity; 3] {
        [
            Velocity::new::<meter_per_second>(self.inner.x),
            Velocity::new::<meter_per_second>(self.inner.y),
            Velocity::new::<meter_per_second>(self.inner.z),
        ]
    }
    fn recast_to_length(v: LengthPossiblyPer<N1>) -> Length {
        Length::new::<meter>(v.get::<meter_per_second>())
    }
}

impl<In> LengthBasedComponents<In, N2> for Vector<In, N2> {
    fn from_cartesian([x, y, z]: [Acceleration; 3]) -> Self {
        Self::from_nalgebra_vector(Vector3::new(
            x.get::<meter_per_second_squared>(),
            y.get::<meter_per_second_squared>(),
            z.get::<meter_per_second_squared>(),
        ))
    }
    fn to_cartesian(&self) -> [Acceleration; 3] {
        [
            Acceleration::new::<meter_per_second_squared>(self.inner.x),
            Acceleration::new::<meter_per_second_squared>(self.inner.y),
            Acceleration::new::<meter_per_second_squared>(self.inner.z),
        ]
    }
    fn recast_to_length(v: LengthPossiblyPer<N2>) -> Length {
        Length::new::<meter>(v.get::<meter_per_second_squared>())
    }
}

mod private {
    pub trait Sealed {}

    impl<In> Sealed for super::Vector<In, typenum::Z0> {}
    impl<In> Sealed for super::Vector<In, typenum::N1> {}
    impl<In> Sealed for super::Vector<In, typenum::N2> {}
}

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

/// Quickly construct a [`Vector`] using named components.
///
/// This macro allows constructing [`Vector`]s for a coordinate system by naming the arguments
/// according to its [`CoordinateSystem::Convention`]. Using the wrong named arguments (eg, trying
/// to make a [`NedLike`] [`Vector`] using `f = `) will produce an error at compile-time.
///
/// For [`NedLike`], use:
///
/// ```rust
/// # use sguaba::{vector, system, Vector};
/// # use uom::si::f64::Length;
/// # use uom::si::length::meter;
/// # system!(struct Ned using NED);
/// # let _: Vector<Ned> =
/// vector! {
///     n = Length::new::<meter>(1.),
///     e = Length::new::<meter>(2.),
///     d = Length::new::<meter>(3.),
/// }
/// # ;
/// ```
///
/// For [`FrdLike`], use:
///
/// ```rust
/// # use sguaba::{vector, system, Vector};
/// # use uom::si::f64::Length;
/// # use uom::si::length::meter;
/// # system!(struct Frd using FRD);
/// # let _: Vector<Frd> =
/// vector! {
///     f = Length::new::<meter>(1.),
///     r = Length::new::<meter>(2.),
///     d = Length::new::<meter>(3.),
/// }
/// # ;
/// ```
///
/// For [`RightHandedXyzLike`], use:
///
/// ```rust
/// # use sguaba::{vector, systems::Ecef, Vector};
/// # use uom::si::f64::Length;
/// # use uom::si::length::meter;
/// # let _: Vector<Ecef> =
/// vector! {
///     x = Length::new::<meter>(1.),
///     y = Length::new::<meter>(2.),
///     z = Length::new::<meter>(3.),
/// }
/// # ;
/// ```
///
/// The macro also allows explicitly specifying the coordinate system `In` for the returned
/// [`Vector`] (which is otherwise inferred) by suffixing the component list with `; in System`,
/// like so:
///
/// ```rust
/// # use sguaba::{vector, system, Vector};
/// # use uom::si::f64::Length;
/// # use uom::si::length::meter;
/// system!(struct Frd using FRD);
/// vector! {
///     f = Length::new::<meter>(1.),
///     r = Length::new::<meter>(2.),
///     d = Length::new::<meter>(3.);
///     in Frd
/// }
/// # ;
/// ```
///
/// The macro automatically determines the vector unit type based on the units provided:
///
/// ```rust
/// # use sguaba::{system, vector};
/// # use uom::si::f64::{Length, Velocity, Acceleration};
/// # use uom::si::{length::meter, velocity::meter_per_second, acceleration::meter_per_second_squared};
/// # system!(struct PlaneFrd using FRD);
///
/// // Creates Vector<PlaneFrd, Z0> / LengthVector (length vector)
/// let position = vector!(
///     f = Length::new::<meter>(1.0),
///     r = Length::new::<meter>(0.0),
///     d = Length::new::<meter>(0.0);
///     in PlaneFrd
/// );
///
/// // Creates Vector<PlaneFrd, N1> / VelocityVector (velocity vector)
/// let velocity = vector!(
///     f = Velocity::new::<meter_per_second>(5.0),
///     r = Velocity::new::<meter_per_second>(0.0),
///     d = Velocity::new::<meter_per_second>(0.0);
///     in PlaneFrd
/// );
///
/// // Creates Vector<PlaneFrd, N2> / AccelerationVector (acceleration vector)
/// let acceleration = vector!(
///     f = Acceleration::new::<meter_per_second_squared>(2.0),
///     r = Acceleration::new::<meter_per_second_squared>(0.0),
///     d = Acceleration::new::<meter_per_second_squared>(0.0);
///     in PlaneFrd
/// );
/// ```
///
#[macro_export]
macro_rules! vector {
    ($x:tt = $xx:expr, $y:tt = $yy:expr, $z:tt = $zz:expr $(,)?) => {
        vector!($x = $xx, $y = $yy, $z = $zz; in _)
    };
    (n = $n:expr, e = $e:expr, d = $d:expr; in $in:ty) => {
        $crate::Vector::<$in, _>::build($crate::systems::NedComponents {
            north: $n.into(),
            east: $e.into(),
            down: $d.into(),
        })
    };
    (e = $e:expr, n = $n:expr, u = $u:expr; in $in:ty) => {
        $crate::Vector::<$in, _>::build($crate::systems::EnuComponents {
            east: $e.into(),
            north: $n.into(),
            up: $u.into(),
        })
    };
    (f = $f:expr, r = $r:expr, d = $d:expr; in $in:ty) => {
        $crate::Vector::<$in, _>::build($crate::systems::FrdComponents {
            front: $f.into(),
            right: $r.into(),
            down: $d.into(),
        })
    };
    (x = $x:expr, y = $y:expr, z = $z:expr; in $in:ty) => {
        $crate::Vector::<$in, _>::build($crate::systems::XyzComponents {
            x: $x.into(),
            y: $y.into(),
            z: $z.into(),
        })
    };
}

impl<In, Time> Vector<In, Time>
where
    Time: Integer,
    Self: LengthBasedComponents<In, Time>,
{
    pub(crate) fn from_nalgebra_vector(value: Vector3) -> Self {
        Self {
            inner: value,
            system: PhantomData::<In>,
            unit: PhantomData::<LengthPossiblyPer<Time>>,
        }
    }

    /// Constructs a [`Vector`] at the given (x, y, z) Cartesian point in the [`CoordinateSystem`]
    /// `In`.
    pub fn build(components: <In::Convention as HasComponents<Time>>::Components) -> Self
    where
        In: CoordinateSystem,
        In::Convention: HasComponents<Time>,
    {
        let [x, y, z] = components.into();
        #[allow(deprecated)]
        Self::from_cartesian(x, y, z)
    }

    /// Provides a constructor for a [`Coordinate`] in the [`CoordinateSystem`] `In`.
    pub fn builder() -> Builder<In, Unset, Unset, Unset, Time>
    where
        In: CoordinateSystem,
    {
        Builder::default()
    }

    /// Constructs a vector with the given (x, y, z) cartesian components in the
    /// [`CoordinateSystem`] `In`.
    ///
    /// Prefer [`Vector::builder`], [`Vector::build`], or [`vector`] to avoid risk of argument
    /// order confusion. This function will be removed in a future version of Sguaba in favor of
    /// those.
    ///
    /// <div class="warning">
    ///
    /// This method is permanently deprecated because it is particularly vulnerable to argument
    /// order confusion (eg, accidentally passing in the "down" component of a FRD vector
    /// first instead of last). Methods like [`Vector::builder`] and the [`vector!`] macro
    /// should be preferred instead, as they do not have this problem. However, this method is
    /// left for use-cases where those alternatives cannot be used, such as when writing code
    /// that is fully generic over the coordinate system, and thus cannot use the safer
    /// constructors provided by those APIs. If this applies to you, make sure you apply due
    /// diligence when writing out the argument ordering.
    ///
    /// </div>
    ///
    /// The meaning of `x`, `y`, and `z` is dictated by the "convention" of `In`. For example, in
    /// [`NedLike`], `x` is North, `y` is East, and `z` is "down" (ie, in the direction of
    /// gravity).
    #[deprecated = "prefer `Vector::builder` to avoid risk of argument order confusion"]
    pub fn from_cartesian(
        x: impl Into<LengthPossiblyPer<Time>>,
        y: impl Into<LengthPossiblyPer<Time>>,
        z: impl Into<LengthPossiblyPer<Time>>,
    ) -> Self {
        LengthBasedComponents::from_cartesian([x.into(), y.into(), z.into()])
    }

    /// Constructs a vector with the given (r, θ, φ) spherical components in the
    /// [`CoordinateSystem`] `In`.
    ///
    /// This constructor follows the physics convention for [spherical coordinates][sph], which
    /// defines
    ///
    /// - r as the radial distance, meaning the slant distance to origin;
    /// - θ (theta) as the polar angle meaning the angle with respect to positive polar axis (Z);
    ///   and
    /// - φ (phi) as the azimuthal angle, meaning the angle of rotation from the initial meridian
    ///   plane (positive X towards positive Y).
    ///
    /// The axes and planes above are in turn dictated by the [`CoordinateSystem::Convention`] of
    /// `In`. For example, in [`NedLike`], the polar angle is the angle relative to straight down
    /// and the azimuthal angle is the angle from North. In [`FrdLike`], the polar angle is the
    /// angle relative to "down" and the azimuthal angle is the angle from forward.
    ///
    /// <div class="warning">
    ///
    /// Note that this convention does not necessarily match the conventions of each coordinate
    /// system. For example, in [`NedLike`] and [`FrdLike`] coordinate systems, we often talk about
    /// "azimuth and elevation", but those are distinct from the spherical coordinates. In that
    /// context, the definition of azimuth (luckily) matches that of spherical coordinates, but
    /// elevation tends to be defined as the angle from the positive X axis rather than from the
    /// positive Z axis. If you want to define a coordinate based on azimuth and elevation, use
    /// [`Vector::from_bearing`].
    ///
    /// </div>
    ///
    /// # Examples
    ///
    /// ```rust
    /// use approx::assert_relative_eq;
    /// use sguaba::{system, vector, Vector};
    /// use uom::si::f64::{Angle, Length};
    /// use uom::si::{angle::degree, length::meter};
    ///
    /// system!(struct Ned using NED);
    ///
    /// let zero = Length::new::<meter>(0.);
    /// let unit = Length::new::<meter>(1.);
    /// assert_relative_eq!(
    ///     Vector::<Ned>::from_spherical(unit, Angle::new::<degree>(0.), Angle::new::<degree>(0.)),
    ///     vector!(n = zero, e = zero, d = unit),
    /// );
    /// assert_relative_eq!(
    ///     Vector::<Ned>::from_spherical(unit, Angle::new::<degree>(90.), Angle::new::<degree>(0.)),
    ///     vector!(n = unit, e = zero, d = zero),
    /// );
    /// assert_relative_eq!(
    ///     Vector::<Ned>::from_spherical(unit, Angle::new::<degree>(90.), Angle::new::<degree>(90.)),
    ///     vector!(n = zero, e = unit, d = zero),
    /// );
    /// ```
    ///
    /// [sph]: https://en.wikipedia.org/wiki/Spherical_coordinate_system
    pub fn from_spherical(
        radius: impl Into<LengthPossiblyPer<Time>>,
        polar: impl Into<Angle>,
        azimuth: impl Into<Angle>,
    ) -> Self {
        let radius: LengthPossiblyPer<Time> = radius.into();
        let azimuth = azimuth.into();
        let polar = polar.into();

        let x = radius
            * FloatMath::sin(polar.get::<radian>())
            * FloatMath::cos(azimuth.get::<radian>());
        let y = radius
            * FloatMath::sin(polar.get::<radian>())
            * FloatMath::sin(azimuth.get::<radian>());
        let z = radius * FloatMath::cos(polar.get::<radian>());

        #[allow(deprecated)]
        Self::from_cartesian(x, y, z)
    }

    /// Constructs a vector with the given azimuth, elevation, and range in the
    /// [`CoordinateSystem`] `In`.
    ///
    /// This constructor is based on the [bearing] as it defined by the implementation of
    /// [`BearingDefined`] for `In`. For [`NedLike`] and [`FrdLike`] coordinate systems, this is:
    ///
    /// - azimuth is the angle clockwise as seen from "up" along the XY plane from the positive X
    ///   axis (eg, North or Forward); and
    /// - elevation is the angle towards Z from the XY plane; and
    /// - r is the radial distance, meaning the slant distance to origin.
    ///
    /// See also [horizontal coordinate systems][azel].
    ///
    /// # Examples
    ///
    /// ```rust
    /// use approx::assert_relative_eq;
    /// use sguaba::{system, vector, Bearing, Vector};
    /// use uom::si::f64::{Angle, Length};
    /// use uom::si::{angle::degree, length::meter};
    ///
    /// system!(struct Ned using NED);
    ///
    /// let zero = Length::new::<meter>(0.);
    /// let unit = Length::new::<meter>(1.);
    /// assert_relative_eq!(
    ///     Vector::<Ned>::from_bearing(
    ///       Bearing::builder()
    ///         .azimuth(Angle::new::<degree>(0.))
    ///         .elevation(Angle::new::<degree>(0.)).expect("elevation is in-range")
    ///         .build(),
    ///       unit
    ///     ),
    ///     vector!(n = unit, e = zero, d = zero),
    /// );
    /// assert_relative_eq!(
    ///     Vector::<Ned>::from_bearing(
    ///       Bearing::builder()
    ///         .azimuth(Angle::new::<degree>(90.))
    ///         .elevation(Angle::new::<degree>(0.)).expect("elevation is in-range")
    ///         .build(),
    ///       unit
    ///     ),
    ///     vector!(n = zero, e = unit, d = zero),
    /// );
    /// assert_relative_eq!(
    ///     Vector::<Ned>::from_bearing(
    ///       Bearing::builder()
    ///         .azimuth(Angle::new::<degree>(90.))
    ///         .elevation(Angle::new::<degree>(90.)).expect("elevation is in-range")
    ///         .build(),
    ///       unit
    ///     ),
    ///     vector!(n = zero, e = zero, d = -unit),
    /// );
    /// ```
    ///
    /// [bearing]: https://en.wikipedia.org/wiki/Bearing_%28navigation%29
    /// [azel]: https://en.wikipedia.org/wiki/Horizontal_coordinate_system
    pub fn from_bearing(bearing: Bearing<In>, range: impl Into<LengthPossiblyPer<Time>>) -> Self
    where
        In: crate::systems::BearingDefined,
    {
        let (polar, azimuth) = In::bearing_to_spherical(bearing);
        Self::from_spherical(range.into(), polar, azimuth)
    }

    /// Changes the coordinate system of the vector to `<NewIn>` with no changes to the components.
    ///
    /// This is useful useful when the transform from one coordinate system to another only
    /// includes translation and not rotation, since vectors are unaffected by translation (see
    /// [`RigidBodyTransform::transform`]) and thus can be equivalently used in both.
    ///
    /// It can also be useful when you have two coordinate systems that you know have exactly
    /// equivalent axes, and you need the types to "work out". This can be the case, for instance,
    /// when two crates have both separately declared a NED-like coordinate system centered on the
    /// same object, and you need to move vectors between them. In such cases, however, prefer
    /// implementing [`EquivalentTo`] and using [`Vector::cast`], as it is harder to accidentally
    /// misuse.
    ///
    /// This is not how you _generally_ want to convert between coordinate systems. For that,
    /// you'll want to use [`RigidBodyTransform`].
    ///
    /// This is exactly equivalent to re-constructing the vector with the same component values
    /// using `<NewIn>` instead of `<In, Time>`, just more concise and legible. That is, it is exactly
    /// equal to:
    ///
    /// ```
    /// use sguaba::{system, vector, Bearing, Vector};
    /// use uom::si::f64::{Angle, Length};
    /// use uom::si::{angle::degree, length::meter};
    ///
    /// system!(struct PlaneNedFromCrate1 using NED);
    /// system!(struct PlaneNedFromCrate2 using NED);
    ///
    /// let zero = Length::new::<meter>(0.);
    /// let unit = Length::new::<meter>(1.);
    /// let vector_in_1 = vector!(n = unit, e = zero, d = unit; in PlaneNedFromCrate1);
    ///
    /// assert_eq!(
    ///     vector! {
    ///       n = vector_in_1.ned_north(),
    ///       e = vector_in_1.ned_east(),
    ///       d = vector_in_1.ned_down();
    ///       in PlaneNedFromCrate2
    ///     },
    ///     vector_in_1.with_same_components_in::<PlaneNedFromCrate2>()
    /// );
    /// ```
    #[must_use]
    pub fn with_same_components_in<NewIn>(self) -> Vector<NewIn, Time> {
        Vector {
            inner: self.inner,
            system: PhantomData::<NewIn>,
            unit: self.unit,
        }
    }

    /// Casts the coordinate system type parameter of the vector to the equivalent coordinate
    /// system `NewIn`.
    ///
    /// See [`EquivalentTo`] for details on when this is useful (and safe).
    ///
    /// Note that this performs no transform on the vector's components, as that should be
    /// unnecessary when `EquivalentTo` is implemented.
    ///
    /// ```
    /// use sguaba::{system, vector, systems::EquivalentTo, Vector};
    /// use uom::si::{f64::Length, length::meter};
    ///
    /// system!(struct PlaneNedFromCrate1 using NED);
    /// system!(struct PlaneNedFromCrate2 using NED);
    ///
    /// // SAFETY: these are truly the same thing just defined in different places
    /// unsafe impl EquivalentTo<PlaneNedFromCrate1> for PlaneNedFromCrate2 {}
    /// unsafe impl EquivalentTo<PlaneNedFromCrate2> for PlaneNedFromCrate1 {}
    ///
    /// let zero = Length::new::<meter>(0.);
    /// let unit = Length::new::<meter>(1.);
    /// let vector_in_1 = vector!(n = unit, e = zero, d = unit; in PlaneNedFromCrate1);
    ///
    /// assert_eq!(
    ///     vector! {
    ///       n = vector_in_1.ned_north(),
    ///       e = vector_in_1.ned_east(),
    ///       d = vector_in_1.ned_down();
    ///       in PlaneNedFromCrate2
    ///     },
    ///     vector_in_1.cast::<PlaneNedFromCrate2>()
    /// );
    /// ```
    #[must_use]
    pub fn cast<NewIn>(self) -> Vector<NewIn, Time>
    where
        In: EquivalentTo<NewIn>,
    {
        self.with_same_components_in::<NewIn>()
    }

    /// Returns a unit vector with the same direction as this vector.
    #[must_use]
    pub fn normalized(&self) -> Self {
        Self::from_nalgebra_vector(self.inner.normalize())
    }

    /// Computes the dot (scalar) product between this vector and another.
    ///
    /// Note that this method's return value is unitless since the unit of the dot product is not
    /// meaningful in terms of the units of the underlying components.
    #[must_use]
    pub fn dot(&self, rhs: &Self) -> f64 {
        self.inner.dot(&rhs.inner)
    }

    /// Linearly interpolate between this vector and another vector.
    ///
    /// Specifically, returns `self * (1.0 - t) + rhs * t`, i.e., the linear blend of the
    /// two vectors using the scalar value `t`.
    ///
    /// The value for `t` is not restricted to the range [0, 1].
    #[must_use]
    pub fn lerp(&self, rhs: &Self, t: f64) -> Self {
        Self {
            inner: self.inner.lerp(&rhs.inner, t),
            system: self.system,
            unit: self.unit,
        }
    }

    /// Computes the bearing of the vector as if the vector starts at the origin of `In`.
    ///
    /// Returns `None` if the vector has zero length, as the azimuth is then ill-defined.
    ///
    /// Returns an azimuth of zero if the vector points directly along the Z axis.
    #[must_use]
    pub fn bearing_at_origin(&self) -> Option<Bearing<In>>
    where
        In: crate::systems::BearingDefined,
    {
        // This is obviously a little sneaky since we're coercing to meters, but since we're
        // coercing all the components the same way, the resulting bearing should remain the same.
        let [x, y, z] = self.to_cartesian();
        let x = Vector::recast_to_length(x);
        let y = Vector::recast_to_length(y);
        let z = Vector::recast_to_length(z);
        #[allow(deprecated)]
        Coordinate::from_cartesian(x, y, z).bearing_from_origin()
    }

    /// Computes the orientation of the vector as if the vector starts at the origin of `In`.
    ///
    /// Since vectors do not include roll information, the desired roll must be passed in. It's
    /// worth reading the documentation on [`Orientation`] for a reminder about the meaning of roll
    /// here. Very briefly, it is intrinsic, and 0° roll means the object's positive Z axis is
    /// aligned with `In`'s positive Z axis.
    ///
    /// Returns `None` if the vector has zero length, as the yaw is then ill-defined.
    ///
    /// Returns a yaw of zero if the vector points directly along the Z axis.
    #[must_use]
    pub fn orientation_at_origin(&self, roll: impl Into<Angle>) -> Option<Orientation<In>> {
        // The vector is effectively an axis-angle representation of the Tait-Bryan orientation
        // we're after (with the angle of rotation being 0°). But, when angle is 0°, the axis-angle
        // representation ends up being a noop from what I can tell. Instead, we compute this from
        // first principles. Note that we cast to the raw length, but that's okay since we only
        // care about the relative magnitudes for these as we're computing angles.
        let [x, y, z] = self.to_cartesian();
        let x = Vector::recast_to_length(x);
        let y = Vector::recast_to_length(y);
        let z = Vector::recast_to_length(z);

        if x == Length::ZERO && y == Length::ZERO && z == Length::ZERO {
            return None;
        }

        // Per `Orientation::from_tait_bryan_angles`, yaw is rotation about the Z axis with an
        // object at 0° yaw facing along the positive X axis. Thus, it is rotation on the XY plane
        // (and uses only the x and y coordinates). Positive yaw is counter-clockwise rotation
        // about Z (ie, towards +Y from +X), and thus we want the sin of the angle between X and Y
        // with no sign flipping.
        //
        // Also note that atan2 guarantees that it returns 0 if both components are 0.
        let yaw = Angle::new::<radian>(FloatMath::atan2(y.get::<meter>(), x.get::<meter>()));
        // Pitch is the rotation about the Y axis, with 0° pitch being aligned with the yaw axis
        // (since we're using intrinsic rotations). Per the right-hand rule, positive pitch
        // rotates from +X toward -Z, so we negate z to get the correct sign.
        let pitch = Angle::new::<radian>(FloatMath::atan2(
            -z.get::<meter>(),
            FloatMath::sqrt(
                FloatMath::powi(x.get::<meter>(), 2) + FloatMath::powi(y.get::<meter>(), 2),
            ),
        ));
        // And the roll is passed in.
        let roll = roll.into();

        Some(
            Orientation::tait_bryan_builder()
                .yaw(yaw)
                .pitch(pitch)
                .roll(roll)
                .build(),
        )
    }

    /// Constructs a vector in coordinate system `In` whose components are all 0.
    ///
    /// This is the [additive identity][id] for `In` under vector addition, meaning adding this
    /// vector to any coordinate or vector in `In` will yield the origin coordinate or vector
    /// unchanged.
    ///
    /// [id]: https://en.wikipedia.org/wiki/Zero_element#Additive_identities
    #[must_use]
    pub fn zero() -> Self {
        Self {
            inner: Vector3::zeros(),
            system: PhantomData::<In>,
            unit: PhantomData::<LengthPossiblyPer<Time>>,
        }
    }
}

impl<In> From<Coordinate<In>> for Vector<In, Z0> {
    fn from(value: Coordinate<In>) -> Self {
        Self::from_nalgebra_vector(value.point.coords)
    }
}

impl<In, Time> Default for Vector<In, Time>
where
    Time: Integer,
    Self: LengthBasedComponents<In, Time>,
{
    fn default() -> Self {
        Self::zero()
    }
}

macro_rules! accessors {
    {
        $convention:ident
        using $x:ident, $y:ident, $z:ident
        // TODO: https://github.com/rust-lang/rust/issues/124225
        + $x_ax:ident, $y_ax:ident, $z_ax:ident
    } => {
        impl<In, Time: typenum::Integer> Vector<In, Time> where In: CoordinateSystem<Convention = $convention>, Self: LengthBasedComponents<In, Time> {
            #[must_use]
            pub fn $x(&self) -> LengthPossiblyPer<Time> {
                let cartesian = LengthBasedComponents::to_cartesian(self);
                cartesian[0]
            }
            #[must_use]
            pub fn $y(&self) -> LengthPossiblyPer<Time> {
                let cartesian = LengthBasedComponents::to_cartesian(self);
                cartesian[1]
            }
            #[must_use]
            pub fn $z(&self) -> LengthPossiblyPer<Time> {
                let cartesian = LengthBasedComponents::to_cartesian(self);
                cartesian[2]
            }

            #[must_use]
            pub fn $x_ax() -> Vector<In, Time> {
                Self {
                    inner: *Vector3::x_axis(),
                    system: core::marker::PhantomData::<In>,
                    unit: core::marker::PhantomData::<LengthPossiblyPer<Time>>,
                }
            }
            #[must_use]
            pub fn $y_ax() -> Vector<In, Time> {
                Self {
                    inner: *Vector3::y_axis(),
                    system: core::marker::PhantomData::<In>,
                    unit: core::marker::PhantomData::<LengthPossiblyPer<Time>>,
                }
            }
            #[must_use]
            pub fn $z_ax() -> Vector<In, Time> {
                Self {
                    inner: *Vector3::z_axis(),
                    system: core::marker::PhantomData::<In>,
                    unit: core::marker::PhantomData::<LengthPossiblyPer<Time>>,
                }
            }
        }
    };
}

accessors!(RightHandedXyzLike using x, y, z + x_axis, y_axis, z_axis);
accessors!(NedLike using ned_north, ned_east, ned_down + ned_north_axis, ned_east_axis, ned_down_axis);
accessors!(FrdLike using frd_front, frd_right, frd_down + frd_front_axis, frd_right_axis, frd_down_axis);
accessors!(EnuLike using enu_east, enu_north, enu_up + enu_east_axis, enu_north_axis, enu_up_axis);

impl<In> Vector<In, Z0> {
    /// Returns the cartesian components of this vector in XYZ order.
    ///
    /// To turn this into a simple (ie, unitless) `[f64; 3]`, use [`array::map`] combined with
    /// `.get::<meter>()`.
    #[doc(alias = "components")]
    #[must_use]
    pub fn to_cartesian(&self) -> [Length; 3] {
        LengthBasedComponents::to_cartesian(self)
    }

    /// Computes the magnitude of the vector (ie, its length).
    #[doc(alias = "norm")]
    #[doc(alias = "distance")]
    #[doc(alias = "length")]
    #[must_use]
    pub fn magnitude(&self) -> Length {
        Length::new::<meter>(self.inner.norm())
    }
}

impl<In> Vector<In, N1> {
    /// Returns the cartesian components of this velocity vector in XYZ order.
    ///
    /// To turn this into a simple (ie, unitless) `[f64; 3]`, use [`array::map`] combined with
    /// `.get::<meter_per_second>()`.
    #[doc(alias = "components")]
    #[must_use]
    pub fn to_cartesian(&self) -> [Velocity; 3] {
        LengthBasedComponents::to_cartesian(self)
    }

    /// Computes the magnitude of the vector (ie, its velocity).
    #[doc(alias = "norm")]
    #[doc(alias = "speed")]
    #[must_use]
    pub fn magnitude(&self) -> Velocity {
        Velocity::new::<meter_per_second>(self.inner.norm())
    }
}

impl<In> Vector<In, N2> {
    /// Returns the cartesian components of this vector in XYZ order.
    ///
    /// To turn this into a simple (ie, unitless) `[f64; 3]`, use [`array::map`] combined with
    /// `.get::<meter_per_second_squared>()`.
    #[doc(alias = "components")]
    #[must_use]
    pub fn to_cartesian(&self) -> [Acceleration; 3] {
        LengthBasedComponents::to_cartesian(self)
    }

    /// Computes the magnitude of the vector (ie, its acceleration).
    #[doc(alias = "norm")]
    #[doc(alias = "acceleration")]
    #[must_use]
    pub fn magnitude(&self) -> Acceleration {
        Acceleration::new::<meter_per_second_squared>(self.inner.norm())
    }
}

impl<In, Time: Integer> Neg for Vector<In, Time> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self {
            inner: -self.inner,
            system: self.system,
            unit: self.unit,
        }
    }
}

impl<In, Time: Integer> Add<Self> for Vector<In, Time> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            inner: self.inner + rhs.inner,
            system: self.system,
            unit: self.unit,
        }
    }
}

impl<In, Time: Integer> AddAssign<Self> for Vector<In, Time> {
    fn add_assign(&mut self, rhs: Self) {
        self.inner += rhs.inner;
    }
}

impl<In, Time: Integer> Sub<Self> for Vector<In, Time> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            inner: self.inner - rhs.inner,
            system: self.system,
            unit: self.unit,
        }
    }
}

impl<In, Time: Integer> SubAssign<Self> for Vector<In, Time> {
    fn sub_assign(&mut self, rhs: Self) {
        self.inner -= rhs.inner;
    }
}

impl<In, Time: Integer> Sum for Vector<In, Time>
where
    Self: LengthBasedComponents<In, Time>,
{
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::zero(), |sum, v| sum + v)
    }
}

// multiplying an acceleration vector by time gives you a velocity vector
impl<In> Mul<uom::si::f64::Time> for Vector<In, N2> {
    type Output = Vector<In, N1>;

    fn mul(self, duration: uom::si::f64::Time) -> Self::Output {
        LengthBasedComponents::from_cartesian(self.to_cartesian().map(|v| v * duration))
    }
}
// multiplying a velocity vector by time gives you a length vector
impl<In> Mul<uom::si::f64::Time> for Vector<In, N1> {
    type Output = Vector<In, Z0>;

    fn mul(self, duration: uom::si::f64::Time) -> Self::Output {
        LengthBasedComponents::from_cartesian(self.to_cartesian().map(|v| v * duration))
    }
}

impl<In, Time: Integer> Mul<f64> for Vector<In, Time> {
    type Output = Self;

    fn mul(self, scalar: f64) -> Self::Output {
        Self {
            inner: self.inner * scalar,
            system: self.system,
            unit: self.unit,
        }
    }
}

impl<In, Time: Integer> Div<f64> for Vector<In, Time> {
    type Output = Self;

    fn div(self, scalar: f64) -> Self::Output {
        Self {
            inner: self.inner / scalar,
            system: self.system,
            unit: self.unit,
        }
    }
}

impl<In, Time: Integer> Div<Length> for Vector<In, Time> {
    type Output = Self;

    fn div(self, rhs: Length) -> Self::Output {
        self / rhs.get::<meter>()
    }
}

impl<In, Time: Integer> Mul<Length> for Vector<In, Time> {
    type Output = Self;

    fn mul(self, rhs: Length) -> Self::Output {
        self * rhs.get::<meter>()
    }
}

impl<In, Time: Integer> PartialEq<Self> for Vector<In, Time> {
    fn eq(&self, other: &Self) -> bool {
        self.inner.eq(&other.inner)
    }
}

impl<In, Time: Integer> Display for Vector<In, Time> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.inner)
    }
}

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

    fn default_epsilon() -> Self::Epsilon {
        // NOTE(jon): this value is in Meters, and realistically we're fine with .1m precision
        0.1
    }

    fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
        self.inner.abs_diff_eq(&other.inner, epsilon)
    }
}

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

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        self.inner.relative_eq(&other.inner, epsilon, max_relative)
    }
}

/// [Builder] for a [`Vector`].
///
/// Construct one through [`Vector::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, X, Y, Z, Time = Z0>
where
    Time: Integer,
{
    under_construction: Vector<In, Time>,
    set: (PhantomData<X>, PhantomData<Y>, PhantomData<Z>),
}
impl<In, Time> Default for Builder<In, Unset, Unset, Unset, Time>
where
    Time: Integer,
    Vector<In, Time>: LengthBasedComponents<In, Time>,
{
    fn default() -> Self {
        Self {
            under_construction: Vector::default(),
            set: (PhantomData, PhantomData, PhantomData),
        }
    }
}

impl<In, Time: Integer> Builder<In, Set, Set, Set, Time> {
    /// Constructs a [`Vector`] at the currently set (x, y, z) Cartesian point in the
    /// [`CoordinateSystem`] `In`.
    #[must_use]
    pub fn build(self) -> Vector<In, Time> {
        self.under_construction
    }
}

macro_rules! constructor {
    ($like:ident, [$x:ident, $y:ident, $z:ident]) => {
        impl<In, X, Y, Z, Time> Builder<In, X, Y, Z, Time>
        where
            In: CoordinateSystem<Convention = $like>,
            Time: typenum::Integer,
            Vector<In, Time>: LengthBasedComponents<In, Time>,
        {
            /// Sets the X component of this [`Vector`]-to-be.
            pub fn $x(
                mut self,
                value: impl Into<LengthPossiblyPer<Time>>,
            ) -> Builder<In, Set, Y, Z, Time> {
                let mut cartesian = LengthBasedComponents::to_cartesian(&self.under_construction);
                cartesian[0] = value.into();
                self.under_construction = LengthBasedComponents::from_cartesian(cartesian);
                Builder {
                    under_construction: self.under_construction,
                    set: (PhantomData::<Set>, self.set.1, self.set.2),
                }
            }

            /// Sets the Y component of this [`Vector`]-to-be.
            pub fn $y(
                mut self,
                value: impl Into<LengthPossiblyPer<Time>>,
            ) -> Builder<In, X, Set, Z, Time> {
                let mut cartesian = LengthBasedComponents::to_cartesian(&self.under_construction);
                cartesian[1] = value.into();
                self.under_construction = LengthBasedComponents::from_cartesian(cartesian);
                Builder {
                    under_construction: self.under_construction,
                    set: (self.set.0, PhantomData::<Set>, self.set.2),
                }
            }

            /// Sets the Z component of this [`Vector`]-to-be.
            pub fn $z(
                mut self,
                value: impl Into<LengthPossiblyPer<Time>>,
            ) -> Builder<In, X, Y, Set, Time> {
                let mut cartesian = LengthBasedComponents::to_cartesian(&self.under_construction);
                cartesian[2] = value.into();
                self.under_construction = LengthBasedComponents::from_cartesian(cartesian);
                Builder {
                    under_construction: self.under_construction,
                    set: (self.set.0, self.set.1, PhantomData::<Set>),
                }
            }
        }
    };
}
constructor!(RightHandedXyzLike, [x, y, z]);
constructor!(NedLike, [ned_north, ned_east, ned_down]);
constructor!(FrdLike, [frd_front, frd_right, frd_down]);
constructor!(EnuLike, [enu_east, enu_north, enu_up]);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::coordinate;
    use crate::systems::EquivalentTo;
    use approx::assert_abs_diff_eq;
    use typenum::{N1, N2, Z0};
    use uom::si::f64::{Acceleration, Angle, Length, Velocity};
    use uom::si::{
        acceleration::meter_per_second_squared, angle::degree, length::meter,
        velocity::meter_per_second,
    };

    // Define test coordinate systems
    system!(struct TestFrd using FRD);
    system!(struct TestNed using NED);
    system!(struct TestEnu using ENU);
    system!(struct TestXyz using right-handed XYZ);

    // Define equivalent coordinate systems for testing
    system!(struct TestFrd2 using FRD);
    unsafe impl EquivalentTo<TestFrd> for TestFrd2 {}
    unsafe impl EquivalentTo<TestFrd2> for TestFrd {}

    // Helper functions for creating units
    fn m(meters: f64) -> Length {
        Length::new::<meter>(meters)
    }

    fn mps(meters_per_second: f64) -> Velocity {
        Velocity::new::<meter_per_second>(meters_per_second)
    }

    fn mps2(meters_per_second_squared: f64) -> Acceleration {
        Acceleration::new::<meter_per_second_squared>(meters_per_second_squared)
    }

    // Tests for Length vectors (Time = Z0)
    mod length_vectors {
        use super::*;

        #[test]
        fn constructor_from_cartesian_works() {
            #[allow(deprecated)]
            let v = Vector::<TestFrd, Z0>::from_cartesian(m(1.0), m(2.0), m(3.0));
            assert_eq!(v.to_cartesian(), [m(1.0), m(2.0), m(3.0)]);
        }

        #[test]
        fn builder_pattern_works() {
            let v = Vector::<TestFrd>::builder()
                .frd_front(m(1.0))
                .frd_right(m(2.0))
                .frd_down(m(3.0))
                .build();
            assert_eq!(v.frd_front(), m(1.0));
            assert_eq!(v.frd_right(), m(2.0));
            assert_eq!(v.frd_down(), m(3.0));
        }

        #[test]
        fn vector_macro_works() {
            let v = vector!(f = m(1.0), r = m(2.0), d = m(3.0); in TestFrd);
            assert_eq!(v.frd_front(), m(1.0));
            assert_eq!(v.frd_right(), m(2.0));
            assert_eq!(v.frd_down(), m(3.0));
        }

        #[test]
        fn build_with_components_works() {
            use crate::systems::FrdComponents;
            let v = Vector::<TestFrd>::build(FrdComponents {
                front: m(1.0),
                right: m(2.0),
                down: m(3.0),
            });
            assert_eq!(v.frd_front(), m(1.0));
            assert_eq!(v.frd_right(), m(2.0));
            assert_eq!(v.frd_down(), m(3.0));
        }

        #[test]
        fn from_spherical_works() {
            let v = Vector::<TestFrd>::from_spherical(
                m(10.0),
                Angle::new::<degree>(90.0), // polar angle (90 degrees)
                Angle::new::<degree>(0.0),  // azimuth angle (0 degrees)
            );
            assert_abs_diff_eq!(v.frd_front().get::<meter>(), 10.0, epsilon = 1e-10);
            assert_abs_diff_eq!(v.frd_right().get::<meter>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(v.frd_down().get::<meter>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn magnitude_works() {
            let v = vector!(f = m(3.0), r = m(4.0), d = m(0.0); in TestFrd);
            assert_eq!(v.magnitude(), m(5.0));
        }

        #[test]
        fn axis_vectors_work() {
            let front = Vector::<TestFrd>::frd_front_axis();
            let right = Vector::<TestFrd>::frd_right_axis();
            let down = Vector::<TestFrd>::frd_down_axis();

            assert_eq!(front.frd_front(), m(1.0));
            assert_eq!(front.frd_right(), m(0.0));
            assert_eq!(front.frd_down(), m(0.0));

            assert_eq!(right.frd_front(), m(0.0));
            assert_eq!(right.frd_right(), m(1.0));
            assert_eq!(right.frd_down(), m(0.0));

            assert_eq!(down.frd_front(), m(0.0));
            assert_eq!(down.frd_right(), m(0.0));
            assert_eq!(down.frd_down(), m(1.0));
        }

        #[test]
        fn arithmetic_operations_work() {
            let v1 = vector!(f = m(1.0), r = m(2.0), d = m(3.0); in TestFrd);
            let v2 = vector!(f = m(4.0), r = m(5.0), d = m(6.0); in TestFrd);

            let sum = v1 + v2;
            assert_eq!(sum.to_cartesian(), [m(5.0), m(7.0), m(9.0)]);

            let diff = v2 - v1;
            assert_eq!(diff.to_cartesian(), [m(3.0), m(3.0), m(3.0)]);

            let neg = -v1;
            assert_eq!(neg.to_cartesian(), [m(-1.0), m(-2.0), m(-3.0)]);

            let scaled = v1 * 2.0;
            assert_eq!(scaled.to_cartesian(), [m(2.0), m(4.0), m(6.0)]);
        }

        #[test]
        fn zero_vector_works() {
            let v = Vector::<TestFrd>::zero();
            assert_eq!(v.to_cartesian(), [m(0.0), m(0.0), m(0.0)]);
        }

        #[test]
        fn lerp_works() {
            let v1 = vector!(f = m(0.0), r = m(0.0), d = m(0.0); in TestFrd);
            let v2 = vector!(f = m(10.0), r = m(20.0), d = m(30.0); in TestFrd);

            let mid = v1.lerp(&v2, 0.5);
            assert_eq!(mid.to_cartesian(), [m(5.0), m(10.0), m(15.0)]);
        }

        #[test]
        fn cast_equivalent_coordinate_systems() {
            let v_frd = vector!(f = m(1.0), r = m(2.0), d = m(3.0); in TestFrd);
            let v_frd2: Vector<TestFrd2> = v_frd.cast();

            assert_eq!(v_frd2.to_cartesian(), v_frd.to_cartesian());
        }

        #[test]
        fn with_same_components_in_works() {
            let v_frd = vector!(f = m(1.0), r = m(2.0), d = m(3.0); in TestFrd);
            let v_ned: Vector<TestNed> = v_frd.with_same_components_in();

            // Same underlying values, different coordinate system
            assert_eq!(v_ned.to_cartesian(), v_frd.to_cartesian());
        }

        #[test]
        fn ned_accessors_work() {
            let v = vector!(n = m(1.0), e = m(2.0), d = m(3.0); in TestNed);
            assert_eq!(v.ned_north(), m(1.0));
            assert_eq!(v.ned_east(), m(2.0));
            assert_eq!(v.ned_down(), m(3.0));

            let north = Vector::<TestNed>::ned_north_axis();
            let east = Vector::<TestNed>::ned_east_axis();
            let down = Vector::<TestNed>::ned_down_axis();

            assert_eq!(north.ned_north(), m(1.0));
            assert_eq!(east.ned_east(), m(1.0));
            assert_eq!(down.ned_down(), m(1.0));
        }

        #[test]
        fn enu_accessors_work() {
            let v = vector!(e = m(1.0), n = m(2.0), u = m(3.0); in TestEnu);
            assert_eq!(v.enu_east(), m(1.0));
            assert_eq!(v.enu_north(), m(2.0));
            assert_eq!(v.enu_up(), m(3.0));
        }

        #[test]
        fn xyz_accessors_work() {
            let v = vector!(x = m(1.0), y = m(2.0), z = m(3.0); in TestXyz);
            assert_eq!(v.x(), m(1.0));
            assert_eq!(v.y(), m(2.0));
            assert_eq!(v.z(), m(3.0));
        }

        #[test]
        fn orientation_at_origin_zero_vector_returns_none() {
            let v = Vector::<TestXyz>::zero();
            assert_eq!(v.orientation_at_origin(Angle::ZERO), None);
        }

        #[test]
        fn orientation_at_origin_positive_x_axis() {
            // Vector pointing along +X should have yaw=0°, pitch=0°
            let v = vector!(x = m(1.0), y = m(0.0), z = m(0.0); in TestXyz);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn orientation_at_origin_positive_y_axis() {
            // Vector pointing along +Y should have yaw=90°, pitch=0°
            let v = vector!(x = m(0.0), y = m(1.0), z = m(0.0); in TestXyz);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 90.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn orientation_at_origin_positive_z_axis() {
            // Vector pointing along +Z should have yaw=0° (arbitrary), pitch=-90°
            let v = vector!(x = m(0.0), y = m(0.0), z = m(1.0); in TestXyz);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), -90.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn orientation_at_origin_negative_x_axis() {
            // Vector pointing along -X should have yaw=180° or -180°, pitch=0°
            let v = vector!(x = m(-1.0), y = m(0.0), z = m(0.0); in TestXyz);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            // atan2(0, -1) = 180° or -180° depending on convention
            assert_abs_diff_eq!(yaw.get::<degree>().abs(), 180.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn orientation_at_origin_45_degree_xy_plane() {
            // Vector at 45° in XY plane should have yaw=45°, pitch=0°
            let v = vector!(x = m(1.0), y = m(1.0), z = m(0.0); in TestXyz);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 45.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn orientation_at_origin_45_degree_xz_plane() {
            // Vector at 45° in XZ plane should have yaw=0°, pitch=-45°
            let v = vector!(x = m(1.0), y = m(0.0), z = m(1.0); in TestXyz);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), -45.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn orientation_at_origin_general_3d_vector() {
            // Vector at (3, 4, 5) - verify the full computation
            // Expected yaw = atan2(4, 3) ≈ 53.13°
            // Expected pitch = atan2(-5, sqrt(3² + 4²)) = atan2(-5, 5) = -45°
            let v = vector!(x = m(3.0), y = m(4.0), z = m(5.0); in TestXyz);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 53.13010235415598, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), -45.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);

            // Sanity-check that if we create an observation in that direction,
            // it matches the original vector.
            let frd = vector!(f = v.magnitude(), r = m(0.), d = m(0.); in TestFrd);
            let pose = crate::engineering::Pose::new(Coordinate::origin(), orientation);
            let pose = unsafe { pose.map_as_zero_in::<TestFrd>() };
            let v2 = pose.inverse_transform(frd);
            assert_abs_diff_eq!(v, v2);
        }

        #[test]
        fn orientation_at_origin_ned() {
            // In NED:
            //
            // - north is +X
            // - +Z is down
            // - positive yaw is from-north-towards-east
            // - positive pitch is towards -Z (as always), so "up"
            //
            // so a 45°-nose-up east vector should have yaw=90° and pitch=45°
            let v = vector!(n = m(0.0), e = m(1.0), d = m(-1.0); in TestNed);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 90.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), 45.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn orientation_at_origin_enu_east_vector() {
            // In ENU:
            //
            // - east is +X
            // - +Z is up
            // - positive yaw is from-east-towards-north
            // - positive pitch is towards -Z (as always), so "down"
            //
            // so a 45°-nose-up north vector should have yaw=90° and pitch=-45°
            let v = vector!(e = m(0.0), n = m(1.0), u = m(1.0); in TestEnu);
            let orientation = v
                .orientation_at_origin(Angle::ZERO)
                .expect("non-zero vector");
            let (yaw, pitch, roll) = orientation.to_tait_bryan_angles();

            assert_abs_diff_eq!(yaw.get::<degree>(), 90.0, epsilon = 1e-10);
            assert_abs_diff_eq!(pitch.get::<degree>(), -45.0, epsilon = 1e-10);
            assert_abs_diff_eq!(roll.get::<degree>(), 0.0, epsilon = 1e-10);
        }
    }

    // Tests for Velocity vectors (Time = N1)
    mod velocity_vectors {
        use super::*;

        #[test]
        fn constructor_from_cartesian_works() {
            #[allow(deprecated)]
            let v = Vector::<TestFrd, N1>::from_cartesian(mps(1.0), mps(2.0), mps(3.0));
            assert_eq!(v.to_cartesian(), [mps(1.0), mps(2.0), mps(3.0)]);
        }

        #[test]
        fn build_with_components_works() {
            use crate::systems::FrdComponents;
            let v = Vector::<TestFrd, N1>::build(FrdComponents {
                front: mps(1.0),
                right: mps(2.0),
                down: mps(3.0),
            });
            let cartesian = v.to_cartesian();
            assert_eq!(cartesian, [mps(1.0), mps(2.0), mps(3.0)]);
        }

        #[test]
        fn builder_pattern_works() {
            let v = Vector::<TestFrd, N1>::builder()
                .frd_front(mps(1.0))
                .frd_right(mps(2.0))
                .frd_down(mps(3.0))
                .build();
            assert_eq!(v.frd_front(), mps(1.0));
            assert_eq!(v.frd_right(), mps(2.0));
            assert_eq!(v.frd_down(), mps(3.0));
        }

        #[test]
        fn magnitude_works() {
            let v = vector!(f = mps(3.0), r = mps(4.0), d = mps(0.0); in TestFrd);
            assert_eq!(v.magnitude(), mps(5.0));
        }

        #[test]
        fn from_spherical_works() {
            let v = Vector::<TestFrd, N1>::from_spherical(
                mps(10.0),
                Angle::new::<degree>(90.0), // polar angle
                Angle::new::<degree>(0.0),  // azimuth angle
            );
            let cartesian = v.to_cartesian();
            assert_abs_diff_eq!(
                cartesian[0].get::<meter_per_second>(),
                10.0,
                epsilon = 1e-10
            );
            assert_abs_diff_eq!(cartesian[1].get::<meter_per_second>(), 0.0, epsilon = 1e-10);
            assert_abs_diff_eq!(cartesian[2].get::<meter_per_second>(), 0.0, epsilon = 1e-10);
        }

        #[test]
        fn axis_vectors_work() {
            let front = Vector::<TestFrd, N1>::frd_front_axis();
            let right = Vector::<TestFrd, N1>::frd_right_axis();
            let down = Vector::<TestFrd, N1>::frd_down_axis();

            let front_cart = front.to_cartesian();
            let right_cart = right.to_cartesian();
            let down_cart = down.to_cartesian();

            assert_eq!(front_cart, [mps(1.0), mps(0.0), mps(0.0)]);
            assert_eq!(right_cart, [mps(0.0), mps(1.0), mps(0.0)]);
            assert_eq!(down_cart, [mps(0.0), mps(0.0), mps(1.0)]);
        }

        #[test]
        fn arithmetic_operations_work() {
            let v1 = vector!(f = mps(1.0), r = mps(2.0), d = mps(3.0); in TestFrd);
            let v2 = vector!(f = mps(4.0), r = mps(5.0), d = mps(6.0); in TestFrd);

            let sum = v1 + v2;
            assert_eq!(sum.to_cartesian(), [mps(5.0), mps(7.0), mps(9.0)]);

            let diff = v2 - v1;
            assert_eq!(diff.to_cartesian(), [mps(3.0), mps(3.0), mps(3.0)]);

            let neg = -v1;
            assert_eq!(neg.to_cartesian(), [mps(-1.0), mps(-2.0), mps(-3.0)]);

            let scaled = v1 * 2.0;
            assert_eq!(scaled.to_cartesian(), [mps(2.0), mps(4.0), mps(6.0)]);
        }

        #[test]
        fn zero_vector_works() {
            let v = Vector::<TestFrd, N1>::zero();
            assert_eq!(v.to_cartesian(), [mps(0.0), mps(0.0), mps(0.0)]);
        }

        #[test]
        fn lerp_works() {
            let v1 = vector!(f = mps(0.0), r = mps(0.0), d = mps(0.0); in TestFrd);
            let v2 = vector!(f = mps(10.0), r = mps(20.0), d = mps(30.0); in TestFrd);

            let mid = v1.lerp(&v2, 0.5);
            assert_eq!(mid.to_cartesian(), [mps(5.0), mps(10.0), mps(15.0)]);
        }

        #[test]
        fn with_same_components_in_works() {
            let v_frd = vector!(f = mps(1.0), r = mps(2.0), d = mps(3.0); in TestFrd);
            let v_ned: Vector<TestNed, N1> = v_frd.with_same_components_in();

            assert_eq!(v_ned.to_cartesian(), v_frd.to_cartesian());
        }

        #[test]
        fn cast_equivalent_coordinate_systems() {
            let v_frd = vector!(f = mps(1.0), r = mps(2.0), d = mps(3.0); in TestFrd);
            let v_frd2: Vector<TestFrd2, N1> = v_frd.cast();

            assert_eq!(v_frd2.to_cartesian(), v_frd.to_cartesian());
        }

        #[test]
        fn ned_accessors_work() {
            let v = vector!(n = mps(1.0), e = mps(2.0), d = mps(3.0); in TestNed);
            assert_eq!(v.ned_north(), mps(1.0));
            assert_eq!(v.ned_east(), mps(2.0));
            assert_eq!(v.ned_down(), mps(3.0));
        }

        #[test]
        fn enu_accessors_work() {
            let v = vector!(e = mps(1.0), n = mps(2.0), u = mps(3.0); in TestEnu);
            assert_eq!(v.enu_east(), mps(1.0));
            assert_eq!(v.enu_north(), mps(2.0));
            assert_eq!(v.enu_up(), mps(3.0));
        }

        #[test]
        fn xyz_accessors_work() {
            let v = vector!(x = mps(1.0), y = mps(2.0), z = mps(3.0); in TestXyz);
            assert_eq!(v.x(), mps(1.0));
            assert_eq!(v.y(), mps(2.0));
            assert_eq!(v.z(), mps(3.0));
        }
    }

    // Tests for Acceleration vectors (Time = N2)
    mod acceleration_vectors {
        use super::*;

        #[test]
        fn constructor_from_cartesian_works() {
            #[allow(deprecated)]
            let v = Vector::<TestFrd, N2>::from_cartesian(mps2(1.0), mps2(2.0), mps2(3.0));
            assert_eq!(v.to_cartesian(), [mps2(1.0), mps2(2.0), mps2(3.0)]);
        }

        #[test]
        fn build_with_components_works() {
            use crate::systems::FrdComponents;
            let v = Vector::<TestFrd, N2>::build(FrdComponents {
                front: mps2(1.0),
                right: mps2(2.0),
                down: mps2(3.0),
            });
            let cartesian = v.to_cartesian();
            assert_eq!(cartesian, [mps2(1.0), mps2(2.0), mps2(3.0)]);
        }

        #[test]
        fn builder_pattern_works() {
            let v = Vector::<TestFrd, N2>::builder()
                .frd_front(mps2(1.0))
                .frd_right(mps2(2.0))
                .frd_down(mps2(3.0))
                .build();
            assert_eq!(v.frd_front(), mps2(1.0));
            assert_eq!(v.frd_right(), mps2(2.0));
            assert_eq!(v.frd_down(), mps2(3.0));
        }

        #[test]
        fn magnitude_works() {
            let v = vector!(f = mps2(3.0), r = mps2(4.0), d = mps2(0.0); in TestFrd);
            assert_eq!(v.magnitude(), mps2(5.0));
        }

        #[test]
        fn from_spherical_works() {
            let v = Vector::<TestFrd, N2>::from_spherical(
                mps2(10.0),
                Angle::new::<degree>(90.0), // polar angle
                Angle::new::<degree>(0.0),  // azimuth angle
            );
            let cartesian = v.to_cartesian();
            assert_abs_diff_eq!(
                cartesian[0].get::<meter_per_second_squared>(),
                10.0,
                epsilon = 1e-10
            );
            assert_abs_diff_eq!(
                cartesian[1].get::<meter_per_second_squared>(),
                0.0,
                epsilon = 1e-10
            );
            assert_abs_diff_eq!(
                cartesian[2].get::<meter_per_second_squared>(),
                0.0,
                epsilon = 1e-10
            );
        }

        #[test]
        fn axis_vectors_work() {
            let front = Vector::<TestFrd, N2>::frd_front_axis();
            let right = Vector::<TestFrd, N2>::frd_right_axis();
            let down = Vector::<TestFrd, N2>::frd_down_axis();

            let front_cart = front.to_cartesian();
            let right_cart = right.to_cartesian();
            let down_cart = down.to_cartesian();

            assert_eq!(front_cart, [mps2(1.0), mps2(0.0), mps2(0.0)]);
            assert_eq!(right_cart, [mps2(0.0), mps2(1.0), mps2(0.0)]);
            assert_eq!(down_cart, [mps2(0.0), mps2(0.0), mps2(1.0)]);
        }

        #[test]
        fn arithmetic_operations_work() {
            let v1 = vector!(f = mps2(1.0), r = mps2(2.0), d = mps2(3.0); in TestFrd);
            let v2 = vector!(f = mps2(4.0), r = mps2(5.0), d = mps2(6.0); in TestFrd);

            let sum = v1 + v2;
            assert_eq!(sum.to_cartesian(), [mps2(5.0), mps2(7.0), mps2(9.0)]);

            let diff = v2 - v1;
            assert_eq!(diff.to_cartesian(), [mps2(3.0), mps2(3.0), mps2(3.0)]);

            let neg = -v1;
            assert_eq!(neg.to_cartesian(), [mps2(-1.0), mps2(-2.0), mps2(-3.0)]);

            let scaled = v1 * 2.0;
            assert_eq!(scaled.to_cartesian(), [mps2(2.0), mps2(4.0), mps2(6.0)]);
        }

        #[test]
        fn zero_vector_works() {
            let v = Vector::<TestFrd, N2>::zero();
            assert_eq!(v.to_cartesian(), [mps2(0.0), mps2(0.0), mps2(0.0)]);
        }

        #[test]
        fn lerp_works() {
            let v1 = vector!(f = mps2(0.0), r = mps2(0.0), d = mps2(0.0); in TestFrd);
            let v2 = vector!(f = mps2(10.0), r = mps2(20.0), d = mps2(30.0); in TestFrd);

            let mid = v1.lerp(&v2, 0.5);
            assert_eq!(mid.to_cartesian(), [mps2(5.0), mps2(10.0), mps2(15.0)]);
        }

        #[test]
        fn with_same_components_in_works() {
            let v_frd = vector!(f = mps2(1.0), r = mps2(2.0), d = mps2(3.0); in TestFrd);
            let v_ned: Vector<TestNed, N2> = v_frd.with_same_components_in();

            assert_eq!(v_ned.to_cartesian(), v_frd.to_cartesian());
        }

        #[test]
        fn cast_equivalent_coordinate_systems() {
            let v_frd = vector!(f = mps2(1.0), r = mps2(2.0), d = mps2(3.0); in TestFrd);
            let v_frd2: Vector<TestFrd2, N2> = v_frd.cast();

            assert_eq!(v_frd2.to_cartesian(), v_frd.to_cartesian());
        }

        #[test]
        fn ned_accessors_work() {
            let v = vector!(n = mps2(1.0), e = mps2(2.0), d = mps2(3.0); in TestNed);
            assert_eq!(v.ned_north(), mps2(1.0));
            assert_eq!(v.ned_east(), mps2(2.0));
            assert_eq!(v.ned_down(), mps2(3.0));
        }

        #[test]
        fn enu_accessors_work() {
            let v = vector!(e = mps2(1.0), n = mps2(2.0), u = mps2(3.0); in TestEnu);
            assert_eq!(v.enu_east(), mps2(1.0));
            assert_eq!(v.enu_north(), mps2(2.0));
            assert_eq!(v.enu_up(), mps2(3.0));
        }

        #[test]
        fn xyz_accessors_work() {
            let v = vector!(x = mps2(1.0), y = mps2(2.0), z = mps2(3.0); in TestXyz);
            assert_eq!(v.x(), mps2(1.0));
            assert_eq!(v.y(), mps2(2.0));
            assert_eq!(v.z(), mps2(3.0));
        }
    }

    // Tests for coordinate/vector interconversion
    mod coordinate_vector_conversions {
        use super::*;

        #[test]
        fn coordinate_to_vector_conversion_works() {
            let coord = coordinate!(f = m(1.0), r = m(2.0), d = m(3.0); in TestFrd);
            let vec: Vector<TestFrd> = coord.into();

            assert_eq!(vec.to_cartesian(), [m(1.0), m(2.0), m(3.0)]);
        }

        #[test]
        fn multiply_acceleration_by_time_gives_velocity() {
            let acc = vector!(f = mps2(1.0), r = mps2(2.0), d = mps2(3.0); in TestFrd);
            let vel: Vector<TestFrd, N1> =
                acc * uom::si::f64::Time::new::<uom::si::time::second>(2.0);
            assert_eq!(vel.to_cartesian(), [mps(2.0), mps(4.0), mps(6.0)]);
        }

        #[test]
        fn multiply_velocity_by_time_gives_length() {
            let vel = vector!(f = mps(1.0), r = mps(2.0), d = mps(3.0); in TestFrd);
            let len: Vector<TestFrd, Z0> =
                vel * uom::si::f64::Time::new::<uom::si::time::second>(2.0);
            assert_eq!(len.to_cartesian(), [m(2.0), m(4.0), m(6.0)]);
        }
    }
}