core: move *_to_depth() functions into struct dive

Seems logical in a C++ code base.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-20 22:47:16 +02:00 committed by bstoeger
parent bf84d66df2
commit bdd5527005
6 changed files with 29 additions and 29 deletions

View file

@ -551,7 +551,7 @@ int deco_allowed_depth(double tissues_tolerance, double surface_pressure, const
/* Avoid negative depths */ /* Avoid negative depths */
pressure_delta = tissues_tolerance > surface_pressure ? tissues_tolerance - surface_pressure : 0.0; pressure_delta = tissues_tolerance > surface_pressure ? tissues_tolerance - surface_pressure : 0.0;
depth = rel_mbar_to_depth(lrint(pressure_delta * 1000), dive); depth = dive->rel_mbar_to_depth(lrint(pressure_delta * 1000));
if (!smooth) if (!smooth)
depth = lrint(ceil(depth / DECO_STOPS_MULTIPLIER_MM) * DECO_STOPS_MULTIPLIER_MM); depth = lrint(ceil(depth / DECO_STOPS_MULTIPLIER_MM) * DECO_STOPS_MULTIPLIER_MM);

View file

@ -2824,10 +2824,10 @@ double dive::depth_to_atm(int depth) const
* (that's the one that some dive computers like the Uemis Zurich * (that's the one that some dive computers like the Uemis Zurich
* provide - for the other models that do this libdivecomputer has to * provide - for the other models that do this libdivecomputer has to
* take care of this, but the Uemis we support natively */ * take care of this, but the Uemis we support natively */
int rel_mbar_to_depth(int mbar, const struct dive *dive) int dive::rel_mbar_to_depth(int mbar) const
{ {
// For downloaded and planned dives, use DC's salinity. Manual dives, use user's salinity // For downloaded and planned dives, use DC's salinity. Manual dives, use user's salinity
int salinity = is_dc_manually_added_dive(&dive->dcs[0]) ? dive->user_salinity : dive->dcs[0].salinity; int salinity = is_dc_manually_added_dive(&dcs[0]) ? user_salinity : dcs[0].salinity;
if (!salinity) if (!salinity)
salinity = SEAWATER_SALINITY; salinity = SEAWATER_SALINITY;
@ -2836,17 +2836,17 @@ int rel_mbar_to_depth(int mbar, const struct dive *dive)
return (int)lrint(mbar / specific_weight); return (int)lrint(mbar / specific_weight);
} }
int mbar_to_depth(int mbar, const struct dive *dive) int dive::mbar_to_depth(int mbar) const
{ {
// For downloaded and planned dives, use DC's pressure. Manual dives, use user's pressure // For downloaded and planned dives, use DC's pressure. Manual dives, use user's pressure
pressure_t surface_pressure = is_dc_manually_added_dive(&dive->dcs[0]) pressure_t surface_pressure = is_dc_manually_added_dive(&dcs[0])
? dive->surface_pressure ? this->surface_pressure
: dive->dcs[0].surface_pressure; : dcs[0].surface_pressure;
if (!surface_pressure.mbar) if (!surface_pressure.mbar)
surface_pressure.mbar = SURFACE_PRESSURE; surface_pressure.mbar = SURFACE_PRESSURE;
return rel_mbar_to_depth(mbar - surface_pressure.mbar, dive); return rel_mbar_to_depth(mbar - surface_pressure.mbar);
} }
/* MOD rounded to multiples of roundto mm */ /* MOD rounded to multiples of roundto mm */
@ -2854,7 +2854,7 @@ depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, const struct dive *dive
{ {
depth_t rounded_depth; depth_t rounded_depth;
double depth = (double) mbar_to_depth(po2_limit.mbar * 1000 / get_o2(mix), dive); double depth = (double) dive->mbar_to_depth(po2_limit.mbar * 1000 / get_o2(mix));
rounded_depth.mm = (int)lrint(depth / roundto) * roundto; rounded_depth.mm = (int)lrint(depth / roundto) * roundto;
return rounded_depth; return rounded_depth;
} }
@ -2874,7 +2874,7 @@ depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int rou
: :
// Actually: Infinity // Actually: Infinity
1000000; 1000000;
rounded_depth.mm = (int)lrint(((double)mbar_to_depth(maxambient, dive)) / roundto) * roundto; rounded_depth.mm = (int)lrint(((double)dive->mbar_to_depth(maxambient)) / roundto) * roundto;
return rounded_depth; return rounded_depth;
} }

View file

@ -91,6 +91,8 @@ struct dive {
double depth_to_mbarf(int depth) const; double depth_to_mbarf(int depth) const;
double depth_to_bar(int depth) const; double depth_to_bar(int depth) const;
double depth_to_atm(int depth) const; double depth_to_atm(int depth) const;
int rel_mbar_to_depth(int mbar) const;
int mbar_to_depth(int mbar) const;
}; };
/* For the top-level list: an entry is either a dive or a trip */ /* For the top-level list: an entry is either a dive or a trip */
@ -133,8 +135,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 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 int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null);
extern int rel_mbar_to_depth(int mbar, const struct dive *dive);
extern int mbar_to_depth(int mbar, const struct dive *dive);
extern depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, const struct dive *dive, int roundto); 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 depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int roundto);

View file

@ -1159,13 +1159,13 @@ static void calculate_gas_information_new(const struct dive *dive, const struct
* EAD just uses N ("Air" for nitrox dives) */ * EAD just uses N ("Air" for nitrox dives) */
pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) }; pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) };
entry.mod = gas_mod(gasmix, modpO2, dive, 1).mm; entry.mod = gas_mod(gasmix, modpO2, dive, 1).mm;
entry.end = mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) * (1000 - fhe) / 1000.0), dive); entry.end = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) * (1000 - fhe) / 1000.0));
entry.ead = mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) * fn2 / (double)N2_IN_AIR), dive); entry.ead = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) * fn2 / (double)N2_IN_AIR));
entry.eadd = mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) * entry.eadd = dive->mbar_to_depth(lrint(dive->depth_to_mbarf(entry.depth) *
(entry.pressures.o2 / amb_pressure * O2_DENSITY + (entry.pressures.o2 / amb_pressure * O2_DENSITY +
entry.pressures.n2 / amb_pressure * N2_DENSITY + entry.pressures.n2 / amb_pressure * N2_DENSITY +
entry.pressures.he / amb_pressure * HE_DENSITY) / entry.pressures.he / amb_pressure * HE_DENSITY) /
(O2_IN_AIR * O2_DENSITY + N2_IN_AIR * N2_DENSITY) * 1000), dive); (O2_IN_AIR * O2_DENSITY + N2_IN_AIR * N2_DENSITY) * 1000));
entry.density = gas_density(entry.pressures); entry.density = gas_density(entry.pressures);
if (entry.mod < 0) if (entry.mod < 0)
entry.mod = 0; entry.mod = 0;

View file

@ -248,7 +248,7 @@ void uemis::event(struct dive *dive, struct divecomputer *dc, struct sample *sam
* flags[3].bit0 | flags[5].bit1 != 0 ==> in deco * flags[3].bit0 | flags[5].bit1 != 0 ==> in deco
* flags[0].bit7 == 1 ==> Safety Stop * flags[0].bit7 == 1 ==> Safety Stop
* otherwise NDL */ * otherwise NDL */
stopdepth = rel_mbar_to_depth(u_sample->hold_depth, dive); stopdepth = dive->rel_mbar_to_depth(u_sample->hold_depth);
if ((flags[3] & 1) | (flags[5] & 2)) { if ((flags[3] & 1) | (flags[5] & 2)) {
/* deco */ /* deco */
sample->in_deco = true; sample->in_deco = true;
@ -344,7 +344,7 @@ void uemis::parse_divelog_binary(std::string_view base64, struct dive *dive)
} }
sample = prepare_sample(dc); sample = prepare_sample(dc);
sample->time.seconds = u_sample->dive_time; sample->time.seconds = u_sample->dive_time;
sample->depth.mm = rel_mbar_to_depth(u_sample->water_pressure, dive); sample->depth.mm = dive->rel_mbar_to_depth(u_sample->water_pressure);
sample->temperature.mkelvin = C_to_mkelvin(u_sample->dive_temperature / 10.0); sample->temperature.mkelvin = C_to_mkelvin(u_sample->dive_temperature / 10.0);
add_sample_pressure(sample, active, (u_sample->tank_pressure_high * 256 + u_sample->tank_pressure_low) * 10); add_sample_pressure(sample, active, (u_sample->tank_pressure_high * 256 + u_sample->tank_pressure_low) * 10);
sample->cns = u_sample->cns; sample->cns = u_sample->cns;

View file

@ -575,7 +575,7 @@ void TestPlan::testVpmbMetric45m30minTx()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 108l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 108l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
// check benchmark run time of 141 minutes, and known Subsurface runtime of 139 minutes // check benchmark run time of 141 minutes, and known Subsurface runtime of 139 minutes
//QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 141u * 60u + 20u, 139u * 60u + 20u)); //QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 141u * 60u + 20u, 139u * 60u + 20u));
} }
@ -604,7 +604,7 @@ void TestPlan::testVpmbMetric60m10minTx()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 162l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 162l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
// check benchmark run time of 141 minutes, and known Subsurface runtime of 139 minutes // check benchmark run time of 141 minutes, and known Subsurface runtime of 139 minutes
//QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 141u * 60u + 20u, 139u * 60u + 20u)); //QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 141u * 60u + 20u, 139u * 60u + 20u));
} }
@ -633,7 +633,7 @@ void TestPlan::testVpmbMetric60m30minAir()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 180l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 180l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
// check benchmark run time of 141 minutes, and known Subsurface runtime of 139 minutes // check benchmark run time of 141 minutes, and known Subsurface runtime of 139 minutes
QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 141u * 60u + 20u, 139u * 60u + 20u)); QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 141u * 60u + 20u, 139u * 60u + 20u));
} }
@ -662,7 +662,7 @@ void TestPlan::testVpmbMetric60m30minEan50()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 155l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 155l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
QVERIFY(dive.dcs[0].events.size() >= 1); QVERIFY(dive.dcs[0].events.size() >= 1);
// check first gas change to EAN50 at 21m // check first gas change to EAN50 at 21m
struct event *ev = &dive.dcs[0].events[0]; struct event *ev = &dive.dcs[0].events[0];
@ -697,7 +697,7 @@ void TestPlan::testVpmbMetric60m30minTx()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 159l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 159l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
// check first gas change to EAN50 at 21m // check first gas change to EAN50 at 21m
QVERIFY(dive.dcs[0].events.size() >= 1); QVERIFY(dive.dcs[0].events.size() >= 1);
struct event *ev = &dive.dcs[0].events[0]; struct event *ev = &dive.dcs[0].events[0];
@ -732,7 +732,7 @@ void TestPlan::testVpmbMetric100m60min()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 157l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 157l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
QVERIFY(dive.dcs[0].events.size() >= 2); QVERIFY(dive.dcs[0].events.size() >= 2);
// check first gas change to EAN50 at 21m // check first gas change to EAN50 at 21m
struct event *ev = &dive.dcs[0].events[0]; struct event *ev = &dive.dcs[0].events[0];
@ -798,7 +798,7 @@ void TestPlan::testVpmbMetricMultiLevelAir()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 101l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 101l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
// check benchmark run time of 167 minutes, and known Subsurface runtime of 169 minutes // check benchmark run time of 167 minutes, and known Subsurface runtime of 169 minutes
QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 167u * 60u + 20u, 169u * 60u + 20u)); QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 167u * 60u + 20u, 169u * 60u + 20u));
} }
@ -827,7 +827,7 @@ void TestPlan::testVpmbMetric100m10min()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 175l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 175l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
QVERIFY(dive.dcs[0].events.size() >= 2); QVERIFY(dive.dcs[0].events.size() >= 2);
// check first gas change to EAN50 at 21m // check first gas change to EAN50 at 21m
struct event *ev = &dive.dcs[0].events[0]; struct event *ev = &dive.dcs[0].events[0];
@ -873,7 +873,7 @@ void TestPlan::testVpmbMetricRepeat()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 61l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 61l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
// check benchmark run time of 27 minutes, and known Subsurface runtime of 28 minutes // check benchmark run time of 27 minutes, and known Subsurface runtime of 28 minutes
QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 27u * 60u + 20u, 27u * 60u + 20u)); QVERIFY(compareDecoTime(dive.dcs[0].duration.seconds, 27u * 60u + 20u, 27u * 60u + 20u));
@ -893,7 +893,7 @@ void TestPlan::testVpmbMetricRepeat()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 80l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 80l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
QVERIFY(dive.dcs[0].events.size() >= 3); QVERIFY(dive.dcs[0].events.size() >= 3);
// check first gas change to 21/35 at 66m // check first gas change to 21/35 at 66m
struct event *ev = &dive.dcs[0].events[0]; struct event *ev = &dive.dcs[0].events[0];
@ -930,7 +930,7 @@ void TestPlan::testVpmbMetricRepeat()
dp = dp->next; dp = dp->next;
QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 61l); QCOMPARE(lrint(dp->minimum_gas.mbar / 1000.0), 61l);
// print first ceiling // print first ceiling
printf("First ceiling %.1f m\n", (mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar, &dive) * 0.001)); printf("First ceiling %.1f m\n", dive.mbar_to_depth(test_deco_state.first_ceiling_pressure.mbar) * 0.001);
// check runtime is exactly the same as the first time // check runtime is exactly the same as the first time
int finalDiveRunTimeSeconds = dive.dcs[0].duration.seconds; int finalDiveRunTimeSeconds = dive.dcs[0].duration.seconds;