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
use std::ops::{ Add, Sub, Mul, Div };
use num_traits::{ ToPrimitive, FromPrimitive };
/// Seconds -> Degrees
pub const SEC2DEG: f64 = 3600.0;
/// Seconds -> Minutes
pub const SEC2MIN: f64 = 60.0;
/// Minutes -> Degrees
pub const MIN2DEG: f64 = 60.0;
#[derive(Debug, Copy, Clone, PartialEq)]
/// An angle in degrees, minutes, and seconds
///
/// See: https://en.wikipedia.org/wiki/Geographic_coordinate_system
///
/// # Example
///
/// ```
/// use dted2::primitives::Angle;
/// let angle = Angle::new(0, 1, 0.0);
/// assert_eq!(angle, Angle::from_secs(60.0));
/// ```
pub struct Angle {
pub deg: i16,
pub min: u8,
pub sec: f64,
pub total_sec: f64,
}
impl Angle {
/// Converts degrees, minutes, and seconds to an angle
///
/// # Arguments
///
/// * `deg` - The number of degrees
/// * `min` - The number of minutes
/// * `sec` - The number of seconds (floating point precision)
///
/// # Returns
///
/// The [Angle] with the number of degrees, minutes, and seconds
///
/// # Examples
///
/// ```
/// use dted2::primitives::Angle;
/// let angle = Angle::new(0, 1, 1.0);
/// assert_eq!(angle, Angle::from_secs(61.0));
/// ```
pub fn new(deg: i16, min: u8, sec: f64) -> Self {
Angle {
deg,
min,
sec,
total_sec: (deg as i32 * 3600 + min as i32 * 60) as f64 + sec,
}
}
/// Converts seconds to degrees, minutes, and seconds
/// AKA an [Angle]
///
/// # Arguments
///
/// * `sec` - The number of seconds
///
/// # Returns
///
/// The number of degrees, minutes, and seconds
///
/// # Examples
///
/// ```
/// use dted2::primitives::Angle;
/// let angle = Angle::from_secs(61.0);
/// assert_eq!(angle, Angle::new(0, 1, 1.0));
/// ```
pub fn from_secs(total_sec: f64) -> Self {
Angle {
deg: Angle::sec2deg(&total_sec),
min: Angle::sec2min(&total_sec),
sec: Angle::sec2sec(&total_sec),
total_sec,
}
}
/// Converts seconds to degrees
///
/// # Arguments
///
/// * `sec` - The number of seconds
///
/// # Returns
///
/// The number of degrees
fn sec2deg(sec: &f64) -> i16 {
*sec as i16 / 3600
}
/// Converts seconds to minutes
///
/// # Arguments
///
/// * `sec` - The number of seconds
///
/// # Returns
///
/// The number of minutes
fn sec2min(sec: &f64) -> u8 {
((*sec as u64 % 3600) / 60) as u8
}
/// Converts seconds to seconds
///
/// # Arguments
///
/// * `sec` - The number of seconds
///
/// # Returns
///
/// The number of seconds
fn sec2sec(sec: &f64) -> f64 {
sec % 60.0
}
}
/// Add's an [Angle] to another [Angle]
///
/// # Returns
///
/// * [Angle]
impl Add for Angle {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let sec = self.sec + rhs.sec;
let min_overflow = sec / 60.0;
let sec = sec % 60.0;
let min = self.min as u16 + rhs.min as u16 + min_overflow as u16;
let deg_overflow = min / 60;
let min = (min % 60) as u8;
let deg = self.deg + rhs.deg + deg_overflow as i16;
Angle::new(deg, min, sec)
}
}
/// Subtracts an [Angle] from another [Angle]
///
/// # Returns
///
/// * [Angle]
impl Sub for Angle {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
let sec_diff = self.sec - rhs.sec;
let sec_underflow = sec_diff.is_sign_negative() as i16;
let sec = (sec_diff + 60.0 * sec_underflow as f64) % 60.0;
let min_diff = self.min as i16 - rhs.min as i16 - sec_underflow;
let min_underflow = min_diff.is_negative() as i16;
let min = ((min_diff + 60 * min_underflow) % 60) as u8;
let deg = self.deg - rhs.deg - min_underflow;
Angle::new(deg, min, sec)
}
}
/// Multiplies an [Angle] by another [Angle]
///
/// # Returns
///
/// * [Angle]
impl Mul for Angle {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Angle::from_secs(self.total_sec * rhs.total_sec)
}
}
/// Multiplies an [Angle] by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [Angle]
impl<T> Mul<T> for Angle
where
T: ToPrimitive,
{
type Output = Self;
fn mul(self, rhs: T) -> Self::Output {
Angle::from_secs(self.total_sec * T::to_f64(&rhs).unwrap_or(0.0))
}
}
/// Divides an [Angle] by another [Angle]
///
/// # Returns
///
/// * [Angle]
impl Div for Angle {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Angle::from_secs(self.total_sec / rhs.total_sec)
}
}
/// Divides an [Angle] by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [Angle]
impl<T> Div<T> for Angle
where
T: ToPrimitive,
{
type Output = Self;
fn div(self, rhs: T) -> Self::Output {
Angle::from_secs(self.total_sec / T::to_f64(&rhs).unwrap_or(1.0))
}
}
/// Converts an [Angle] to radians of variable precision
macro_rules! impl_angle_into_type {
($($type:ty),*) => {
$(
#[doc = concat!(" Converts an [Angle] (degrees, minutes, seconds) to radians as ")]
#[doc = concat!(" a specific numeric type (`", stringify!($type), "`).")]
#[doc = concat!("")]
#[doc = concat!(" # Example")]
#[doc = concat!("")]
#[doc = concat!(" ```")]
#[doc = concat!(" use dted2::primitives::Angle;")]
#[doc = concat!(" let angle = Angle::new(0, 0, 0.0);")]
#[doc = concat!(" let radians: ", stringify!($type), " = angle.into();")]
#[doc = concat!(" assert_eq!(radians, 0.0 as ", stringify!($type), ");")]
#[doc = concat!(" ```")]
impl ::std::convert::Into<$type> for Angle {
fn into(self) -> $type {
self.deg.abs() as $type +
self.deg.signum() as $type * (
self.min as $type / (60.0 as $type) +
self.sec as $type / (3600.0 as $type)
)
}
}
#[doc = concat!(" Converts an [AxisElement<Angle>] to [AxisElement<", stringify!($type), ">].")]
impl ::std::convert::Into<AxisElement<$type>> for AxisElement<Angle> {
fn into(self) -> AxisElement<$type> {
AxisElement {
lat: self.lat.into(),
lon: self.lon.into(),
}
}
}
)*
};
}
impl_angle_into_type!(f32);
impl_angle_into_type!(f64);
impl_angle_into_type!(i16);
impl_angle_into_type!(i32);
impl_angle_into_type!(i64);
impl_angle_into_type!(i128);
impl_angle_into_type!(isize);
#[derive(Copy, Clone, Debug, PartialEq)]
/// An Axis element
///
/// # Fields
///
/// * `lat`: Latitude
/// * `lon`: Longitude
pub struct AxisElement<T> {
pub lat: T,
pub lon: T,
}
impl<T> AxisElement<T> {
pub fn new(lat: T, lon: T) -> Self {
Self { lat, lon }
}
}
/// Adds a [AxisElement]<[Angle]> to another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(Angle::new(0, 0, 59.0), Angle::new(0, 0, 0.0));
/// let b = AxisElement::new(Angle::new(3, 1, 1.0), Angle::new(0, 2, 0.0));
/// let c = a + b;
/// assert_eq!(c, AxisElement::new(Angle::new(3, 2, 0.0), Angle::new(0, 2, 0.0)));
/// ```
impl Add for AxisElement<Angle> {
type Output = AxisElement<Angle>;
fn add(self, rhs: Self) -> Self::Output {
AxisElement {
lat: self.lat + rhs.lat,
lon: self.lon + rhs.lon,
}
}
}
/// Adds a to a scalar `A` (max precision of f64) to a [AxisElement]`<T>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(-10, 20);
/// let b = 12.0;
/// let c = a + b;
/// assert_eq!(c, AxisElement::new(2, 32));
/// ```
impl<A, T> Add<A> for AxisElement<T>
where
A: Copy + ToPrimitive,
T: Copy + FromPrimitive + Add<Output = T>,
{
type Output = AxisElement<T>;
fn add(self, rhs: A) -> Self::Output {
let rhs = A::to_f64(&rhs).expect("Failed to convert RHS to f64");
AxisElement {
lat: self.lat + T::from_f64(rhs).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
lon: self.lon + T::from_f64(rhs).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
}
}
}
/// Adds a [AxisElement]`<A>` to a scalar [AxisElement]`<T>`,
/// using max precision of f64
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(10, 20);
/// let b = AxisElement::new(12, 32);
/// let c = a + b;
/// assert_eq!(c, AxisElement::new(22, 52));
/// ```
impl<A, T> Add<AxisElement<A>> for AxisElement<T>
where
A: ToPrimitive,
T: ToPrimitive + FromPrimitive + Add<Output = T>,
{
type Output = AxisElement<T>;
fn add(self, rhs: AxisElement<A>) -> Self::Output {
let rhs_lat: f64 = A::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
let rhs_lon: f64 = A::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
AxisElement {
lat: T::from_f64(lat + rhs_lat).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
lon: T::from_f64(lon + rhs_lon).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
}
}
}
/// Subtracts a [AxisElement]<[Angle]> from another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(Angle::new(3, 1, 1.0), Angle::new(0, 2, 0.0));
/// let b = AxisElement::new(Angle::new(0, 10, 59.0), Angle::new(0, 1, 0.0));
/// let c = a - b;
/// assert_eq!(c, AxisElement::new(Angle::new(2, 50, 2.0), Angle::new(0, 1, 0.0)));
/// ```
impl Sub for AxisElement<Angle> {
type Output = AxisElement<Angle>;
fn sub(self, rhs: Self) -> Self::Output {
AxisElement {
lat: self.lat - rhs.lat,
lon: self.lon - rhs.lon,
}
}
}
/// Subtracts a scalar `S` (max precision of f64) from a [AxisElement]`<T>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(-10, 20);
/// let b = 12.0;
/// let c = a - b;
/// assert_eq!(c, AxisElement::new(-22, 8));
/// ```
impl<S, T> Sub<S> for AxisElement<T>
where
S: Copy + ToPrimitive,
T: Copy + FromPrimitive + Sub<Output = T>,
{
type Output = AxisElement<T>;
fn sub(self, rhs: S) -> Self::Output {
let rhs = S::to_f64(&rhs).expect("Failed to convert RHS to f64");
AxisElement {
lat: self.lat - T::from_f64(rhs).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
lon: self.lon - T::from_f64(rhs).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
}
}
}
/// Subtracts a [AxisElement]`<S>` from a [AxisElement]`<T>`,
/// using max precision of f64
///
/// # Returns
///
/// * [AxisElement]`<T>`
///
/// # Example
///
/// ```
/// use dted2::primitives::{AxisElement, Angle};
///
/// let a = AxisElement::new(-10, 20);
/// let b = AxisElement::new(12, 32);
/// let c = a - b;
/// assert_eq!(c, AxisElement::new(-22, -12));
/// ```
impl<S, T> Sub<AxisElement<S>> for AxisElement<T>
where
S: ToPrimitive,
T: ToPrimitive + FromPrimitive + Sub<Output = T>,
{
type Output = AxisElement<T>;
fn sub(self, rhs: AxisElement<S>) -> Self::Output {
let rhs_lat: f64 = S::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
let rhs_lon: f64 = S::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
AxisElement {
lat: T::from_f64(lat - rhs_lat).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
lon: T::from_f64(lon - rhs_lon).expect(&format!("Failed to convert f64 to {}", std::any::type_name::<T>())),
}
}
}
/// Multiplies a [AxisElement]<[Angle]> by another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Mul for AxisElement<Angle> {
type Output = AxisElement<Angle>;
fn mul(self, rhs: Self) -> Self::Output {
AxisElement {
lat: self.lat * rhs.lat,
lon: self.lon * rhs.lon,
}
}
}
/// Multiplies a [AxisElement]<[Angle]> by a [Angle]
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Mul<Angle> for AxisElement<Angle> {
type Output = AxisElement<Angle>;
fn mul(self, rhs: Angle) -> Self::Output {
AxisElement {
lat: self.lat * rhs,
lon: self.lon * rhs,
}
}
}
/// Multiplies a [AxisElement]<[Angle]> by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<M> Mul<M> for AxisElement<Angle>
where
M: Copy + ToPrimitive,
{
type Output = AxisElement<Angle>;
fn mul(self, rhs: M) -> Self::Output {
AxisElement {
lat: self.lat * rhs,
lon: self.lon * rhs,
}
}
}
/// Multiplies a [AxisElement]<[Angle]> by a [AxisElement]`<M>`
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<M> Mul<AxisElement<M>> for AxisElement<Angle>
where
M: Copy + ToPrimitive,
{
type Output = AxisElement<Angle>;
fn mul(self, rhs: AxisElement<M>) -> Self::Output {
AxisElement {
lat: self.lat * rhs.lat,
lon: self.lon * rhs.lon,
}
}
}
/// Multiplies a [AxisElement]`<T>` by a [AxisElement]`<M>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<M, T> Mul<AxisElement<M>> for AxisElement<T>
where
M: ToPrimitive + FromPrimitive,
T: ToPrimitive,
{
type Output = AxisElement<M>;
fn mul(self, rhs: AxisElement<M>) -> Self::Output {
let rhs_lat: f64 = M::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
let rhs_lon: f64 = M::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
AxisElement {
lat: M::from_f64(lat * rhs_lat).expect("Failed to convert latitude from f64"),
lon: M::from_f64(lon * rhs_lon).expect("Failed to convert longitude from f64"),
}
}
}
/// Multiplies a [AxisElement]`<T>` by a scalar `M` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<M, T> Mul<M> for AxisElement<T>
where
M: ToPrimitive + FromPrimitive,
T: ToPrimitive,
{
type Output = AxisElement<M>;
fn mul(self, rhs: M) -> Self::Output {
let rhs: f64 = M::to_f64(&rhs).expect("Failed to convert RHS to f64");
let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
AxisElement {
lat: M::from_f64(lat * rhs).expect("Failed to convert latitude from f64"),
lon: M::from_f64(lon * rhs).expect("Failed to convert longitude from f64"),
}
}
}
/// Divides a [AxisElement]<[Angle]> by another [AxisElement]<[Angle]>
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Div for AxisElement<Angle> {
type Output = AxisElement<Angle>;
fn div(self, rhs: Self) -> Self::Output {
AxisElement {
lat: self.lat / rhs.lat,
lon: self.lon / rhs.lon,
}
}
}
/// Divides a [AxisElement]<[Angle]> by a [Angle]
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl Div<Angle> for AxisElement<Angle> {
type Output = AxisElement<Angle>;
fn div(self, rhs: Angle) -> Self::Output {
AxisElement {
lat: self.lat / rhs,
lon: self.lon / rhs,
}
}
}
/// Divides a [AxisElement]<[Angle]> by a scalar `D` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<D> Div<D> for AxisElement<Angle>
where
D: Copy + ToPrimitive,
{
type Output = AxisElement<Angle>;
fn div(self, rhs: D) -> Self::Output {
AxisElement {
lat: self.lat / rhs,
lon: self.lon / rhs,
}
}
}
/// Divides a [AxisElement]<[Angle]> by a [AxisElement]`<D>`
///
/// # Returns
///
/// * [AxisElement]<[Angle]>
impl<D> Div<AxisElement<D>> for AxisElement<Angle>
where
D: Copy + ToPrimitive,
{
type Output = AxisElement<Angle>;
fn div(self, rhs: AxisElement<D>) -> Self::Output {
AxisElement {
lat: self.lat / rhs.lat,
lon: self.lon / rhs.lon,
}
}
}
/// Divides a [AxisElement]`<T>` by a [AxisElement]`<D>`
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<D, T> Div<AxisElement<D>> for AxisElement<T>
where
D: Copy + ToPrimitive + FromPrimitive,
T: Copy + ToPrimitive,
{
type Output = AxisElement<D>;
fn div(self, rhs: AxisElement<D>) -> Self::Output {
let rhs_lat: f64 = D::to_f64(&rhs.lat).expect("Failed to convert RHS lat to f64");
let rhs_lon: f64 = D::to_f64(&rhs.lon).expect("Failed to convert RHS lon to f64");
let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
AxisElement {
lat: D::from_f64(lat / rhs_lat).expect("Failed to convert latitude from f64"),
lon: D::from_f64(lon / rhs_lon).expect("Failed to convert longitude from f64"),
}
}
}
/// Divides a [AxisElement]`<T>` by a scalar `D` (max precision of f64)
///
/// # Returns
///
/// * [AxisElement]`<T>`
impl<D, T> Div<D> for AxisElement<T>
where
D: Copy + ToPrimitive + FromPrimitive,
T: Copy + ToPrimitive,
{
type Output = AxisElement<D>;
fn div(self, rhs: D) -> Self::Output {
let rhs: f64 = D::to_f64(&rhs).expect("Failed to convert RHS to f64");
let lat: f64 = T::to_f64(&self.lat).expect("Failed to convert latitude to f64");
let lon: f64 = T::to_f64(&self.lon).expect("Failed to convert longitude to f64");
AxisElement {
lat: D::from_f64(lat / rhs).expect("Failed to convert latitude from f64"),
lon: D::from_f64(lon / rhs).expect("Failed to convert longitude from f64"),
}
}
}