From 5a79cc45cd8978927d2a857483ee78d171bf97fe Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 18 Aug 2025 09:33:33 +0000 Subject: [PATCH] feat: Refactor Patient and implement mean-reverting simulation - Broke out the Patient struct and its related functions (initializePatient, updatePatient) into their own files (Patient.h, Patient.cpp) for better organization. - Implemented the initial UpdateState logic in the updatePatient function using a mean-reverting model. This ensures that vital signs fluctuate realistically around a healthy baseline. - Updated CMakeLists.txt to include the new Patient.cpp file in the build. --- CMakeLists.txt | 2 +- include/MedicalLib/MedicalLib.h | 38 +-------------------- include/MedicalLib/Patient.h | 39 ++++++++++++++++++++++ src/MedicalLib.cpp | 19 ----------- src/Patient.cpp | 59 +++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 57 deletions(-) create mode 100644 include/MedicalLib/Patient.h create mode 100644 src/Patient.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d8e267..fdc4907 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.10) project(MedicalLib) # Add the library -add_library(MedicalLib SHARED src/MedicalLib.cpp) +add_library(MedicalLib SHARED src/MedicalLib.cpp src/Patient.cpp) # Define MEDICAL_LIB_EXPORT, so that __declspec(dllexport) is used target_compile_definitions(MedicalLib PRIVATE MEDICAL_LIB_EXPORT) diff --git a/include/MedicalLib/MedicalLib.h b/include/MedicalLib/MedicalLib.h index 3ade42c..2a74d21 100644 --- a/include/MedicalLib/MedicalLib.h +++ b/include/MedicalLib/MedicalLib.h @@ -1,28 +1,6 @@ #pragma once -/** - * @brief Holds all the vital signs and other medical information for a patient. - */ -struct Patient { - int patientId; - double bloodPressureSystolic; - double bloodPressureDiastolic; - double heartRate; - double respirationRate; - double bodyTemperature; - double oxygenSaturation; -}; - -// Define MEDICAL_LIB_EXPORT for exporting symbols from the DLL -#if defined(_WIN32) - #if defined(MEDICAL_LIB_EXPORT) - #define MEDICAL_LIB_API __declspec(dllexport) - #else - #define MEDICAL_LIB_API __declspec(dllimport) - #endif -#else - #define MEDICAL_LIB_API -#endif +#include "MedicalLib/Patient.h" /** * @brief Calculates the Body Mass Index (BMI). @@ -31,17 +9,3 @@ struct Patient { * @return The calculated BMI. */ MEDICAL_LIB_API double calculateBMI(double weight_kg, double height_m); - -/** - * @brief Initializes a new patient with baseline vital signs. - * @param patientId The ID for the new patient. - * @return A Patient struct with default healthy values. - */ -MEDICAL_LIB_API Patient initializePatient(int patientId); - -/** - * @brief Updates the patient's vital signs based on the time elapsed. - * @param patient The patient to update. - * @param deltaTime_s The time elapsed in seconds. - */ -MEDICAL_LIB_API void updatePatient(Patient& patient, double deltaTime_s); diff --git a/include/MedicalLib/Patient.h b/include/MedicalLib/Patient.h new file mode 100644 index 0000000..e86d3b0 --- /dev/null +++ b/include/MedicalLib/Patient.h @@ -0,0 +1,39 @@ +#pragma once + +/** + * @brief Holds all the vital signs and other medical information for a patient. + */ +struct Patient { + int patientId; + double bloodPressureSystolic; + double bloodPressureDiastolic; + double heartRate; + double respirationRate; + double bodyTemperature; + double oxygenSaturation; +}; + +// Define MEDICAL_LIB_EXPORT for exporting symbols from the DLL +#if defined(_WIN32) + #if defined(MEDICAL_LIB_EXPORT) + #define MEDICAL_LIB_API __declspec(dllexport) + #else + #define MEDICAL_LIB_API __declspec(dllimport) + #endif +#else + #define MEDICAL_LIB_API +#endif + +/** + * @brief Initializes a new patient with baseline vital signs. + * @param patientId The ID for the new patient. + * @return A Patient struct with default healthy values. + */ +MEDICAL_LIB_API Patient initializePatient(int patientId); + +/** + * @brief Updates the patient's vital signs based on the time elapsed. + * @param patient The patient to update. + * @param deltaTime_s The time elapsed in seconds. + */ +MEDICAL_LIB_API void updatePatient(Patient& patient, double deltaTime_s); diff --git a/src/MedicalLib.cpp b/src/MedicalLib.cpp index b84fb7a..8adf398 100644 --- a/src/MedicalLib.cpp +++ b/src/MedicalLib.cpp @@ -10,22 +10,3 @@ double calculateBMI(double weight_kg, double height_m) { } return weight_kg / (height_m * height_m); } - -Patient initializePatient(int patientId) { - Patient patient; - patient.patientId = patientId; - patient.bloodPressureSystolic = 120.0; - patient.bloodPressureDiastolic = 80.0; - patient.heartRate = 75.0; - patient.respirationRate = 16.0; - patient.bodyTemperature = 37.0; - patient.oxygenSaturation = 98.0; - return patient; -} - -void updatePatient(Patient& patient, double deltaTime_s) { - // For this initial implementation, we will not change the patient's vitals. - // This function serves as a placeholder for future, more complex simulation logic. - (void)patient; - (void)deltaTime_s; -} diff --git a/src/Patient.cpp b/src/Patient.cpp new file mode 100644 index 0000000..95d0ace --- /dev/null +++ b/src/Patient.cpp @@ -0,0 +1,59 @@ +#include "MedicalLib/Patient.h" +#include +#include // For std::clamp + +// Helper function to generate random fluctuations +double getFluctuation(double stddev) { + static std::random_device rd; + static std::mt19937 gen(rd()); + std::normal_distribution<> d(0, stddev); + return d(gen); +} + +Patient initializePatient(int patientId) { + Patient patient; + patient.patientId = patientId; + patient.bloodPressureSystolic = 120.0; + patient.bloodPressureDiastolic = 80.0; + patient.heartRate = 75.0; + patient.respirationRate = 16.0; + patient.bodyTemperature = 37.0; + patient.oxygenSaturation = 98.0; + return patient; +} + +void updatePatient(Patient& patient, double deltaTime_s) { + // Baseline healthy values + const double baseline_hr = 75.0; + const double baseline_bp_systolic = 120.0; + const double baseline_bp_diastolic = 80.0; + const double baseline_rr = 16.0; + const double baseline_temp = 37.0; + const double baseline_spo2 = 98.0; + + // Reversion speed (how quickly vitals return to baseline) + const double theta = 0.1; + + // Standard deviations for random fluctuations per second + const double hr_stddev = 0.1; + const double bp_stddev = 0.1; + const double rr_stddev = 0.05; + const double temp_stddev = 0.01; + const double spo2_stddev = 0.02; + + // Update vitals using a mean-reverting model + patient.heartRate += theta * (baseline_hr - patient.heartRate) * deltaTime_s + getFluctuation(hr_stddev * deltaTime_s); + patient.bloodPressureSystolic += theta * (baseline_bp_systolic - patient.bloodPressureSystolic) * deltaTime_s + getFluctuation(bp_stddev * deltaTime_s); + patient.bloodPressureDiastolic += theta * (baseline_bp_diastolic - patient.bloodPressureDiastolic) * deltaTime_s + getFluctuation(bp_stddev * deltaTime_s); + patient.respirationRate += theta * (baseline_rr - patient.respirationRate) * deltaTime_s + getFluctuation(rr_stddev * deltaTime_s); + patient.bodyTemperature += theta * (baseline_temp - patient.bodyTemperature) * deltaTime_s + getFluctuation(temp_stddev * deltaTime_s); + patient.oxygenSaturation += theta * (baseline_spo2 - patient.oxygenSaturation) * deltaTime_s + getFluctuation(spo2_stddev * deltaTime_s); + + // Clamp values to within healthy physiological ranges + patient.heartRate = std::clamp(patient.heartRate, 60.0, 100.0); + patient.bloodPressureSystolic = std::clamp(patient.bloodPressureSystolic, 90.0, 120.0); + patient.bloodPressureDiastolic = std::clamp(patient.bloodPressureDiastolic, 60.0, 80.0); + patient.respirationRate = std::clamp(patient.respirationRate, 12.0, 20.0); + patient.bodyTemperature = std::clamp(patient.bodyTemperature, 36.5, 37.3); + patient.oxygenSaturation = std::clamp(patient.oxygenSaturation, 96.0, 100.0); +}