use std::fmt;
use super::effort::{Mass, Power};
use super::ftp::Ftp;
use super::Error;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct EstimatedVo2(f64);
impl EstimatedVo2 {
pub const fn value(self) -> f64 {
self.0
}
}
impl fmt::Display for EstimatedVo2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:.1} ml/kg/min", self.0)
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for EstimatedVo2 {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_f64(self.0)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for EstimatedVo2 {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let v = <f64 as serde::Deserialize>::deserialize(deserializer)?;
if v.is_finite() && v > 0.0 {
Ok(EstimatedVo2(v))
} else {
Err(serde::de::Error::custom(
"estimated VO2 must be a positive finite value",
))
}
}
}
pub const ACSM_A: f64 = 10.8;
pub const ACSM_B: f64 = 7.0;
pub const HAWLEY_NOAKES_A: f64 = 0.01141;
pub const HAWLEY_NOAKES_B: f64 = 0.435;
pub const FTP_TO_MAP: f64 = 1.20;
pub fn estimated_vo2max(map: Power, mass: Mass) -> EstimatedVo2 {
EstimatedVo2(ACSM_A * (map.watts() / mass.kg()) + ACSM_B)
}
pub fn estimated_vo2max_hawley_noakes(map: Power, mass: Mass) -> EstimatedVo2 {
let l_min = HAWLEY_NOAKES_A * map.watts() + HAWLEY_NOAKES_B;
EstimatedVo2(l_min * 1000.0 / mass.kg())
}
pub fn estimated_vo2max_from_ftp(ftp: Ftp, mass: Mass) -> EstimatedVo2 {
let map = Power::new(FTP_TO_MAP * ftp.watts()).expect("FTP implies positive MAP");
estimated_vo2max(map, mass)
}
pub fn map_from_vo2(vo2_ml_kg_min: f64, mass: Mass) -> Result<Power, Error> {
if !vo2_ml_kg_min.is_finite() || vo2_ml_kg_min <= ACSM_B {
return Err(Error::InvalidPower);
}
let map_w = (vo2_ml_kg_min - ACSM_B) / ACSM_A * mass.kg();
Power::new(map_w)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::path::PathBuf;
fn fixtures_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/cycling")
}
#[test]
fn fixture_vo2_equations() {
let text = fs::read_to_string(fixtures_dir().join("vo2.csv")).unwrap();
for (i, line) in text.lines().enumerate() {
if i == 0 || line.trim().is_empty() {
continue;
}
let c: Vec<_> = line.split(',').collect();
let equation = c[0];
let mass = Mass::from_kg(c[1].parse().unwrap()).unwrap();
let map = Power::new(c[2].parse().unwrap()).unwrap();
let expected: f64 = c[3].parse().unwrap();
let vo2 = match equation {
"acsm" => estimated_vo2max(map, mass),
"hawley" => estimated_vo2max_hawley_noakes(map, mass),
other => panic!("unknown equation {other}"),
};
assert!(
(vo2.value() - expected).abs() < 0.05,
"row {i} ({equation}): got {} want {expected}",
vo2.value()
);
if equation == "acsm" {
let back = map_from_vo2(vo2.value(), mass).unwrap();
assert!((back.watts() - map.watts()).abs() < 0.05);
}
}
}
#[test]
fn from_ftp_uses_map_factor() {
let ftp = Ftp::new(250.0).unwrap();
let mass = Mass::from_kg(70.0).unwrap();
let from_ftp = estimated_vo2max_from_ftp(ftp, mass);
let from_map = estimated_vo2max(Power::new(300.0).unwrap(), mass);
assert!((from_ftp.value() - from_map.value()).abs() < 1e-12);
}
}