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
use std::f64::consts::PI;
const DEG2RAD: f64 = PI / 180.;
const RAD2DEG: f64 = 180. / PI;
use crate::consts::WGS84_A;
use crate::consts::WGS84_F;
use nalgebra as na;
use crate::skerror;
use crate::types::Quaternion as Quat;
use crate::types::Vec3;
use crate::SKResult;
///
/// Representation of a coordinate in the
/// International Terrestrial Reference Frame (ITRF)
///
/// This coordinate object can be created from and also
/// output to Geodetic coordinates (latitude, longitude,
/// height above ellipsoid).
///
/// Functions are also available to provide rotation
/// quaternions to the East-North-Up frame
/// and North-East-Down frame at this coordinate
///
#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)]
pub struct ITRFCoord {
pub itrf: Vec3,
}
impl std::fmt::Display for ITRFCoord {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let (lat, lon, hae) = self.to_geodetic_deg();
write!(
f,
"ITRFCoord(lat: {:8.4} deg, lon: {:8.4} deg, altitude: {:5.2} km)",
lat,
lon,
hae / 1.0e3
)
}
}
impl std::ops::Add<Vec3> for ITRFCoord {
type Output = Self;
fn add(self, other: Vec3) -> Self::Output {
Self {
itrf: self.itrf + other,
}
}
}
impl std::ops::Add<Vec3> for &ITRFCoord {
type Output = ITRFCoord;
fn add(self, other: Vec3) -> Self::Output {
ITRFCoord {
itrf: self.itrf + other,
}
}
}
impl std::ops::Add<&Vec3> for ITRFCoord {
type Output = Self;
fn add(self, other: &Vec3) -> Self::Output {
Self {
itrf: self.itrf + other,
}
}
}
impl std::ops::Add<&Vec3> for &ITRFCoord {
type Output = ITRFCoord;
fn add(self, other: &Vec3) -> Self::Output {
ITRFCoord {
itrf: self.itrf + other,
}
}
}
impl std::ops::Sub<Vec3> for ITRFCoord {
type Output = Self;
fn sub(self, other: Vec3) -> Self::Output {
Self {
itrf: self.itrf - other,
}
}
}
impl std::ops::Sub<ITRFCoord> for ITRFCoord {
type Output = Vec3;
fn sub(self, other: ITRFCoord) -> Vec3 {
self.itrf - other.itrf
}
}
impl std::ops::Sub<ITRFCoord> for &ITRFCoord {
type Output = Vec3;
fn sub(self, other: ITRFCoord) -> Vec3 {
self.itrf - other.itrf
}
}
impl std::ops::Sub<&ITRFCoord> for &ITRFCoord {
type Output = Vec3;
fn sub(self, other: &ITRFCoord) -> Vec3 {
self.itrf - other.itrf
}
}
impl std::ops::Sub<&ITRFCoord> for ITRFCoord {
type Output = Vec3;
fn sub(self, other: &ITRFCoord) -> Vec3 {
self.itrf - other.itrf
}
}
impl std::convert::From<[f64; 3]> for ITRFCoord {
fn from(v: [f64; 3]) -> Self {
ITRFCoord {
itrf: Vec3::from(v),
}
}
}
impl std::convert::From<&[f64]> for ITRFCoord {
fn from(v: &[f64]) -> Self {
assert!(v.len() == 3);
ITRFCoord {
itrf: Vec3::from_row_slice(v),
}
}
}
impl std::convert::From<Vec3> for ITRFCoord {
fn from(v: Vec3) -> Self {
ITRFCoord { itrf: v }
}
}
impl std::convert::From<ITRFCoord> for Vec3 {
fn from(itrf: ITRFCoord) -> Self {
itrf.itrf
}
}
impl ITRFCoord {
/// Returns an ITRF Coordinate given the geodetic inputs
/// with degree units for latitude & longitude
///
/// # Arguments:
///
/// * `lat` - Geodetic latitude in degrees
/// * `lon` - Geodetic longitude in degrees
/// * `hae` - Height above ellipsoid, in meters
///
/// # Examples:
/// ```
/// // Create coord for ~ Boston, MA
/// use satkit::itrfcoord::ITRFCoord;
/// let itrf = ITRFCoord::from_geodetic_deg(42.466, -71.1516, 150.0);
/// ```
///
pub fn from_geodetic_deg(lat: f64, lon: f64, hae: f64) -> ITRFCoord {
ITRFCoord::from_geodetic_rad(lat * DEG2RAD, lon * DEG2RAD, hae)
}
///
/// Returns an ITRF Coordinate given Cartesian ITRF coordinates
///
/// # Arguments:
///
/// * `v` - `nalgebra::Vector3<f64>` representing ITRF coordinates in meters
///
/// # Examples:
///
/// ```
/// // Create coord for ~ Boston, MA
/// use satkit::itrfcoord::ITRFCoord;
/// use nalgebra as na;
/// let itrf = ITRFCoord::from_vector(&na::Vector3::new(1522386.15660978, -4459627.78585002, 4284030.6890791));
/// ```
///
///
pub fn from_vector(v: &na::Vector3<f64>) -> ITRFCoord {
ITRFCoord { itrf: v.clone() }
}
/// Returns an ITRF Coordinate given Cartesian ITRF coordinates represented as a slice
///
/// # Arguments:
///
/// * `v` - Slice representing ITRF coordinates in meters
///
/// # Examples:
///
/// ```
/// // Create coord for ~ Boston, MA
/// use satkit::itrfcoord::ITRFCoord;
/// let itrf = ITRFCoord::from_slice(&[1522386.15660978, -4459627.78585002, 4284030.6890791]);
/// ```
///
pub fn from_slice(v: &[f64]) -> SKResult<ITRFCoord> {
if v.len() != 3 {
return skerror!("Input slice must have 3 elements");
}
Ok(ITRFCoord {
itrf: Vec3::from_row_slice(v),
})
}
/// Returns an ITRF Coordinate given the geodetic inputs
/// with radian units for latitude & longitude
///
/// # Arguments:
///
/// * `lat` - Geodetic latitude in radians
/// * `lon` - Geodetic longitude in radians
/// * `hae` - Height above ellipsoid, in meters
///
/// # Examples:
/// ```
/// // Create coord for ~ Boston, MA
/// use satkit::itrfcoord::ITRFCoord;
/// use std::f64::consts::PI;
/// const DEG2RAD: f64 = PI / 180.0;
/// let itrf = ITRFCoord::from_geodetic_rad(42.466*DEG2RAD, -71.1516*DEG2RAD, 150.0);
/// ```
///
pub fn from_geodetic_rad(lat: f64, lon: f64, hae: f64) -> ITRFCoord {
let sinp: f64 = lat.sin();
let cosp: f64 = lat.cos();
let sinl: f64 = lon.sin();
let cosl: f64 = lon.cos();
let f2 = (1.0 - WGS84_F).powf(2.0);
let c = 1.0 / (cosp * cosp + f2 * sinp * sinp).sqrt();
let s = f2 * c;
ITRFCoord {
itrf: Vec3::from([
(WGS84_A * c + hae) * cosp * cosl,
(WGS84_A * c + hae) * cosp * sinl,
(WGS84_A * s + hae) * sinp,
]),
}
}
/// Returns 3-element tuple representing geodetic coordinates
///
/// # Tuple contents:
///
/// * `.0` - latitude in radians
/// * `.1` - longitude in radians
/// * `.2` - height above ellipsoid, in meters
///
pub fn to_geodetic_rad(&self) -> (f64, f64, f64) {
const B: f64 = WGS84_A * (1.0 - WGS84_F);
const E2: f64 = 1.0 - (1.0 - WGS84_F) * (1.0 - WGS84_F);
const EP2: f64 = E2 / (1.0 - E2);
let rho = (self.itrf[0] * self.itrf[0] + self.itrf[1] * self.itrf[1]).sqrt();
let mut beta: f64 = f64::atan2(self.itrf[2], (1.0 - WGS84_F) * rho);
let mut sinbeta: f64 = beta.sin();
let mut cosbeta: f64 = beta.cos();
let mut phi: f64 = f64::atan2(
self.itrf[2] + B * EP2 * sinbeta.powf(3.0),
rho - WGS84_A * E2 * cosbeta.powf(3.0),
);
let mut betanew: f64 = f64::atan2((1.0 - WGS84_F) * phi.sin(), phi.cos());
for _x in 0..5 {
beta = betanew;
sinbeta = beta.sin();
cosbeta = beta.cos();
phi = f64::atan2(
self.itrf[2] + B * EP2 * sinbeta.powf(3.0),
rho - WGS84_A * E2 * cosbeta.powf(3.0),
);
betanew = f64::atan2((1.0 - WGS84_F) * phi.sin(), phi.cos());
}
let lat: f64 = phi;
let lon: f64 = f64::atan2(self.itrf[1], self.itrf[0]);
let sinphi: f64 = phi.sin();
let n: f64 = WGS84_A / (1.0 - E2 * sinphi * sinphi).sqrt();
let h = rho * phi.cos() + (self.itrf[2] + E2 * n * sinphi) * sinphi - n;
(lat, lon, h)
}
/// Returns 3-element tuple representing geodetic coordinates
///
/// # Tuple contents:
///
/// * `.0` - latitude in degrees
/// * `.1` - longitude in degrees
/// * `.2` - height above ellipsoid, in meters
///
pub fn to_geodetic_deg(&self) -> (f64, f64, f64) {
let (lat_rad, lon_rad, hae) = self.to_geodetic_rad();
(lat_rad * RAD2DEG, lon_rad * RAD2DEG, hae)
}
/// Return geodetic longitude in radians, [-π, π]
///
#[inline]
pub fn longitude_rad(&self) -> f64 {
f64::atan2(self.itrf[1], self.itrf[0])
}
/// Return geodetic longitude in degrees, [-180, 180]
#[inline]
pub fn longitude_deg(&self) -> f64 {
self.longitude_rad() * RAD2DEG
}
/// return geodetic latitude in radians, [-π/2, π/2]
#[inline]
pub fn latitude_rad(&self) -> f64 {
let (lat, _a, _b) = self.to_geodetic_rad();
lat
}
/// Return height above ellipsoid in meters
#[inline]
pub fn hae(&self) -> f64 {
let (_a, _b, hae) = self.to_geodetic_rad();
hae
}
/// Return geodetic latitude in degrees, [-180, 180]
#[inline]
pub fn latitude_deg(&self) -> f64 {
self.latitude_rad() * RAD2DEG
}
/// Compute location when moving a given Distance at a given heading along the Earth's surface
/// Altitude assumed to be zero
///
/// # Arguments:
/// * `distance_m` - Distance in meters to travel along surface of Earth
/// * `heading_rad` - Initial heading, in radians
///
/// # Returns:
/// * ITRFCoord representing final position
///
/// # References:
/// * Uses Vincenty's formula
/// See: <https://en.wikipedia.org/wiki/Vincenty%27s_formulae>
///
/// # Arguments:
///
/// * `distance_m` - Distance in meters to travel along surface of Earth
/// * `heading_rad` - Initial heading, in radians
///
/// # Returns:
///
/// * ITRFCoord representing final position
///
pub fn move_with_heading(&self, distance_m: f64, heading_rad: f64) -> ITRFCoord {
let phi1 = self.latitude_rad();
#[allow(non_upper_case_globals)]
const a: f64 = WGS84_A;
#[allow(non_upper_case_globals)]
const b: f64 = (1.0 - WGS84_F) * WGS84_A;
let u1 = ((1.0 - WGS84_F) * phi1.tan()).atan();
let sigma1 = f64::atan2(u1.tan(), heading_rad.cos());
let sinalpha = u1.cos() * heading_rad.sin();
let usq = (1.0 - sinalpha.powf(2.0)) * ((a / b).powf(2.0) - 1.0);
let big_a = 1.0 + usq / 16384.0 * (4096.0 + usq * (-768.0 + usq * (320.0 - 175.0 * usq)));
let big_b = usq / 1024.0 * (256.0 + usq * (-128.0 + usq * (74.0 - 47.0 * usq)));
let mut sigma = distance_m / b / big_a;
let mut costwosigmam = 0.0;
for _ in 0..5 {
costwosigmam = (2.0 * sigma1 + sigma).cos();
let dsigma = big_b
* sigma.sin()
* (costwosigmam
+ 0.25
* big_b
* (sigma.cos() * (-1.0 + 2.0 * costwosigmam.powf(2.0))
- big_b / 6.0
* costwosigmam
* (-3.0 + 4.0 * sigma.sin().powf(2.0))
* (-3.0 + 4.0 * costwosigmam.powf(2.0))));
sigma = distance_m / b / big_a + dsigma;
}
let phi2 = f64::atan2(
u1.sin() * sigma.cos() + u1.cos() * sigma.sin() * heading_rad.cos(),
(1.0 - WGS84_F)
* (sinalpha.powf(2.0)
+ (u1.sin() * sigma.sin() - u1.cos() * sigma.cos() * heading_rad.cos())
.powf(2.0))
.sqrt(),
);
let lam = f64::atan2(
sigma.sin() * heading_rad.sin(),
u1.cos() * sigma.cos() - u1.sin() * sigma.sin() * heading_rad.cos(),
);
let cossqalpha = 1.0 - sinalpha.powf(2.0);
let big_c = WGS84_F / 16.0 * cossqalpha * (4.0 + WGS84_F * (4.0 - 3.0 * cossqalpha));
let delta_lon = lam
- (1.0 - big_c)
* WGS84_F
* sinalpha
* (sigma
+ big_c
* sigma.sin()
* (costwosigmam
+ big_c * sigma.cos() * (-1.0 + 2.0 * costwosigmam.powf(2.0))));
let lambda2 = delta_lon + self.longitude_rad();
ITRFCoord::from_geodetic_rad(phi2, lambda2, 0.0)
}
/// Geodesic distance between two coordinates
///
/// Return Geodesic distance (shortest distance along Earth's surface) in meters
/// between self and another ITRF coordinate
///
/// Also returns initial and final heading
///
/// # Arguments:
///
/// * `other` - ITRF coordinate for which distance will be computed
///
/// # Outputs:
/// Tuple with following values
///
/// * `0` - Distance in meters
/// * `1` - Starting heading (at self) in radians
/// * `2` - Final heading (at other) in radians
//
/// # References
// * Vincenty's formula inverse
/// See: <https://en.wikipedia.org/wiki/Vincenty%27s_formulae>
/// See: <https://geodesyapps.ga.gov.au/vincenty-inverse>
///
pub fn geodesic_distance(&self, other: &ITRFCoord) -> (f64, f64, f64) {
#[allow(non_upper_case_globals)]
const a: f64 = WGS84_A;
#[allow(non_upper_case_globals)]
const b: f64 = (1.0 - WGS84_F) * WGS84_A;
let lata = self.latitude_rad();
let latb = other.latitude_rad();
let lona = self.longitude_rad();
let lonb = other.longitude_rad();
let u1 = ((1.0 - WGS84_F) * lata.tan()).atan();
let u2 = ((1.0 - WGS84_F) * latb.tan()).atan();
let lam = lonb - lona;
let londiff = lam;
let mut lam = lonb - lona;
let mut cossqalpha = 0.0;
let mut sinsigma = 0.0;
let mut cossigma = 0.0;
let mut cos2sm = 0.0;
let mut sigma = 0.0;
for _ in 0..5 {
sinsigma = ((u2.cos() * lam.sin()).powf(2.0)
+ (u1.cos() * u2.sin() - u1.sin() * u2.cos() * lam.cos()).powf(2.0))
.sqrt();
cossigma = u1.sin() * u2.sin() + u1.cos() * u2.cos() * lam.cos();
sigma = f64::atan2(sinsigma, cossigma);
let sinalpha = (u1.cos() * u2.cos() * lam.sin()) / sigma.sin();
cossqalpha = 1.0 - sinalpha.powf(2.0);
cos2sm = sigma.cos() - (2.0 * u1.sin() * u2.sin()) / cossqalpha;
let c = WGS84_F / 16.0 * cossqalpha * (4.0 + WGS84_F * (4.0 - 3.0 * cossqalpha));
lam = londiff
+ (1.0 - c)
* WGS84_F
* sinalpha
* (sigma
+ c * sinsigma * (cos2sm + c * cossigma * (-1.0 + 2.0 * cos2sm.powf(2.0))));
}
let usq = cossqalpha * ((a / b).powf(2.0) - 1.0);
let biga = 1.0 + usq / 16384.0 * (4096.0 + usq * (-768.0 + usq * (320.0 - 175.0 * usq)));
let bigb = usq / 1024.0 * (256.0 + usq * (-128.0 + usq * (74.0 - 47.0 * usq)));
let dsigma = bigb
* sinsigma
* (cos2sm
+ 0.25
* bigb
* (cossigma * (-1.0 + 2.0 * cos2sm.powf(2.0))
- bigb / 6.0
* cos2sm
* (-3.0 + 4.0 * sinsigma.powf(2.0))
* (-3.0 + 4.0 * cos2sm.powf(2.0))));
let s = b * biga * (sigma - dsigma);
let alpha1 = f64::atan2(
u2.cos() * lam.sin(),
u1.cos() * u2.sin() - u1.sin() * u2.cos() * lam.cos(),
);
let alpha2 = f64::atan2(
u1.cos() * lam.sin(),
-u1.sin() * u2.cos() + u1.cos() * u2.sin() * lam.cos(),
);
(s, alpha1, alpha2)
}
/// Return quaternion representing rotation from the
/// North-East-Down (NED) coordinate frame to the
/// ITRF coordinate frame
#[inline]
pub fn q_ned2itrf(&self) -> Quat {
let (lat, lon, _) = self.to_geodetic_rad();
Quat::from_axis_angle(&Vec3::z_axis(), lon)
* Quat::from_axis_angle(&Vec3::y_axis(), -lat - PI / 2.0)
}
/// Convert coordinate to a North-East-Down (NED)
/// coordinate relative to a reference coordinate
///
/// # Arguments
///
/// * `ref_coord`` - `&ITRFCoord`` representing reference
///
/// # Return
///
/// * `nalgebra::Vector3<f64>` representing NED position
/// relative to reference. Units are meters
///
/// # Examples:
/// ```
/// use satkit::itrfcoord::ITRFCoord;
/// // Create coord
/// let itrf1 = ITRFCoord::from_geodetic_deg(42.466, -71.1516, 150.0);
/// // Crate 2nd coord 100 meters above
/// let itrf2 = ITRFCoord::from_geodetic_deg(42.466, -71.1516, 250.0);
///
/// // Get NED of itrf1 relative to itrf2
/// let ned = itrf1.to_ned(&itrf2);
/// // Should return [0.0, 0.0, 100.0]
/// ```
///
pub fn to_ned(&self, ref_coord: &ITRFCoord) -> Vec3 {
self.q_ned2itrf().conjugate() * (self.itrf - ref_coord.itrf)
}
/// Return quaternion representing rotation from the
/// East-North-Up (ENU) coordinate frame to the
/// ITRF coordinate frame
pub fn q_enu2itrf(&self) -> Quat {
let (lat, lon, _) = self.to_geodetic_rad();
Quat::from_axis_angle(&Vec3::z_axis(), lon + PI / 2.0)
* Quat::from_axis_angle(&Vec3::x_axis(), PI / 2.0 - lat)
}
/// Convert coordinate to a East-North-Up (ENU)
/// coordinate relative to a reference coordinate
///
/// # Arguments
///
/// * ref_coord - &ITRFCoord representing reference
///
/// # Return
///
/// * `nalgebra::Vector3<f64>` representing ENU position
/// relative to reference. Units are meters
///
/// # Examples:
/// ```
/// use satkit::itrfcoord::ITRFCoord;
/// // Create coord
/// let itrf1 = ITRFCoord::from_geodetic_deg(42.466, -71.1516, 150.0);
/// // Crate 2nd coord 100 meters above
/// let itrf2 = ITRFCoord::from_geodetic_deg(42.466, -71.1516, 250.0);
///
/// // Get ENU of itrf1 relative to itrf2
/// let enu = itrf1.to_ned(&itrf2);
/// // Should return [0.0, 0.0, -100.0]
/// ```
///
pub fn to_enu(&self, other: &ITRFCoord) -> Vec3 {
self.q_enu2itrf().conjugate() * (self.itrf - other.itrf)
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::{assert_abs_diff_eq, assert_relative_eq};
#[test]
fn test_geodesic() {
// Lets pick a random point and try it out...
let mumbai = ITRFCoord::from_geodetic_deg(19.16488608334183, 72.8314881731579, 0.0);
let dubai = ITRFCoord::from_geodetic_deg(25.207843059422945, 55.27053859644447, 0.0);
let (dist, h0, h1) = mumbai.geodesic_distance(&dubai);
// from google maps, distance is 1,926.80 km
// From <https://geodesyapps.ga.gov.au/vincenty-inverse>
// Distance = 1928536.609m
// Forward azimuth = 293.466588 deg
// Reverse azimuth = 106.780805 deg
assert_relative_eq!(dist, 1928536.609, max_relative = 1.0e-8);
assert_relative_eq!(h0 + 2.0 * PI, 293.466588 * DEG2RAD, max_relative = 1.0e-6);
assert_relative_eq!(h1 + PI, 106.780805 * DEG2RAD, max_relative = 1.0e-6);
// Moving from Mumbai at the given distance and heading should get us to Dubai
let testpoint = mumbai.move_with_heading(dist, h0);
// Check differences
let delta = dubai - testpoint;
assert_abs_diff_eq!(delta.norm(), 0.0, epsilon = 1.0e-6);
}
#[test]
fn geodetic() {
let lat_deg: f64 = 42.466;
let lon_deg: f64 = -71.0;
let hae: f64 = 150.0;
let itrf = ITRFCoord::from_geodetic_deg(lat_deg, lon_deg, hae);
println!("{}", itrf);
// Check conversions
assert!(((lat_deg - 42.466) / 42.466).abs() < 1.0e-6);
assert!(((lon_deg + 71.0) / 71.0).abs() < 1.0e-6);
assert!(((hae - 150.0) / 150.0).abs() < 1.0e-6);
}
#[test]
fn test_ned_enu() {
let lat_deg: f64 = 42.466;
let lon_deg: f64 = -74.0;
let hae: f64 = 150.0;
let itrf1 = ITRFCoord::from_geodetic_deg(lat_deg, lon_deg, hae);
let itrf2 = ITRFCoord::from_geodetic_deg(lat_deg, lon_deg, hae + 100.0);
let ned = itrf2.to_ned(&itrf1);
let enu = itrf2.to_enu(&itrf1);
assert!(enu[0].abs() < 1.0e-6);
assert!(enu[1].abs() < 1.0e-6);
assert!(((enu[2] - 100.0) / 100.0).abs() < 1.0e-6);
assert!(ned[0].abs() < 1.0e-6);
assert!(ned[1].abs() < 1.0e-6);
assert!(((ned[2] + 100.0) / 100.0).abs() < 1.0e-6);
let dvec = Vec3::from([-100.0, -200.0, 300.0]);
let itrf3 = itrf2 + itrf2.q_ned2itrf() * dvec;
let nedvec = itrf3.to_ned(&itrf2);
let itrf4 = itrf2 + itrf2.q_enu2itrf() * dvec;
let enuvec = itrf4.to_enu(&itrf2);
for x in 0..3 {
assert!(((nedvec[x] - dvec[x]) / nedvec[x]).abs() < 1.0e-3);
assert!(((enuvec[x] - dvec[x]) / nedvec[x]).abs() < 1.0e-3);
}
/*
let q = Quat::from_axis_angle(&Vec3::z_axis(), -0.003);
println!("{}", q);
println!("{}", q.to_rotation_matrix());
*/
let itrf1 = ITRFCoord::from_geodetic_deg(lat_deg, lon_deg, hae);
let itrf2 = itrf1 + itrf1.q_ned2itrf() * na::vector![0.0, 0.0, 10000.0];
println!("height diff = {}", itrf2.hae() - itrf1.hae());
}
}