core: move best_o2() and best_he() to struct dive

Feels natural in a C++ code base.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-30 21:38:32 +02:00 committed by bstoeger
parent 6e349793d1
commit 28814829e0
3 changed files with 13 additions and 14 deletions

View file

@ -2342,12 +2342,12 @@ std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number)
}
//Calculate O2 in best mix
fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planner)
fraction_t dive::best_o2(depth_t depth, bool in_planner) const
{
fraction_t fo2;
int po2 = in_planner ? prefs.bottompo2 : (int)(prefs.modpO2 * 1000.0);
fo2.permille = (po2 * 100 / dive->depth_to_mbar(depth.mm)) * 10; //use integer arithmetic to round down to nearest percent
fo2.permille = (po2 * 100 / depth_to_mbar(depth.mm)) * 10; //use integer arithmetic to round down to nearest percent
// Don't permit >100% O2
if (fo2.permille > 1000)
fo2.permille = 1000;
@ -2355,12 +2355,12 @@ fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planner)
}
//Calculate He in best mix. O2 is considered narcopic
fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, fraction_t fo2)
fraction_t dive::best_he(depth_t depth, bool o2narcotic, fraction_t fo2) const
{
fraction_t fhe;
int pnarcotic, ambient;
pnarcotic = dive->depth_to_mbar(prefs.bestmixend.mm);
ambient = dive->depth_to_mbar(depth.mm);
pnarcotic = depth_to_mbar(prefs.bestmixend.mm);
ambient = depth_to_mbar(depth.mm);
if (o2narcotic) {
fhe.permille = (100 - 100 * pnarcotic / ambient) * 10; //use integer arithmetic to round up to nearest percent
} else {

View file

@ -125,6 +125,8 @@ struct dive {
pressure_t un_fixup_surface_pressure() const;
depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, int roundto) const;
depth_t gas_mnd(struct gasmix mix, depth_t end, int roundto) const;
fraction_t best_o2(depth_t depth, bool in_planner) const;
fraction_t best_he(depth_t depth, bool o2narcotic, fraction_t fo2) const;
bool dive_has_gps_location() const;
location_t get_gps_location() const;
@ -162,9 +164,6 @@ struct dive_components {
unsigned int when : 1;
};
extern fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planner);
extern fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, fraction_t fo2);
extern std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number);
extern bool time_during_dive_with_offset(const struct dive *dive, timestamp_t when, timestamp_t offset);

View file

@ -424,11 +424,11 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
if (QString::compare(qPrintable(vString), "*") == 0) {
cyl.bestmix_o2 = true;
// Calculate fO2 for max. depth
cyl.gasmix.o2 = best_o2(d->maxdepth, d, inPlanner);
cyl.gasmix.o2 = d->best_o2(d->maxdepth, inPlanner);
} else {
cyl.bestmix_o2 = false;
// Calculate fO2 for input depth
cyl.gasmix.o2 = best_o2(string_to_depth(qPrintable(vString)), d, inPlanner);
cyl.gasmix.o2 = d->best_o2(string_to_depth(qPrintable(vString)), inPlanner);
}
pressure_t modpO2;
modpO2.mbar = prefs.decopo2;
@ -440,11 +440,11 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
if (QString::compare(qPrintable(vString), "*") == 0) {
cyl.bestmix_he = true;
// Calculate fO2 for max. depth
cyl.gasmix.he = best_he(d->maxdepth, d, prefs.o2narcotic, make_fraction(get_o2(cyl.gasmix)));
cyl.gasmix.he = d->best_he(d->maxdepth, prefs.o2narcotic, make_fraction(get_o2(cyl.gasmix)));
} else {
cyl.bestmix_he = false;
// Calculate fHe for input depth
cyl.gasmix.he = best_he(string_to_depth(qPrintable(vString)), d, prefs.o2narcotic, make_fraction(get_o2(cyl.gasmix)));
cyl.gasmix.he = d->best_he(string_to_depth(qPrintable(vString)), prefs.o2narcotic, make_fraction(get_o2(cyl.gasmix)));
}
type = Command::EditCylinderType::GASMIX;
break;
@ -663,7 +663,7 @@ bool CylindersModel::updateBestMixes()
bool gasUpdated = false;
for (auto &cyl: d->cylinders) {
if (cyl.bestmix_o2) {
cyl.gasmix.o2 = best_o2(d->maxdepth, d, inPlanner);
cyl.gasmix.o2 = d->best_o2(d->maxdepth, inPlanner);
// fO2 + fHe must not be greater than 1
if (get_o2(cyl.gasmix) + get_he(cyl.gasmix) > 1000)
cyl.gasmix.he.permille = 1000 - get_o2(cyl.gasmix);
@ -673,7 +673,7 @@ bool CylindersModel::updateBestMixes()
gasUpdated = true;
}
if (cyl.bestmix_he) {
cyl.gasmix.he = best_he(d->maxdepth, d, prefs.o2narcotic, cyl.gasmix.o2);
cyl.gasmix.he = d->best_he(d->maxdepth, prefs.o2narcotic, cyl.gasmix.o2);
// fO2 + fHe must not be greater than 1
if (get_o2(cyl.gasmix) + get_he(cyl.gasmix) > 1000)
cyl.gasmix.o2.permille = 1000 - get_he(cyl.gasmix);