v0.3.2
This commit is contained in:
parent
788c68a1d8
commit
35a380b808
8 changed files with 954 additions and 46 deletions
|
|
@ -1,7 +1,7 @@
|
|||
//! Size-independent Cairo drawing of the synthetic bedside monitor.
|
||||
use gtk::cairo::{Context, FontSlant, FontWeight, LineCap, LineJoin};
|
||||
|
||||
use crate::model::{mean_arterial_pressure, EcgRhythm, Parameters, PulseConduction};
|
||||
use crate::model::{mean_arterial_pressure, EcgRhythm, Parameters, PulseConduction, Spo2Quality};
|
||||
use crate::simulation::{Sample, Simulator};
|
||||
|
||||
type Color = (f64, f64, f64);
|
||||
|
|
@ -43,8 +43,18 @@ pub fn draw(cr: &Context, width: i32, height: i32, p: &Parameters, sim: &Simulat
|
|||
let trace_right = (split_x - pad).max(trace_left + 40.0);
|
||||
let text_scale = (w.min(h * 1.7) / 850.0).clamp(0.65, 1.5);
|
||||
|
||||
text(cr, "PATIENT MONITOR", pad + 5.0, header * 0.58, 18.0 * text_scale,
|
||||
LIGHT_GREY, true);
|
||||
let patient_title = if p.patient_name.trim().is_empty() {
|
||||
"PATIENT MONITOR"
|
||||
} else {
|
||||
p.patient_name.trim()
|
||||
};
|
||||
let title_max_width = if w >= 850.0 {
|
||||
split_x - pad * 2.0 - 145.0 * text_scale
|
||||
} else {
|
||||
split_x - pad * 2.0
|
||||
};
|
||||
fitted_text(cr, patient_title, pad + 5.0, header * 0.58,
|
||||
18.0 * text_scale, title_max_width.max(25.0), LIGHT_GREY);
|
||||
if w >= 850.0 {
|
||||
text(cr, "SIMULATION", split_x - 128.0 * text_scale, header * 0.58,
|
||||
11.0 * text_scale, MUTED, false);
|
||||
|
|
@ -84,6 +94,24 @@ pub fn draw(cr: &Context, width: i32, height: i32, p: &Parameters, sim: &Simulat
|
|||
}
|
||||
}
|
||||
|
||||
fn pleth_trace_enabled(p: &Parameters) -> bool {
|
||||
p.spo2_sensor_attached && p.spo2_quality != Spo2Quality::None
|
||||
}
|
||||
|
||||
fn spo2_vitals(p: &Parameters, pulse_rate: Option<i32>) -> (String, String, String) {
|
||||
if !p.spo2_sensor_attached {
|
||||
return ("SpO₂".to_owned(), "NO SENSOR".to_owned(), "SpO₂ -- PR -- bpm".to_owned());
|
||||
}
|
||||
if p.spo2_quality == Spo2Quality::None {
|
||||
return ("SpO₂".to_owned(), "--".to_owned(), "NO SIGNAL PR -- bpm".to_owned());
|
||||
}
|
||||
(
|
||||
format!("SpO₂ {}", p.spo2_quality.stars()),
|
||||
if pulse_rate.is_some() { p.spo2.to_string() } else { "--".to_owned() },
|
||||
format!("% PR {} bpm", pulse_rate.map_or("--".to_owned(), |pr| pr.to_string())),
|
||||
)
|
||||
}
|
||||
|
||||
fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
||||
top: f64, row_h: f64, width: f64, p: &Parameters, sim: &Simulator) {
|
||||
let label_size = (row_h * 0.135).clamp(11.0, 19.0);
|
||||
|
|
@ -95,7 +123,7 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
|||
let pulse_rate = if has_capture { sim.pulse_rate() } else { None };
|
||||
let (label, value, info) = match ch {
|
||||
Channel::Ecg => (
|
||||
"HR",
|
||||
"HR".to_owned(),
|
||||
if !p.ecg_rhythm.has_qrs() || p.heart_rate <= 0 { "--".to_owned() }
|
||||
else { p.heart_rate.to_string() },
|
||||
format!("bpm · {}", match p.ecg_rhythm {
|
||||
|
|
@ -106,13 +134,9 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
|||
EcgRhythm::Asystole => "Asystole",
|
||||
}),
|
||||
),
|
||||
Channel::Pleth => (
|
||||
"SpO₂",
|
||||
if pulse_rate.is_some() { p.spo2.to_string() } else { "--".to_owned() },
|
||||
format!("% PR {} bpm", pulse_rate.map_or("--".to_owned(), |pr| pr.to_string())),
|
||||
),
|
||||
Channel::Pleth => spo2_vitals(p, pulse_rate),
|
||||
Channel::Art => (
|
||||
"ART",
|
||||
"ART".to_owned(),
|
||||
if pulse_rate.is_some() {
|
||||
format!("{}/{}", p.art_systolic, p.art_diastolic)
|
||||
} else { "--/--".to_owned() },
|
||||
|
|
@ -126,13 +150,20 @@ fn draw_vitals(cr: &Context, ch: Channel, color: Color, x: f64,
|
|||
}
|
||||
} else { "MAP -- mmHg".to_owned() },
|
||||
),
|
||||
Channel::Capno => ("EtCO₂", p.etco2.to_string(),
|
||||
Channel::Capno => ("EtCO₂".to_owned(), p.etco2.to_string(),
|
||||
format!("mmHg RR {} /min", p.respiratory_rate)),
|
||||
};
|
||||
text(cr, label, x, top + row_h * 0.24, label_size, color, false);
|
||||
// Reduce wide BP values so they fit within the sidebar.
|
||||
let value_size = if matches!(ch, Channel::Art) { number_size * 0.78 } else { number_size };
|
||||
text(cr, &value, x, top + row_h * 0.64, value_size, color, true);
|
||||
text(cr, &label, x, top + row_h * 0.24, label_size, color, false);
|
||||
// Reduce wide readings so they fit within the sidebar.
|
||||
let value_size = if value == "NO SENSOR" {
|
||||
number_size.min(0.19 * width).min(26.0)
|
||||
} else if matches!(ch, Channel::Art) {
|
||||
number_size * 0.78
|
||||
} else {
|
||||
number_size
|
||||
};
|
||||
fitted_text(cr, &value, x, top + row_h * 0.64, value_size,
|
||||
width.max(20.0), color);
|
||||
text(cr, &info, x, top + row_h * 0.87, sub_size * 0.85, color, false);
|
||||
}
|
||||
|
||||
|
|
@ -175,6 +206,13 @@ fn draw_trace(cr: &Context, channel: Channel, color: Color,
|
|||
right, top + trace_height * 0.5, GRID, 0.7);
|
||||
}
|
||||
|
||||
// Disconnected or absent pleth signal: no curve, including historical pulses.
|
||||
if matches!(channel, Channel::Pleth) && !pleth_trace_enabled(p)
|
||||
{
|
||||
let _ = cr.restore();
|
||||
return;
|
||||
}
|
||||
|
||||
set_color(cr, color);
|
||||
cr.set_line_width((trace_height * 0.016).clamp(1.2, 2.4));
|
||||
cr.set_line_cap(LineCap::Round);
|
||||
|
|
@ -194,13 +232,68 @@ fn draw_trace(cr: &Context, channel: Channel, color: Color,
|
|||
fn normalized(channel: Channel, s: &Sample, p: &Parameters) -> f64 {
|
||||
match channel {
|
||||
Channel::Ecg => (0.40 + s.ecg * 0.38).clamp(0.02, 0.98),
|
||||
Channel::Pleth => (0.10 + s.pleth * 0.78).clamp(0.02, 0.98),
|
||||
Channel::Pleth => (0.10 + pleth_visual_amplitude(s.pleth, s.time, p.spo2_quality) * 0.78)
|
||||
.clamp(0.02, 0.98),
|
||||
Channel::Art => (s.art / 200.0).clamp(0.0, 1.0),
|
||||
Channel::Capno => (0.08 + (s.co2 / f64::from(p.etco2.max(1))) * 0.80)
|
||||
.clamp(0.02, 0.98),
|
||||
}
|
||||
}
|
||||
|
||||
/// Signal degradation is presentation-only: no quality setting can invent
|
||||
/// pulses or change the ECG/ART generator. The noise is smoothly interpolated
|
||||
/// and reproducible, so redrawing a sample does not make it flicker.
|
||||
fn pleth_visual_amplitude(pleth: f64, time: f64, quality: Spo2Quality) -> f64 {
|
||||
let scale = quality.amplitude_scale();
|
||||
if scale == 0.0 || pleth <= 0.0 { return 0.0; }
|
||||
let distortion = if quality == Spo2Quality::Poor {
|
||||
(1.0 + 0.55 * smooth_noise(time * 7.0)
|
||||
+ 0.20 * smooth_noise(time * 23.0 + 4.0)).clamp(0.15, 1.75)
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
(pleth * scale * distortion).clamp(0.0, 1.0)
|
||||
}
|
||||
|
||||
fn smooth_noise(position: f64) -> f64 {
|
||||
let index = position.floor() as i64;
|
||||
let fraction = position - index as f64;
|
||||
let smooth = fraction * fraction * (3.0 - 2.0 * fraction);
|
||||
let a = noise_value(index);
|
||||
let b = noise_value(index.wrapping_add(1));
|
||||
a + (b - a) * smooth
|
||||
}
|
||||
|
||||
fn noise_value(index: i64) -> f64 {
|
||||
let mut x = (index as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
|
||||
x ^= x >> 30;
|
||||
x = x.wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
||||
x ^= x >> 27;
|
||||
x = x.wrapping_mul(0x94D0_49BB_1331_11EB);
|
||||
x ^= x >> 31;
|
||||
(x as f64 / u64::MAX as f64) * 2.0 - 1.0
|
||||
}
|
||||
|
||||
/// Fit a name or numeric/status reading horizontally without changing the
|
||||
/// other monitor dimensions. For exceptionally long values, clip safely.
|
||||
fn fitted_text(cr: &Context, value: &str, x: f64, baseline: f64,
|
||||
nominal_size: f64, available_width: f64, color: Color) {
|
||||
cr.select_font_face("Sans", FontSlant::Normal, FontWeight::Bold);
|
||||
cr.set_font_size(nominal_size);
|
||||
let actual_width = cr.text_extents(value).map(|ext| ext.x_advance()).unwrap_or(0.0);
|
||||
let fitted_size = if actual_width > available_width && actual_width > 0.0 {
|
||||
(nominal_size * available_width / actual_width).max(8.0)
|
||||
} else {
|
||||
nominal_size
|
||||
};
|
||||
let _ = cr.save();
|
||||
cr.rectangle(x, baseline - fitted_size * 1.25,
|
||||
available_width.max(1.0), fitted_size * 1.55);
|
||||
cr.clip();
|
||||
text(cr, value, x, baseline, fitted_size, color, true);
|
||||
let _ = cr.restore();
|
||||
}
|
||||
|
||||
fn set_color(cr: &Context, c: Color) {
|
||||
cr.set_source_rgb(c.0, c.1, c.2);
|
||||
}
|
||||
|
|
@ -242,6 +335,53 @@ mod tests {
|
|||
assert!(visible_channels(&p).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pleth_degradation_preserves_capture_timing_and_suppresses_false_pulses() {
|
||||
let pulse = 0.6;
|
||||
let t = 1.234;
|
||||
assert_eq!(pleth_visual_amplitude(pulse, t, Spo2Quality::None), 0.0);
|
||||
assert_eq!(pleth_visual_amplitude(0.0, t, Spo2Quality::Poor), 0.0);
|
||||
assert!(pleth_visual_amplitude(pulse, t, Spo2Quality::Poor)
|
||||
< pleth_visual_amplitude(pulse, t, Spo2Quality::Adequate));
|
||||
assert!(pleth_visual_amplitude(pulse, t, Spo2Quality::Adequate)
|
||||
< pleth_visual_amplitude(pulse, t, Spo2Quality::Good));
|
||||
assert_eq!(pleth_visual_amplitude(pulse, t, Spo2Quality::Good), pulse);
|
||||
assert_eq!(pleth_visual_amplitude(pulse, t, Spo2Quality::Poor),
|
||||
pleth_visual_amplitude(pulse, t, Spo2Quality::Poor));
|
||||
assert_ne!(pleth_visual_amplitude(pulse, t, Spo2Quality::Poor),
|
||||
pleth_visual_amplitude(pulse, t + 0.5, Spo2Quality::Poor));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnected_or_no_quality_means_no_pleth_or_spo2_reading() {
|
||||
let mut p = Parameters::default();
|
||||
assert!(pleth_trace_enabled(&p));
|
||||
p.spo2_sensor_attached = false;
|
||||
assert!(!pleth_trace_enabled(&p));
|
||||
assert_eq!(spo2_vitals(&p, Some(75)).1, "NO SENSOR");
|
||||
assert!(spo2_vitals(&p, Some(75)).2.contains("PR --"));
|
||||
p.spo2_sensor_attached = true;
|
||||
p.spo2_quality = Spo2Quality::None;
|
||||
assert!(!pleth_trace_enabled(&p));
|
||||
assert_eq!(spo2_vitals(&p, Some(75)).1, "--");
|
||||
assert!(spo2_vitals(&p, Some(75)).2.contains("NO SIGNAL"));
|
||||
assert!(spo2_vitals(&p, Some(75)).2.contains("PR --"));
|
||||
assert!(p.show_spo2); // no readings, but visible row and status
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn poor_and_good_quality_keep_derived_pr_and_quality_indicators() {
|
||||
let mut p = Parameters::default();
|
||||
p.spo2_quality = Spo2Quality::Poor;
|
||||
assert_eq!(spo2_vitals(&p, Some(75)).0, "SpO₂ ★");
|
||||
assert_eq!(spo2_vitals(&p, Some(75)).1, "98");
|
||||
assert!(spo2_vitals(&p, Some(75)).2.contains("PR 75 bpm"));
|
||||
p.spo2_quality = Spo2Quality::Good;
|
||||
assert_eq!(spo2_vitals(&p, Some(75)).0, "SpO₂ ★★★");
|
||||
assert_eq!(spo2_vitals(&p, None).1, "--");
|
||||
assert!(spo2_vitals(&p, None).2.contains("PR --"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arterial_axis_is_absolute_and_fixed_to_200_mmhg() {
|
||||
let p = Parameters::default();
|
||||
|
|
|
|||
Loading…
Reference in a new issue