-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpositions.rs
416 lines (362 loc) · 12.8 KB
/
positions.rs
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
use crate::Length;
use {crate::Angle, crate::Vec3};
/// Cartesian 3D position vector: allows to represent the position of a general coordinate frame B
/// relative to a reference coordinate frame A as the position vector from A to B.
pub trait Cartesian3DVector: Sized {
/// Returns the x component of this vector.
fn x(&self) -> Length;
/// Returns the y component of this vector.
fn y(&self) -> Length;
/// Returns the z component of this vector.
fn z(&self) -> Length;
/// Returns the (x, y, z) components of this vector in metres.
fn as_metres(&self) -> Vec3 {
Vec3::new(
self.x().as_metres(),
self.y().as_metres(),
self.z().as_metres(),
)
}
/// Rounds the (x, y, z) components of this vector to the nearest metre.
fn round_m(&self) -> Self {
self.round(|l| l.round_m())
}
/// Rounds the (x, y, z) components of this vector to the nearest decimetre.
fn round_dm(&self) -> Self {
self.round(|l| l.round_dm())
}
/// Rounds the (x, y, z) components of this vector to the nearest centimetre.
fn round_cm(&self) -> Self {
self.round(|l| l.round_cm())
}
/// Rounds the (x, y, z) components of this vector to the nearest millimetre.
fn round_mm(&self) -> Self {
self.round(|l| l.round_mm())
}
/// Rounds the (x, y, z) components of this vector using the given rounding function.
fn round<F>(&self, round: F) -> Self
where
F: Fn(Length) -> Length;
}
/// A geocentric position or Earth Centred Earth Fixed (ECEF) vector.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct GeocentricPosition {
x: Length,
y: Length,
z: Length,
}
impl GeocentricPosition {
/// Creates a [GeocentricPosition] from the given coordinates.
pub const fn new(x: Length, y: Length, z: Length) -> Self {
Self { x, y, z }
}
/// Creates a [GeocentricPosition] from the given coordinates in metres.
pub fn from_metres(x: f64, y: f64, z: f64) -> Self {
Self::new(
Length::from_metres(x),
Length::from_metres(y),
Length::from_metres(z),
)
}
/// Creates a [GeocentricPosition] from the given coordinates in metres.
pub(crate) fn from_vec3_metres(v: Vec3) -> Self {
Self::from_metres(v.x(), v.y(), v.z())
}
}
impl Cartesian3DVector for GeocentricPosition {
#[inline]
fn x(&self) -> Length {
self.x
}
#[inline]
fn y(&self) -> Length {
self.y
}
#[inline]
fn z(&self) -> Length {
self.z
}
fn round<F>(&self, round: F) -> Self
where
F: Fn(Length) -> Length,
{
Self::new(round(self.x()), round(self.y()), round(self.z()))
}
}
/// A geodetic position: the horiztonal coordinates (as a [NVector]) and height above the surface.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct GeodeticPosition {
hp: NVector,
height: Length,
}
impl GeodeticPosition {
/// Creates a new [GeodeticPosition] from the given horizontal coordinates and height above the surface.
pub const fn new(hp: NVector, height: Length) -> Self {
Self { hp, height }
}
/// Returns the [NVector] representing the horizontal coordinates of this [GeodeticPosition].
#[inline]
pub fn horizontal_position(&self) -> NVector {
self.hp
}
/// Returns the height above the surface of this [GeodeticPosition].
#[inline]
pub fn height(&self) -> Length {
self.height
}
}
/// An horizontal position represented by a pair of latitude-longitude.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct LatLong {
latitude: Angle,
longitude: Angle,
}
impl LatLong {
// TODO(CL): normalise?
/// Creates a new [LatLong] from the given latitude and longitude.
pub const fn new(latitude: Angle, longitude: Angle) -> Self {
Self {
latitude,
longitude,
}
}
/// Creates a new [LatLong] from the given latitude and longitudes in degrees.
pub fn from_degrees(latitude: f64, longitude: f64) -> Self {
Self::new(
Angle::from_degrees(latitude),
Angle::from_degrees(longitude),
)
}
/// Converts the given [NVector] into a [LatLong].
pub fn from_nvector(nvector: NVector) -> Self {
let (lat, lng) = nvector_to_latlong(nvector.0);
Self::new(lat, lng)
}
/// Converts this [LatLong] into an [NVector].
pub fn to_nvector(&self) -> NVector {
NVector::new(latlong_to_nvector(self.latitude, self.longitude))
}
/// Returns the latitude of this [LatLong].
#[inline]
pub fn latitude(&self) -> Angle {
self.latitude
}
/// Returns the longitude of this [LatLong].
#[inline]
pub fn longitude(&self) -> Angle {
self.longitude
}
/// Rounds the latitude and longitude of this latlong to the nearest decimal degrees with 5 decimal places.
///
/// The precision of the returned latlong corresponds to the accuracy achieved by commercial GPS
/// units with differential correction; it allows to distinguish 2 positions about 1.11 metres apart.
///
/// See also: [Angle::round_d5](crate::Angle::round_d5).
pub fn round_d5(&self) -> Self {
Self {
latitude: self.latitude.round_d5(),
longitude: self.longitude.round_d5(),
}
}
/// Rounds the latitude and longitude of this latlong to the nearest decimal degrees with 6 decimal places.
///
/// The precision of the returned latlong corresponds to the accuracy achieved by
/// differentially corrected GPS; it allows to distinguish 2 positions about 111 millimetres apart.
///
/// See also: [Angle::round_d6](crate::Angle::round_d6).
pub fn round_d6(&self) -> Self {
Self {
latitude: self.latitude.round_d6(),
longitude: self.longitude.round_d6(),
}
}
/// Rounds the latitude and longitude of this latlong to the nearest decimal degrees with 7 decimal places.
///
/// The precision of the returned latlong corresponds to the near limit of GPS-based
/// techniques; it allows to distinguish 2 positions about 11.1 millimetres apart.
///
/// See also: [Angle::round_d7](crate::Angle::round_d7).
pub fn round_d7(&self) -> Self {
Self {
latitude: self.latitude.round_d7(),
longitude: self.longitude.round_d7(),
}
}
}
/// An horizontal position represented by a n-vector: the unit and normal vector to the surface.
///
/// Orientation:
/// - z-axis points to the North Pole along the body's rotation axis,
/// - x-axis points towards the position where latitude = longitude = 0
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct NVector(Vec3);
impl NVector {
/// Creates a new [NVector] from the given [unit](crate::Vec3::new_unit) 3D vector.
pub const fn new(v: Vec3) -> Self {
Self(v)
}
/// Creates a new [NVector] from the given latitude and longitude in degrees.
pub fn from_lat_long_degrees(latitude_degrees: f64, longitude_degrees: f64) -> Self {
Self::new(latlong_to_nvector(
Angle::from_degrees(latitude_degrees),
Angle::from_degrees(longitude_degrees),
))
}
/// Returns the [NVector] which is the antipode of this [NVector].
pub fn antipode(&self) -> Self {
Self::new(-self.0)
}
/// Determines whether the given [NVector] is the antipode of this [NVector].
pub fn is_antipode_of(&self, o: Self) -> bool {
self.0 + o.0 == Vec3::ZERO
}
/// Returns this [NVector] as a [Vec3].
#[inline]
pub fn as_vec3(&self) -> Vec3 {
self.0
}
}
fn nvector_to_latlong(nvector: Vec3) -> (Angle, Angle) {
let x: f64 = nvector.x();
let y = nvector.y();
let z = nvector.z();
let lat = z.atan2((x * x + y * y).sqrt());
let lon = y.atan2(x);
(Angle::from_radians(lat), Angle::from_radians(lon))
}
fn latlong_to_nvector(latitude: Angle, longitude: Angle) -> Vec3 {
if latitude == Angle::QUARTER_CIRCLE {
return Vec3::UNIT_Z;
}
if latitude == Angle::NEG_QUARTER_CIRCLE {
return Vec3::NEG_UNIT_Z;
}
let latitude_rads = latitude.as_radians();
let longitude_rads = longitude.as_radians();
let cl = latitude_rads.cos();
let x = cl * longitude_rads.cos();
let y = cl * longitude_rads.sin();
let z = latitude_rads.sin();
Vec3::new(x, y, z)
}
#[cfg(test)]
pub(crate) fn assert_nv_eq_d7(expected: NVector, actual: NVector) {
let lle = LatLong::from_nvector(expected).round_d7();
let lla: LatLong = LatLong::from_nvector(actual).round_d7();
if lle != lla {
panic!(
"Expected position was {:#?} but actual position is {:#?}",
lle, lla
)
}
}
#[cfg(test)]
pub(crate) fn assert_opt_nv_eq_d7(expected: NVector, actual: Option<NVector>) {
match actual {
Some(a) => assert_nv_eq_d7(expected, a),
None => panic!(
"Expected position was {:#?} but actual position is None",
LatLong::from_nvector(expected).round_d7()
),
}
}
#[cfg(test)]
pub(crate) fn assert_geod_eq_d7_mm(expected: GeodeticPosition, actual: GeodeticPosition) {
assert_nv_eq_d7(expected.horizontal_position(), actual.horizontal_position());
assert_eq!(expected.height().round_mm(), actual.height().round_mm());
}
#[cfg(test)]
mod tests {
use crate::{Cartesian3DVector, GeocentricPosition, LatLong, NVector, Vec3};
#[test]
fn nvector_from_north_pole() {
for lng in -180..180 {
let nv = NVector::from_lat_long_degrees(90.0, lng as f64);
assert_eq!(Vec3::UNIT_Z, nv.0);
let ll: LatLong = LatLong::from_degrees(90.0, lng as f64);
assert_eq!(Vec3::UNIT_Z, ll.to_nvector().0);
}
}
#[test]
fn nvector_from_south_pole() {
for lng in -180..180 {
let nv = NVector::from_lat_long_degrees(-90.0, lng as f64);
assert_eq!(Vec3::NEG_UNIT_Z, nv.0);
let ll: LatLong = LatLong::from_degrees(-90.0, lng as f64);
assert_eq!(Vec3::NEG_UNIT_Z, ll.to_nvector().0);
}
}
#[test]
fn lat_long_from_unit_z() {
assert_eq!(
LatLong::from_degrees(90.0, 0.0),
LatLong::from_nvector(NVector::new(Vec3::UNIT_Z))
);
}
#[test]
fn lat_long_from_neg_unit_z() {
assert_eq!(
LatLong::from_degrees(-90.0, 0.0),
LatLong::from_nvector(NVector::new(Vec3::NEG_UNIT_Z))
);
}
#[test]
fn round() {
assert_eq!(
LatLong::from_degrees(54.00001, -154.00001),
LatLong::from_degrees(54.000009, -154.000011).round_d5()
);
assert_eq!(
LatLong::from_degrees(54.000001, -154.000001),
LatLong::from_degrees(54.0000009, -154.0000011).round_d6()
);
assert_eq!(
LatLong::from_degrees(54.0000001, -154.0000001),
LatLong::from_degrees(54.00000009, -154.00000011).round_d7()
);
}
#[test]
fn round_mm_geocentric() {
let actual = GeocentricPosition::from_metres(
-3387528.4972551535,
1652208.0428068785,
5152924.171316559,
);
let expected = GeocentricPosition::from_metres(-3387528.497, 1652208.043, 5152924.171);
assert_eq!(expected, actual.round_mm());
}
#[test]
fn round_cm_geocentric() {
let actual = GeocentricPosition::from_metres(
-3387528.4972551535,
1652208.0428068785,
5152924.171316559,
);
let expected = GeocentricPosition::from_metres(-3387528.5, 1652208.04, 5152924.17);
assert_eq!(expected, actual.round_cm());
}
#[test]
fn round_dm_geocentric() {
let actual = GeocentricPosition::from_metres(
-3387528.4972551535,
1652208.0428068785,
5152924.171316559,
);
let expected = GeocentricPosition::from_metres(-3387528.5, 1652208.0, 5152924.2);
assert_eq!(expected, actual.round_dm());
}
#[test]
fn round_m_geocentric() {
let actual = GeocentricPosition::from_metres(
-3387528.4972551535,
1652208.0428068785,
5152924.171316559,
);
let expected = GeocentricPosition::from_metres(-3387528.0, 1652208.0, 5152924.0);
assert_eq!(expected, actual.round_m());
}
}