core: move split_dive*() functions to struct dive_table

These functions have to access other dives in the list to
calculate CNS, etc, so let's call them from there.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-24 19:17:43 +02:00 committed by bstoeger
parent a2903b31a7
commit 8ec1f008ab
5 changed files with 217 additions and 216 deletions

View file

@ -2296,176 +2296,6 @@ merge_result merge_dives(const struct dive &a_in, const struct dive &b_in, int o
return res;
}
/*
* 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.
*/
static std::array<std::unique_ptr<dive>, 2> split_dive_at(const struct dive &dive, int a, int b)
{
size_t nr = divelog.dives.get_idx(&dive);
/* if we can't find the dive in the dive list, don't bother */
if (nr == std::string::npos)
return {};
/* 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;
}
divelog.dives.force_fixup_dive(*d1);
divelog.dives.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) {
if (divelog.dives.size() == nr + 1)
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> split_dive(const struct dive &dive)
{
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> split_dive_at_time(const struct dive &dive, duration_t time)
{
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));
}
/*
* "dc_maxtime()" is how much total time this dive computer
* has for this dive. Note that it can differ from "duration"
@ -2606,45 +2436,6 @@ std::unique_ptr<dive> clone_delete_divecomputer(const struct dive &d, int dc_num
return res;
}
/*
* 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> split_divecomputer(const struct dive &src, int num)
{
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.
divelog.dives.fixup_dive(*out1);
divelog.dives.fixup_dive(*out2);
return { std::move(out1), std::move(out2) };
}
//Calculate O2 in best mix
fraction_t best_o2(depth_t depth, const struct dive *dive, bool in_planner)
{