core: move get_max_mod() and get_max_mnd() 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 16:17:20 +02:00 committed by bstoeger
parent d36fd79527
commit 286d8fe21c
6 changed files with 36 additions and 42 deletions

View file

@ -2476,21 +2476,16 @@ int dive::mbar_to_depth(int mbar) const
}
/* MOD rounded to multiples of roundto mm */
depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, const struct dive *dive, int roundto)
depth_t dive::gas_mod(struct gasmix mix, pressure_t po2_limit, int roundto) const
{
depth_t rounded_depth;
double depth = (double) dive->mbar_to_depth(po2_limit.mbar * 1000 / get_o2(mix));
rounded_depth.mm = (int)lrint(depth / roundto) * roundto;
return rounded_depth;
double depth = (double) mbar_to_depth(po2_limit.mbar * 1000 / get_o2(mix));
return depth_t { (int)lrint(depth / roundto) * roundto };
}
/* Maximum narcotic depth rounded to multiples of roundto mm */
depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int roundto)
depth_t dive::gas_mnd(struct gasmix mix, depth_t end, int roundto) const
{
depth_t rounded_depth;
pressure_t ppo2n2;
ppo2n2.mbar = dive->depth_to_mbar(end.mm);
pressure_t ppo2n2 { depth_to_mbar(end.mm) };
int maxambient = prefs.o2narcotic ?
(int)lrint(ppo2n2.mbar / (1 - get_he(mix) / 1000.0))
@ -2500,8 +2495,7 @@ depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int rou
:
// Actually: Infinity
1000000;
rounded_depth.mm = (int)lrint(((double)dive->mbar_to_depth(maxambient)) / roundto) * roundto;
return rounded_depth;
return depth_t { (int)lrint(((double)mbar_to_depth(maxambient)) / roundto) * roundto };
}
struct dive_site *get_dive_site_for_dive(const struct dive *dive)

View file

@ -115,6 +115,8 @@ struct dive {
pressure_t calculate_surface_pressure() const;
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;
/* Don't call directly, use dive_table::merge_dives()! */
static std::unique_ptr<dive> create_merged_dive(const struct dive &a, const struct dive &b, int offset, bool prefer_downloaded);
@ -153,8 +155,6 @@ extern fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planne
extern fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, fraction_t fo2);
extern int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null);
extern depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, const struct dive *dive, int roundto);
extern depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int roundto);
extern struct dive_site *get_dive_site_for_dive(const struct dive *dive);
extern std::string get_dive_country(const struct dive *dive);

View file

@ -333,7 +333,7 @@ void reset_cylinders(struct dive *dive, bool track_gas)
for (cylinder_t &cyl: dive->cylinders) {
if (cyl.depth.mm == 0) /* if the gas doesn't give a mod, calculate based on prefs */
cyl.depth = gas_mod(cyl.gasmix, decopo2, dive, M_OR_FT(3,10));
cyl.depth = dive->gas_mod(cyl.gasmix, decopo2, M_OR_FT(3,10));
if (track_gas)
cyl.start.mbar = cyl.end.mbar = cyl.type.workingpressure.mbar;
cyl.gas_used.mliter = 0;
@ -400,7 +400,7 @@ void fill_default_cylinder(const struct dive *dive, cylinder_t *cyl)
cyl->type.size.mliter = lrint(cuft_to_l(ti.cuft) * 1000 / bar_to_atm(psi_to_bar(ti.psi)));
}
// MOD of air
cyl->depth = gas_mod(cyl->gasmix, pO2, dive, 1);
cyl->depth = dive->gas_mod(cyl->gasmix, pO2, 1);
return;
}
}

View file

@ -1129,7 +1129,7 @@ static void calculate_gas_information_new(const struct dive *dive, const struct
* END takes O + N (air) into account ("Narcotic" for trimix dives)
* EAD just uses N ("Air" for nitrox dives) */
pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) };
entry.mod = gas_mod(gasmix, modpO2, dive, 1).mm;
entry.mod = dive->gas_mod(gasmix, modpO2, 1).mm;
entry.end = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) * (1000 - fhe) / 1000.0));
entry.ead = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) * fn2 / (double)N2_IN_AIR));
entry.eadd = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) *

View file

@ -226,13 +226,13 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const
} else {
pressure_t modpO2;
modpO2.mbar = inPlanner ? prefs.bottompo2 : (int)(prefs.modpO2 * 1000.0);
return get_depth_string(gas_mod(cyl->gasmix, modpO2, d, M_OR_FT(1,1)), true);
return get_depth_string(d->gas_mod(cyl->gasmix, modpO2, M_OR_FT(1,1)), true);
}
case MND:
if (cyl->bestmix_he)
return QStringLiteral("*");
else
return get_depth_string(gas_mnd(cyl->gasmix, prefs.bestmixend, d, M_OR_FT(1,1)), true);
return get_depth_string(d->gas_mnd(cyl->gasmix, prefs.bestmixend, M_OR_FT(1,1)), true);
break;
case USE:
return gettextFromC::tr(cylinderuse_text[cyl->cylinder_use]);
@ -403,7 +403,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
prefs.o2consumption / prefs.decosac / prefs.pscr_ratio;
else
modpO2.mbar = prefs.decopo2;
cyl.depth = gas_mod(cyl.gasmix, modpO2, d, M_OR_FT(3, 10));
cyl.depth = d->gas_mod(cyl.gasmix, modpO2, M_OR_FT(3, 10));
cyl.bestmix_o2 = false;
}
type = Command::EditCylinderType::GASMIX;
@ -432,7 +432,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
}
pressure_t modpO2;
modpO2.mbar = prefs.decopo2;
cyl.depth = gas_mod(cyl.gasmix, modpO2, d, M_OR_FT(3, 10));
cyl.depth = d->gas_mod(cyl.gasmix, modpO2, M_OR_FT(3, 10));
}
type = Command::EditCylinderType::GASMIX;
break;
@ -638,8 +638,8 @@ void CylindersModel::updateDecoDepths(pressure_t olddecopo2)
for (auto &cyl: d->cylinders) {
/* If the gas's deco MOD matches the old pO2, it will have been automatically calculated and should be updated.
* If they don't match, we should leave the user entered depth as it is */
if (cyl.depth.mm == gas_mod(cyl.gasmix, olddecopo2, d, M_OR_FT(3, 10)).mm) {
cyl.depth = gas_mod(cyl.gasmix, decopo2, d, M_OR_FT(3, 10));
if (cyl.depth.mm == d->gas_mod(cyl.gasmix, olddecopo2, M_OR_FT(3, 10)).mm) {
cyl.depth = d->gas_mod(cyl.gasmix, decopo2, M_OR_FT(3, 10));
}
}
emit dataChanged(createIndex(0, 0), createIndex(numRows - 1, COLUMNS - 1));
@ -669,7 +669,7 @@ bool CylindersModel::updateBestMixes()
cyl.gasmix.he.permille = 1000 - get_o2(cyl.gasmix);
pressure_t modpO2;
modpO2.mbar = prefs.decopo2;
cyl.depth = gas_mod(cyl.gasmix, modpO2, d, M_OR_FT(3, 10));
cyl.depth = d->gas_mod(cyl.gasmix, modpO2, M_OR_FT(3, 10));
gasUpdated = true;
}
if (cyl.bestmix_he) {

View file

@ -67,8 +67,8 @@ void setupPlan(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(79, 260) * 60 / M_OR_FT(23, 75);
plan_add_segment(dp, 0, gas_mod(ean36, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, gas_mod(oxygen, po2, &dive, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(ean36, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(79, 260), 0, 0, 1, OC);
plan_add_segment(dp, 30 * 60 - droptime, M_OR_FT(79, 260), 0, 0, 1, OC);
}
@ -101,8 +101,8 @@ void setupPlanVpmb45m30mTx(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(45, 150) * 60 / M_OR_FT(23, 75);
plan_add_segment(dp, 0, gas_mod(ean50, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, gas_mod(oxygen, po2, &dive, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(45, 150), 0, 0, 1, OC);
plan_add_segment(dp, 30 * 60 - droptime, M_OR_FT(45, 150), 0, 0, 1, OC);
}
@ -135,8 +135,8 @@ void setupPlanVpmb60m10mTx(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(60, 200) * 60 / M_OR_FT(23, 75);
plan_add_segment(dp, 0, gas_mod(tx50_15, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, gas_mod(oxygen, po2, &dive, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(tx50_15, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(60, 200), 0, 0, 1, OC);
plan_add_segment(dp, 10 * 60 - droptime, M_OR_FT(60, 200), 0, 0, 1, OC);
}
@ -186,7 +186,7 @@ void setupPlanVpmb60m30minEan50(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(60, 200) * 60 / M_OR_FT(99, 330);
plan_add_segment(dp, 0, gas_mod(ean50, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(60, 200), 0, 0, 1, OC);
plan_add_segment(dp, 30 * 60 - droptime, M_OR_FT(60, 200), 0, 0, 1, OC);
}
@ -215,7 +215,7 @@ void setupPlanVpmb60m30minTx(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(60, 200) * 60 / M_OR_FT(99, 330);
plan_add_segment(dp, 0, gas_mod(ean50, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(60, 200), 0, 0, 1, OC);
plan_add_segment(dp, 30 * 60 - droptime, M_OR_FT(60, 200), 0, 0, 1, OC);
}
@ -270,8 +270,8 @@ void setupPlanVpmb100m60min(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(100, 330) * 60 / M_OR_FT(99, 330);
plan_add_segment(dp, 0, gas_mod(ean50, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, gas_mod(oxygen, po2, &dive, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(100, 330), 0, 0, 1, OC);
plan_add_segment(dp, 60 * 60 - droptime, M_OR_FT(100, 330), 0, 0, 1, OC);
}
@ -303,8 +303,8 @@ void setupPlanVpmb100m10min(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(100, 330) * 60 / M_OR_FT(99, 330);
plan_add_segment(dp, 0, gas_mod(ean50, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, gas_mod(oxygen, po2, &dive, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(100, 330), 0, 0, 1, OC);
plan_add_segment(dp, 10 * 60 - droptime, M_OR_FT(100, 330), 0, 0, 1, OC);
}
@ -360,9 +360,9 @@ void setupPlanVpmb100mTo70m30min(struct diveplan *dp)
free_dps(dp);
int droptime = M_OR_FT(100, 330) * 60 / M_OR_FT(18, 60);
plan_add_segment(dp, 0, gas_mod(tx21_35, po2, &dive, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, gas_mod(ean50, po2, &dive, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, 0, gas_mod(oxygen, po2, &dive, M_OR_FT(3, 10)).mm, 3, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(tx21_35, po2, M_OR_FT(3, 10)).mm, 1, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, M_OR_FT(3, 10)).mm, 2, 0, 1, OC);
plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, M_OR_FT(3, 10)).mm, 3, 0, 1, OC);
plan_add_segment(dp, droptime, M_OR_FT(100, 330), 0, 0, 1, OC);
plan_add_segment(dp, 20 * 60 - droptime, M_OR_FT(100, 330), 0, 0, 1, OC);
plan_add_segment(dp, 3 * 60, M_OR_FT(70, 230), 0, 0, 1, OC);
@ -420,14 +420,14 @@ void setupPlanCcr(struct diveplan *dp)
cylinder_t *cyl0 = get_or_create_cylinder(&dive, 0);
cylinder_t *cyl1 = get_or_create_cylinder(&dive, 1);
cyl0->gasmix = diluent;
cyl0->depth = gas_mod(diluent, po2, &dive, M_OR_FT(3, 10));
cyl0->depth = dive.gas_mod(diluent, po2, M_OR_FT(3, 10));
cyl0->type.size.mliter = 3000;
cyl0->type.workingpressure.mbar = 200000;
cyl0->cylinder_use = DILUENT;
cyl1->gasmix = ean53;
cyl1->depth = gas_mod(ean53, po2, &dive, M_OR_FT(3, 10));
cyl1->depth = dive.gas_mod(ean53, po2, M_OR_FT(3, 10));
cyl2->gasmix = tx19_33;
cyl2->depth = gas_mod(tx19_33, po2, &dive, M_OR_FT(3, 10));
cyl2->depth = dive.gas_mod(tx19_33, po2, M_OR_FT(3, 10));
reset_cylinders(&dive, true);
free_dps(dp);