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
//! Spherical and Geographic coordinates.
//!
//! TODO: more explaination here

use nalgebra::{Vector3, Dot, Vector2};
use std::f64::consts::PI;
use std::f64::*;
use std::fmt;
use navdata::geohash;

static TWO_PI: f64 = PI * 2.0;
static HALF_PI: f64 = PI / 2.0;

/// Mean sea level on earth
pub static EARTH_MSL_RADIUS: f64 = 6371008.8;

/// Represents a coordinate in the spherical coordinate system.
///
/// The normal range for the variables is as follows:
///
/// |name  |   range       |
/// |------|---------------|
/// | r    | 0 -> infinity |
/// |theta |  (0 -> 2PI)   |
/// |phi   |  (0 -> PI)  |
///
/// See: http://mathworld.wolfram.com/SphericalCoordinates.html for more details.
///
/// The design decision to combine Spherical with Geographical coordinates was made
/// beceause many of the algorithms available, and literature deal with the spherical
/// coordinate system, so it is easier to implement it with that. It is fairly low cost
/// to go between the two representations, and this only really occurs twice, once in, and
/// once out to display. Most of the heavy calculations will be while the coordinate is
/// treated as a spherical coordinate.
///
///
/// # Examples
///
/// It is very common to instantiate from a geographical point and then access those values later
/// in the form of latitude and longitude:
///
/// ```
/// # use oldnav_lib::navdata::coord::SphericalCoordinate;
/// # let accuracy = 0.0001;
///
/// let pos = SphericalCoordinate::from_geographic(324.567, 82.123, 23.452);
/// println!("{},{}", pos.lat(), pos.lon());
///
/// # assert!((pos.alt()-324.567).abs() < accuracy);
/// # assert!((pos.lat()-82.123).abs() < accuracy);
/// # assert!((pos.lon()-23.452).abs() < accuracy);
/// ```
///
/// If you are manually setting the r, theta or phi values you need to ensure that your values
/// fall within the range specified, or that you use the `rectify_bounds()` method to
/// transform the coordinate into the specified range after you set the value
///
/// ```
/// # use oldnav_lib::navdata::coord::SphericalCoordinate;
/// # use std::f64::consts::PI;
/// let accuracy = 0.0001;
///
/// let mut pos = SphericalCoordinate::new(30.0, 0.0, PI);
/// println!("{:?}", pos);
///
/// # assert!((pos.r-30.0).abs() < accuracy);
/// # assert!((pos.theta-0.0).abs() < accuracy);
/// # assert!((pos.phi-PI).abs() < accuracy);
///
/// // negative theta puts coordinate outside bounds of (0 -> 2PI)
/// pos.theta = -1.0 * PI;
/// pos.rectify_bounds_inplace();
/// println!("{:?}", pos);
///
/// // test to see that coordinate is now properly within the bounds
/// # assert!((pos.r-30.0).abs() < accuracy);
/// assert!((pos.theta-PI).abs() < accuracy);
/// assert!((pos.phi-PI).abs() < accuracy);
///
/// ```
///
#[derive(Copy, Clone)]
pub struct SphericalCoordinate {
    // todo: replace this struct with a trait for Vector3
    /// Radius component
    pub r: f64,

    /// Theta component
    pub theta: f64,

    /// Phi component
    pub phi: f64,
}


impl SphericalCoordinate {
    /// Constructor for `SphericalCoordinate`
    ///
    /// # Examples
    ///
    /// ```
    /// # use oldnav_lib::navdata::coord::SphericalCoordinate;
    /// # use std::f64::consts::PI;
    /// # let accuracy = 0.0001;
    ///
    /// let pos = SphericalCoordinate::new(10.0, PI, 0.0);
    /// println!("theta: {:?}", pos.theta);
    ///
    /// # assert!((pos.r-10.0).abs() < accuracy);
    /// # assert!((pos.theta-PI).abs() < accuracy);
    /// # assert!((pos.phi-0.0).abs() < accuracy);
    /// ```
    ///
    /// When the values exceed the normal bountaries for the spherical coordinate system, they
    /// are wrapped back around to the equivalent angle (rectified) to ensure they stay within
    /// the expected values for other calculations:
    ///
    /// ```
    /// # use oldnav_lib::navdata::coord::SphericalCoordinate;
    /// # use std::f64::consts::PI;
    ///
    /// let accuracy = 0.0001;
    /// let pos = SphericalCoordinate::new(10.0, 3.0*PI, 0.0);
    ///
    /// // test to see if values are assigned accurately
    /// assert!((pos.r-10.0).abs() < accuracy);
    /// // theta has been rectified to be within boundary (0 -> 2PI)
    /// assert!((pos.theta-PI).abs() < accuracy);
    /// assert!((pos.phi-0.0).abs() < accuracy);
    /// ```
    ///
    pub fn new(r: f64, theta: f64, phi: f64) -> SphericalCoordinate {
        let mut coord = SphericalCoordinate {
            r: r,
            theta: theta,
            phi: phi,
        };
        coord.rectify_bounds_inplace();
        return coord;
    }

    /// Create a new SphericalCoordinate from geographic coordinate.
    ///
    /// **alt**: metres above the surface as defined by `EARTH_MSL_RADIUS`
    ///
    /// **lat**: latitude in degrees
    ///
    /// **lon**: longitude in degrees
    ///
    /// # Examples
    ///
    ///
    pub fn from_geographic(alt: f64, lat: f64, lon: f64) -> SphericalCoordinate {
        return SphericalCoordinate::new(alt + EARTH_MSL_RADIUS,
                                        f64::to_radians(lon) + PI,
                                        f64::to_radians(lat) + HALF_PI);
    }

    /// Create a new SphericalCoordinate from cartesian coordinate.
    ///
    /// Scale of vector v needs to be in meters, with reference position being the centre of
    /// the sphere.

    pub fn from_cartesian(v: Vector3<f64>) -> SphericalCoordinate {
        let r = f64::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
        let mut theta = NAN;

        if v.x > 0.0 {
            theta = f64::atan(v.y / v.x);
        } else if v.x < 0.0 {
            if v.y > 0.0 {
                theta = f64::atan(v.y / v.x) + PI;
            } else if v.y < 0.0 {
                theta = f64::atan(v.y / v.x) - PI;
            } else {
                theta = PI;
            }
        } else {
            if v.y > 0.0 {
                theta = PI;
            } else if v.y < 0.0 {
                theta = -PI;
            }
            // else theta = NAN by default
        }

        let phi = f64::acos(v.z / r);

        return SphericalCoordinate::new(r, theta, phi);
    }

    /// Create a clone with values that fall within the normal
    /// boundary of Spherical coordinate system.
    pub fn rectify_bounds(&self) -> SphericalCoordinate {
        let mut new_coord = self.clone();
        new_coord.rectify_bounds_inplace();
        return new_coord;
    }

    /// Ensure that this object's values fall within the normal
    /// boundary of Spherical coordinate system.
    pub fn rectify_bounds_inplace(&mut self) {
        while self.phi < 0.0 {
            self.phi += TWO_PI;
        }

        while self.phi > TWO_PI {
            self.phi -= TWO_PI;
        }

        if self.phi > PI {
            self.phi = TWO_PI - self.phi;
            self.theta += PI;
        } else {
            if self.phi < 0.0 {
                self.phi = -(TWO_PI + self.phi);
                self.theta += PI;
            }
        }

        while self.theta > TWO_PI {
            self.theta -= TWO_PI;
        }

        while self.theta < 0.0 {
            self.theta += TWO_PI;
        }
    }

    /// Check whether this `SphericalCoordinate` and another are approximately equal
    ///
    /// # Examples
    ///
    /// ```
    /// # use oldnav_lib::navdata::coord::SphericalCoordinate;
    /// # use std::f64::consts::PI;
    ///
    /// let pos1 = SphericalCoordinate::new(10.0, PI, 0.0);
    /// let pos2 = SphericalCoordinate::new(10.0, PI, 0.1);
    /// assert!(pos1.approx_eq(&pos2, 0.2));
    ///
    /// ```
    pub fn approx_eq(&self, other: &SphericalCoordinate, epsilon: f64) -> bool {
        let mut eq = true;
        eq &= (self.r - other.r).abs() < epsilon;
        eq &= (self.theta - other.theta).abs() < epsilon;
        eq &= (self.phi - other.phi).abs() < epsilon;
        return eq;
    }

    /// get the altitude
    pub fn alt(&self) -> f64 {
        return self.r - EARTH_MSL_RADIUS;
    }

    /// set the altitude above MSL (in metres)
    pub fn set_alt(&mut self, alt: f64) {
        self.r = alt + EARTH_MSL_RADIUS;
    }

    /// get the latitude (in degrees)
    pub fn lat(&self) -> f64 {
        return (self.phi - HALF_PI).to_degrees();
    }

    /// set the latitude (in degrees)
    pub fn set_lat(&mut self, lat: f64) {
        self.phi = lat.to_radians() + HALF_PI;
        self.rectify_bounds_inplace();
    }

    /// get the longitude (in degrees)
    pub fn lon(&self) -> f64 {
        return (self.theta - PI).to_degrees();
    }

    /// set the longitude (in degrees)
    pub fn set_lon(&mut self, lon: f64) {
        self.theta = lon.to_radians() + PI;
        self.rectify_bounds_inplace();
    }

    /// get the r cartesian unit vector
    pub fn r_cart_uv(&self) -> Vector3<f64> {
        return Vector3 {
            x: self.theta.cos() * self.phi.sin(),
            y: self.theta.sin() * self.phi.sin(),
            z: self.phi.cos(),
        };
    }

    /// get the phi cartesian unit vector
    pub fn phi_cart_uv(&self) -> Vector3<f64> {
        return Vector3 {
            x: self.phi.cos() * self.theta.cos(),
            y: self.phi.cos() * self.theta.sin(),
            z: -self.phi.sin(),
        };
    }

    /// get the theta cartesian unit vector
    pub fn theta_cart_uv(&self) -> Vector3<f64> {
        return Vector3 {
            x: -self.theta.sin(),
            y: self.theta.cos(),
            z: 0.0,
        };
    }

    /// arc distance between two points along the surface of the sphere.
    /// **warning: only tested to be accurate to within 5 meters at earth's surface**
    pub fn arc_distance(&self, other: &SphericalCoordinate) -> f64 {
        // if greater accuracy is required, might be worth checking out the haversine formula
        // or this: https://goo.gl/Niyn91
        return self.r * (self.r_cart_uv().dot(&other.r_cart_uv()).acos());
    }

    /// Format the `SphericalCoordinate` as a Geographical point string (altitude,
    /// latitude and longitude).
    pub fn fmt_geographic(&self) -> String {
        format!("Point {{alt: {}, lat: {}, lon: {}}}",
                self.alt(),
                self.lat(),
                self.lon())
    }

    /// A very simple difference in angle heuristic
    pub fn angle_difference_heuristic(&self, other: &SphericalCoordinate) -> f64 {
        let dtheta = other.theta - self.theta;
        let dphi = other.phi - self.phi;
        return f64::sqrt(dtheta * dtheta + dphi * dphi);
    }
}

impl fmt::Debug for SphericalCoordinate {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "SphericalCoordinate {{r: {}, theta: {}, phi: {}}}",
               self.r,
               self.theta,
               self.phi)
    }
}


impl fmt::Display for SphericalCoordinate {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,
               "SphericalCoordinate {{r: {}, theta: {}, phi: {}}}",
               self.r,
               self.theta,
               self.phi)
    }
}

impl geohash::Geohashable<SphericalCoordinate> for SphericalCoordinate {
    fn integer_decode(geohash: u64) -> Result<SphericalCoordinate, String> {
        let bounds = try!(geohash::decode(geohash, &geohash::LATLON_BOUNDS));
        let pos = bounds.mid();
        let coord = SphericalCoordinate::from_geographic(0.0, pos.y, pos.x);
        return Ok(coord);
    }
    fn integer_encode(&self, precision: u8) -> Result<u64, String> {
        return geohash::encode(&Vector2 {
                                   x: self.lon(),
                                   y: self.lat(),
                               },
                               precision,
                               &geohash::LATLON_BOUNDS);
    }
}


/// Convert degrees minutes seconds format into seconds.
///
/// Examples:
///
/// ```
/// # use oldnav_lib::navdata::coord::dms_to_deg;
/// let accuracy = 0.0001;
/// let deg = dms_to_deg(10.0, 4.0, 6.0);
/// assert!((deg - 10.06833).abs() < accuracy);
/// ```
pub fn dms_to_deg(degrees: f64, minutes: f64, seconds: f64) -> f64 {
    return degrees + minutes / 60.0 + seconds / 3600.0;
}