From 6b57b3b745646d84c21f4fc73061fc0ccfd3b0ac Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 14 Dec 2024 10:35:56 +0100 Subject: [PATCH] core: make round_to parameter of gas_mod() and gas_mnd() a depth_t Simplifies practically all the callers. Signed-off-by: Berthold Stoeger --- core/dive.cpp | 8 ++++---- core/dive.h | 4 ++-- core/equipment.cpp | 4 ++-- core/profile.cpp | 2 +- qt-models/cylindermodel.cpp | 14 +++++++------- tests/testplan.cpp | 36 ++++++++++++++++++------------------ 6 files changed, 34 insertions(+), 34 deletions(-) diff --git a/core/dive.cpp b/core/dive.cpp index 222915045..8c7b23932 100644 --- a/core/dive.cpp +++ b/core/dive.cpp @@ -2455,16 +2455,16 @@ depth_t dive::mbar_to_depth(int mbar) const } /* MOD rounded to multiples of roundto mm */ -depth_t dive::gas_mod(struct gasmix mix, pressure_t po2_limit, int roundto) const +depth_t dive::gas_mod(struct gasmix mix, pressure_t po2_limit, depth_t roundto) const { double depth = (double) mbar_to_depth(po2_limit.mbar * 1000 / get_o2(mix)).mm; // Rounding should be towards lower=safer depths but we give a bit // of fudge to all to switch to o2 at 6m. So from .9 we round up. - return depth_t { .mm = (int)(depth / roundto + 0.1) * roundto }; + return depth_t { .mm = (int)(depth / roundto.mm + 0.1) * roundto.mm }; } /* Maximum narcotic depth rounded to multiples of roundto mm */ -depth_t dive::gas_mnd(struct gasmix mix, depth_t end, int roundto) const +depth_t dive::gas_mnd(struct gasmix mix, depth_t end, depth_t roundto) const { pressure_t ppo2n2 { .mbar = depth_to_mbar(end) }; @@ -2477,7 +2477,7 @@ depth_t dive::gas_mnd(struct gasmix mix, depth_t end, int roundto) const // Actually: Infinity 1000000; double depth = static_cast(mbar_to_depth(maxambient).mm); - return depth_t { .mm = int_cast(depth / roundto) * roundto }; + return depth_t { .mm = int_cast(depth / roundto.mm) * roundto.mm }; } std::string dive::get_country() const diff --git a/core/dive.h b/core/dive.h index 1594c00a2..c2fc174d5 100644 --- a/core/dive.h +++ b/core/dive.h @@ -125,8 +125,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; + depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, depth_t roundto) const; + depth_t gas_mnd(struct gasmix mix, depth_t end, depth_t 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; diff --git a/core/equipment.cpp b/core/equipment.cpp index a418e209f..27ff4f854 100644 --- a/core/equipment.cpp +++ b/core/equipment.cpp @@ -314,7 +314,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 = dive->gas_mod(cyl.gasmix, decopo2, m_or_ft(3, 10).mm); + 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 = 0_l; @@ -364,7 +364,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 = dive->gas_mod(cyl->gasmix, pO2, 1); + cyl->depth = dive->gas_mod(cyl->gasmix, pO2, 1_mm); return; } } diff --git a/core/profile.cpp b/core/profile.cpp index 85c9366e7..ac38ab33d 100644 --- a/core/profile.cpp +++ b/core/profile.cpp @@ -1131,7 +1131,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 = dive->gas_mod(gasmix, modpO2, 1).mm; + entry.mod = dive->gas_mod(gasmix, modpO2, 1_mm).mm; entry.end = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(depth) * (1000 - fhe) / 1000.0)).mm; entry.ead = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(depth) * fn2 / (double)N2_IN_AIR)).mm; entry.eadd = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(depth) * diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp index 62e65d181..dbdd44de1 100644 --- a/qt-models/cylindermodel.cpp +++ b/qt-models/cylindermodel.cpp @@ -237,13 +237,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(d->gas_mod(cyl->gasmix, modpO2, m_or_ft(1, 1).mm), 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(d->gas_mnd(cyl->gasmix, prefs.bestmixend, m_or_ft(1, 1).mm), 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]); @@ -414,7 +414,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 = d->gas_mod(cyl.gasmix, modpO2, m_or_ft(3, 10).mm); + cyl.depth = d->gas_mod(cyl.gasmix, modpO2, m_or_ft(3, 10)); cyl.bestmix_o2 = false; } type = Command::EditCylinderType::GASMIX; @@ -443,7 +443,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in } pressure_t modpO2; modpO2.mbar = prefs.decopo2; - cyl.depth = d->gas_mod(cyl.gasmix, modpO2, m_or_ft(3, 10).mm); + cyl.depth = d->gas_mod(cyl.gasmix, modpO2, m_or_ft(3, 10)); } type = Command::EditCylinderType::GASMIX; break; @@ -649,8 +649,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 == d->gas_mod(cyl.gasmix, olddecopo2, m_or_ft(3, 10).mm).mm) { - cyl.depth = d->gas_mod(cyl.gasmix, decopo2, m_or_ft(3, 10).mm); + 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)); @@ -680,7 +680,7 @@ bool CylindersModel::updateBestMixes() cyl.gasmix.he.permille = 1000 - get_o2(cyl.gasmix); pressure_t modpO2; modpO2.mbar = prefs.decopo2; - cyl.depth = d->gas_mod(cyl.gasmix, modpO2, m_or_ft(3, 10).mm); + cyl.depth = d->gas_mod(cyl.gasmix, modpO2, m_or_ft(3, 10)); gasUpdated = true; } if (cyl.bestmix_he) { diff --git a/tests/testplan.cpp b/tests/testplan.cpp index b6029345f..2017463de 100644 --- a/tests/testplan.cpp +++ b/tests/testplan.cpp @@ -65,8 +65,8 @@ diveplan setupPlan() reset_cylinders(&dive, true); int droptime = m_or_ft(79, 260).mm * 60 / m_or_ft(23, 75).mm; - plan_add_segment(dp, 0, dive.gas_mod(ean36, po2, m_or_ft(3, 10).mm).mm, 1, 0, 1, OC); - plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 30 * 60 - droptime, m_or_ft(79, 260).mm, 0, 0, 1, OC); return dp; @@ -100,8 +100,8 @@ diveplan setupPlanVpmb45m30mTx() reset_cylinders(&dive, true); int droptime = m_or_ft(45, 150).mm * 60 / m_or_ft(23, 75).mm; - plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, m_or_ft(3, 10).mm).mm, 1, 0, 1, OC); - plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 30 * 60 - droptime, m_or_ft(45, 150).mm, 0, 0, 1, OC); return dp; @@ -135,8 +135,8 @@ diveplan setupPlanVpmb60m10mTx() reset_cylinders(&dive, true); int droptime = m_or_ft(60, 200).mm * 60 / m_or_ft(23, 75).mm; - plan_add_segment(dp, 0, dive.gas_mod(tx50_15, po2, m_or_ft(3, 10).mm).mm, 1, 0, 1, OC); - plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 10 * 60 - droptime, m_or_ft(60, 200).mm, 0, 0, 1, OC); return dp; @@ -188,7 +188,7 @@ diveplan setupPlanVpmb60m30minEan50() reset_cylinders(&dive, true); int droptime = m_or_ft(60, 200).mm * 60 / m_or_ft(99, 330).mm; - plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 30 * 60 - droptime, m_or_ft(60, 200).mm, 0, 0, 1, OC); return dp; @@ -218,7 +218,7 @@ diveplan setupPlanVpmb60m30minTx() reset_cylinders(&dive, true); int droptime = m_or_ft(60, 200).mm * 60 / m_or_ft(99, 330).mm; - plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 30 * 60 - droptime, m_or_ft(60, 200).mm, 0, 0, 1, OC); return dp; @@ -275,8 +275,8 @@ diveplan setupPlanVpmb100m60min() reset_cylinders(&dive, true); int droptime = m_or_ft(100, 330).mm * 60 / m_or_ft(99, 330).mm; - plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, m_or_ft(3, 10).mm).mm, 1, 0, 1, OC); - plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 60 * 60 - droptime, m_or_ft(100, 330).mm, 0, 0, 1, OC); return dp; @@ -309,8 +309,8 @@ diveplan setupPlanVpmb100m10min() reset_cylinders(&dive, true); int droptime = m_or_ft(100, 330).mm * 60 / m_or_ft(99, 330).mm; - plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, m_or_ft(3, 10).mm).mm, 1, 0, 1, OC); - plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 10 * 60 - droptime, m_or_ft(100, 330).mm, 0, 0, 1, OC); return dp; @@ -368,9 +368,9 @@ diveplan setupPlanVpmb100mTo70m30min() reset_cylinders(&dive, true); int droptime = m_or_ft(100, 330).mm * 60 / m_or_ft(18, 60).mm; - plan_add_segment(dp, 0, dive.gas_mod(tx21_35, po2, m_or_ft(3, 10).mm).mm, 1, 0, 1, OC); - plan_add_segment(dp, 0, dive.gas_mod(ean50, po2, m_or_ft(3, 10).mm).mm, 2, 0, 1, OC); - plan_add_segment(dp, 0, dive.gas_mod(oxygen, po2, m_or_ft(3, 10).mm).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).mm, 0, 0, 1, OC); plan_add_segment(dp, 20 * 60 - droptime, m_or_ft(100, 330).mm, 0, 0, 1, OC); plan_add_segment(dp, 3 * 60, m_or_ft(70, 230).mm, 0, 0, 1, OC); @@ -431,14 +431,14 @@ diveplan setupPlanCcr() cylinder_t *cyl0 = dive.get_or_create_cylinder(0); cylinder_t *cyl1 = dive.get_or_create_cylinder(1); cyl0->gasmix = diluent; - cyl0->depth = dive.gas_mod(diluent, po2, m_or_ft(3, 10).mm); + cyl0->depth = dive.gas_mod(diluent, po2, m_or_ft(3, 10)); cyl0->type.size = 3_l; cyl0->type.workingpressure = 200_bar; cyl0->cylinder_use = DILUENT; cyl1->gasmix = ean53; - cyl1->depth = dive.gas_mod(ean53, po2, m_or_ft(3, 10).mm); + cyl1->depth = dive.gas_mod(ean53, po2, m_or_ft(3, 10)); cyl2->gasmix = tx19_33; - cyl2->depth = dive.gas_mod(tx19_33, po2, m_or_ft(3, 10).mm); + cyl2->depth = dive.gas_mod(tx19_33, po2, m_or_ft(3, 10)); reset_cylinders(&dive, true); plan_add_segment(dp, 0, cyl1->depth.mm, 1, 0, false, OC);