From 9c726d8d6f4d813b307627712d630f0b0ee70638 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 2 Jul 2024 11:12:27 +0200 Subject: [PATCH] core: move gas_volume() to cylinder_t Feels natural in a C++ code base. The commit is somewhat complex, because it also changes the return type to volume_t. The units system really needs some work. :( Signed-off-by: Berthold Stoeger --- core/divelist.cpp | 3 ++- core/equipment.cpp | 6 +++--- core/equipment.h | 3 ++- core/libdivecomputer.cpp | 4 ++-- core/profile.cpp | 10 +++++++--- core/statistics.cpp | 3 ++- qt-models/cylindermodel.cpp | 19 +++++++++---------- 7 files changed, 27 insertions(+), 21 deletions(-) diff --git a/core/divelist.cpp b/core/divelist.cpp index 5caa81bc0..c77f3937e 100644 --- a/core/divelist.cpp +++ b/core/divelist.cpp @@ -384,7 +384,8 @@ static double calculate_airuse(const struct dive &dive) continue; } - airuse += gas_volume(&cyl, start) - gas_volume(&cyl, end); + // TODO: implement subtraction for units.h types + airuse += cyl.gas_volume(start).mliter - cyl.gas_volume(end).mliter; } return airuse / 1000.0; } diff --git a/core/equipment.cpp b/core/equipment.cpp index 75b008ae9..f553acb39 100644 --- a/core/equipment.cpp +++ b/core/equipment.cpp @@ -198,11 +198,11 @@ const char *gasname(struct gasmix gasmix) return gas; } -int gas_volume(const cylinder_t *cyl, pressure_t p) +volume_t cylinder_t::gas_volume(pressure_t p) const { double bar = p.mbar / 1000.0; - double z_factor = gas_compressibility_factor(cyl->gasmix, bar); - return lrint(cyl->type.size.mliter * bar_to_atm(bar) / z_factor); + double z_factor = gas_compressibility_factor(gasmix, bar); + return volume_t { static_cast(lrint(type.size.mliter * bar_to_atm(bar) / z_factor)) }; } int find_best_gasmix_match(struct gasmix mix, const struct cylinder_table &cylinders) diff --git a/core/equipment.h b/core/equipment.h index 028a7131b..bf3c1339c 100644 --- a/core/equipment.h +++ b/core/equipment.h @@ -35,6 +35,8 @@ struct cylinder_t cylinder_t(); ~cylinder_t(); + + volume_t gas_volume(pressure_t p) const; /* Volume of a cylinder at pressure 'p' */ }; /* Table of cylinders. @@ -78,7 +80,6 @@ extern enum cylinderuse cylinderuse_from_text(const char *text); extern void copy_cylinder_types(const struct dive *s, struct dive *d); extern void remove_cylinder(struct dive *dive, int idx); extern void reset_cylinders(struct dive *dive, bool track_gas); -extern int gas_volume(const cylinder_t *cyl, pressure_t p); /* Volume in mliter of a cylinder at pressure 'p' */ extern int find_best_gasmix_match(struct gasmix mix, const struct cylinder_table &cylinders); extern void fill_default_cylinder(const struct dive *dive, cylinder_t *cyl); /* dive is needed to fill out MOD, which depends on salinity. */ extern cylinder_t default_cylinder(const struct dive *d); diff --git a/core/libdivecomputer.cpp b/core/libdivecomputer.cpp index 408923b3f..e1e865d55 100644 --- a/core/libdivecomputer.cpp +++ b/core/libdivecomputer.cpp @@ -246,8 +246,8 @@ static dc_status_t parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_ cyl.type.workingpressure.mbar = lrint( cyl.type.workingpressure.mbar * 206.843 / 206.7 ); char name_buffer[17]; - int rounded_size = lrint(ml_to_cuft(gas_volume(&cyl, - cyl.type.workingpressure))); + int rounded_size = lrint(ml_to_cuft(cyl.gas_volume( + cyl.type.workingpressure).mliter)); rounded_size = (int)((rounded_size + 5) / 10) * 10; switch (cyl.type.workingpressure.mbar) { case 206843: diff --git a/core/profile.cpp b/core/profile.cpp index eae5f718f..f065056d4 100644 --- a/core/profile.cpp +++ b/core/profile.cpp @@ -128,7 +128,8 @@ static int get_local_sac(struct plot_info &pi, int idx1, int idx2, struct dive * cyl = dive->get_cylinder(index); - airuse = gas_volume(cyl, a) - gas_volume(cyl, b); + // TODO: Implement addition/subtraction on units.h types + airuse = cyl->gas_volume(a).mliter - cyl->gas_volume(b).mliter; /* milliliters per minute */ return lrint(airuse / atm * 60 / duration); @@ -480,7 +481,8 @@ static int sac_between(const struct dive *dive, const struct plot_info &pi, int a.mbar = get_plot_pressure(pi, first, i); b.mbar = get_plot_pressure(pi, last, i); const cylinder_t *cyl = dive->get_cylinder(i); - int cyluse = gas_volume(cyl, a) - gas_volume(cyl, b); + // TODO: Implement addition/subtraction on units.h types + int cyluse = cyl->gas_volume(a).mliter - cyl->gas_volume(b).mliter; if (cyluse > 0) airuse += cyluse; } @@ -1504,7 +1506,9 @@ std::vector compare_samples(const struct dive *d, const struct plot const cylinder_t *cyl = d->get_cylinder(cylinder_index); - volumes_used[cylinder_index] += gas_volume(cyl, (pressure_t){ last_pressures[cylinder_index] }) - gas_volume(cyl, (pressure_t){ next_pressure }); + // TODO: Implement addition/subtraction on units.h types + volumes_used[cylinder_index] += cyl->gas_volume((pressure_t){ last_pressures[cylinder_index] }).mliter - + cyl->gas_volume((pressure_t){ next_pressure }).mliter; } // check if the gas in this cylinder is being used diff --git a/core/statistics.cpp b/core/statistics.cpp index 3b7569707..fbff6a39b 100644 --- a/core/statistics.cpp +++ b/core/statistics.cpp @@ -258,8 +258,9 @@ std::vector get_gas_used(struct dive *dive) start = cyl.start.mbar ? cyl.start : cyl.sample_start; end = cyl.end.mbar ? cyl.end : cyl.sample_end; + // TODO: Implement addition/subtraction on units.h types if (end.mbar && start.mbar > end.mbar) - gases[idx].mliter = gas_volume(&cyl, start) - gas_volume(&cyl, end); + gases[idx].mliter = cyl.gas_volume(start).mliter - cyl.gas_volume(end).mliter; else gases[idx].mliter = 0; } diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp index 46e84c018..c372e854a 100644 --- a/qt-models/cylindermodel.cpp +++ b/qt-models/cylindermodel.cpp @@ -64,13 +64,13 @@ static QString get_cylinder_string(const cylinder_t *cyl) return QString("%L1").arg(value, 0, 'f', decimals) + unit; } -static QString gas_volume_string(int ml, const char *tail) +static QString gas_volume_string(volume_t volume, const char *tail) { double vol; const char *unit; int decimals; - vol = get_volume_units(ml, NULL, &unit); + vol = get_volume_units(volume.mliter, NULL, &unit); decimals = (vol > 20.0) ? 0 : (vol > 2.0) ? 1 : 2; return QString("%L1 %2 %3").arg(vol, 0, 'f', decimals).arg(unit).arg(tail); @@ -83,13 +83,12 @@ static QVariant gas_usage_tooltip(const cylinder_t *cyl) pressure_t startp = cyl->start.mbar ? cyl->start : cyl->sample_start; pressure_t endp = cyl->end.mbar ? cyl->end : cyl->sample_end; - int start, end, used; + volume_t start = cyl->gas_volume(startp); + volume_t end = cyl->gas_volume(endp); + // TOOO: implement comparison and subtraction on units.h types. + volume_t used = (end.mliter && start.mliter > end.mliter) ? volume_t { start.mliter - end.mliter } : volume_t(); - start = gas_volume(cyl, startp); - end = gas_volume(cyl, endp); - used = (end && start > end) ? start - end : 0; - - if (!used) + if (!used.mliter) return gas_wp_tooltip(cyl); return gas_volume_string(used, "(") + @@ -99,10 +98,10 @@ static QVariant gas_usage_tooltip(const cylinder_t *cyl) static QVariant gas_volume_tooltip(const cylinder_t *cyl, pressure_t p) { - int vol = gas_volume(cyl, p); + volume_t vol = cyl->gas_volume(p); double Z; - if (!vol) + if (!vol.mliter) return QVariant(); Z = gas_compressibility_factor(cyl->gasmix, p.mbar / 1000.0);