use crate::quantized::Quantized;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct QuantizedCoordinates {
lat_ddeg: Quantized,
long_ddeg: Quantized,
alt_km: Quantized,
}
impl QuantizedCoordinates {
pub fn from_decimal_degrees(lat: f64, long: f64, alt_km: f64) -> Self {
Self {
lat_ddeg: Quantized::auto_scaled(lat),
long_ddeg: Quantized::auto_scaled(long),
alt_km: Quantized::auto_scaled(alt_km),
}
}
#[cfg(test)]
pub fn new(
lat_ddeg: f64,
lat_exponent: i8,
long_ddeg: f64,
long_exponent: i8,
alt_km: f64,
alt_exponent: i8,
) -> Self {
Self {
lat_ddeg: Quantized::new(lat_ddeg, lat_exponent),
long_ddeg: Quantized::new(long_ddeg, long_exponent),
alt_km: Quantized::new(alt_km, alt_exponent),
}
}
pub(crate) fn from_quantized(
lat_ddeg: Quantized,
long_ddeg: Quantized,
alt_km: Quantized,
) -> Self {
Self {
lat_ddeg,
long_ddeg,
alt_km,
}
}
pub fn latitude_ddeg(&self) -> f64 {
self.lat_ddeg.real_value()
}
pub fn longitude_ddeg(&self) -> f64 {
self.long_ddeg.real_value()
}
pub fn altitude_km(&self) -> f64 {
self.alt_km.real_value()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn quantized_coords() {
let coords = QuantizedCoordinates::new(1.0, 1, 2.0, 1, 3.0, 1);
assert_eq!(coords.latitude_ddeg(), 1.0);
assert_eq!(coords.longitude_ddeg(), 2.0);
assert_eq!(coords.altitude_km(), 3.0);
let coords = QuantizedCoordinates::new(1.5, 1, 2.0, 1, 3.12, 2);
assert_eq!(coords.latitude_ddeg(), 1.5);
assert_eq!(coords.longitude_ddeg(), 2.0);
assert_eq!(coords.altitude_km(), 3.12);
}
}