From d787e8812c4cb62e323eb98bc0d9cbd1f714ffa7 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 1 May 2020 10:55:42 +0200 Subject: profile: for maxtime calculation include the sample after the last event When plotting profiles with surface segments, there were strange artifacts. As we found out with Robert, these were due to the fact that the calculated maxtime was set to the last event which is just one second inside the surface segment. This terribly confused the profile code. For example, it didn't properly allocate samples for the surface segment. Thus, when calculating maxtime, consider the last sample beyond the last event. Signed-off-by: Berthold Stoeger --- core/profile.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/profile.c b/core/profile.c index efcfe45a4..6723f3734 100644 --- a/core/profile.c +++ b/core/profile.c @@ -391,6 +391,7 @@ static void calculate_max_limits_new(struct dive *dive, struct divecomputer *giv { struct divecomputer *dc = &(dive->dc); bool seen = false; + bool found_sample_beyond_last_event = false; int maxdepth = dive->maxdepth.mm; int maxtime = 0; int maxpressure = 0, minpressure = INT_MAX; @@ -418,6 +419,14 @@ static void calculate_max_limits_new(struct dive *dive, struct divecomputer *giv struct sample *s = dc->sample; struct event *ev; + /* Make sure we can fit all events */ + ev = dc->events; + while (ev) { + if (ev->time.seconds > maxtime) + maxtime = ev->time.seconds; + ev = ev->next; + } + while (--i >= 0) { int depth = s->depth.mm; int pressure = s->pressure[0].mbar; @@ -440,21 +449,18 @@ static void calculate_max_limits_new(struct dive *dive, struct divecomputer *giv if (depth > maxdepth) maxdepth = s->depth.mm; - if ((depth > SURFACE_THRESHOLD || lastdepth > SURFACE_THRESHOLD || in_planner()) && - s->time.seconds > maxtime) + /* Make sure that we get the first sample beyond the last event. + * If maxtime is somewhere in the middle of the last segment, + * populate_plot_entries() gets confused leading to display artifacts. */ + if ((depth > SURFACE_THRESHOLD || lastdepth > SURFACE_THRESHOLD || in_planner() || !found_sample_beyond_last_event) && + s->time.seconds > maxtime) { + found_sample_beyond_last_event = true; maxtime = s->time.seconds; + } lastdepth = depth; s++; } - /* Make sure we can fit all events */ - ev = dc->events; - while (ev) { - if (ev->time.seconds > maxtime) - maxtime = ev->time.seconds; - ev = ev->next; - } - dc = dc->next; if (dc == NULL && !seen) { dc = given_dc; -- cgit v1.2.3-70-g09d2