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
use crate::enu::ENU;
use crate::nvector::NVector;
use crate::utils::RealFieldCopy;
use crate::wgs84::{ECCENTRICITY_SQ, SEMI_MAJOR_AXIS, SEMI_MINOR_AXIS, WGS84};
use crate::Access;
use na::{Matrix3, Point3};
use std::convert::{From, Into};
use std::ops::{Add, AddAssign, Sub, SubAssign};

/// Earth Centered Earth Fixed position
///
/// This struct represents a position in the ECEF coordinate system.
/// See: [ECEF](https://en.wikipedia.org/wiki/ECEF) for general description.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ECEF<N: RealFieldCopy>(Point3<N>);

impl<N: RealFieldCopy> ECEF<N> {
    /// Create a new ECEF position
    pub fn new(x: N, y: N, z: N) -> ECEF<N> {
        ECEF(Point3::new(x, y, z))
    }
}

impl<N: RealFieldCopy> ECEF<N> {
    /// Get the X component of this position
    pub fn x(&self) -> N {
        self.0.x
    }

    /// Get the Y component of this position
    pub fn y(&self) -> N {
        self.0.y
    }

    /// Get the Z component of this position
    pub fn z(&self) -> N {
        self.0.z
    }
}

impl<N: RealFieldCopy> ECEF<N> {
    /// Create a rotation matrix from ECEF frame to ENU frame
    fn r_enu_from_ecef(self) -> Matrix3<N> {
        let wgs = WGS84::from(self);
        Matrix3::new(
            -wgs.longitude_radians().sin(),
            wgs.longitude_radians().cos(),
            N::zero(),
            -wgs.latitude_radians().sin() * wgs.longitude_radians().cos(),
            -wgs.latitude_radians().sin() * wgs.longitude_radians().sin(),
            wgs.latitude_radians().cos(),
            wgs.latitude_radians().cos() * wgs.longitude_radians().cos(),
            wgs.latitude_radians().cos() * wgs.longitude_radians().sin(),
            wgs.latitude_radians().sin(),
        )
    }

    /// Create a rotation matrix from ENU frame to ECEF frame
    fn r_ecef_from_enu(self) -> Matrix3<N> {
        self.r_enu_from_ecef().transpose()
    }

    /// Euclidean distance between two ECEF positions
    pub fn distance(&self, other: &ECEF<N>) -> N
    where
        N: RealFieldCopy,
    {
        (other.0 - self.0).norm()
    }
}

impl<N, T> Add<T> for ECEF<N>
where
    N: RealFieldCopy,
    T: Into<ENU<N>>,
{
    type Output = ECEF<N>;
    fn add(self, right: T) -> ECEF<N> {
        let enu = T::into(right);
        let pos = self.0 + self.r_ecef_from_enu() * enu.access();
        ECEF(pos)
    }
}

impl<N, T> AddAssign<T> for ECEF<N>
where
    N: RealFieldCopy + AddAssign,
    T: Into<ENU<N>>,
{
    fn add_assign(&mut self, right: T) {
        let enu = T::into(right);
        self.0 = self.0 + (self.r_ecef_from_enu() * enu.access());
    }
}

impl<N: RealFieldCopy> Sub<ECEF<N>> for ECEF<N> {
    type Output = ENU<N>;
    fn sub(self, right: ECEF<N>) -> ENU<N> {
        let enu = right.r_enu_from_ecef() * (self.0 - right.0);
        ENU::new(enu.x, enu.y, enu.z)
    }
}

impl<N, T> Sub<T> for ECEF<N>
where
    N: RealFieldCopy,
    T: Into<ENU<N>>,
{
    type Output = ECEF<N>;
    fn sub(self, right: T) -> ECEF<N> {
        let enu = T::into(right);
        ECEF(self.0 - (self.r_ecef_from_enu() * enu.access()))
    }
}

impl<N, T> SubAssign<T> for ECEF<N>
where
    N: RealFieldCopy + SubAssign,
    T: Into<ENU<N>>,
{
    fn sub_assign(&mut self, right: T) {
        let enu = T::into(right);
        self.0 = self.0 - (self.r_ecef_from_enu() * enu.access());
    }
}

// This macro implements most standard operations for position types that
// can be converted to ECEF
macro_rules! ecef_impl {
    ($T:ident) => {
        impl<N, T> Add<T> for $T<N>
        where
            N: RealFieldCopy,
            T: Into<ENU<N>>,
        {
            type Output = $T<N>;
            fn add(self, right: T) -> Self {
                $T::from(ECEF::from(self) + right)
            }
        }

        impl<N, T> AddAssign<T> for $T<N>
        where
            N: RealFieldCopy + AddAssign,
            T: Into<ENU<N>>,
        {
            fn add_assign(&mut self, right: T) {
                *self = $T::from(ECEF::from(*self) + right);
            }
        }

        impl<N, T> Sub<T> for $T<N>
        where
            N: RealFieldCopy,
            T: Into<ENU<N>>,
        {
            type Output = $T<N>;
            fn sub(self, right: T) -> $T<N> {
                $T::from(ECEF::from(self) - right)
            }
        }

        impl<N: RealFieldCopy> Sub<$T<N>> for $T<N> {
            type Output = ENU<N>;
            fn sub(self, right: $T<N>) -> ENU<N> {
                ECEF::from(self) - ECEF::from(right)
            }
        }

        impl<N, T> SubAssign<T> for $T<N>
        where
            N: RealFieldCopy + SubAssign,
            T: Into<ENU<N>>,
        {
            fn sub_assign(&mut self, right: T) {
                *self = $T::from(ECEF::from(*self) - right);
            }
        }
    };
}

// In accordance with Gade(2010) all N-Vector operations works through
// converting to ECEF and then back
ecef_impl!(NVector);
// The only way to work with Latitude/Longitude is to convert to ECEF
ecef_impl!(WGS84);

impl<N: RealFieldCopy> From<WGS84<N>> for ECEF<N> {
    fn from(wgs: WGS84<N>) -> ECEF<N> {
        // Conversion from:
        // https://doi.org/10.1007/s00190-004-0375-4
        let semi_major_axis = N::from_f64(SEMI_MAJOR_AXIS).unwrap();
        let ecc_part = N::from_f64(ECCENTRICITY_SQ).unwrap();
        let sin_part = N::from_f64(0.5).unwrap()
            * (N::one() - (N::from_f64(2.0).unwrap() * wgs.latitude_radians()).cos());

        let n = semi_major_axis / (N::one() - ecc_part * sin_part).sqrt();
        let altitude = wgs.altitude();

        let x = (altitude + n) * wgs.latitude_radians().cos() * wgs.longitude_radians().cos();
        let y = (altitude + n) * wgs.latitude_radians().cos() * wgs.longitude_radians().sin();
        let z = (altitude + n - N::from_f64(ECCENTRICITY_SQ).unwrap() * n)
            * wgs.latitude_radians().sin();

        ECEF::new(x, y, z)
    }
}

impl<N: RealFieldCopy> From<NVector<N>> for ECEF<N> {
    fn from(f: NVector<N>) -> ECEF<N> {
        // Constants used for calculation
        let x = f.vector().z;
        let y = f.vector().y;
        let z = -f.vector().x;
        let a_over_b = N::from_f64(SEMI_MAJOR_AXIS).unwrap().powi(2)
            / N::from_f64(SEMI_MINOR_AXIS).unwrap().powi(2);
        // Multiplication part
        let mul = N::from_f64(SEMI_MINOR_AXIS).unwrap()
            / (x.powi(2) + a_over_b * y.powi(2) + a_over_b * z.powi(2)).sqrt();
        // NOTE: The following has been rearranged to follow ECEF convention
        // that Z points towards the north pole
        ECEF::new(
            -(mul * a_over_b * z + z * f.altitude()),
            mul * a_over_b * y + y * f.altitude(),
            mul * x + x * f.altitude(),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::enu::ENU;
    use crate::ned::NED;
    use crate::nvector::NVector;
    use crate::wgs84::WGS84;
    use crate::Access;
    use assert::close;

    // Helper method to check that two ECEF positions are equal
    fn ecef_close(a: ECEF<f64>, b: ECEF<f64>) {
        close(a.x(), b.x(), 0.000001);
        close(a.y(), b.y(), 0.000001);
        close(a.z(), b.z(), 0.000001);
    }

    quickcheck! {
        fn from_wgs84(wgs: WGS84<f64>) -> () {
            let test = WGS84::from(ECEF::from(wgs));

            close(wgs.latitude_radians(), test.latitude_radians(), 0.000001);
            close(wgs.longitude_radians(), test.longitude_radians(), 0.000001);
            close(wgs.altitude(), test.altitude(), 0.000001);
        }

        fn from_nvector(wgs: WGS84<f64>) -> () {
            let ans = ECEF::from(wgs);
            let test = ECEF::from(NVector::from(wgs));

            ecef_close(ans, test);
        }

        fn rotation(a: WGS84<f64>, b: WGS84<f64>) -> () {
            // Convert position into ECEF, we use WGS84 for simplicity
            let ecef_a = ECEF::from(a);
            let ecef_b = ECEF::from(b);

            // Calculate the vector between the two positions in ENU frame
            // and in Earth frame
            let vec_enu = ecef_b - ecef_a;
            let vec_e = ecef_b.0 - ecef_a.0;

            // Retrieve the rotation matrix for A converting ENU -> Earth
            let r_ecef_from_enu = ecef_a.r_ecef_from_enu();
            let vec_e2 = r_ecef_from_enu * vec_enu.access();

            // These should be equivalent:
            close(vec_e.as_ref(), vec_e2.as_ref(), 0.0000001);
        }

        fn add_vector(a: WGS84<f64>, b: WGS84<f64>) -> () {
            // Test that the difference between two positions added to
            // the first position leads to the second position
            let ecef_a = ECEF::from(a);
            let ecef_b = ECEF::from(b);

            let vec_ab = ecef_b - ecef_a;
            let ecef_b2 = ecef_a + vec_ab;

            ecef_close(ecef_b, ecef_b2);
        }

        // fn distance(a: WGS84<f64>, b: WGS84<f64>) -> () {
        //     // Test that the vector A->B and B->A is of equal length
        //     let dist_ab = (ECEF::from(b) - ECEF::from(a)).norm();
        //     let dist_ba = (ECEF::from(a) - ECEF::from(b)).norm();

        //     close(dist_ab, dist_ba, 0.000001);
        // }

        fn add_ned(a: WGS84<f64>, n: f64, e: f64, d: f64) -> () {
            // Test that adding a random NED vector to an ECEF position
            // is the same as adding the ENU version of the vector
            let ecef = ECEF::from(a);
            let enu_vec = ENU::new(e, n, -d);
            let ned_vec = NED::new(n, e, d);

            let ecef_enu = ecef + enu_vec;
            let ecef_ned = ecef + ned_vec;
            ecef_close(ecef_enu, ecef_ned);
        }

        fn sub_ned(a: WGS84<f64>, n: f64, e: f64, d: f64) -> () {
            // See `add_ned`
            let ecef = ECEF::from(a);
            let enu_vec = ENU::new(e, n, -d);
            let ned_vec = NED::new(n, e, d);

            let ecef_enu = ecef - enu_vec;
            let ecef_ned = ecef - ned_vec;
            ecef_close(ecef_enu, ecef_ned);
        }

        fn add_enu(a: WGS84<f64>, e: f64, n: f64, u: f64) -> () {
            // Test that adding a random ENU vector to an ECEF position
            // than taking the difference between those two positions
            // result in the original ENU vector
            let ecef = ECEF::from(a);
            let enu = ENU::new(e, n, u);

            let ecef_2 = ecef + enu;
            let enu_2 = ecef_2 - ecef;
            close(enu.access().as_ref(), enu_2.access().as_ref(), 0.0000001);
        }
    }
}