2020-05-14 22:03:19 +03:00
|
|
|
use std::{fmt, ops};
|
2019-02-27 03:17:37 +03:00
|
|
|
|
2020-10-06 06:26:11 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-10-10 22:24:42 +03:00
|
|
|
use crate::{trim_f64, Distance, Duration, EPSILON_DIST};
|
2020-10-06 06:26:11 +03:00
|
|
|
|
2020-10-09 07:13:08 +03:00
|
|
|
/// In meters per second. Can be negative.
|
2019-02-27 03:17:37 +03:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
|
|
|
|
pub struct Speed(f64);
|
|
|
|
|
|
|
|
impl Speed {
|
|
|
|
pub const ZERO: Speed = Speed::const_meters_per_second(0.0);
|
|
|
|
|
2020-10-09 07:13:08 +03:00
|
|
|
/// Is a speed effectively zero based on the timestep?
|
2019-02-27 03:17:37 +03:00
|
|
|
// TODO Probably better to tweak the rounding so that uselessly tiny speeds round to 0.
|
|
|
|
pub fn is_zero(self, timestep: Duration) -> bool {
|
|
|
|
self * timestep <= EPSILON_DIST
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn meters_per_second(value: f64) -> Speed {
|
|
|
|
if !value.is_finite() {
|
|
|
|
panic!("Bad Speed {}", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Speed(trim_f64(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn const_meters_per_second(value: f64) -> Speed {
|
|
|
|
Speed(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn miles_per_hour(value: f64) -> Speed {
|
|
|
|
Speed::meters_per_second(0.44704 * value)
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:09:35 +03:00
|
|
|
pub fn km_per_hour(value: f64) -> Speed {
|
|
|
|
Speed::meters_per_second(0.277778 * value)
|
|
|
|
}
|
|
|
|
|
2019-05-24 01:35:12 +03:00
|
|
|
pub fn from_dist_time(d: Distance, t: Duration) -> Speed {
|
|
|
|
Speed::meters_per_second(d.inner_meters() / t.inner_seconds())
|
|
|
|
}
|
|
|
|
|
2019-02-27 03:17:37 +03:00
|
|
|
// TODO Remove if possible.
|
|
|
|
pub fn inner_meters_per_second(self) -> f64 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn max(self, other: Speed) -> Speed {
|
|
|
|
if self >= other {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn min(self, other: Speed) -> Speed {
|
|
|
|
if self <= other {
|
|
|
|
self
|
|
|
|
} else {
|
|
|
|
other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add for Speed {
|
|
|
|
type Output = Speed;
|
|
|
|
|
|
|
|
fn add(self, other: Speed) -> Speed {
|
|
|
|
Speed::meters_per_second(self.0 + other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub for Speed {
|
|
|
|
type Output = Speed;
|
|
|
|
|
|
|
|
fn sub(self, other: Speed) -> Speed {
|
|
|
|
Speed::meters_per_second(self.0 - other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 22:23:02 +03:00
|
|
|
impl ops::Div for Speed {
|
|
|
|
type Output = f64;
|
|
|
|
|
|
|
|
fn div(self, other: Speed) -> f64 {
|
|
|
|
self.0 / other.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 03:17:37 +03:00
|
|
|
impl ops::Neg for Speed {
|
|
|
|
type Output = Speed;
|
|
|
|
|
|
|
|
fn neg(self) -> Speed {
|
|
|
|
Speed::meters_per_second(-self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul<f64> for Speed {
|
|
|
|
type Output = Speed;
|
|
|
|
|
|
|
|
fn mul(self, scalar: f64) -> Speed {
|
|
|
|
Speed::meters_per_second(self.0 * scalar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul<Speed> for f64 {
|
|
|
|
type Output = Speed;
|
|
|
|
|
|
|
|
fn mul(self, other: Speed) -> Speed {
|
|
|
|
Speed::meters_per_second(self * other.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Mul<Duration> for Speed {
|
|
|
|
type Output = Distance;
|
|
|
|
|
|
|
|
fn mul(self, other: Duration) -> Distance {
|
|
|
|
Distance::meters(self.0 * other.inner_seconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Speed {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-01-26 01:56:30 +03:00
|
|
|
write!(f, "{} mph", (self.0 * 2.23694).round())
|
2019-02-27 03:17:37 +03:00
|
|
|
}
|
|
|
|
}
|