abstreet/geom/src/speed.rs

120 lines
2.7 KiB
Rust
Raw Normal View History

use std::{cmp, ops};
2019-02-27 03:17:37 +03:00
use serde::{Deserialize, Serialize};
use crate::{deserialize_f64, serialize_f64, trim_f64, Distance, Duration, UnitFmt};
/// 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(
#[serde(serialize_with = "serialize_f64", deserialize_with = "deserialize_f64")] f64,
);
2019-02-27 03:17:37 +03:00
// By construction, Speed is a finite f64 with trimmed precision.
impl Eq for Speed {}
Fix compilation failures and most clippy warnings (#642) * abstutil: Fix compilation failure * map_gui: Fix compilation * traffic_signal_data: Fix compilation failure * map_model: Fix compilation failure * abstutil: Fix doctests * abstio: Fix most clippy warnings * abstutil: Fix most clippy warnings * collisions: Fix clippy warning * convert_osm: Fix clippy warnings * sim: Fix most clippy warnings * geom: Fix clippy warnings * kml: Fix clippy warnings * map_model: Fix most clippy warnings * fifteen_min: Fix clippy warnings * game: Fix many clippy warnings * Disable some noisy clippy warnings * headless: Fix clippy warnings * importer: Fix clippy warnings * map_editor: Fix clippy warnings * map_gui: Fix clippy warnings * osm_viewer: Fix clippy warnings * parking_mapper: Fix most clippy warnings * popdat: Fix clippy warnings * santa: Fix clippy warnings * sumo: Fix clippy warnings * traffic_seitan: Fix clippy warning * updater: Fix clippy warnings * widgetry: Fix clippy warnings * tests: Fix some clippy warnings * Fix compilation on stable Rust * Simplify unwrapping * Make use of `Entry` more readable * Fix formatting * Fix code that was broken in the refactoring * Apply cargo +stable fmt * Fix code that was broken in the refactoring, second try * Remove `Default` impls that are equivalent to `new` * Remove obsolete clippy wrapper * Avoid turbofish * Prefer `unwrap_or_else` over allowing `clippy::or_fun_call` * Remove redundant `into_iter` * Fix typo * Prefer `&& false` over commenting code out * Fix some clippy warnings Co-authored-by: Dustin Carlino <dabreegster@gmail.com>
2021-05-14 18:32:56 +03:00
#[allow(clippy::derive_ord_xor_partial_ord)] // false positive
impl Ord for Speed {
fn cmp(&self, other: &Speed) -> cmp::Ordering {
self.partial_cmp(other).unwrap()
}
}
2019-02-27 03:17:37 +03:00
impl Speed {
pub const ZERO: Speed = Speed::const_meters_per_second(0.0);
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)
}
pub fn km_per_hour(value: f64) -> Speed {
Speed::meters_per_second(0.277778 * value)
}
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
}
/// Describes the speed according to formatting rules.
pub fn to_string(self, fmt: &UnitFmt) -> String {
if fmt.metric {
format!("{} km/h", (self.0 * 3.6).round())
} else {
format!("{} mph", (self.0 * 2.23694).round())
}
}
2019-02-27 03:17:37 +03:00
}
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)
}
}
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())
}
}