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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-07-02 11:12:27 +02:00 committed by bstoeger
parent 4cb3db2548
commit 9c726d8d6f
7 changed files with 27 additions and 21 deletions

View file

@ -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;
}

View file

@ -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<int>(lrint(type.size.mliter * bar_to_atm(bar) / z_factor)) };
}
int find_best_gasmix_match(struct gasmix mix, const struct cylinder_table &cylinders)

View file

@ -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);

View file

@ -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:

View file

@ -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<std::string> 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

View file

@ -258,8 +258,9 @@ std::vector<volume_t> 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;
}

View file

@ -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);