Skip to content

Commit

Permalink
[terrain] split math into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Nov 22, 2023
1 parent 06bb10d commit 76ed1b6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 40 deletions.
38 changes: 38 additions & 0 deletions terrain/src/math/elevation_angle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use num_traits::{Float, FloatConst};

/// Returns the up/down angle (in radians) from a to b.
pub fn elevation_angle<T>(start_elev_m: T, distance_m: T, end_elev_m: T, earth_radius: T) -> T
where
T: Float + FloatConst,
{
let a = distance_m;
let b = start_elev_m + earth_radius;
let c = end_elev_m + earth_radius;
let inner = {
let inner = (a.powi(2) + b.powi(2) - c.powi(2)) / ((T::one() + T::one()) * a * b);
if inner < -T::one() {
-T::one()
} else if inner > T::one() {
T::one()
} else {
inner
}
};
inner.acos() - T::FRAC_PI_2()
}

#[cfg(test)]
mod tests {
use super::elevation_angle;
use crate::constants::MEAN_EARTH_RADIUS;
use approx::assert_relative_eq;

#[test]
fn test_elevation_angle() {
assert_relative_eq!(
0.100_167_342_359_641_42,
elevation_angle(1.0, 1.0, 1.1, MEAN_EARTH_RADIUS),
epsilon = f64::EPSILON
);
}
}
42 changes: 2 additions & 40 deletions terrain/src/math.rs → terrain/src/math/haversine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use crate::constants::MEAN_EARTH_RADIUS;
use geo::{CoordFloat, Point};
use num_traits::{AsPrimitive, Float, FloatConst, FromPrimitive};
use num_traits::{AsPrimitive, FromPrimitive};

pub struct HaversineIter<T: CoordFloat = f32> {
params: HaversineParams<T>,
Expand Down Expand Up @@ -147,38 +147,9 @@ where
}
}

/// Returns the up/down angle (in radians) from a to b.
pub fn elevation_angle<T>(start_elev_m: T, distance_m: T, end_elev_m: T, earth_radius: T) -> T
where
T: Float + FloatConst,
{
let a = distance_m;
let b = start_elev_m + earth_radius;
let c = end_elev_m + earth_radius;
let inner = {
let inner = (a.powi(2) + b.powi(2) - c.powi(2)) / ((T::one() + T::one()) * a * b);
if inner < -T::one() {
-T::one()
} else if inner > T::one() {
T::one()
} else {
inner
}
};
inner.acos() - T::FRAC_PI_2()
}

pub fn linspace<T>(y_start: T, y_end: T, n: usize) -> impl Iterator<Item = T>
where
T: Float + FromPrimitive,
{
let dy = (y_end - y_start) / T::from(n - 1).unwrap();
(0..n).map(move |x| y_start + T::from(x).unwrap() * dy)
}

#[cfg(test)]
mod tests {
use super::{elevation_angle, HaversineIter};
use super::HaversineIter;
use approx::assert_relative_eq;
use geo::point;

Expand All @@ -205,13 +176,4 @@ mod tests {
];
assert_eq!(points, expected);
}

#[test]
fn test_elevation_angle() {
assert_relative_eq!(
0.100_167_342_359_641_42,
elevation_angle(1.0, 1.0, 1.1, super::MEAN_EARTH_RADIUS),
epsilon = f64::EPSILON
);
}
}
9 changes: 9 additions & 0 deletions terrain/src/math/linspace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use num_traits::{Float, FromPrimitive};

pub fn linspace<T>(y_start: T, y_end: T, n: usize) -> impl Iterator<Item = T>
where
T: Float + FromPrimitive,
{
let dy = (y_end - y_start) / T::from(n - 1).unwrap();
(0..n).map(move |x| y_start + T::from(x).unwrap() * dy)
}
5 changes: 5 additions & 0 deletions terrain/src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod elevation_angle;
mod haversine;
mod linspace;

pub(crate) use {elevation_angle::elevation_angle, haversine::HaversineIter, linspace::linspace};

0 comments on commit 76ed1b6

Please sign in to comment.