2017-04-27 18:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2024-05-01 16:11:35 +00:00
|
|
|
/* divelist.cpp */
|
2011-08-31 17:27:58 +00:00
|
|
|
|
2020-05-01 11:43:52 +00:00
|
|
|
#include "divelist.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "subsurface-string.h"
|
2019-07-15 21:36:14 +00:00
|
|
|
#include "deco.h"
|
2020-04-23 21:32:42 +00:00
|
|
|
#include "device.h"
|
2020-05-01 11:43:52 +00:00
|
|
|
#include "dive.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "divelog.h"
|
|
|
|
#include "divesite.h"
|
2020-10-25 08:14:16 +00:00
|
|
|
#include "event.h"
|
2024-02-14 09:59:13 +00:00
|
|
|
#include "eventtype.h"
|
2020-05-26 16:32:52 +00:00
|
|
|
#include "filterpreset.h"
|
2020-02-16 21:26:47 +00:00
|
|
|
#include "fulltext.h"
|
2020-10-25 17:14:23 +00:00
|
|
|
#include "interpolate.h"
|
2014-07-18 06:39:53 +00:00
|
|
|
#include "planner.h"
|
2024-06-19 20:45:25 +00:00
|
|
|
#include "qthelper.h" // for emit_reset_signal() -> should be removed
|
2024-05-19 10:38:38 +00:00
|
|
|
#include "range.h"
|
2019-11-24 12:51:01 +00:00
|
|
|
#include "gettext.h"
|
2016-03-23 19:09:18 +00:00
|
|
|
#include "git-access.h"
|
2019-11-24 14:02:34 +00:00
|
|
|
#include "selection.h"
|
2020-10-25 12:28:55 +00:00
|
|
|
#include "sample.h"
|
2019-05-31 14:09:14 +00:00
|
|
|
#include "trip.h"
|
2024-06-25 12:33:36 +00:00
|
|
|
#include "version.h"
|
2011-08-31 17:27:58 +00:00
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
void dive_table::record_dive(std::unique_ptr<dive> d)
|
|
|
|
{
|
2024-06-23 12:20:59 +00:00
|
|
|
fixup_dive(*d);
|
2024-06-07 08:25:09 +00:00
|
|
|
put(std::move(d));
|
|
|
|
}
|
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
void dive_table::fixup_dive(struct dive &dive) const
|
|
|
|
{
|
|
|
|
dive.fixup_no_cylinder();
|
|
|
|
update_cylinder_related_info(dive);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct start_end_pressure {
|
|
|
|
pressure_t start;
|
|
|
|
pressure_t end;
|
|
|
|
};
|
|
|
|
|
|
|
|
void dive_table::force_fixup_dive(struct dive &d) const
|
|
|
|
{
|
|
|
|
struct divecomputer *dc = &d.dcs[0];
|
|
|
|
int old_temp = dc->watertemp.mkelvin;
|
|
|
|
int old_mintemp = d.mintemp.mkelvin;
|
|
|
|
int old_maxtemp = d.maxtemp.mkelvin;
|
|
|
|
duration_t old_duration = d.duration;
|
|
|
|
std::vector<start_end_pressure> old_pressures(d.cylinders.size());
|
|
|
|
|
|
|
|
d.maxdepth.mm = 0;
|
|
|
|
dc->maxdepth.mm = 0;
|
|
|
|
d.watertemp.mkelvin = 0;
|
|
|
|
dc->watertemp.mkelvin = 0;
|
|
|
|
d.duration.seconds = 0;
|
|
|
|
d.maxtemp.mkelvin = 0;
|
|
|
|
d.mintemp.mkelvin = 0;
|
|
|
|
for (auto [i, cyl]: enumerated_range(d.cylinders)) {
|
|
|
|
old_pressures[i].start = cyl.start;
|
|
|
|
old_pressures[i].end = cyl.end;
|
|
|
|
cyl.start.mbar = 0;
|
|
|
|
cyl.end.mbar = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fixup_dive(d);
|
|
|
|
|
|
|
|
if (!d.watertemp.mkelvin)
|
|
|
|
d.watertemp.mkelvin = old_temp;
|
|
|
|
|
|
|
|
if (!dc->watertemp.mkelvin)
|
|
|
|
dc->watertemp.mkelvin = old_temp;
|
|
|
|
|
|
|
|
if (!d.maxtemp.mkelvin)
|
|
|
|
d.maxtemp.mkelvin = old_maxtemp;
|
|
|
|
|
|
|
|
if (!d.mintemp.mkelvin)
|
|
|
|
d.mintemp.mkelvin = old_mintemp;
|
|
|
|
|
|
|
|
if (!d.duration.seconds)
|
|
|
|
d.duration = old_duration;
|
|
|
|
for (auto [i, cyl]: enumerated_range(d.cylinders)) {
|
|
|
|
if (!cyl.start.mbar)
|
|
|
|
cyl.start = old_pressures[i].start;
|
|
|
|
if (!cyl.end.mbar)
|
|
|
|
cyl.end = old_pressures[i].end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a dive an hour from now with a default depth (15m/45ft) and duration (40 minutes)
|
|
|
|
// as a starting point for the user to edit
|
|
|
|
std::unique_ptr<dive> dive_table::default_dive()
|
|
|
|
{
|
|
|
|
auto d = std::make_unique<dive>();
|
|
|
|
d->when = time(nullptr) + gettimezoneoffset() + 3600;
|
|
|
|
d->dcs[0].duration.seconds = 40 * 60;
|
|
|
|
d->dcs[0].maxdepth.mm = M_OR_FT(15, 45);
|
|
|
|
d->dcs[0].meandepth.mm = M_OR_FT(13, 39); // this creates a resonable looking safety stop
|
|
|
|
make_manually_added_dive_dc(&d->dcs[0]);
|
|
|
|
fake_dc(&d->dcs[0]);
|
|
|
|
add_default_cylinder(d.get());
|
|
|
|
fixup_dive(*d);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int active_o2(const struct dive &dive, const struct divecomputer *dc, duration_t time)
|
First step in cleaning up cylinder pressure sensor logic
This clarifies/changes the meaning of our "cylinderindex" entry in our
samples. It has been rather confused, because different dive computers
have done things differently, and the naming really hasn't helped.
There are two totally different - and independent - cylinder "indexes":
- the pressure sensor index, which indicates which cylinder the sensor
data is from.
- the "active cylinder" index, which indicates which cylinder we actually
breathe from.
These two values really are totally independent, and have nothing
what-so-ever to do with each other. The sensor index may well be fixed:
many dive computers only support a single pressure sensor (whether
wireless or wired), and the sensor index is thus always zero.
Other dive computers may support multiple pressure sensors, and the gas
switch event may - or may not - indicate that the sensor changed too. A
dive computer might give the sensor data for *all* cylinders it can read,
regardless of which one is the one we're actively breathing. In fact, some
dive computers might give sensor data for not just *your* cylinder, but
your buddies.
This patch renames "cylinderindex" in the samples as "sensor", making it
quite clear that it's about which sensor index the pressure data in the
sample is about.
The way we figure out which is the currently active gas is with an
explicit has change event. If a computer (like the Uemis Zurich) joins the
two concepts together, then a sensor change should also create a gas
switch event. This patch also changes the Uemis importer to do that.
Finally, it should be noted that the plot info works totally separately
from the sample data, and is about what we actually *display*, not about
the sample pressures etc. In the plot info, the "cylinderindex" does in
fact mean the currently active cylinder, and while it is initially set to
match the sensor information from the samples, we then walk the gas change
events and fix it up - and if the active cylinder differs from the sensor
cylinder, we clear the sensor data.
[Dirk Hohndel: this conflicted with some of my recent changes - I think
I merged things correctly...]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-31 04:00:51 +00:00
|
|
|
{
|
2024-06-25 05:43:32 +00:00
|
|
|
struct gasmix gas = dive.get_gasmix_at_time(*dc, time);
|
2018-08-16 17:10:10 +00:00
|
|
|
return get_o2(gas);
|
First step in cleaning up cylinder pressure sensor logic
This clarifies/changes the meaning of our "cylinderindex" entry in our
samples. It has been rather confused, because different dive computers
have done things differently, and the naming really hasn't helped.
There are two totally different - and independent - cylinder "indexes":
- the pressure sensor index, which indicates which cylinder the sensor
data is from.
- the "active cylinder" index, which indicates which cylinder we actually
breathe from.
These two values really are totally independent, and have nothing
what-so-ever to do with each other. The sensor index may well be fixed:
many dive computers only support a single pressure sensor (whether
wireless or wired), and the sensor index is thus always zero.
Other dive computers may support multiple pressure sensors, and the gas
switch event may - or may not - indicate that the sensor changed too. A
dive computer might give the sensor data for *all* cylinders it can read,
regardless of which one is the one we're actively breathing. In fact, some
dive computers might give sensor data for not just *your* cylinder, but
your buddies.
This patch renames "cylinderindex" in the samples as "sensor", making it
quite clear that it's about which sensor index the pressure data in the
sample is about.
The way we figure out which is the currently active gas is with an
explicit has change event. If a computer (like the Uemis Zurich) joins the
two concepts together, then a sensor change should also create a gas
switch event. This patch also changes the Uemis importer to do that.
Finally, it should be noted that the plot info works totally separately
from the sample data, and is about what we actually *display*, not about
the sample pressures etc. In the plot info, the "cylinderindex" does in
fact mean the currently active cylinder, and while it is initially set to
match the sensor information from the samples, we then walk the gas change
events and fix it up - and if the active cylinder differs from the sensor
cylinder, we clear the sensor data.
[Dirk Hohndel: this conflicted with some of my recent changes - I think
I merged things correctly...]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-31 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 21:38:15 +00:00
|
|
|
// Do not call on first sample as it acccesses the previous sample
|
2024-06-23 12:20:59 +00:00
|
|
|
static int get_sample_o2(const struct dive &dive, const struct divecomputer *dc, const struct sample &sample, const struct sample &psample)
|
2021-08-18 21:38:15 +00:00
|
|
|
{
|
|
|
|
int po2i, po2f, po2;
|
|
|
|
// Use sensor[0] if available
|
2024-05-19 10:38:38 +00:00
|
|
|
if ((dc->divemode == CCR || dc->divemode == PSCR) && sample.o2sensor[0].mbar) {
|
|
|
|
po2i = psample.o2sensor[0].mbar;
|
|
|
|
po2f = sample.o2sensor[0].mbar; // then use data from the first o2 sensor
|
2021-08-18 21:38:15 +00:00
|
|
|
po2 = (po2f + po2i) / 2;
|
2024-05-19 10:38:38 +00:00
|
|
|
} else if (sample.setpoint.mbar > 0) {
|
|
|
|
po2 = std::min((int) sample.setpoint.mbar,
|
2024-06-23 12:20:59 +00:00
|
|
|
dive.depth_to_mbar(sample.depth.mm));
|
2021-08-18 21:38:15 +00:00
|
|
|
} else {
|
2024-06-23 12:20:59 +00:00
|
|
|
double amb_presure = dive.depth_to_bar(sample.depth.mm);
|
|
|
|
double pamb_pressure = dive.depth_to_bar(psample.depth.mm );
|
2021-08-18 21:38:15 +00:00
|
|
|
if (dc->divemode == PSCR) {
|
2024-06-25 05:43:32 +00:00
|
|
|
po2i = pscr_o2(pamb_pressure, dive.get_gasmix_at_time(*dc, psample.time));
|
|
|
|
po2f = pscr_o2(amb_presure, dive.get_gasmix_at_time(*dc, sample.time));
|
2021-08-18 21:38:15 +00:00
|
|
|
} else {
|
2024-05-19 10:38:38 +00:00
|
|
|
int o2 = active_o2(dive, dc, psample.time); // ... calculate po2 from depth and FiO2.
|
2021-08-18 21:38:15 +00:00
|
|
|
po2i = lrint(o2 * pamb_pressure); // (initial) po2 at start of segment
|
|
|
|
po2f = lrint(o2 * amb_presure); // (final) po2 at end of segment
|
|
|
|
}
|
|
|
|
po2 = (po2i + po2f) / 2;
|
|
|
|
}
|
|
|
|
return po2;
|
|
|
|
}
|
|
|
|
|
2018-11-11 10:33:11 +00:00
|
|
|
/* Calculate OTU for a dive - this only takes the first divecomputer into account.
|
|
|
|
Implement the protocol in Erik Baker's document "Oxygen Toxicity Calculations". This code
|
|
|
|
implements a third-order continuous approximation of Baker's Eq. 2 and enables OTU
|
|
|
|
calculation for rebreathers. Baker obtained his information from:
|
|
|
|
Comroe Jr. JH et al. (1945) Oxygen toxicity. J. Am. Med. Assoc. 128,710-717
|
|
|
|
Clark JM & CJ Lambertsen (1970) Pulmonary oxygen tolerance in man and derivation of pulmonary
|
|
|
|
oxygen tolerance curves. Inst. env. Med. Report 1-70, University of Pennsylvania, Philadelphia, USA. */
|
2024-06-23 12:20:59 +00:00
|
|
|
static int calculate_otu(const struct dive &dive)
|
2011-09-22 20:45:53 +00:00
|
|
|
{
|
|
|
|
double otu = 0.0;
|
2024-06-23 12:20:59 +00:00
|
|
|
const struct divecomputer *dc = &dive.dcs[0];
|
2024-05-19 10:38:38 +00:00
|
|
|
for (auto [psample, sample]: pairwise_range(dc->samples)) {
|
2011-09-22 20:45:53 +00:00
|
|
|
int t;
|
2018-11-11 10:33:11 +00:00
|
|
|
int po2i, po2f;
|
|
|
|
double pm;
|
2024-05-19 10:38:38 +00:00
|
|
|
t = sample.time.seconds - psample.time.seconds;
|
2021-08-18 21:38:15 +00:00
|
|
|
// if there is sensor data use sensor[0]
|
2024-05-19 10:38:38 +00:00
|
|
|
if ((dc->divemode == CCR || dc->divemode == PSCR) && sample.o2sensor[0].mbar) {
|
|
|
|
po2i = psample.o2sensor[0].mbar;
|
|
|
|
po2f = sample.o2sensor[0].mbar; // ... use data from the first o2 sensor
|
2012-12-21 01:42:10 +00:00
|
|
|
} else {
|
2024-05-19 10:38:38 +00:00
|
|
|
if (sample.setpoint.mbar > 0) {
|
|
|
|
po2f = std::min((int) sample.setpoint.mbar,
|
2024-06-23 12:20:59 +00:00
|
|
|
dive.depth_to_mbar(sample.depth.mm));
|
2024-05-19 10:38:38 +00:00
|
|
|
if (psample.setpoint.mbar > 0)
|
|
|
|
po2i = std::min((int) psample.setpoint.mbar,
|
2024-06-23 12:20:59 +00:00
|
|
|
dive.depth_to_mbar(psample.depth.mm));
|
2021-08-18 21:38:15 +00:00
|
|
|
else
|
|
|
|
po2i = po2f;
|
2018-11-11 10:33:11 +00:00
|
|
|
} else { // For OC and rebreather without o2 sensor/setpoint
|
2024-06-23 12:20:59 +00:00
|
|
|
double amb_presure = dive.depth_to_bar(sample.depth.mm);
|
|
|
|
double pamb_pressure = dive.depth_to_bar(psample.depth.mm);
|
2021-03-11 22:27:13 +00:00
|
|
|
if (dc->divemode == PSCR) {
|
2024-06-25 05:43:32 +00:00
|
|
|
po2i = pscr_o2(pamb_pressure, dive.get_gasmix_at_time(*dc, psample.time));
|
|
|
|
po2f = pscr_o2(amb_presure, dive.get_gasmix_at_time(*dc, sample.time));
|
2021-03-11 22:27:13 +00:00
|
|
|
} else {
|
2024-05-19 10:38:38 +00:00
|
|
|
int o2 = active_o2(dive, dc, psample.time); // ... calculate po2 from depth and FiO2.
|
2021-03-11 22:27:13 +00:00
|
|
|
po2i = lrint(o2 * pamb_pressure); // (initial) po2 at start of segment
|
|
|
|
po2f = lrint(o2 * amb_presure); // (final) po2 at end of segment
|
|
|
|
}
|
2018-11-11 10:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((po2i > 500) || (po2f > 500)) { // If PO2 in segment is above 500 mbar then calculate otu
|
|
|
|
if (po2i <= 500) { // For descent segment with po2i <= 500 mbar ..
|
|
|
|
t = t * (po2f - 500) / (po2f - po2i); // .. only consider part with PO2 > 500 mbar
|
|
|
|
po2i = 501; // Mostly important for the dive planner with long segments
|
|
|
|
} else {
|
|
|
|
if (po2f <= 500){
|
|
|
|
t = t * (po2i - 500) / (po2i - po2f); // For ascent segment with po2f <= 500 mbar ..
|
|
|
|
po2f = 501; // .. only consider part with PO2 > 500 mbar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pm = (po2f + po2i)/1000.0 - 1.0;
|
|
|
|
// This is a 3rd order continuous approximation of Baker's eq. 2, therefore Baker's eq. 1 is not used:
|
|
|
|
otu += t / 60.0 * pow(pm, 5.0/6.0) * (1.0 - 5.0 * (po2f - po2i) * (po2f - po2i) / 216000000.0 / (pm * pm));
|
2012-12-21 01:42:10 +00:00
|
|
|
}
|
2011-09-22 20:45:53 +00:00
|
|
|
}
|
2017-03-08 06:41:41 +00:00
|
|
|
return lrint(otu);
|
2011-09-22 20:45:53 +00:00
|
|
|
}
|
2019-06-04 18:41:31 +00:00
|
|
|
|
More accurate CNS calculations (following comments on github)
Update table of maximum oxygen exposure durations, used in CNS calulations.
This table shows the official NOAA maximum O2 exposure limits
(in seconds) for different PO2 values. It also gives
slope values for linear interpolation for intermediate PO2 values
between the tabulated PO2 values in the 1st column.
Top & bottom rows are inserted that are not in the NOAA table:
(1) For PO2 > 1.6 the same slope value as between
1.5 & 1.6 is used. This exptrapolation for PO2 > 1.6 likely
gives an underestimate above 1.6 but is better than the
value for PO2=1.6 (45 min). (2) The NOAA table only
tabulates values for PO2 >= 0.6. Since O2-uptake occurs down to
PO2=0.5, the same slope is used as for 0.7 > PO2 > 0.6.
This gives a conservative estimate for 0.6 > PO2 > 0.5. To
preserve the integer structure of the table, all slopes are
given as slope*10: divide by 10 to get the valid slope.
The columns below are:
po2 (mbar), Maximum Single Exposure (seconds), single_slope,
Maximum 24 hour Exposure (seconds), 24h_slope */
Then update Calculations of the CNS for a single dive -
this only takes the first divecomputer into account.
The previous version of the code did a table lookup and
used the max O2 exposure for the next-higher PO2 category.
This gave a shorter max O2 exposure time and a higher CNS
contribution for a specific dive segment, resulting in a
slightly conservative value of CNS, often some 2 - 3 % too high.
This code does an interpolation for PO2 values inbetween
PO2 entries in the lookup table and therefore results in a more
accurate maximum O2 exposure time for that PO2.
The maximum O2 exposure duration for each segment
is also calculated based on the mean depth of the two
samples (start & end) that define each segment. The CNS
contribution of each segment is found by dividing the
time duration of the segment by its maximum exposure duration.
The contributions of all segments of the dive are summed to
get the total CNS% value. This is a partial implementation
of the proposals in Erik Baker's document "Oxygen Toxicity Calculations" */
Overall, this PR does not radically alter the existing CNS calculation,
it only makes it more accurate and more consistent by doing
interpolation and by using mean segment depth to find PO2.
Signed-off-by: willemferguson <willemferguson@zoology.up.ac.za>
2018-11-10 11:49:52 +00:00
|
|
|
/* Calculate the CNS for a single dive - this only takes the first divecomputer into account.
|
|
|
|
The CNS contributions are summed for dive segments defined by samples. The maximum O2 exposure duration for each
|
|
|
|
segment is calculated based on the mean depth of the two samples (start & end) that define each segment. The CNS
|
|
|
|
contribution of each segment is found by dividing the time duration of the segment by its maximum exposure duration.
|
|
|
|
The contributions of all segments of the dive are summed to get the total CNS% value. This is a partial implementation
|
|
|
|
of the proposals in Erik Baker's document "Oxygen Toxicity Calculations" using fixed-depth calculations for the mean
|
|
|
|
po2 for each segment. Empirical testing showed that, for large changes in depth, the cns calculation for the mean po2
|
|
|
|
value is extremely close, if not identical to the additive calculations for 0.1 bar increments in po2 from the start
|
|
|
|
to the end of the segment, assuming a constant rate of change in po2 (i.e. depth) with time. */
|
2024-06-18 20:23:57 +00:00
|
|
|
static double calculate_cns_dive(const struct dive &dive)
|
2013-04-09 17:25:21 +00:00
|
|
|
{
|
2024-06-18 20:23:57 +00:00
|
|
|
const struct divecomputer *dc = &dive.dcs[0];
|
2017-10-02 18:35:21 +00:00
|
|
|
double cns = 0.0;
|
2019-08-17 19:46:00 +00:00
|
|
|
double rate;
|
More accurate CNS calculations (following comments on github)
Update table of maximum oxygen exposure durations, used in CNS calulations.
This table shows the official NOAA maximum O2 exposure limits
(in seconds) for different PO2 values. It also gives
slope values for linear interpolation for intermediate PO2 values
between the tabulated PO2 values in the 1st column.
Top & bottom rows are inserted that are not in the NOAA table:
(1) For PO2 > 1.6 the same slope value as between
1.5 & 1.6 is used. This exptrapolation for PO2 > 1.6 likely
gives an underestimate above 1.6 but is better than the
value for PO2=1.6 (45 min). (2) The NOAA table only
tabulates values for PO2 >= 0.6. Since O2-uptake occurs down to
PO2=0.5, the same slope is used as for 0.7 > PO2 > 0.6.
This gives a conservative estimate for 0.6 > PO2 > 0.5. To
preserve the integer structure of the table, all slopes are
given as slope*10: divide by 10 to get the valid slope.
The columns below are:
po2 (mbar), Maximum Single Exposure (seconds), single_slope,
Maximum 24 hour Exposure (seconds), 24h_slope */
Then update Calculations of the CNS for a single dive -
this only takes the first divecomputer into account.
The previous version of the code did a table lookup and
used the max O2 exposure for the next-higher PO2 category.
This gave a shorter max O2 exposure time and a higher CNS
contribution for a specific dive segment, resulting in a
slightly conservative value of CNS, often some 2 - 3 % too high.
This code does an interpolation for PO2 values inbetween
PO2 entries in the lookup table and therefore results in a more
accurate maximum O2 exposure time for that PO2.
The maximum O2 exposure duration for each segment
is also calculated based on the mean depth of the two
samples (start & end) that define each segment. The CNS
contribution of each segment is found by dividing the
time duration of the segment by its maximum exposure duration.
The contributions of all segments of the dive are summed to
get the total CNS% value. This is a partial implementation
of the proposals in Erik Baker's document "Oxygen Toxicity Calculations" */
Overall, this PR does not radically alter the existing CNS calculation,
it only makes it more accurate and more consistent by doing
interpolation and by using mean segment depth to find PO2.
Signed-off-by: willemferguson <willemferguson@zoology.up.ac.za>
2018-11-10 11:49:52 +00:00
|
|
|
/* Calculate the CNS for each sample in this dive and sum them */
|
2024-05-19 10:38:38 +00:00
|
|
|
for (auto [psample, sample]: pairwise_range(dc->samples)) {
|
|
|
|
int t = sample.time.seconds - psample.time.seconds;
|
2024-06-23 12:20:59 +00:00
|
|
|
int po2 = get_sample_o2(dive, dc, sample, psample);
|
More accurate CNS calculations (following comments on github)
Update table of maximum oxygen exposure durations, used in CNS calulations.
This table shows the official NOAA maximum O2 exposure limits
(in seconds) for different PO2 values. It also gives
slope values for linear interpolation for intermediate PO2 values
between the tabulated PO2 values in the 1st column.
Top & bottom rows are inserted that are not in the NOAA table:
(1) For PO2 > 1.6 the same slope value as between
1.5 & 1.6 is used. This exptrapolation for PO2 > 1.6 likely
gives an underestimate above 1.6 but is better than the
value for PO2=1.6 (45 min). (2) The NOAA table only
tabulates values for PO2 >= 0.6. Since O2-uptake occurs down to
PO2=0.5, the same slope is used as for 0.7 > PO2 > 0.6.
This gives a conservative estimate for 0.6 > PO2 > 0.5. To
preserve the integer structure of the table, all slopes are
given as slope*10: divide by 10 to get the valid slope.
The columns below are:
po2 (mbar), Maximum Single Exposure (seconds), single_slope,
Maximum 24 hour Exposure (seconds), 24h_slope */
Then update Calculations of the CNS for a single dive -
this only takes the first divecomputer into account.
The previous version of the code did a table lookup and
used the max O2 exposure for the next-higher PO2 category.
This gave a shorter max O2 exposure time and a higher CNS
contribution for a specific dive segment, resulting in a
slightly conservative value of CNS, often some 2 - 3 % too high.
This code does an interpolation for PO2 values inbetween
PO2 entries in the lookup table and therefore results in a more
accurate maximum O2 exposure time for that PO2.
The maximum O2 exposure duration for each segment
is also calculated based on the mean depth of the two
samples (start & end) that define each segment. The CNS
contribution of each segment is found by dividing the
time duration of the segment by its maximum exposure duration.
The contributions of all segments of the dive are summed to
get the total CNS% value. This is a partial implementation
of the proposals in Erik Baker's document "Oxygen Toxicity Calculations" */
Overall, this PR does not radically alter the existing CNS calculation,
it only makes it more accurate and more consistent by doing
interpolation and by using mean segment depth to find PO2.
Signed-off-by: willemferguson <willemferguson@zoology.up.ac.za>
2018-11-10 11:49:52 +00:00
|
|
|
/* Don't increase CNS when po2 below 500 matm */
|
2021-08-18 21:38:15 +00:00
|
|
|
if (po2 <= 500)
|
2014-06-17 18:01:16 +00:00
|
|
|
continue;
|
2019-08-17 19:46:00 +00:00
|
|
|
|
|
|
|
// This formula is the result of fitting two lines to the Log of the NOAA CNS table
|
2021-08-18 21:38:15 +00:00
|
|
|
rate = po2 <= 1500 ? exp(-11.7853 + 0.00193873 * po2) : exp(-23.6349 + 0.00980829 * po2);
|
2019-08-28 15:53:28 +00:00
|
|
|
cns += (double) t * rate * 100.0;
|
2013-04-09 17:25:21 +00:00
|
|
|
}
|
2017-10-02 18:35:21 +00:00
|
|
|
return cns;
|
|
|
|
}
|
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
/* this only gets called if dive.maxcns == 0 which means we know that
|
2017-10-02 18:35:21 +00:00
|
|
|
* none of the divecomputers has tracked any CNS for us
|
|
|
|
* so we calculated it "by hand" */
|
2024-06-23 12:20:59 +00:00
|
|
|
int dive_table::calculate_cns(struct dive &dive) const
|
2017-10-02 18:35:21 +00:00
|
|
|
{
|
|
|
|
double cns = 0.0;
|
|
|
|
timestamp_t last_starttime, last_endtime = 0;
|
|
|
|
|
|
|
|
/* shortcut */
|
2024-06-23 12:20:59 +00:00
|
|
|
if (dive.cns)
|
|
|
|
return dive.cns;
|
2017-10-02 18:35:21 +00:00
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
size_t divenr = get_idx(&dive);
|
2024-06-18 20:23:57 +00:00
|
|
|
int nr_dives = static_cast<int>(size());
|
2024-06-07 08:25:09 +00:00
|
|
|
int i = divenr != std::string::npos ? static_cast<int>(divenr)
|
2024-06-18 20:23:57 +00:00
|
|
|
: nr_dives;
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-06-18 20:23:57 +00:00
|
|
|
if (static_cast<size_t>(i) < size())
|
|
|
|
printf("\n\n*** CNS for dive #%d %d\n", i, ()[i]->number);
|
2018-05-13 22:29:54 +00:00
|
|
|
else
|
2017-10-02 18:35:21 +00:00
|
|
|
printf("\n\n*** CNS for dive #%d\n", i);
|
|
|
|
#endif
|
|
|
|
/* Look at next dive in dive list table and correct i when needed */
|
2024-06-07 08:25:09 +00:00
|
|
|
while (i < nr_dives - 1) {
|
2024-06-23 12:20:59 +00:00
|
|
|
if ((*this)[i]->when > dive.when)
|
2017-10-02 18:35:21 +00:00
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Look at previous dive in dive list table and correct i when needed */
|
|
|
|
while (i > 0) {
|
2024-06-23 12:20:59 +00:00
|
|
|
if ((*this)[i - 1]->when < dive.when)
|
2017-10-02 18:35:21 +00:00
|
|
|
break;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Dive number corrected to #%d\n", i);
|
|
|
|
#endif
|
2024-06-23 12:20:59 +00:00
|
|
|
last_starttime = dive.when;
|
2017-10-02 18:35:21 +00:00
|
|
|
/* Walk backwards to check previous dives - how far do we need to go back? */
|
|
|
|
while (i--) {
|
2024-06-07 08:25:09 +00:00
|
|
|
if (static_cast<size_t>(i) == divenr && i > 0)
|
2017-10-02 18:35:21 +00:00
|
|
|
i--;
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-06-18 20:23:57 +00:00
|
|
|
printf("Check if dive #%d %d has to be considered as prev dive: ", i, (*this)[i]->number);
|
2017-10-02 18:35:21 +00:00
|
|
|
#endif
|
2024-06-18 20:23:57 +00:00
|
|
|
const struct dive &pdive = *(*this)[i];
|
2017-10-02 18:35:21 +00:00
|
|
|
/* we don't want to mix dives from different trips as we keep looking
|
|
|
|
* for how far back we need to go */
|
2024-06-23 12:20:59 +00:00
|
|
|
if (dive.divetrip && pdive.divetrip != dive.divetrip) {
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2018-05-13 22:29:54 +00:00
|
|
|
printf("No - other dive trip\n");
|
2017-10-02 18:35:21 +00:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-23 12:20:59 +00:00
|
|
|
if (pdive.when >= dive.when || pdive.endtime() + 12 * 60 * 60 < last_starttime) {
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("No\n");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2024-06-18 20:23:57 +00:00
|
|
|
last_starttime = pdive.when;
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Yes\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* Walk forward and add dives and surface intervals to CNS */
|
2024-06-07 08:25:09 +00:00
|
|
|
while (++i < nr_dives) {
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-06-18 20:23:57 +00:00
|
|
|
printf("Check if dive #%d %d will be really added to CNS calc: ", i, (*this)[i]->number);
|
2017-10-02 18:35:21 +00:00
|
|
|
#endif
|
2024-06-18 20:23:57 +00:00
|
|
|
const struct dive &pdive = *(*this)[i];
|
2017-10-02 18:35:21 +00:00
|
|
|
/* again skip dives from different trips */
|
2024-06-23 12:20:59 +00:00
|
|
|
if (dive.divetrip && dive.divetrip != pdive.divetrip) {
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2018-05-13 22:29:54 +00:00
|
|
|
printf("No - other dive trip\n");
|
2017-10-02 18:35:21 +00:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Don't add future dives */
|
2024-06-23 12:20:59 +00:00
|
|
|
if (pdive.when >= dive.when) {
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("No - future or same dive\n");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Don't add the copy of the dive itself */
|
2024-06-07 08:25:09 +00:00
|
|
|
if (static_cast<size_t>(i) == divenr) {
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("No - copy of dive\n");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Yes\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* CNS reduced with 90min halftime during surface interval */
|
2018-05-13 22:29:54 +00:00
|
|
|
if (last_endtime)
|
2024-06-18 20:23:57 +00:00
|
|
|
cns /= pow(2, (pdive.when - last_endtime) / (90.0 * 60.0));
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("CNS after surface interval: %f\n", cns);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cns += calculate_cns_dive(pdive);
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("CNS after previous dive: %f\n", cns);
|
|
|
|
#endif
|
|
|
|
|
2024-06-18 20:23:57 +00:00
|
|
|
last_starttime = pdive.when;
|
|
|
|
last_endtime = pdive.endtime();
|
2017-10-02 18:35:21 +00:00
|
|
|
}
|
2018-05-13 22:29:54 +00:00
|
|
|
|
2017-10-02 18:35:21 +00:00
|
|
|
/* CNS reduced with 90min halftime during surface interval */
|
|
|
|
if (last_endtime)
|
2024-06-23 12:20:59 +00:00
|
|
|
cns /= pow(2, (dive.when - last_endtime) / (90.0 * 60.0));
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("CNS after last surface interval: %f\n", cns);
|
|
|
|
#endif
|
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
cns += calculate_cns_dive(dive);
|
2017-10-02 18:35:21 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("CNS after dive: %f\n", cns);
|
|
|
|
#endif
|
|
|
|
|
2013-04-09 17:25:21 +00:00
|
|
|
/* save calculated cns in dive struct */
|
2024-06-23 12:20:59 +00:00
|
|
|
dive.cns = lrint(cns);
|
|
|
|
return dive.cns;
|
2013-04-09 17:25:21 +00:00
|
|
|
}
|
2024-06-24 17:17:43 +00:00
|
|
|
|
2011-09-19 23:11:38 +00:00
|
|
|
/*
|
|
|
|
* Return air usage (in liters).
|
|
|
|
*/
|
2024-06-23 12:20:59 +00:00
|
|
|
static double calculate_airuse(const struct dive &dive)
|
2011-09-19 23:11:38 +00:00
|
|
|
{
|
2013-02-25 23:23:16 +00:00
|
|
|
int airuse = 0;
|
2011-09-19 23:11:38 +00:00
|
|
|
|
2021-11-14 02:55:33 +00:00
|
|
|
// SAC for a CCR dive does not make sense.
|
2024-06-23 12:20:59 +00:00
|
|
|
if (dive.dcs[0].divemode == CCR)
|
2021-11-14 02:55:33 +00:00
|
|
|
return 0.0;
|
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
for (auto [i, cyl]: enumerated_range(dive.cylinders)) {
|
2011-11-09 15:51:00 +00:00
|
|
|
pressure_t start, end;
|
2011-09-19 23:11:38 +00:00
|
|
|
|
2024-05-28 19:31:11 +00:00
|
|
|
start = cyl.start.mbar ? cyl.start : cyl.sample_start;
|
|
|
|
end = cyl.end.mbar ? cyl.end : cyl.sample_end;
|
2018-05-17 19:25:57 +00:00
|
|
|
if (!end.mbar || start.mbar <= end.mbar) {
|
|
|
|
// If a cylinder is used but we do not have info on amout of gas used
|
|
|
|
// better not pretend we know the total gas use.
|
|
|
|
// Eventually, logic should be fixed to compute average depth and total time
|
|
|
|
// for those segments where cylinders with known pressure drop are breathed from.
|
2024-06-25 05:43:32 +00:00
|
|
|
if (dive.is_cylinder_used(i))
|
2018-05-17 19:25:57 +00:00
|
|
|
return 0.0;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-19 23:11:38 +00:00
|
|
|
|
2024-07-02 09:12:27 +00:00
|
|
|
// TODO: implement subtraction for units.h types
|
|
|
|
airuse += cyl.gas_volume(start).mliter - cyl.gas_volume(end).mliter;
|
2011-09-19 23:11:38 +00:00
|
|
|
}
|
2013-02-25 23:23:16 +00:00
|
|
|
return airuse / 1000.0;
|
2011-09-19 23:11:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 06:48:07 +00:00
|
|
|
/* this only uses the first divecomputer to calculate the SAC rate */
|
2024-06-23 12:20:59 +00:00
|
|
|
static int calculate_sac(const struct dive &dive)
|
2011-09-19 20:32:10 +00:00
|
|
|
{
|
2024-06-23 12:20:59 +00:00
|
|
|
const struct divecomputer *dc = &dive.dcs[0];
|
2011-09-19 23:11:38 +00:00
|
|
|
double airuse, pressure, sac;
|
2013-02-24 18:50:18 +00:00
|
|
|
int duration, meandepth;
|
2011-09-19 23:11:38 +00:00
|
|
|
|
|
|
|
airuse = calculate_airuse(dive);
|
|
|
|
if (!airuse)
|
2011-11-02 02:56:14 +00:00
|
|
|
return 0;
|
2013-02-24 18:50:18 +00:00
|
|
|
|
2013-02-24 19:39:51 +00:00
|
|
|
duration = dc->duration.seconds;
|
2013-02-24 18:50:18 +00:00
|
|
|
if (!duration)
|
|
|
|
return 0;
|
2013-02-08 06:48:07 +00:00
|
|
|
|
2013-02-24 19:39:51 +00:00
|
|
|
meandepth = dc->meandepth.mm;
|
|
|
|
if (!meandepth)
|
|
|
|
return 0;
|
|
|
|
|
Fix up SAC calculations for ATM/bar confusion
We even documented that we did SAC in bar*l/min, but the "S" in SAC
stands for "Surface". So we should normalize SAC rate to surface
pressure, not one bar.
It's a tiny 1% difference, and doesn't actually matter in practice, but
it's noticeable when you want to explicitly test for SAC-rate by
creating a test-dive that averages exactly 10m. Suddenly you don't get
the round numbers you expect.
[ Side note: 10m is not _exactly_ one extra atmosphere according to our
calculations, but it's darn close in sea water: the standard salinity
of 1.03 kg/l together with the standard acceleration of 9.81m/s^2
gives an additional pressure of 1.01 bar, which is within a fraction
of a percent of one ATM.
Of course, divers have likely chosen that value exactly for the math
to come out that way, since the true average salinity of seawater is
actually slightly lower ]
So here's a few test-dives, along with the SAC rate fixup to make them
look right.
(There's also a one-liner to dive.c that makes the duration come out
right if the last sample has a non-zero depth, and the previous sample
did not: one of my original test-dives did the "average 10m depth" by
starting at 0 and ending at 20m, and dive.c got a tiny bit confused
about that ;)
[ The rationale for me testing our SAC rate calculations in the first
place was that on snorkkeli.net user "Poltsi" reported that our SAC rate
calculations differ from the ones that Suunto DM4 reports. So I wanted
to verify that we did things right.
Note that Poltsi reported differences larger than the difference of
BAR/ATM, so this is not the cause. I'll continue to look at this. ]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-24 18:01:18 +00:00
|
|
|
/* Mean pressure in ATM (SAC calculations are in atm*l/min) */
|
2024-06-23 12:20:59 +00:00
|
|
|
pressure = dive.depth_to_atm(meandepth);
|
2011-11-21 21:23:13 +00:00
|
|
|
sac = airuse / pressure * 60 / duration;
|
2011-09-19 23:11:38 +00:00
|
|
|
|
|
|
|
/* milliliters per minute.. */
|
2017-03-09 16:07:30 +00:00
|
|
|
return lrint(sac * 1000);
|
2011-09-20 02:13:36 +00:00
|
|
|
}
|
2011-09-19 23:11:38 +00:00
|
|
|
|
2013-01-04 04:45:20 +00:00
|
|
|
/* for now we do this based on the first divecomputer */
|
2024-06-23 12:20:59 +00:00
|
|
|
static void add_dive_to_deco(struct deco_state *ds, const struct dive &dive, bool in_planner)
|
2013-01-04 04:45:20 +00:00
|
|
|
{
|
2024-06-23 12:20:59 +00:00
|
|
|
const struct divecomputer *dc = &dive.dcs[0];
|
2017-07-28 17:35:25 +00:00
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
gasmix_loop loop(dive, dive.dcs[0]);
|
|
|
|
divemode_loop loop_d(dive.dcs[0]);
|
2024-05-19 10:38:38 +00:00
|
|
|
for (auto [psample, sample]: pairwise_range(dc->samples)) {
|
|
|
|
int t0 = psample.time.seconds;
|
|
|
|
int t1 = sample.time.seconds;
|
2013-01-04 04:45:20 +00:00
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = t0; j < t1; j++) {
|
2024-05-19 10:38:38 +00:00
|
|
|
int depth = interpolate(psample.depth.mm, sample.depth.mm, j - t0, t1 - t0);
|
2024-05-25 06:16:57 +00:00
|
|
|
auto gasmix = loop.next(j);
|
2024-06-23 12:20:59 +00:00
|
|
|
add_segment(ds, dive.depth_to_bar(depth), gasmix, 1, sample.setpoint.mbar,
|
|
|
|
loop_d.next(j), dive.sac,
|
2021-02-12 16:39:46 +00:00
|
|
|
in_planner);
|
2013-01-04 04:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* take into account previous dives until there is a 48h gap between dives */
|
2017-10-02 09:17:10 +00:00
|
|
|
/* return last surface time before this dive or dummy value of 48h */
|
|
|
|
/* return negative surface time if dives are overlapping */
|
2017-11-22 19:42:33 +00:00
|
|
|
/* The place you call this function is likely the place where you want
|
|
|
|
* to create the deco_state */
|
2024-06-18 20:23:57 +00:00
|
|
|
int dive_table::init_decompression(struct deco_state *ds, const struct dive *dive, bool in_planner) const
|
2013-01-04 04:45:20 +00:00
|
|
|
{
|
2017-10-02 09:17:10 +00:00
|
|
|
int surface_time = 48 * 60 * 60;
|
|
|
|
timestamp_t last_endtime = 0, last_starttime = 0;
|
2014-01-15 18:54:41 +00:00
|
|
|
bool deco_init = false;
|
2015-08-31 21:25:28 +00:00
|
|
|
double surface_pressure;
|
2013-01-04 04:45:20 +00:00
|
|
|
|
|
|
|
if (!dive)
|
2016-12-15 22:22:54 +00:00
|
|
|
return false;
|
2014-03-09 00:47:06 +00:00
|
|
|
|
2024-06-18 20:23:57 +00:00
|
|
|
int nr_dives = static_cast<int>(size());
|
|
|
|
size_t divenr = get_idx(dive);
|
2024-06-07 08:25:09 +00:00
|
|
|
int i = divenr != std::string::npos ? static_cast<int>(divenr)
|
2024-06-18 20:23:57 +00:00
|
|
|
: nr_dives;
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-06-07 08:25:09 +00:00
|
|
|
if (i < dive_table.nr)
|
2024-06-18 20:23:57 +00:00
|
|
|
printf("\n\n*** Init deco for dive #%d %d\n", i, (*this)[i]->number);
|
2017-10-02 09:17:10 +00:00
|
|
|
else
|
|
|
|
printf("\n\n*** Init deco for dive #%d\n", i);
|
|
|
|
#endif
|
|
|
|
/* Look at next dive in dive list table and correct i when needed */
|
2024-06-07 08:25:09 +00:00
|
|
|
while (i + 1 < nr_dives) {
|
2024-06-18 20:23:57 +00:00
|
|
|
if ((*this)[i]->when > dive->when)
|
2017-10-02 09:17:10 +00:00
|
|
|
break;
|
2014-07-08 19:42:17 +00:00
|
|
|
i++;
|
|
|
|
}
|
2017-10-02 09:17:10 +00:00
|
|
|
/* Look at previous dive in dive list table and correct i when needed */
|
|
|
|
while (i > 0) {
|
2024-06-18 20:23:57 +00:00
|
|
|
if ((*this)[i - 1]->when < dive->when)
|
2017-10-02 09:17:10 +00:00
|
|
|
break;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Dive number corrected to #%d\n", i);
|
|
|
|
#endif
|
|
|
|
last_starttime = dive->when;
|
|
|
|
/* Walk backwards to check previous dives - how far do we need to go back? */
|
2014-07-08 19:42:17 +00:00
|
|
|
while (i--) {
|
2024-06-07 08:25:09 +00:00
|
|
|
if (static_cast<size_t>(i) == divenr && i > 0)
|
2017-10-02 09:17:10 +00:00
|
|
|
i--;
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-06-18 20:23:57 +00:00
|
|
|
printf("Check if dive #%d %d has to be considered as prev dive: ", i, (*this)[i]->number);
|
2017-10-02 09:17:10 +00:00
|
|
|
#endif
|
2024-06-18 20:23:57 +00:00
|
|
|
const struct dive &pdive = *(*this)[i];
|
2013-01-14 03:37:41 +00:00
|
|
|
/* we don't want to mix dives from different trips as we keep looking
|
|
|
|
* for how far back we need to go */
|
2024-06-18 20:23:57 +00:00
|
|
|
if (dive->divetrip && pdive.divetrip != dive->divetrip) {
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2018-05-13 22:29:54 +00:00
|
|
|
printf("No - other dive trip\n");
|
2017-10-02 09:17:10 +00:00
|
|
|
#endif
|
2013-01-14 03:37:41 +00:00
|
|
|
continue;
|
2017-10-02 09:17:10 +00:00
|
|
|
}
|
2024-06-18 20:23:57 +00:00
|
|
|
if (pdive.when >= dive->when || pdive.endtime() + 48 * 60 * 60 < last_starttime) {
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("No\n");
|
|
|
|
#endif
|
2013-01-04 04:45:20 +00:00
|
|
|
break;
|
2017-10-02 09:17:10 +00:00
|
|
|
}
|
2024-06-18 20:23:57 +00:00
|
|
|
last_starttime = pdive.when;
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Yes\n");
|
|
|
|
#endif
|
2013-01-04 04:45:20 +00:00
|
|
|
}
|
2017-10-02 09:17:10 +00:00
|
|
|
/* Walk forward an add dives and surface intervals to deco */
|
2024-06-07 08:25:09 +00:00
|
|
|
while (++i < nr_dives) {
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-06-18 20:23:57 +00:00
|
|
|
printf("Check if dive #%d %d will be really added to deco calc: ", i, (*this)[i]->number);
|
2017-10-02 09:17:10 +00:00
|
|
|
#endif
|
2024-06-18 20:23:57 +00:00
|
|
|
const struct dive &pdive = *(*this)[i];
|
2013-01-14 03:37:41 +00:00
|
|
|
/* again skip dives from different trips */
|
2024-06-18 20:23:57 +00:00
|
|
|
if (dive->divetrip && dive->divetrip != pdive.divetrip) {
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2018-05-13 22:29:54 +00:00
|
|
|
printf("No - other dive trip\n");
|
2017-10-02 09:17:10 +00:00
|
|
|
#endif
|
2013-01-14 03:37:41 +00:00
|
|
|
continue;
|
2017-10-02 09:17:10 +00:00
|
|
|
}
|
2016-06-08 19:51:02 +00:00
|
|
|
/* Don't add future dives */
|
2024-06-18 20:23:57 +00:00
|
|
|
if (pdive.when >= dive->when) {
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("No - future or same dive\n");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Don't add the copy of the dive itself */
|
2024-06-07 08:25:09 +00:00
|
|
|
if (static_cast<size_t>(i) == divenr) {
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("No - copy of dive\n");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Yes\n");
|
|
|
|
#endif
|
|
|
|
|
2024-06-30 15:26:12 +00:00
|
|
|
surface_pressure = pdive.get_surface_pressure().mbar / 1000.0;
|
2017-10-02 09:17:10 +00:00
|
|
|
/* Is it the first dive we add? */
|
2013-01-04 04:45:20 +00:00
|
|
|
if (!deco_init) {
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Init deco\n");
|
|
|
|
#endif
|
2021-02-12 16:52:31 +00:00
|
|
|
clear_deco(ds, surface_pressure, in_planner);
|
2014-01-15 18:54:41 +00:00
|
|
|
deco_init = true;
|
2013-01-04 19:56:43 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2017-10-02 09:17:10 +00:00
|
|
|
printf("Tissues after init:\n");
|
2018-02-11 21:23:59 +00:00
|
|
|
dump_tissues(ds);
|
2013-01-04 04:45:20 +00:00
|
|
|
#endif
|
2019-05-15 14:42:14 +00:00
|
|
|
} else {
|
2024-06-18 20:23:57 +00:00
|
|
|
surface_time = pdive.when - last_endtime;
|
2017-10-02 09:17:10 +00:00
|
|
|
if (surface_time < 0) {
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
|
|
|
printf("Exit because surface intervall is %d\n", surface_time);
|
|
|
|
#endif
|
|
|
|
return surface_time;
|
|
|
|
}
|
2024-05-01 16:11:35 +00:00
|
|
|
add_segment(ds, surface_pressure, gasmix_air, surface_time, 0, OC, prefs.decosac, in_planner);
|
2013-01-14 03:37:41 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-05-01 09:01:06 +00:00
|
|
|
printf("Tissues after surface intervall of %d:%02u:\n", FRACTION_TUPLE(surface_time, 60));
|
2017-11-22 19:42:33 +00:00
|
|
|
dump_tissues(ds);
|
2013-01-14 03:37:41 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-10-02 09:17:10 +00:00
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
add_dive_to_deco(ds, pdive, in_planner);
|
2017-10-02 09:17:10 +00:00
|
|
|
|
2024-06-18 20:23:57 +00:00
|
|
|
last_starttime = pdive.when;
|
|
|
|
last_endtime = pdive.endtime();
|
2017-11-22 19:42:33 +00:00
|
|
|
clear_vpmb_state(ds);
|
2016-06-08 19:51:02 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-06-18 20:23:57 +00:00
|
|
|
printf("Tissues after added dive #%d:\n", pdive.number);
|
2017-11-22 19:42:33 +00:00
|
|
|
dump_tissues(ds);
|
2016-06-08 19:51:02 +00:00
|
|
|
#endif
|
2013-01-14 03:37:41 +00:00
|
|
|
}
|
2017-10-02 09:17:10 +00:00
|
|
|
|
2024-06-30 15:26:12 +00:00
|
|
|
surface_pressure = dive->get_surface_pressure().mbar / 1000.0;
|
2017-10-02 09:17:10 +00:00
|
|
|
/* We don't have had a previous dive at all? */
|
|
|
|
if (!deco_init) {
|
|
|
|
#if DECO_CALC_DEBUG & 2
|
2018-05-13 22:29:54 +00:00
|
|
|
printf("Init deco\n");
|
2017-10-02 09:17:10 +00:00
|
|
|
#endif
|
2021-02-12 16:52:31 +00:00
|
|
|
clear_deco(ds, surface_pressure, in_planner);
|
2013-01-04 19:56:43 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2017-10-02 09:17:10 +00:00
|
|
|
printf("Tissues after no previous dive, surface time set to 48h:\n");
|
2017-11-22 19:42:33 +00:00
|
|
|
dump_tissues(ds);
|
2013-01-04 04:45:20 +00:00
|
|
|
#endif
|
2019-05-15 14:42:14 +00:00
|
|
|
} else {
|
2017-10-02 09:17:10 +00:00
|
|
|
surface_time = dive->when - last_endtime;
|
|
|
|
if (surface_time < 0) {
|
2013-01-04 19:56:43 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2017-10-02 09:17:10 +00:00
|
|
|
printf("Exit because surface intervall is %d\n", surface_time);
|
|
|
|
#endif
|
|
|
|
return surface_time;
|
|
|
|
}
|
2024-05-01 16:11:35 +00:00
|
|
|
add_segment(ds, surface_pressure, gasmix_air, surface_time, 0, OC, prefs.decosac, in_planner);
|
2017-10-02 09:17:10 +00:00
|
|
|
#if DECO_CALC_DEBUG & 2
|
2024-05-01 09:01:06 +00:00
|
|
|
printf("Tissues after surface intervall of %d:%02u:\n", FRACTION_TUPLE(surface_time, 60));
|
2017-11-22 19:42:33 +00:00
|
|
|
dump_tissues(ds);
|
2013-01-04 04:45:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-10-02 09:17:10 +00:00
|
|
|
|
2016-12-15 22:22:54 +00:00
|
|
|
// I do not dare to remove this call. We don't need the result but it might have side effects. Bummer.
|
2021-02-12 17:19:24 +00:00
|
|
|
tissue_tolerance_calc(ds, dive, surface_pressure, in_planner);
|
2017-01-03 14:14:42 +00:00
|
|
|
return surface_time;
|
2013-01-04 04:45:20 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 12:20:59 +00:00
|
|
|
void dive_table::update_cylinder_related_info(struct dive &dive) const
|
2011-11-13 17:29:07 +00:00
|
|
|
{
|
2024-06-23 12:20:59 +00:00
|
|
|
dive.sac = calculate_sac(dive);
|
|
|
|
dive.otu = calculate_otu(dive);
|
|
|
|
if (dive.maxcns == 0)
|
|
|
|
dive.maxcns = calculate_cns(dive);
|
2011-11-13 17:29:07 +00:00
|
|
|
}
|
|
|
|
|
2024-05-27 15:09:48 +00:00
|
|
|
/* Compare list of dive computers by model name */
|
|
|
|
static int comp_dc(const struct dive *d1, const struct dive *d2)
|
2019-05-15 09:48:41 +00:00
|
|
|
{
|
2024-05-27 15:09:48 +00:00
|
|
|
auto it1 = d1->dcs.begin();
|
|
|
|
auto it2 = d2->dcs.begin();
|
|
|
|
while (it1 != d1->dcs.end() || it2 != d2->dcs.end()) {
|
|
|
|
if (it1 == d1->dcs.end())
|
2019-05-15 09:48:41 +00:00
|
|
|
return -1;
|
2024-05-27 15:09:48 +00:00
|
|
|
if (it2 == d2->dcs.end())
|
2019-05-15 09:48:41 +00:00
|
|
|
return 1;
|
2024-05-27 15:09:48 +00:00
|
|
|
int cmp = it1->model.compare(it2->model);
|
|
|
|
if (cmp != 0)
|
2019-05-15 09:48:41 +00:00
|
|
|
return cmp;
|
2024-05-27 15:09:48 +00:00
|
|
|
++it1;
|
|
|
|
++it2;
|
2019-05-15 09:48:41 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-24 08:57:14 +00:00
|
|
|
/* This function defines the sort ordering of dives. The core
|
|
|
|
* and the UI models should use the same sort function, which
|
|
|
|
* should be stable. This is not crucial at the moment, as the
|
|
|
|
* indices in core and UI are independent, but ultimately we
|
|
|
|
* probably want to unify the models.
|
|
|
|
* After editing a key used in this sort-function, the order of
|
|
|
|
* the dives must be re-astablished.
|
|
|
|
* Currently, this does a lexicographic sort on the
|
2019-05-11 06:46:08 +00:00
|
|
|
* (start-time, trip-time, number, id) tuple.
|
2018-11-24 08:57:14 +00:00
|
|
|
* trip-time is defined such that dives that do not belong to
|
|
|
|
* a trip are sorted *after* dives that do. Thus, in the default
|
|
|
|
* chronologically-descending sort order, they are shown *before*.
|
2024-05-19 10:38:38 +00:00
|
|
|
* "id" is a stable, strictly increasing unique number, which
|
|
|
|
* is generated when a dive is added to the system.
|
2018-11-24 08:57:14 +00:00
|
|
|
* We might also consider sorting by end-time and other criteria,
|
2024-05-19 10:38:38 +00:00
|
|
|
* but see the caveat above (editing means reordering of the dives).
|
2018-11-24 08:57:14 +00:00
|
|
|
*/
|
2024-06-07 08:25:09 +00:00
|
|
|
int comp_dives(const struct dive &a, const struct dive &b)
|
2018-11-24 09:01:03 +00:00
|
|
|
{
|
2019-05-15 09:48:41 +00:00
|
|
|
int cmp;
|
2024-06-07 08:25:09 +00:00
|
|
|
if (&a == &b)
|
2024-06-01 20:05:57 +00:00
|
|
|
return 0; /* reflexivity */
|
2024-06-07 08:25:09 +00:00
|
|
|
if (a.when < b.when)
|
2018-11-24 08:57:14 +00:00
|
|
|
return -1;
|
2024-06-07 08:25:09 +00:00
|
|
|
if (a.when > b.when)
|
2018-11-24 08:57:14 +00:00
|
|
|
return 1;
|
2024-06-07 08:25:09 +00:00
|
|
|
if (a.divetrip != b.divetrip) {
|
|
|
|
if (!b.divetrip)
|
2018-11-24 08:57:14 +00:00
|
|
|
return -1;
|
2024-06-07 08:25:09 +00:00
|
|
|
if (!a.divetrip)
|
2018-11-24 08:57:14 +00:00
|
|
|
return 1;
|
2024-06-08 13:28:16 +00:00
|
|
|
if (a.divetrip->date() < b.divetrip->date())
|
2018-11-24 08:57:14 +00:00
|
|
|
return -1;
|
2024-06-08 13:28:16 +00:00
|
|
|
if (a.divetrip->date() > b.divetrip->date())
|
2018-11-24 08:57:14 +00:00
|
|
|
return 1;
|
2018-11-24 09:01:03 +00:00
|
|
|
}
|
2024-06-07 08:25:09 +00:00
|
|
|
if (a.number < b.number)
|
2019-05-11 06:46:08 +00:00
|
|
|
return -1;
|
2024-06-07 08:25:09 +00:00
|
|
|
if (a.number > b.number)
|
2019-05-11 06:46:08 +00:00
|
|
|
return 1;
|
2024-06-07 08:25:09 +00:00
|
|
|
if ((cmp = comp_dc(&a, &b)) != 0)
|
2019-05-15 09:48:41 +00:00
|
|
|
return cmp;
|
2024-06-07 08:25:09 +00:00
|
|
|
if (a.id < b.id)
|
2018-11-24 08:57:14 +00:00
|
|
|
return -1;
|
2024-06-07 08:25:09 +00:00
|
|
|
if (a.id > b.id)
|
2018-11-24 08:57:14 +00:00
|
|
|
return 1;
|
2024-06-07 08:25:09 +00:00
|
|
|
return &a < &b ? -1 : 1; /* give up. */
|
2018-11-24 09:01:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
int comp_dives_ptr(const struct dive *a, const struct dive *b)
|
2024-05-16 18:11:21 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
return comp_dives(*a, *b);
|
2019-04-14 13:37:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 12:01:10 +00:00
|
|
|
/* This removes a dive from the global dive table but doesn't free the
|
|
|
|
* resources associated with the dive. The caller must removed the dive
|
|
|
|
* from the trip-list. Returns a pointer to the unregistered dive.
|
2024-06-18 19:19:14 +00:00
|
|
|
* The unregistered dive has the selection- and hidden-flags cleared.
|
|
|
|
* TODO: This makes me unhappy, as it touches global state, viz.
|
|
|
|
* selection and fulltext. */
|
2024-06-07 08:25:09 +00:00
|
|
|
std::unique_ptr<dive> dive_table::unregister_dive(int idx)
|
2011-09-07 19:01:37 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
if (idx < 0 || static_cast<size_t>(idx) >= size())
|
|
|
|
return {}; /* this should never happen */
|
|
|
|
|
|
|
|
auto dive = pull_at(idx);
|
|
|
|
|
2020-04-13 10:17:43 +00:00
|
|
|
/* When removing a dive from the global dive table,
|
|
|
|
* we also have to unregister its fulltext cache. */
|
2024-06-07 08:25:09 +00:00
|
|
|
fulltext_unregister(dive.get());
|
2018-08-01 08:47:09 +00:00
|
|
|
if (dive->selected)
|
|
|
|
amount_selected--;
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
dive->selected = false;
|
Undo: fix multi-level undo of delete-dive and remove-dive-from-trip
The original undo-code was fundamentally broken. Not only did it leak
resources (copied trips were never freed), it also kept references
to trips or dives that could be changed by other commands. Thus,
anything more than a single undo could lead to crashes.
Two ways of fixing this were considered
1) Don't store pointers, but unique dive-ids and trip-ids.
Whereas such unique ids exist for dives, they would have to be
implemented for trips.
2) Don't free objects in the backend.
Instead, take ownership of deleted objects in the undo-object.
Thus, all references in previous undo-objects are guaranteed to
still exist (unless the objects are deleted elsewhere).
After some contemplation, the second method was chosen, because
it is significantly less intrusive. While touching the undo-objects,
clearly separate backend from ui-code, such that they can ultimately
be reused for mobile.
Note that if other parts of the code delete dives, crashes can still
be provoked. Notable examples are split/merge dives. These will have
to be fixed later. Nevertheless, the new code is a significant
improvement over the old state.
While touching the code, implement proper translation string based
on Qt's plural-feature (using %n).
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-19 12:44:27 +00:00
|
|
|
return dive;
|
|
|
|
}
|
|
|
|
|
2024-06-04 11:52:48 +00:00
|
|
|
/* Add a dive to the global dive table.
|
|
|
|
* Index it in the fulltext cache and make sure that it is written
|
|
|
|
* in git_save().
|
2024-06-18 19:19:14 +00:00
|
|
|
* TODO: This makes me unhappy, as it touches global state, viz.
|
|
|
|
* selection and fulltext. */
|
|
|
|
struct dive *dive_table::register_dive(std::unique_ptr<dive> d)
|
2024-06-04 11:52:48 +00:00
|
|
|
{
|
|
|
|
// When we add dives, we start in hidden-by-filter status. Once all
|
|
|
|
// dives have been added, their status will be updated.
|
|
|
|
d->hidden_by_filter = true;
|
|
|
|
|
2024-06-25 12:04:01 +00:00
|
|
|
fulltext_register(d.get()); // Register the dive's fulltext cache
|
|
|
|
d->invalidate_cache(); // Ensure that dive is written in git_save()
|
2024-06-18 19:19:14 +00:00
|
|
|
auto [res, idx] = put(std::move(d));
|
2024-06-04 11:52:48 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
return res;
|
2024-06-04 11:52:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
/* return the number a dive gets when inserted at the given index.
|
|
|
|
* this function is supposed to be called *before* a dive was added.
|
|
|
|
* this returns:
|
|
|
|
* - 1 for an empty log
|
|
|
|
* - last_nr+1 for addition at end of log (if last dive had a number)
|
|
|
|
* - 0 for all other cases
|
|
|
|
*/
|
2024-06-18 19:07:58 +00:00
|
|
|
int dive_table::get_dive_nr_at_idx(int idx) const
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2024-06-18 19:07:58 +00:00
|
|
|
if (static_cast<size_t>(idx) < size())
|
2020-04-12 11:44:08 +00:00
|
|
|
return 0;
|
2024-06-18 19:07:58 +00:00
|
|
|
auto it = std::find_if(rbegin(), rend(), [](auto &d) { return !d->invalid; });
|
|
|
|
if (it == rend())
|
2018-07-19 20:35:25 +00:00
|
|
|
return 1;
|
2024-06-18 19:07:58 +00:00
|
|
|
return (*it)->number ? (*it)->number + 1 : 0;
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
/* lookup of trip in main trip_table based on its id */
|
|
|
|
dive *dive_table::get_by_uniq_id(int id) const
|
|
|
|
{
|
|
|
|
auto it = std::find_if(begin(), end(), [id](auto &d) { return d->id == id; });
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (it == end()) {
|
|
|
|
report_info("Invalid id %x passed to get_dive_by_diveid, try to fix the code", id);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return it != end() ? it->get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-24 20:18:30 +00:00
|
|
|
void clear_dive_file_data()
|
|
|
|
{
|
2020-02-16 21:26:47 +00:00
|
|
|
fulltext_unregister_all();
|
2024-05-01 16:11:35 +00:00
|
|
|
select_single_dive(NULL); // This is propagated up to the UI and clears all the information.
|
2020-02-16 21:26:47 +00:00
|
|
|
|
2019-11-15 19:16:02 +00:00
|
|
|
current_dive = NULL;
|
2024-05-13 04:17:07 +00:00
|
|
|
divelog.clear();
|
2015-07-24 20:18:30 +00:00
|
|
|
|
2024-02-14 09:59:13 +00:00
|
|
|
clear_event_types();
|
2015-08-25 20:55:51 +00:00
|
|
|
|
2024-06-25 12:33:36 +00:00
|
|
|
clear_min_datafile_version();
|
2019-07-25 05:25:54 +00:00
|
|
|
clear_git_id();
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
|
2024-05-03 21:18:45 +00:00
|
|
|
reset_tank_info_table(tank_info_table);
|
2020-12-11 21:34:35 +00:00
|
|
|
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
/* Inform frontend of reset data. This should reset all the models. */
|
|
|
|
emit_reset_signal();
|
2015-07-24 20:18:30 +00:00
|
|
|
}
|
2018-08-01 19:57:24 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
bool dive_less_than(const struct dive &a, const struct dive &b)
|
2018-08-26 12:42:38 +00:00
|
|
|
{
|
|
|
|
return comp_dives(a, b) < 0;
|
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
bool dive_less_than_ptr(const struct dive *a, const struct dive *b)
|
|
|
|
{
|
|
|
|
return comp_dives(*a, *b) < 0;
|
|
|
|
}
|
|
|
|
|
2018-11-10 08:07:42 +00:00
|
|
|
/* When comparing a dive to a trip, use the first dive of the trip. */
|
|
|
|
static int comp_dive_to_trip(struct dive *a, struct dive_trip *b)
|
|
|
|
{
|
|
|
|
/* This should never happen, nevertheless don't crash on trips
|
|
|
|
* with no (or worse a negative number of) dives. */
|
2024-06-02 15:06:18 +00:00
|
|
|
if (!b || b->dives.empty())
|
2018-11-10 08:07:42 +00:00
|
|
|
return -1;
|
2024-06-07 08:25:09 +00:00
|
|
|
return comp_dives(*a, *b->dives[0]);
|
2018-11-10 08:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int comp_dive_or_trip(struct dive_or_trip a, struct dive_or_trip b)
|
|
|
|
{
|
2019-10-27 20:29:22 +00:00
|
|
|
/* we should only be called with both a and b having exactly one of
|
|
|
|
* dive or trip not NULL. But in an abundance of caution, make sure
|
|
|
|
* we still give a consistent answer even when called with invalid
|
|
|
|
* arguments, as otherwise we might be hunting down crashes at a later
|
|
|
|
* time...
|
|
|
|
*/
|
|
|
|
if (!a.dive && !a.trip && !b.dive && !b.trip)
|
|
|
|
return 0;
|
|
|
|
if (!a.dive && !a.trip)
|
|
|
|
return -1;
|
|
|
|
if (!b.dive && !b.trip)
|
|
|
|
return 1;
|
2018-11-10 08:07:42 +00:00
|
|
|
if (a.dive && b.dive)
|
2024-06-07 08:25:09 +00:00
|
|
|
return comp_dives(*a.dive, *b.dive);
|
2018-11-10 08:07:42 +00:00
|
|
|
if (a.trip && b.trip)
|
2024-06-01 20:05:57 +00:00
|
|
|
return comp_trips(*a.trip, *b.trip);
|
2018-11-10 08:07:42 +00:00
|
|
|
if (a.dive)
|
|
|
|
return comp_dive_to_trip(a.dive, b.trip);
|
|
|
|
else
|
|
|
|
return -comp_dive_to_trip(b.dive, a.trip);
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
bool dive_or_trip_less_than(struct dive_or_trip a, struct dive_or_trip b)
|
2018-11-10 08:07:42 +00:00
|
|
|
{
|
|
|
|
return comp_dive_or_trip(a, b) < 0;
|
|
|
|
}
|
|
|
|
|
2018-10-06 07:21:27 +00:00
|
|
|
/*
|
|
|
|
* Calculate surface interval for dive starting at "when". Currently, we
|
|
|
|
* might display dives which are not yet in the divelist, therefore the
|
|
|
|
* input parameter is a timestamp.
|
|
|
|
* If the given dive starts during a different dive, the surface interval
|
|
|
|
* is 0. If we can't determine a surface interval (first dive), <0 is
|
|
|
|
* returned. This does *not* consider pathological cases such as dives
|
|
|
|
* that happened inside other dives. The interval will always be calculated
|
|
|
|
* with respect to the dive that started previously.
|
|
|
|
*/
|
2024-06-18 19:01:16 +00:00
|
|
|
timestamp_t dive_table::get_surface_interval(timestamp_t when) const
|
2018-10-06 07:21:27 +00:00
|
|
|
{
|
|
|
|
/* find previous dive. might want to use a binary search. */
|
2024-06-18 19:01:16 +00:00
|
|
|
auto it = std::find_if(rbegin(), rend(),
|
2024-06-07 08:25:09 +00:00
|
|
|
[when] (auto &d) { return d->when < when; });
|
2024-06-18 19:01:16 +00:00
|
|
|
if (it == rend())
|
2018-10-06 07:21:27 +00:00
|
|
|
return -1;
|
|
|
|
|
2024-06-18 19:01:16 +00:00
|
|
|
timestamp_t prev_end = (*it)->endtime();
|
2018-10-06 07:21:27 +00:00
|
|
|
if (prev_end > when)
|
|
|
|
return 0;
|
|
|
|
return when - prev_end;
|
|
|
|
}
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
|
|
|
/* Find visible dive close to given date. First search towards older,
|
|
|
|
* then newer dives. */
|
2024-06-18 18:57:10 +00:00
|
|
|
struct dive *dive_table::find_next_visible_dive(timestamp_t when)
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
{
|
|
|
|
/* we might want to use binary search here */
|
2024-06-18 18:57:10 +00:00
|
|
|
auto it = std::find_if(begin(), end(),
|
2024-06-07 08:25:09 +00:00
|
|
|
[when] (auto &d) { return d->when <= when; });
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
2024-06-18 18:57:10 +00:00
|
|
|
for (auto it2 = it; it2 != begin(); --it2) {
|
2024-06-07 08:25:09 +00:00
|
|
|
if (!(*std::prev(it2))->hidden_by_filter)
|
|
|
|
return it2->get();
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-18 18:57:10 +00:00
|
|
|
for (auto it2 = it; it2 != end(); ++it2) {
|
2024-06-07 08:25:09 +00:00
|
|
|
if (!(*it2)->hidden_by_filter)
|
|
|
|
return it2->get();
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
return nullptr;
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
}
|
2021-10-30 19:39:36 +00:00
|
|
|
|
2024-06-25 12:40:51 +00:00
|
|
|
bool dive_table::has_dive(unsigned int deviceid, unsigned int diveid) const
|
2021-10-30 19:39:36 +00:00
|
|
|
{
|
2024-06-25 12:40:51 +00:00
|
|
|
return std::any_of(begin(), end(), [deviceid,diveid] (auto &d) {
|
2024-06-07 08:25:09 +00:00
|
|
|
return std::any_of(d->dcs.begin(), d->dcs.end(), [deviceid,diveid] (auto &dc) {
|
|
|
|
return dc.deviceid == deviceid && dc.diveid == diveid;
|
|
|
|
});
|
|
|
|
});
|
2021-10-30 19:39:36 +00:00
|
|
|
}
|
2024-06-24 17:17:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This splits the dive src by dive computer. The first output dive has all
|
|
|
|
* dive computers except num, the second only dive computer num.
|
|
|
|
* The dives will not be associated with a trip.
|
|
|
|
* On error, both output parameters are set to NULL.
|
|
|
|
*/
|
|
|
|
std::array<std::unique_ptr<dive>, 2> dive_table::split_divecomputer(const struct dive &src, int num) const
|
|
|
|
{
|
|
|
|
if (num < 0 || src.dcs.size() < 2 || static_cast<size_t>(num) >= src.dcs.size())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// Copy the dive with full divecomputer list
|
|
|
|
auto out1 = std::make_unique<dive>(src);
|
|
|
|
|
|
|
|
// Remove all DCs, stash them and copy the dive again.
|
|
|
|
// Then, we have to dives without DCs and a list of DCs.
|
|
|
|
std::vector<divecomputer> dcs;
|
|
|
|
std::swap(out1->dcs, dcs);
|
|
|
|
auto out2 = std::make_unique<dive>(*out1);
|
|
|
|
|
|
|
|
// Give the dives new unique ids and remove them from the trip.
|
|
|
|
out1->id = dive_getUniqID();
|
|
|
|
out2->id = dive_getUniqID();
|
|
|
|
out1->divetrip = out2->divetrip = NULL;
|
|
|
|
|
|
|
|
// Now copy the divecomputers
|
|
|
|
out1->dcs.reserve(src.dcs.size() - 1);
|
|
|
|
for (auto [idx, dc]: enumerated_range(dcs)) {
|
|
|
|
auto &dcs = idx == num ? out2->dcs : out1->dcs;
|
|
|
|
dcs.push_back(std::move(dc));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recalculate gas data, etc.
|
|
|
|
fixup_dive(*out1);
|
|
|
|
fixup_dive(*out2);
|
|
|
|
|
|
|
|
return { std::move(out1), std::move(out2) };
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Split a dive that has a surface interval from samples 'a' to 'b'
|
|
|
|
* into two dives, but don't add them to the log yet.
|
|
|
|
* Returns the nr of the old dive or <0 on failure.
|
|
|
|
* Moreover, on failure both output dives are set to NULL.
|
|
|
|
* On success, the newly allocated dives are returned in out1 and out2.
|
|
|
|
*/
|
|
|
|
std::array<std::unique_ptr<dive>, 2> dive_table::split_dive_at(const struct dive &dive, int a, int b) const
|
|
|
|
{
|
|
|
|
size_t nr = get_idx(&dive);
|
|
|
|
|
|
|
|
/* if we can't find the dive in the dive list, don't bother */
|
|
|
|
if (nr == std::string::npos)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
bool is_last_dive = size() == nr + 1;
|
|
|
|
|
|
|
|
/* Splitting should leave at least 3 samples per dive */
|
|
|
|
if (a < 3 || static_cast<size_t>(b + 4) > dive.dcs[0].samples.size())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
/* We're not trying to be efficient here.. */
|
|
|
|
auto d1 = std::make_unique<struct dive>(dive);
|
|
|
|
auto d2 = std::make_unique<struct dive>(dive);
|
|
|
|
d1->id = dive_getUniqID();
|
|
|
|
d2->id = dive_getUniqID();
|
|
|
|
d1->divetrip = d2->divetrip = nullptr;
|
|
|
|
|
|
|
|
/* now unselect the first first segment so we don't keep all
|
|
|
|
* dives selected by mistake. But do keep the second one selected
|
|
|
|
* so the algorithm keeps splitting the dive further */
|
|
|
|
d1->selected = false;
|
|
|
|
|
|
|
|
struct divecomputer &dc1 = d1->dcs[0];
|
|
|
|
struct divecomputer &dc2 = d2->dcs[0];
|
|
|
|
/*
|
|
|
|
* Cut off the samples of d1 at the beginning
|
|
|
|
* of the interval.
|
|
|
|
*/
|
|
|
|
dc1.samples.resize(a);
|
|
|
|
|
|
|
|
/* And get rid of the 'b' first samples of d2 */
|
|
|
|
dc2.samples.erase(dc2.samples.begin(), dc2.samples.begin() + b);
|
|
|
|
|
|
|
|
/* Now the secondary dive computers */
|
|
|
|
int32_t t = dc2.samples[0].time.seconds;
|
|
|
|
for (auto it1 = d1->dcs.begin() + 1; it1 != d1->dcs.end(); ++it1) {
|
|
|
|
auto it = std::find_if(it1->samples.begin(), it1->samples.end(),
|
|
|
|
[t](auto &sample) { return sample.time.seconds >= t; });
|
|
|
|
it1->samples.erase(it, it1->samples.end());
|
|
|
|
}
|
|
|
|
for (auto it2 = d2->dcs.begin() + 1; it2 != d2->dcs.end(); ++it2) {
|
|
|
|
auto it = std::find_if(it2->samples.begin(), it2->samples.end(),
|
|
|
|
[t](auto &sample) { return sample.time.seconds >= t; });
|
|
|
|
it2->samples.erase(it2->samples.begin(), it);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is where we cut off events from d1,
|
|
|
|
* and shift everything in d2
|
|
|
|
*/
|
|
|
|
d2->when += t;
|
|
|
|
auto it1 = d1->dcs.begin();
|
|
|
|
auto it2 = d2->dcs.begin();
|
|
|
|
while (it1 != d1->dcs.end() && it2 != d2->dcs.end()) {
|
|
|
|
it2->when += t;
|
|
|
|
for (auto &sample: it2->samples)
|
|
|
|
sample.time.seconds -= t;
|
|
|
|
|
|
|
|
/* Remove the events past 't' from d1 */
|
|
|
|
auto it = std::lower_bound(it1->events.begin(), it1->events.end(), t,
|
|
|
|
[] (struct event &ev, int t)
|
|
|
|
{ return ev.time.seconds < t; });
|
|
|
|
it1->events.erase(it, it1->events.end());
|
|
|
|
|
|
|
|
/* Remove the events before 't' from d2, and shift the rest */
|
|
|
|
it = std::lower_bound(it2->events.begin(), it2->events.end(), t,
|
|
|
|
[] (struct event &ev, int t)
|
|
|
|
{ return ev.time.seconds < t; });
|
|
|
|
it2->events.erase(it2->events.begin(), it);
|
|
|
|
for (auto &ev: it2->events)
|
|
|
|
ev.time.seconds -= t;
|
|
|
|
|
|
|
|
++it1;
|
|
|
|
++it2;
|
|
|
|
}
|
|
|
|
|
|
|
|
force_fixup_dive(*d1);
|
|
|
|
force_fixup_dive(*d2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Was the dive numbered? If it was the last dive, then we'll
|
|
|
|
* increment the dive number for the tail part that we split off.
|
|
|
|
* Otherwise the tail is unnumbered.
|
|
|
|
*/
|
|
|
|
if (d2->number && is_last_dive)
|
|
|
|
d2->number++;
|
|
|
|
else
|
|
|
|
d2->number = 0;
|
|
|
|
|
|
|
|
return { std::move(d1), std::move(d2) };
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in freedive mode we split for as little as 10 seconds on the surface,
|
|
|
|
* otherwise we use a minute */
|
|
|
|
static bool should_split(const struct divecomputer *dc, int t1, int t2)
|
|
|
|
{
|
|
|
|
int threshold = dc->divemode == FREEDIVE ? 10 : 60;
|
|
|
|
|
|
|
|
return t2 - t1 >= threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to split a dive into multiple dives at a surface interval point.
|
|
|
|
*
|
|
|
|
* NOTE! We will split when there is at least one surface event that has
|
|
|
|
* non-surface events on both sides.
|
|
|
|
*
|
|
|
|
* The surface interval points are determined using the first dive computer.
|
|
|
|
*
|
|
|
|
* In other words, this is a (simplified) reversal of the dive merging.
|
|
|
|
*/
|
|
|
|
std::array<std::unique_ptr<dive>, 2> dive_table::split_dive(const struct dive &dive) const
|
|
|
|
{
|
|
|
|
const struct divecomputer *dc = &dive.dcs[0];
|
|
|
|
bool at_surface = true;
|
|
|
|
if (dc->samples.empty())
|
|
|
|
return {};
|
|
|
|
auto surface_start = dc->samples.begin();
|
|
|
|
for (auto it = dc->samples.begin() + 1; it != dc->samples.end(); ++it) {
|
|
|
|
bool surface_sample = it->depth.mm < SURFACE_THRESHOLD;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We care about the transition from and to depth 0,
|
|
|
|
* not about the depth staying similar.
|
|
|
|
*/
|
|
|
|
if (at_surface == surface_sample)
|
|
|
|
continue;
|
|
|
|
at_surface = surface_sample;
|
|
|
|
|
|
|
|
// Did it become surface after having been non-surface? We found the start
|
|
|
|
if (at_surface) {
|
|
|
|
surface_start = it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Going down again? We want at least a minute from
|
|
|
|
// the surface start.
|
|
|
|
if (surface_start == dc->samples.begin())
|
|
|
|
continue;
|
|
|
|
if (!should_split(dc, surface_start->time.seconds, std::prev(it)->time.seconds))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return split_dive_at(dive, surface_start - dc->samples.begin(), it - dc->samples.begin() - 1);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<std::unique_ptr<dive>, 2> dive_table::split_dive_at_time(const struct dive &dive, duration_t time) const
|
|
|
|
{
|
|
|
|
auto it = std::find_if(dive.dcs[0].samples.begin(), dive.dcs[0].samples.end(),
|
|
|
|
[time](auto &sample) { return sample.time.seconds >= time.seconds; });
|
|
|
|
if (it == dive.dcs[0].samples.end())
|
|
|
|
return {};
|
|
|
|
size_t idx = it - dive.dcs[0].samples.begin();
|
|
|
|
if (idx < 1)
|
|
|
|
return {};
|
|
|
|
return split_dive_at(dive, static_cast<int>(idx), static_cast<int>(idx - 1));
|
|
|
|
}
|
2024-06-24 18:47:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pick a trip for a dive
|
|
|
|
*/
|
|
|
|
static struct dive_trip *get_preferred_trip(const struct dive *a, const struct dive *b)
|
|
|
|
{
|
|
|
|
dive_trip *atrip, *btrip;
|
|
|
|
|
|
|
|
/* If only one dive has a trip, choose that */
|
|
|
|
atrip = a->divetrip;
|
|
|
|
btrip = b->divetrip;
|
|
|
|
if (!atrip)
|
|
|
|
return btrip;
|
|
|
|
if (!btrip)
|
|
|
|
return atrip;
|
|
|
|
|
|
|
|
/* Both dives have a trip - prefer the non-autogenerated one */
|
|
|
|
if (atrip->autogen && !btrip->autogen)
|
|
|
|
return btrip;
|
|
|
|
if (!atrip->autogen && btrip->autogen)
|
|
|
|
return atrip;
|
|
|
|
|
|
|
|
/* Otherwise, look at the trip data and pick the "better" one */
|
|
|
|
if (atrip->location.empty())
|
|
|
|
return btrip;
|
|
|
|
if (btrip->location.empty())
|
|
|
|
return atrip;
|
|
|
|
if (atrip->notes.empty())
|
|
|
|
return btrip;
|
|
|
|
if (btrip->notes.empty())
|
|
|
|
return atrip;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, so both have location and notes.
|
|
|
|
* Pick the earlier one.
|
|
|
|
*/
|
|
|
|
if (a->when < b->when)
|
|
|
|
return atrip;
|
|
|
|
return btrip;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Merging two dives can be subtle, because there's two different ways
|
|
|
|
* of merging:
|
|
|
|
*
|
|
|
|
* (a) two distinctly _different_ dives that have the same dive computer
|
|
|
|
* are merged into one longer dive, because the user asked for it
|
|
|
|
* in the divelist.
|
|
|
|
*
|
|
|
|
* Because this case is with the same dive computer, we *know* the
|
|
|
|
* two must have a different start time, and "offset" is the relative
|
|
|
|
* time difference between the two.
|
|
|
|
*
|
|
|
|
* (b) two different dive computers that we might want to merge into
|
|
|
|
* one single dive with multiple dive computers.
|
|
|
|
*
|
|
|
|
* This is the "try_to_merge()" case, which will have offset == 0,
|
|
|
|
* even if the dive times might be different.
|
|
|
|
*
|
|
|
|
* If new dives are merged into the dive table, dive a is supposed to
|
|
|
|
* be the old dive and dive b is supposed to be the newly imported
|
|
|
|
* dive. If the flag "prefer_downloaded" is set, data of the latter
|
|
|
|
* will take priority over the former.
|
|
|
|
*
|
|
|
|
* The trip the new dive should be associated with (if any) is returned
|
|
|
|
* in the "trip" output parameter.
|
|
|
|
*
|
|
|
|
* The dive site the new dive should be added to (if any) is returned
|
|
|
|
* in the "dive_site" output parameter.
|
|
|
|
*/
|
|
|
|
merge_result dive_table::merge_dives(const struct dive &a_in, const struct dive &b_in, int offset, bool prefer_downloaded) const
|
|
|
|
{
|
|
|
|
merge_result res = { };
|
|
|
|
|
|
|
|
const dive *a = &a_in;
|
|
|
|
const dive *b = &b_in;
|
|
|
|
if (is_dc_planner(&a->dcs[0]))
|
|
|
|
std::swap(a, b);
|
|
|
|
|
|
|
|
res.dive = dive::create_merged_dive(*a, *b, offset, prefer_downloaded);
|
|
|
|
|
|
|
|
/* The CNS values will be recalculated from the sample in fixup_dive() */
|
|
|
|
res.dive->cns = res.dive->maxcns = 0;
|
|
|
|
|
|
|
|
res.trip = get_preferred_trip(a, b);
|
|
|
|
|
|
|
|
/* we take the first dive site, unless it's empty */
|
|
|
|
res.site = a->dive_site && !a->dive_site->is_empty() ? a->dive_site : b->dive_site;
|
2024-06-30 15:38:36 +00:00
|
|
|
if (res.site && !res.site->has_gps_location() && b->dive_site && b->dive_site->has_gps_location()) {
|
2024-06-24 18:47:02 +00:00
|
|
|
/* we picked the first dive site and that didn't have GPS data, but the new dive has
|
|
|
|
* GPS data (that could be a download from a GPS enabled dive computer).
|
|
|
|
* Keep the dive site, but add the GPS data */
|
|
|
|
res.site->location = b->dive_site->location;
|
|
|
|
}
|
|
|
|
fixup_dive(*res.dive);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This could do a lot more merging. Right now it really only
|
|
|
|
* merges almost exact duplicates - something that happens easily
|
|
|
|
* with overlapping dive downloads.
|
|
|
|
*
|
|
|
|
* If new dives are merged into the dive table, dive a is supposed to
|
|
|
|
* be the old dive and dive b is supposed to be the newly imported
|
|
|
|
* dive. If the flag "prefer_downloaded" is set, data of the latter
|
|
|
|
* will take priority over the former.
|
|
|
|
*
|
|
|
|
* Attn: The dive_site parameter of the dive will be set, but the caller
|
|
|
|
* still has to register the dive in the dive site!
|
|
|
|
*/
|
|
|
|
struct std::unique_ptr<dive> dive_table::try_to_merge(const struct dive &a, const struct dive &b, bool prefer_downloaded) const
|
|
|
|
{
|
|
|
|
if (!a.likely_same(b))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto [res, trip, site] = merge_dives(a, b, 0, prefer_downloaded);
|
|
|
|
res->dive_site = site; /* Caller has to call site->add_dive()! */
|
|
|
|
return std::move(res);
|
|
|
|
}
|
2024-06-25 12:45:33 +00:00
|
|
|
|
|
|
|
/* Clone a dive and delete given dive computer */
|
|
|
|
std::unique_ptr<dive> dive_table::clone_delete_divecomputer(const struct dive &d, int dc_number)
|
|
|
|
{
|
|
|
|
/* copy the dive */
|
|
|
|
auto res = std::make_unique<dive>(d);
|
|
|
|
|
|
|
|
/* make a new unique id, since we still can't handle two equal ids */
|
|
|
|
res->id = dive_getUniqID();
|
|
|
|
|
|
|
|
if (res->dcs.size() <= 1)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (dc_number < 0 || static_cast<size_t>(dc_number) >= res->dcs.size())
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res->dcs.erase(res->dcs.begin() + dc_number);
|
|
|
|
|
|
|
|
force_fixup_dive(*res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|