From 9f4a72a6924df7e853554f85ab19a75fdc771320 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 3 May 2024 18:51:03 +0200 Subject: [PATCH] profile: C++-ify plot_info Use more C++ style memory management for plot_info: Use std::vector for array data. Return the plot_info instead of filling an output parameter. Add a constructor/destructor pair so that the caller isn't bothered with memory management. The bulk of the commit is replacement of pointers with references, which is kind of gratuitous. But I started and then went on... Default initializiation of gas_pressures made it necessary to convert gas.c to c++, though with minimal changes to the code. Signed-off-by: Berthold Stoeger --- Subsurface-mobile.pro | 2 +- core/CMakeLists.txt | 2 +- core/{gas.c => gas.cpp} | 1 + core/gas.h | 4 +- core/gaspressures.cpp | 74 ++- core/gaspressures.h | 6 +- core/profile.cpp | 865 ++++++++++++++--------------- core/profile.h | 153 +++-- core/save-profiledata.cpp | 141 +++-- profile-widget/diveeventitem.cpp | 5 +- profile-widget/diveprofileitem.cpp | 22 +- profile-widget/divetooltipitem.cpp | 4 +- profile-widget/profilescene.cpp | 24 +- profile-widget/ruleritem.cpp | 2 +- 14 files changed, 635 insertions(+), 670 deletions(-) rename core/{gas.c => gas.cpp} (99%) diff --git a/Subsurface-mobile.pro b/Subsurface-mobile.pro index 23f411ad6..3e4727b41 100644 --- a/Subsurface-mobile.pro +++ b/Subsurface-mobile.pro @@ -88,7 +88,7 @@ SOURCES += subsurface-mobile-main.cpp \ core/deco.cpp \ core/divesite.cpp \ core/equipment.c \ - core/gas.c \ + core/gas.cpp \ core/membuffer.cpp \ core/selection.cpp \ core/sha1.cpp \ diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index f9d945557..24b6f90bf 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -99,7 +99,7 @@ set(SUBSURFACE_CORE_LIB_SRCS format.h fulltext.cpp fulltext.h - gas.c + gas.cpp gas.h gas-model.cpp gaspressures.cpp diff --git a/core/gas.c b/core/gas.cpp similarity index 99% rename from core/gas.c rename to core/gas.cpp index d43781e8a..82d24ea90 100644 --- a/core/gas.c +++ b/core/gas.cpp @@ -5,6 +5,7 @@ #include "gettext.h" #include #include +#include // for QT_TRANSLATE_NOOP /* Perform isobaric counterdiffusion calculations for gas changes in trimix dives. * Here we use the rule-of-fifths where, during a change involving trimix gas, the increase in nitrogen diff --git a/core/gas.h b/core/gas.h index d1b466eeb..51403935c 100644 --- a/core/gas.h +++ b/core/gas.h @@ -60,9 +60,11 @@ static inline int get_n2(struct gasmix mix) int pscr_o2(const double amb_pressure, struct gasmix mix); +#ifdef __cplusplus struct gas_pressures { - double o2, n2, he; + double o2 = 0.0, n2 = 0.0, he = 0.0; }; +#endif extern void sanitize_gasmix(struct gasmix *mix); extern int gasmix_distance(struct gasmix a, struct gasmix b); diff --git a/core/gaspressures.cpp b/core/gaspressures.cpp index 10f7a17ba..891b36100 100644 --- a/core/gaspressures.cpp +++ b/core/gaspressures.cpp @@ -86,8 +86,6 @@ static void dump_pr_track(int cyl, std::vector &track_pr) */ static void fill_missing_segment_pressures(std::vector &list, enum interpolation_strategy strategy) { - double magic; - for (auto it = list.begin(); it != list.end(); ++it) { int start = it->start, end; int pt_sum = 0, pt = 0; @@ -134,7 +132,7 @@ static void fill_missing_segment_pressures(std::vector &list, enum i break; case TIME: if (it->t_end && (tmp->t_start - tmp->t_end)) { - magic = (it->t_start - tmp->t_end) / (tmp->t_start - tmp->t_end); + double magic = (it->t_start - tmp->t_end) / (tmp->t_start - tmp->t_end); it->end = lrint(start - (start - end) * magic); } else { it->end = start; @@ -155,35 +153,33 @@ void dump_pr_interpolate(int i, pr_interpolate_t interpolate_pr) #endif -static pr_interpolate_t get_pr_interpolate_data(const pr_track_t &segment, struct plot_info *pi, int cur) -{ // cur = index to pi->entry corresponding to t_end of segment; +static pr_interpolate_t get_pr_interpolate_data(const pr_track_t &segment, struct plot_info &pi, int cur) +{ // cur = index to pi.entry corresponding to t_end of segment; pr_interpolate_t interpolate; int i; - struct plot_data *entry; interpolate.start = segment.start; interpolate.end = segment.end; interpolate.acc_pressure_time = 0; interpolate.pressure_time = 0; - for (i = 0; i < pi->nr; i++) { - entry = pi->entry + i; + for (i = 0; i < pi.nr; i++) { + const plot_data &entry = pi.entry[i]; - if (entry->sec < segment.t_start) + if (entry.sec < segment.t_start) continue; - interpolate.pressure_time += entry->pressure_time; - if (entry->sec >= segment.t_end) + interpolate.pressure_time += entry.pressure_time; + if (entry.sec >= segment.t_end) break; if (i <= cur) - interpolate.acc_pressure_time += entry->pressure_time; + interpolate.acc_pressure_time += entry.pressure_time; } return interpolate; } -static void fill_missing_tank_pressures(const struct dive *dive, struct plot_info *pi, std::vector &track_pr, int cyl) +static void fill_missing_tank_pressures(const struct dive *dive, struct plot_info &pi, std::vector &track_pr, int cyl) { int i; - struct plot_data *entry; pr_interpolate_t interpolate = { 0, 0, 0, 0 }; int cur_pr; enum interpolation_strategy strategy; @@ -214,13 +210,10 @@ static void fill_missing_tank_pressures(const struct dive *dive, struct plot_inf * The first two pi structures are "fillers", but in case we don't have a sample * at time 0 we need to process the second of them here, therefore i=1 */ auto last_segment = track_pr.end(); - for (i = 1; i < pi->nr; i++) { // For each point on the profile: - double magic; - int pressure; + for (i = 1; i < pi.nr; i++) { // For each point on the profile: + const struct plot_data &entry = pi.entry[i]; - entry = pi->entry + i; - - pressure = get_plot_pressure(pi, i, cyl); + int pressure = get_plot_pressure(pi, i, cyl); if (pressure) { // If there is a valid pressure value, last_segment = track_pr.end(); // get rid of interpolation data, @@ -230,15 +223,15 @@ static void fill_missing_tank_pressures(const struct dive *dive, struct plot_inf // If there is NO valid pressure value.. // Find the pressure segment corresponding to this entry.. auto it = track_pr.begin(); - while (it != track_pr.end() && it->t_end < entry->sec) // Find the track_pr with end time.. - ++it; // ..that matches the plot_info time (entry->sec) + while (it != track_pr.end() && it->t_end < entry.sec) // Find the track_pr with end time.. + ++it; // ..that matches the plot_info time (entry.sec) // After last segment? All done. if (it == track_pr.end()) break; // Before first segment, or between segments.. Go on, no interpolation. - if (it->t_start > entry->sec) + if (it->t_start > entry.sec) continue; if (!it->pressure_time) { // Empty segment? @@ -249,7 +242,7 @@ static void fill_missing_tank_pressures(const struct dive *dive, struct plot_inf // If there is a valid segment but no tank pressure .. if (it == last_segment) { - interpolate.acc_pressure_time += entry->pressure_time; + interpolate.acc_pressure_time += entry.pressure_time; } else { // Set up an interpolation structure interpolate = get_pr_interpolate_data(*it, pi, i); @@ -261,14 +254,14 @@ static void fill_missing_tank_pressures(const struct dive *dive, struct plot_inf /* if this segment has pressure_time, then calculate a new interpolated pressure */ if (interpolate.pressure_time) { /* Overall pressure change over total pressure-time for this segment*/ - magic = (interpolate.end - interpolate.start) / (double)interpolate.pressure_time; + double magic = (interpolate.end - interpolate.start) / (double)interpolate.pressure_time; /* Use that overall pressure change to update the current pressure */ cur_pr = lrint(interpolate.start + magic * interpolate.acc_pressure_time); } } else { - magic = (interpolate.end - interpolate.start) / (it->t_end - it->t_start); - cur_pr = lrint(it->start + magic * (entry->sec - it->t_start)); + double magic = (interpolate.end - interpolate.start) / (it->t_end - it->t_start); + cur_pr = lrint(it->start + magic * (entry.sec - it->t_start)); } set_plot_pressure_data(pi, i, INTERPOLATED_PR, cyl, cur_pr); // and store the interpolated data in plot_info } @@ -285,10 +278,10 @@ static void fill_missing_tank_pressures(const struct dive *dive, struct plot_inf * scale pressures, so it ends up being a unitless scaling * factor. */ -static inline int calc_pressure_time(const struct dive *dive, struct plot_data *a, struct plot_data *b) +static inline int calc_pressure_time(const struct dive *dive, const struct plot_data &a, const struct plot_data &b) { - int time = b->sec - a->sec; - int depth = (a->depth + b->depth) / 2; + int time = b.sec - a.sec; + int depth = (a.depth + b.depth) / 2; if (depth <= SURFACE_THRESHOLD) return 0; @@ -298,10 +291,10 @@ static inline int calc_pressure_time(const struct dive *dive, struct plot_data * #ifdef PRINT_PRESSURES_DEBUG // A CCR debugging tool that prints the gas pressures in cylinder 0 and in the diluent cylinder, used in populate_pressure_information(): -static void debug_print_pressures(struct plot_info *pi) +static void debug_print_pressures(struct plot_info &pi) { int i; - for (i = 0; i < pi->nr; i++) + for (i = 0; i < pi.nr; i++) printf("%5d |%9d | %9d |\n", i, get_plot_sensor_pressure(pi, i), get_plot_interpolated_pressure(pi, i)); } #endif @@ -315,8 +308,7 @@ static void debug_print_pressures(struct plot_info *pi) * calculates the summed pressure-time value for the duration of the dive and stores these in the pr_track_t * structures. This function is called by create_plot_info_new() in profile.cpp */ -extern "C" -void populate_pressure_information(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi, int sensor) +void populate_pressure_information(const struct dive *dive, const struct divecomputer *dc, struct plot_info &pi, int sensor) { int first, last, cyl; cylinder_t *cylinder = get_cylinder(dive, sensor); @@ -337,7 +329,7 @@ void populate_pressure_information(const struct dive *dive, const struct divecom /* Get a rough range of where we have any pressures at all */ first = last = -1; - for (int i = 0; i < pi->nr; i++) { + for (int i = 0; i < pi.nr; i++) { int pressure = get_plot_sensor_pressure(pi, i, sensor); if (!pressure) @@ -366,9 +358,9 @@ void populate_pressure_information(const struct dive *dive, const struct divecom b_ev = get_next_event(dc->events, "modechange"); for (int i = first; i <= last; i++) { - struct plot_data *entry = pi->entry + i; + struct plot_data &entry = pi.entry[i]; int pressure = get_plot_sensor_pressure(pi, i, sensor); - int time = entry->sec; + int time = entry.sec; while (ev && ev->time.seconds <= time) { // Find 1st gaschange event after cyl = get_cylinder_index(dive, ev); // the current gas change. @@ -383,9 +375,9 @@ void populate_pressure_information(const struct dive *dive, const struct divecom } if (current != std::string::npos) { // calculate pressure-time, taking into account the dive mode for this specific segment. - entry->pressure_time = (int)(calc_pressure_time(dive, entry - 1, entry) * gasfactor[dmode] + 0.5); - track[current].pressure_time += entry->pressure_time; - track[current].t_end = entry->sec; + entry.pressure_time = (int)(calc_pressure_time(dive, pi.entry[i - 1], entry) * gasfactor[dmode] + 0.5); + track[current].pressure_time += entry.pressure_time; + track[current].t_end = entry.sec; if (pressure) track[current].end = pressure; } @@ -422,7 +414,7 @@ void populate_pressure_information(const struct dive *dive, const struct divecom // missing entries that need to be interpolated. // Or maybe we didn't have a previous one at all, // and this is the first pressure entry. - track.emplace_back(pressure, entry->sec); + track.emplace_back(pressure, entry.sec); current = track.size() - 1; dense = 1; } diff --git a/core/gaspressures.h b/core/gaspressures.h index a2d764816..cc839e6f8 100644 --- a/core/gaspressures.h +++ b/core/gaspressures.h @@ -3,12 +3,8 @@ #define GASPRESSURES_H #ifdef __cplusplus -extern "C" { -#endif -void populate_pressure_information(const struct dive *, const struct divecomputer *, struct plot_info *, int); +void populate_pressure_information(const struct dive *, const struct divecomputer *, struct plot_info &, int); -#ifdef __cplusplus -} #endif #endif // GASPRESSURES_H diff --git a/core/profile.cpp b/core/profile.cpp index 992434937..36cb77645 100644 --- a/core/profile.cpp +++ b/core/profile.cpp @@ -35,24 +35,24 @@ extern "C" int ascent_velocity(int depth, int avg_depth, int bottom_time); #ifdef DEBUG_PI /* debugging tool - not normally used */ -static void dump_pi(struct plot_info *pi) +static void dump_pi(const struct plot_info &pi) { int i; printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n" " maxpressure:%d mintemp:%d maxtemp:%d\n", - pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth, - pi->maxpressure, pi->mintemp, pi->maxtemp); - for (i = 0; i < pi->nr; i++) { - struct plot_data *entry = &pi->entry[i]; + pi.nr, pi.maxtime, pi.meandepth, pi.maxdepth, + pi.maxpressure, pi.mintemp, pi.maxtemp); + for (i = 0; i < pi.nr; i++) { + struct plot_data &entry = pi.entry[i]; printf(" entry[%d]:{cylinderindex:%d sec:%d pressure:{%d,%d}\n" " time:%d:%02d temperature:%d depth:%d stopdepth:%d stoptime:%d ndl:%d smoothed:%d po2:%lf phe:%lf pn2:%lf sum-pp %lf}\n", - i, entry->sensor[0], entry->sec, - entry->pressure[0], entry->pressure[1], - entry->sec / 60, entry->sec % 60, - entry->temperature, entry->depth, entry->stopdepth, entry->stoptime, entry->ndl, entry->smoothed, - entry->pressures.o2, entry->pressures.he, entry->pressures.n2, - entry->pressures.o2 + entry->pressures.he + entry->pressures.n2); + i, entry.sensor[0], entry.sec, + entry.pressure[0], entry.pressure[1], + entry.sec / 60, entry.sec % 60, + entry.temperature, entry.depth, entry.stopdepth, entry.stoptime, entry.ndl, entry.smoothed, + entry.pressures.o2, entry.pressures.he, entry.pressures.n2, + entry.pressures.o2 + entry.pressures.he + entry.pressures.n2); } printf(" }\n"); } @@ -70,38 +70,46 @@ static T div_up(T x, T y) return (x + y - 1) / y; } +plot_info::plot_info() +{ +} + +plot_info::~plot_info() +{ +} + /* * When showing dive profiles, we scale things to the * current dive. However, we don't scale past less than * 30 minutes or 90 ft, just so that small dives show * up as such unless zoom is enabled. */ -extern "C" int get_maxtime(const struct plot_info *pi) +int get_maxtime(const struct plot_info &pi) { - int seconds = pi->maxtime; + int seconds = pi.maxtime; int min = prefs.zoomed_plot ? 30 : 30 * 60; return std::max(min, seconds); } /* get the maximum depth to which we want to plot */ -extern "C" int get_maxdepth(const struct plot_info *pi) +int get_maxdepth(const struct plot_info &pi) { /* 3m to spare */ - int mm = pi->maxdepth + 3000; + int mm = pi.maxdepth + 3000; return prefs.zoomed_plot ? mm : std::max(30000, mm); } /* UNUSED! */ -static int get_local_sac(struct plot_info *pi, int idx1, int idx2, struct dive *dive) __attribute__((unused)); +static int get_local_sac(struct plot_info &pi, int idx1, int idx2, struct dive *dive) __attribute__((unused)); /* Get local sac-rate (in ml/min) between entry1 and entry2 */ -static int get_local_sac(struct plot_info *pi, int idx1, int idx2, struct dive *dive) +static int get_local_sac(struct plot_info &pi, int idx1, int idx2, struct dive *dive) { int index = 0; cylinder_t *cyl; - struct plot_data *entry1 = pi->entry + idx1; - struct plot_data *entry2 = pi->entry + idx2; - int duration = entry2->sec - entry1->sec; + struct plot_data &entry1 = pi.entry[idx1]; + struct plot_data &entry2 = pi.entry[idx2]; + int duration = entry2.sec - entry1.sec; int depth, airuse; pressure_t a, b; double atm; @@ -114,7 +122,7 @@ static int get_local_sac(struct plot_info *pi, int idx1, int idx2, struct dive * return 0; /* Mean pressure in ATM */ - depth = (entry1->depth + entry2->depth) / 2; + depth = (entry1.depth + entry2.depth) / 2; atm = depth_to_atm(depth, dive); cyl = get_cylinder(dive, index); @@ -153,36 +161,33 @@ static velocity_t velocity(int speed) return v; } -static void analyze_plot_info(struct plot_info *pi) +static void analyze_plot_info(struct plot_info &pi) { - int i; - int nr = pi->nr; - /* Smoothing function: 5-point triangular smooth */ - for (i = 2; i < nr; i++) { - struct plot_data *entry = pi->entry + i; + for (size_t i = 2; i < pi.entry.size(); i++) { + struct plot_data &entry = pi.entry[i]; int depth; - if (i < nr - 2) { - depth = entry[-2].depth + 2 * entry[-1].depth + 3 * entry[0].depth + 2 * entry[1].depth + entry[2].depth; - entry->smoothed = (depth + 4) / 9; + if (i + 2 < pi.entry.size()) { + depth = pi.entry[i-2].depth + 2 * pi.entry[i-1].depth + 3 * pi.entry[i].depth + 2 * pi.entry[i+1].depth + pi.entry[i+2].depth; + entry.smoothed = (depth + 4) / 9; } /* vertical velocity in mm/sec */ /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */ - if (entry[0].sec - entry[-1].sec) { - entry->speed = (entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec); - entry->velocity = velocity(entry->speed); + if (pi.entry[i].sec - pi.entry[i-1].sec) { + entry.speed = (pi.entry[i+0].depth - pi.entry[i-1].depth) / (pi.entry[i].sec - pi.entry[i-1].sec); + entry.velocity = velocity(entry.speed); /* if our samples are short and we aren't too FAST*/ - if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) { + if (pi.entry[i].sec - pi.entry[i-1].sec < 15 && entry.velocity < FAST) { int past = -2; - while (i + past > 0 && entry[0].sec - entry[past].sec < 15) + while (i + past > 0 && pi.entry[i].sec - pi.entry[i+past].sec < 15) past--; - entry->velocity = velocity((entry[0].depth - entry[past].depth) / - (entry[0].sec - entry[past].sec)); + entry.velocity = velocity((pi.entry[i].depth - pi.entry[i+past].depth) / + (pi.entry[i].sec - pi.entry[i+past].sec)); } } else { - entry->velocity = STABLE; - entry->speed = 0; + entry.velocity = STABLE; + entry.speed = 0; } } } @@ -195,7 +200,7 @@ static void analyze_plot_info(struct plot_info *pi) * Some dive computers give cylinder indices, some * give just the gas mix. */ -extern "C" int get_cylinder_index(const struct dive *dive, const struct event *ev) +int get_cylinder_index(const struct dive *dive, const struct event *ev) { int best; struct gasmix mix; @@ -216,7 +221,7 @@ extern "C" int get_cylinder_index(const struct dive *dive, const struct event *e return best < 0 ? 0 : best; } -extern "C" struct event *get_next_event_mutable(struct event *event, const char *name) +struct event *get_next_event_mutable(struct event *event, const char *name) { if (!name || !*name) return NULL; @@ -228,7 +233,7 @@ extern "C" struct event *get_next_event_mutable(struct event *event, const char return event; } -extern "C" const struct event *get_next_event(const struct event *event, const char *name) +const struct event *get_next_event(const struct event *event, const char *name) { return get_next_event_mutable((struct event *)event, name); } @@ -244,21 +249,21 @@ static int count_events(const struct divecomputer *dc) return result; } -static int set_setpoint(struct plot_info *pi, int i, int setpoint, int end) +static size_t set_setpoint(struct plot_info &pi, size_t i, int setpoint, int end) { - while (i < pi->nr) { - struct plot_data *entry = pi->entry + i; - if (entry->sec > end) + while (i < pi.entry.size()) { + struct plot_data &entry = pi.entry[i]; + if (entry.sec > end) break; - entry->o2pressure.mbar = setpoint; + entry.o2pressure.mbar = setpoint; i++; } return i; } -static void check_setpoint_events(const struct dive *, const struct divecomputer *dc, struct plot_info *pi) +static void check_setpoint_events(const struct dive *, const struct divecomputer *dc, struct plot_info &pi) { - int i = 0; + size_t i = 0; pressure_t setpoint; setpoint.mbar = 0; const struct event *ev = get_next_event(dc->events, "SP change"); @@ -274,7 +279,7 @@ static void check_setpoint_events(const struct dive *, const struct divecomputer set_setpoint(pi, i, setpoint.mbar, INT_MAX); } -static void calculate_max_limits_new(const struct dive *dive, const struct divecomputer *given_dc, struct plot_info *pi, bool in_planner) +static void calculate_max_limits_new(const struct dive *dive, const struct divecomputer *given_dc, struct plot_info &pi, bool in_planner) { const struct divecomputer *dc = &(dive->dc); bool seen = false; @@ -363,49 +368,47 @@ static void calculate_max_limits_new(const struct dive *dive, const struct divec if (minhr > maxhr) minhr = maxhr; - memset(pi, 0, sizeof(*pi)); - pi->maxdepth = maxdepth; - pi->maxtime = maxtime; - pi->maxpressure = maxpressure; - pi->minpressure = minpressure; - pi->minhr = minhr; - pi->maxhr = maxhr; - pi->mintemp = mintemp; - pi->maxtemp = maxtemp; + pi.maxdepth = maxdepth; + pi.maxtime = maxtime; + pi.maxpressure = maxpressure; + pi.minpressure = minpressure; + pi.minhr = minhr; + pi.maxhr = maxhr; + pi.mintemp = mintemp; + pi.maxtemp = maxtemp; +} + +static plot_data &add_entry(struct plot_info &pi) +{ + pi.entry.emplace_back(); + pi.pressures.resize(pi.pressures.size() + pi.nr_cylinders); + return pi.entry.back(); } /* copy the previous entry (we know this exists), update time and depth * and zero out the sensor pressure (since this is a synthetic entry) * increment the entry pointer and the count of synthetic entries. */ -static void insert_entry(struct plot_info *pi, int idx, int time, int depth, int sac) +static void insert_entry(struct plot_info &pi, int time, int depth, int sac) { - struct plot_data *entry = pi->entry + idx; - struct plot_data *prev = pi->entry + idx - 1; - *entry = *prev; - entry->sec = time; - entry->depth = depth; - entry->running_sum = prev->running_sum + (time - prev->sec) * (depth + prev->depth) / 2; - entry->sac = sac; - entry->ndl = -1; - entry->bearing = -1; + struct plot_data &entry = add_entry(pi); + struct plot_data &prev = pi.entry[pi.entry.size() - 2]; + entry = prev; + entry.sec = time; + entry.depth = depth; + entry.running_sum = prev.running_sum + (time - prev.sec) * (depth + prev.depth) / 2; + entry.sac = sac; + entry.ndl = -1; + entry.bearing = -1; } -extern "C" void free_plot_info_data(struct plot_info *pi) +static void populate_plot_entries(const struct dive *dive, const struct divecomputer *dc, struct plot_info &pi) { - free(pi->entry); - free(pi->pressures); - memset(pi, 0, sizeof(*pi)); -} - -static void populate_plot_entries(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi) -{ - int idx, maxtime, nr, i; - int lastdepth, lasttime, lasttemp = 0; - struct plot_data *plot_data; struct event *ev = dc->events; - maxtime = pi->maxtime; + + pi.nr_cylinders = dive->cylinders.nr; /* + * To avoid continuous reallocation, allocate the expected number of entries. * We want to have a plot_info event at least every 10s (so "maxtime/10+1"), * but samples could be more dense than that (so add in dc->samples). We also * need to have one for every event (so count events and add that) and @@ -414,28 +417,26 @@ static void populate_plot_entries(const struct dive *dive, const struct divecomp * that has time > maxtime (because there can be surface samples * past "maxtime" in the original sample data) */ - nr = dc->samples + 6 + maxtime / 10 + count_events(dc); - plot_data = (struct plot_data *)calloc(nr, sizeof(struct plot_data)); - pi->entry = plot_data; - pi->nr_cylinders = dive->cylinders.nr; - pi->pressures = (struct plot_pressure_data *)calloc(nr * (size_t)pi->nr_cylinders, sizeof(struct plot_pressure_data)); - if (!plot_data) - return; - pi->nr = nr; - idx = 2; /* the two extra events at the start */ + size_t nr = dc->samples + 6 + pi.maxtime / 10 + count_events(dc); + pi.entry.reserve(nr); + pi.pressures.reserve(nr * pi.nr_cylinders); - lastdepth = 0; - lasttime = 0; + // The two extra events at the start + pi.entry.resize(2); + pi.pressures.resize(pi.nr_cylinders * 2); + + int lastdepth = 0; + int lasttime = 0; + int lasttemp = 0; /* skip events at time = 0 */ while (ev && ev->time.seconds == 0) ev = ev->next; - for (i = 0; i < dc->samples; i++) { - struct plot_data *entry = plot_data + idx; - struct sample *sample = dc->sample + i; - int time = sample->time.seconds; + for (int i = 0; i < dc->samples; i++) { + const struct sample &sample = dc->sample[i]; + int time = sample.time.seconds; int offset, delta; - int depth = sample->depth.mm; - int sac = sample->sac.mliter; + int depth = sample.depth.mm; + int sac = sample.sac.mliter; /* Add intermediate plot entries if required */ delta = time - lasttime; @@ -444,21 +445,17 @@ static void populate_plot_entries(const struct dive *dive, const struct divecomp delta = 1; // avoid divide by 0 } for (offset = 10; offset < delta; offset += 10) { - if (lasttime + offset > maxtime) + if (lasttime + offset > pi.maxtime) break; /* Add events if they are between plot entries */ while (ev && (int)ev->time.seconds < lasttime + offset) { - insert_entry(pi, idx, ev->time.seconds, interpolate(lastdepth, depth, ev->time.seconds - lasttime, delta), sac); - entry++; - idx++; + insert_entry(pi, ev->time.seconds, interpolate(lastdepth, depth, ev->time.seconds - lasttime, delta), sac); ev = ev->next; } /* now insert the time interpolated entry */ - insert_entry(pi, idx, lasttime + offset, interpolate(lastdepth, depth, offset, delta), sac); - entry++; - idx++; + insert_entry(pi, lasttime + offset, interpolate(lastdepth, depth, offset, delta), sac); /* skip events that happened at this time */ while (ev && (int)ev->time.seconds == lasttime + offset) @@ -467,72 +464,68 @@ static void populate_plot_entries(const struct dive *dive, const struct divecomp /* Add events if they are between plot entries */ while (ev && (int)ev->time.seconds < time) { - insert_entry(pi, idx, ev->time.seconds, interpolate(lastdepth, depth, ev->time.seconds - lasttime, delta), sac); - entry++; - idx++; + insert_entry(pi, ev->time.seconds, interpolate(lastdepth, depth, ev->time.seconds - lasttime, delta), sac); ev = ev->next; } - entry->sec = time; - entry->depth = depth; + plot_data &entry = add_entry(pi); + plot_data &prev = pi.entry[pi.entry.size() - 2]; + entry.sec = time; + entry.depth = depth; - entry->running_sum = (entry - 1)->running_sum + (time - (entry - 1)->sec) * (depth + (entry - 1)->depth) / 2; - entry->stopdepth = sample->stopdepth.mm; - entry->stoptime = sample->stoptime.seconds; - entry->ndl = sample->ndl.seconds; - entry->tts = sample->tts.seconds; - entry->in_deco = sample->in_deco; - entry->cns = sample->cns; + entry.running_sum = prev.running_sum + (time - prev.sec) * (depth + prev.depth) / 2; + entry.stopdepth = sample.stopdepth.mm; + entry.stoptime = sample.stoptime.seconds; + entry.ndl = sample.ndl.seconds; + entry.tts = sample.tts.seconds; + entry.in_deco = sample.in_deco; + entry.cns = sample.cns; if (dc->divemode == CCR || (dc->divemode == PSCR && dc->no_o2sensors)) { - entry->o2pressure.mbar = entry->o2setpoint.mbar = sample->setpoint.mbar; // for rebreathers + entry.o2pressure.mbar = entry.o2setpoint.mbar = sample.setpoint.mbar; // for rebreathers int i; for (i = 0; i < MAX_O2_SENSORS; i++) - entry->o2sensor[i].mbar = sample->o2sensor[i].mbar; + entry.o2sensor[i].mbar = sample.o2sensor[i].mbar; } else { - entry->pressures.o2 = sample->setpoint.mbar / 1000.0; + entry.pressures.o2 = sample.setpoint.mbar / 1000.0; } - if (sample->pressure[0].mbar && sample->sensor[0] != NO_SENSOR) - set_plot_pressure_data(pi, idx, SENSOR_PR, sample->sensor[0], sample->pressure[0].mbar); - if (sample->pressure[1].mbar && sample->sensor[1] != NO_SENSOR) - set_plot_pressure_data(pi, idx, SENSOR_PR, sample->sensor[1], sample->pressure[1].mbar); - if (sample->temperature.mkelvin) - entry->temperature = lasttemp = sample->temperature.mkelvin; + if (sample.pressure[0].mbar && sample.sensor[0] != NO_SENSOR) + set_plot_pressure_data(pi, pi.entry.size() - 1, SENSOR_PR, sample.sensor[0], sample.pressure[0].mbar); + if (sample.pressure[1].mbar && sample.sensor[1] != NO_SENSOR) + set_plot_pressure_data(pi, pi.entry.size() - 1, SENSOR_PR, sample.sensor[1], sample.pressure[1].mbar); + if (sample.temperature.mkelvin) + entry.temperature = lasttemp = sample.temperature.mkelvin; else - entry->temperature = lasttemp; - entry->heartbeat = sample->heartbeat; - entry->bearing = sample->bearing.degrees; - entry->sac = sample->sac.mliter; - if (sample->rbt.seconds) - entry->rbt = sample->rbt.seconds; + entry.temperature = lasttemp; + entry.heartbeat = sample.heartbeat; + entry.bearing = sample.bearing.degrees; + entry.sac = sample.sac.mliter; + if (sample.rbt.seconds) + entry.rbt = sample.rbt.seconds; /* skip events that happened at this time */ while (ev && (int)ev->time.seconds == time) ev = ev->next; lasttime = time; lastdepth = depth; - idx++; - if (time > maxtime) + if (time > pi.maxtime) break; } /* Add any remaining events */ while (ev) { - struct plot_data *entry = plot_data + idx; int time = ev->time.seconds; if (time > lasttime) { - insert_entry(pi, idx, ev->time.seconds, 0, 0); + insert_entry(pi, ev->time.seconds, 0, 0); lasttime = time; - idx++; - entry++; } ev = ev->next; } /* Add two final surface events */ - plot_data[idx++].sec = lasttime + 1; - plot_data[idx++].sec = lasttime + 2; - pi->nr = idx; + add_entry(pi).sec = lasttime + 1; + add_entry(pi).sec = lasttime + 2; + pi.nr = (int)pi.entry.size(); } /* @@ -540,7 +533,7 @@ static void populate_plot_entries(const struct dive *dive, const struct divecomp * * Everything in between has a cylinder pressure for at least some of the cylinders. */ -static int sac_between(const struct dive *dive, struct plot_info *pi, int first, int last, const char gases[]) +static int sac_between(const struct dive *dive, const struct plot_info &pi, int first, int last, const char gases[]) { int i, airuse; double pressuretime; @@ -550,7 +543,7 @@ static int sac_between(const struct dive *dive, struct plot_info *pi, int first, /* Get airuse for the set of cylinders over the range */ airuse = 0; - for (i = 0; i < pi->nr_cylinders; i++) { + for (i = 0; i < pi.nr_cylinders; i++) { pressure_t a, b; cylinder_t *cyl; int cyluse; @@ -571,10 +564,10 @@ static int sac_between(const struct dive *dive, struct plot_info *pi, int first, /* Calculate depthpressure integrated over time */ pressuretime = 0.0; do { - struct plot_data *entry = pi->entry + first; - struct plot_data *next = entry + 1; - int depth = (entry->depth + next->depth) / 2; - int time = next->sec - entry->sec; + const struct plot_data &entry = pi.entry[first]; + const struct plot_data &next = pi.entry[first + 1]; + int depth = (entry.depth + next.depth) / 2; + int time = next.sec - entry.sec; double atm = depth_to_atm(depth, dive); pressuretime += atm * time; @@ -588,11 +581,11 @@ static int sac_between(const struct dive *dive, struct plot_info *pi, int first, } /* Is there pressure data for all gases? */ -static bool all_pressures(struct plot_info *pi, int idx, const char gases[]) +static bool all_pressures(const struct plot_info &pi, int idx, const char gases[]) { int i; - for (i = 0; i < pi->nr_cylinders; i++) { + for (i = 0; i < pi.nr_cylinders; i++) { if (gases[i] && !get_plot_pressure(pi, idx, i)) return false; } @@ -601,12 +594,12 @@ static bool all_pressures(struct plot_info *pi, int idx, const char gases[]) } /* Which of the set of gases have pressure data? Returns false if none of them. */ -static bool filter_pressures(struct plot_info *pi, int idx, const char gases_in[], char gases_out[]) +static bool filter_pressures(const struct plot_info &pi, int idx, const char gases_in[], char gases_out[]) { int i; bool has_pressure = false; - for (i = 0; i < pi->nr_cylinders; i++) { + for (i = 0; i < pi.nr_cylinders; i++) { gases_out[i] = gases_in[i] && get_plot_pressure(pi, idx, i); has_pressure |= gases_out[i]; } @@ -620,13 +613,13 @@ static bool filter_pressures(struct plot_info *pi, int idx, const char gases_in[ * an array of gases, the caller passes in scratch memory in the last * argument. */ -static void fill_sac(const struct dive *dive, struct plot_info *pi, int idx, const char gases_in[], char gases[]) +static void fill_sac(const struct dive *dive, struct plot_info &pi, int idx, const char gases_in[], char gases[]) { - struct plot_data *entry = pi->entry + idx; + struct plot_data &entry = pi.entry[idx]; int first, last; int time; - if (entry->sac) + if (entry.sac) return; /* @@ -641,14 +634,14 @@ static void fill_sac(const struct dive *dive, struct plot_info *pi, int idx, con * Stop if the cylinder pressure data set changes. */ first = idx; - time = entry->sec - 30; + time = entry.sec - 30; while (idx > 0) { - struct plot_data *entry = pi->entry + idx; - struct plot_data *prev = pi->entry + idx - 1; + const struct plot_data &entry = pi.entry[idx]; + const struct plot_data &prev = pi.entry[idx - 1]; - if (prev->depth < SURFACE_THRESHOLD && entry->depth < SURFACE_THRESHOLD) + if (prev.depth < SURFACE_THRESHOLD && entry.depth < SURFACE_THRESHOLD) break; - if (prev->sec < time) + if (prev.sec < time) break; if (!all_pressures(pi, idx - 1, gases)) break; @@ -658,13 +651,13 @@ static void fill_sac(const struct dive *dive, struct plot_info *pi, int idx, con /* Now find an entry a minute after the first one */ last = first; - time = pi->entry[first].sec + 60; - while (++idx < pi->nr) { - struct plot_data *entry = pi->entry + last; - struct plot_data *next = pi->entry + last + 1; - if (next->depth < SURFACE_THRESHOLD && entry->depth < SURFACE_THRESHOLD) + time = pi.entry[first].sec + 60; + while (++idx < pi.nr) { + const struct plot_data &entry = pi.entry[last]; + const struct plot_data &next = pi.entry[last + 1]; + if (next.depth < SURFACE_THRESHOLD && entry.depth < SURFACE_THRESHOLD) break; - if (next->sec > time) + if (next.sec > time) break; if (!all_pressures(pi, idx + 1, gases)) break; @@ -672,7 +665,7 @@ static void fill_sac(const struct dive *dive, struct plot_info *pi, int idx, con } /* Ok, now calculate the SAC between 'first' and 'last' */ - entry->sac = sac_between(dive, pi, first, last, gases); + entry.sac = sac_between(dive, pi, first, last, gases); } /* @@ -680,26 +673,24 @@ static void fill_sac(const struct dive *dive, struct plot_info *pi, int idx, con */ static void matching_gases(const struct dive *dive, struct gasmix gasmix, char gases[]) { - int i; - - for (i = 0; i < dive->cylinders.nr; i++) + for (int i = 0; i < dive->cylinders.nr; i++) gases[i] = same_gasmix(gasmix, get_cylinder(dive, i)->gasmix); } -static void calculate_sac(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi) +static void calculate_sac(const struct dive *dive, const struct divecomputer *dc, struct plot_info &pi) { struct gasmix gasmix = gasmix_invalid; const struct event *ev = NULL; - std::vector gases(pi->nr_cylinders, false); + std::vector gases(pi.nr_cylinders, false); /* This might be premature optimization, but let's allocate the gas array for * the fill_sac function only once an not once per sample */ - std::vector gases_scratch(pi->nr_cylinders); + std::vector gases_scratch(pi.nr_cylinders); - for (int i = 0; i < pi->nr; i++) { - struct plot_data *entry = pi->entry + i; - struct gasmix newmix = get_gasmix(dive, dc, entry->sec, &ev, gasmix); + for (int i = 0; i < pi.nr; i++) { + const struct plot_data &entry = pi.entry[i]; + struct gasmix newmix = get_gasmix(dive, dc, entry.sec, &ev, gasmix); if (!same_gasmix(newmix, gasmix)) { gasmix = newmix; matching_gases(dive, newmix, gases.data()); @@ -709,27 +700,27 @@ static void calculate_sac(const struct dive *dive, const struct divecomputer *dc } } -static void populate_secondary_sensor_data(const struct divecomputer *dc, struct plot_info *pi) +static void populate_secondary_sensor_data(const struct divecomputer *dc, struct plot_info &pi) { - std::vector seen(pi->nr_cylinders, 0); - for (int idx = 0; idx < pi->nr; ++idx) - for (int c = 0; c < pi->nr_cylinders; ++c) + std::vector seen(pi.nr_cylinders, 0); + for (int idx = 0; idx < pi.nr; ++idx) + for (int c = 0; c < pi.nr_cylinders; ++c) if (get_plot_pressure_data(pi, idx, SENSOR_PR, c)) ++seen[c]; // Count instances so we can differentiate a real sensor from just start and end pressure int idx = 0; /* We should try to see if it has interesting pressure data here */ - for (int i = 0; i < dc->samples && idx < pi->nr; i++) { - struct sample *sample = dc->sample + i; - for (; idx < pi->nr; ++idx) { - if (idx == pi->nr - 1 || pi->entry[idx].sec >= sample->time.seconds) + for (int i = 0; i < dc->samples && idx < pi.nr; i++) { + const struct sample &sample = dc->sample[i]; + for (; idx < pi.nr; ++idx) { + if (idx == pi.nr - 1 || pi.entry[idx].sec >= sample.time.seconds) // We've either found the entry at or just after the sample's time, // or this is the last entry so use for the last sensor readings if there are any. break; } for (int s = 0; s < MAX_SENSORS; ++s) // Copy sensor data if available, but don't add if this dc already has sensor data - if (sample->sensor[s] != NO_SENSOR && seen[sample->sensor[s]] < 3 && sample->pressure[s].mbar) - set_plot_pressure_data(pi, idx, SENSOR_PR, sample->sensor[s], sample->pressure[s].mbar); + if (sample.sensor[s] != NO_SENSOR && seen[sample.sensor[s]] < 3 && sample.pressure[s].mbar) + set_plot_pressure_data(pi, idx, SENSOR_PR, sample.sensor[s], sample.pressure[s].mbar); } } @@ -737,26 +728,26 @@ static void populate_secondary_sensor_data(const struct divecomputer *dc, struct * This adds a pressure entry to the plot_info based on the gas change * information and the manually filled in pressures. */ -static void add_plot_pressure(struct plot_info *pi, int time, int cyl, pressure_t p) +static void add_plot_pressure(struct plot_info &pi, int time, int cyl, pressure_t p) { - for (int i = 0; i < pi->nr; i++) { - if (i == pi->nr - 1 || pi->entry[i].sec >= time) { + for (int i = 0; i < pi.nr; i++) { + if (i == pi.nr - 1 || pi.entry[i].sec >= time) { set_plot_pressure_data(pi, i, SENSOR_PR, cyl, p.mbar); return; } } } -static void setup_gas_sensor_pressure(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi) +static void setup_gas_sensor_pressure(const struct dive *dive, const struct divecomputer *dc, struct plot_info &pi) { int i; const struct event *ev; - if (pi->nr_cylinders == 0) + if (pi.nr_cylinders == 0) return; /* FIXME: The planner uses a dummy one-past-end cylinder for surface air! */ - int num_cyl = pi->nr_cylinders + 1; + int num_cyl = pi.nr_cylinders + 1; std::vector seen(num_cyl, 0); std::vector first(num_cyl, 0); std::vector last(num_cyl, INT_MAX); @@ -791,7 +782,7 @@ static void setup_gas_sensor_pressure(const struct dive *dive, const struct dive // Fill in "seen[]" array - mark cylinders we're not interested // in as negative. - for (i = 0; i < pi->nr_cylinders; i++) { + for (i = 0; i < pi.nr_cylinders; i++) { const cylinder_t *cyl = get_cylinder(dive, i); int start = cyl->start.mbar; int end = cyl->end.mbar; @@ -820,7 +811,7 @@ static void setup_gas_sensor_pressure(const struct dive *dive, const struct dive } } - for (i = 0; i < pi->nr_cylinders; i++) { + for (i = 0; i < pi.nr_cylinders; i++) { if (seen[i] >= 0) { const cylinder_t *cyl = get_cylinder(dive, i); @@ -843,7 +834,7 @@ static void setup_gas_sensor_pressure(const struct dive *dive, const struct dive } /* calculate DECO STOP / TTS / NDL */ -static void calculate_ndl_tts(struct deco_state *ds, const struct dive *dive, struct plot_data *entry, struct gasmix gasmix, +static void calculate_ndl_tts(struct deco_state *ds, const struct dive *dive, struct plot_data &entry, struct gasmix gasmix, double surface_pressure, enum divemode_t divemode, bool in_planner) { /* should this be configurable? */ @@ -855,67 +846,67 @@ static void calculate_ndl_tts(struct deco_state *ds, const struct dive *dive, st const int deco_stepsize = M_OR_FT(3, 10); /* at what depth is the current deco-step? */ int next_stop = round_up(deco_allowed_depth( - tissue_tolerance_calc(ds, dive, depth_to_bar(entry->depth, dive), in_planner), + tissue_tolerance_calc(ds, dive, depth_to_bar(entry.depth, dive), in_planner), surface_pressure, dive, 1), deco_stepsize); - int ascent_depth = entry->depth; + int ascent_depth = entry.depth; /* at what time should we give up and say that we got enuff NDL? */ - /* If iterating through a dive, entry->tts_calc needs to be reset */ - entry->tts_calc = 0; + /* If iterating through a dive, entry.tts_calc needs to be reset */ + entry.tts_calc = 0; /* If we don't have a ceiling yet, calculate ndl. Don't try to calculate * a ndl for lower values than 3m it would take forever */ if (next_stop == 0) { - if (entry->depth < 3000) { - entry->ndl = MAX_PROFILE_DECO; + if (entry.depth < 3000) { + entry.ndl = MAX_PROFILE_DECO; return; } /* stop if the ndl is above max_ndl seconds, and call it plenty of time */ - while (entry->ndl_calc < MAX_PROFILE_DECO && - deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(entry->depth, dive), in_planner), + while (entry.ndl_calc < MAX_PROFILE_DECO && + deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(entry.depth, dive), in_planner), surface_pressure, dive, 1) <= 0 ) { - entry->ndl_calc += time_stepsize; - add_segment(ds, depth_to_bar(entry->depth, dive), - gasmix, time_stepsize, entry->o2pressure.mbar, divemode, prefs.bottomsac, in_planner); + entry.ndl_calc += time_stepsize; + add_segment(ds, depth_to_bar(entry.depth, dive), + gasmix, time_stepsize, entry.o2pressure.mbar, divemode, prefs.bottomsac, in_planner); } /* we don't need to calculate anything else */ return; } /* We are in deco */ - entry->in_deco_calc = true; + entry.in_deco_calc = true; /* Add segments for movement to stopdepth */ - for (; ascent_depth > next_stop; ascent_depth -= ascent_s_per_step * ascent_velocity(ascent_depth, entry->running_sum / entry->sec, 0), entry->tts_calc += ascent_s_per_step) { + for (; ascent_depth > next_stop; ascent_depth -= ascent_s_per_step * ascent_velocity(ascent_depth, entry.running_sum / entry.sec, 0), entry.tts_calc += ascent_s_per_step) { add_segment(ds, depth_to_bar(ascent_depth, dive), - gasmix, ascent_s_per_step, entry->o2pressure.mbar, divemode, prefs.decosac, in_planner); + gasmix, ascent_s_per_step, entry.o2pressure.mbar, divemode, prefs.decosac, in_planner); next_stop = round_up(deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(ascent_depth, dive), in_planner), surface_pressure, dive, 1), deco_stepsize); } ascent_depth = next_stop; /* And how long is the current deco-step? */ - entry->stoptime_calc = 0; - entry->stopdepth_calc = next_stop; + entry.stoptime_calc = 0; + entry.stopdepth_calc = next_stop; next_stop -= deco_stepsize; /* And how long is the total TTS */ while (next_stop >= 0) { /* save the time for the first stop to show in the graph */ - if (ascent_depth == entry->stopdepth_calc) - entry->stoptime_calc += time_stepsize; + if (ascent_depth == entry.stopdepth_calc) + entry.stoptime_calc += time_stepsize; - entry->tts_calc += time_stepsize; - if (entry->tts_calc > MAX_PROFILE_DECO) + entry.tts_calc += time_stepsize; + if (entry.tts_calc > MAX_PROFILE_DECO) break; add_segment(ds, depth_to_bar(ascent_depth, dive), - gasmix, time_stepsize, entry->o2pressure.mbar, divemode, prefs.decosac, in_planner); + gasmix, time_stepsize, entry.o2pressure.mbar, divemode, prefs.decosac, in_planner); if (deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(ascent_depth,dive), in_planner), surface_pressure, dive, 1) <= next_stop) { /* move to the next stop and add the travel between stops */ - for (; ascent_depth > next_stop; ascent_depth -= ascent_s_per_deco_step * ascent_velocity(ascent_depth, entry->running_sum / entry->sec, 0), entry->tts_calc += ascent_s_per_deco_step) + for (; ascent_depth > next_stop; ascent_depth -= ascent_s_per_deco_step * ascent_velocity(ascent_depth, entry.running_sum / entry.sec, 0), entry.tts_calc += ascent_s_per_deco_step) add_segment(ds, depth_to_bar(ascent_depth, dive), - gasmix, ascent_s_per_deco_step, entry->o2pressure.mbar, divemode, prefs.decosac, in_planner); + gasmix, ascent_s_per_deco_step, entry.o2pressure.mbar, divemode, prefs.decosac, in_planner); ascent_depth = next_stop; next_stop -= deco_stepsize; } @@ -925,7 +916,7 @@ static void calculate_ndl_tts(struct deco_state *ds, const struct dive *dive, st /* Let's try to do some deco calculations. */ static void calculate_deco_information(struct deco_state *ds, const struct deco_state *planner_ds, const struct dive *dive, - const struct divecomputer *dc, struct plot_info *pi) + const struct divecomputer *dc, struct plot_info &pi) { int i, count_iteration = 0; double surface_pressure = (dc->surface_pressure.mbar ? dc->surface_pressure.mbar : get_surface_pressure_in_mbar(dive, true)) / 1000.0; @@ -957,15 +948,16 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ const struct event *ev = NULL, *evd = NULL; enum divemode_t current_divemode = UNDEF_COMP_TYPE; - for (i = 1; i < pi->nr; i++) { - struct plot_data *entry = pi->entry + i; - int j, t0 = (entry - 1)->sec, t1 = entry->sec; + for (i = 1; i < pi.nr; i++) { + struct plot_data &entry = pi.entry[i]; + struct plot_data &prev = pi.entry[i - 1]; + int j, t0 = prev.sec, t1 = entry.sec; int time_stepsize = 20, max_ceiling = -1; - current_divemode = get_current_divemode(dc, entry->sec, &evd, ¤t_divemode); + current_divemode = get_current_divemode(dc, entry.sec, &evd, ¤t_divemode); gasmix = get_gasmix(dive, dc, t1, &ev, gasmix); - entry->ambpressure = depth_to_bar(entry->depth, dive); - entry->gfline = get_gf(ds, entry->ambpressure, dive) * (100.0 - AMB_PERCENTAGE) + AMB_PERCENTAGE; + entry.ambpressure = depth_to_bar(entry.depth, dive); + entry.gfline = get_gf(ds, entry.ambpressure, dive) * (100.0 - AMB_PERCENTAGE) + AMB_PERCENTAGE; if (t0 > t1) { report_info("non-monotonous dive stamps %d %d", t0, t1); int xchg = t1; @@ -975,15 +967,15 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ if (t0 != t1 && t1 - t0 < time_stepsize) time_stepsize = t1 - t0; for (j = t0 + time_stepsize; j <= t1; j += time_stepsize) { - int depth = interpolate(entry[-1].depth, entry[0].depth, j - t0, t1 - t0); + int depth = interpolate(prev.depth, entry.depth, j - t0, t1 - t0); add_segment(ds, depth_to_bar(depth, dive), - gasmix, time_stepsize, entry->o2pressure.mbar, current_divemode, entry->sac, in_planner); - entry->icd_warning = ds->icd_warning; + gasmix, time_stepsize, entry.o2pressure.mbar, current_divemode, entry.sac, in_planner); + entry.icd_warning = ds->icd_warning; if ((t1 - j < time_stepsize) && (j < t1)) time_stepsize = t1 - j; } if (t0 == t1) { - entry->ceiling = (entry - 1)->ceiling; + entry.ceiling = prev.ceiling; } else { /* Keep updating the VPM-B gradients until the start of the ascent phase of the dive. */ if (decoMode(in_planner) == VPMB && last_ceiling >= first_ceiling && first_iteration == true) { @@ -993,16 +985,16 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ if (!first_iteration || in_planner) vpmb_next_gradient(ds, ds->deco_time, surface_pressure / 1000.0, in_planner); } - entry->ceiling = deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(entry->depth, dive), in_planner), surface_pressure, dive, !prefs.calcceiling3m); + entry.ceiling = deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(entry.depth, dive), in_planner), surface_pressure, dive, !prefs.calcceiling3m); if (prefs.calcceiling3m) - current_ceiling = deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(entry->depth, dive), in_planner), surface_pressure, dive, true); + current_ceiling = deco_allowed_depth(tissue_tolerance_calc(ds, dive, depth_to_bar(entry.depth, dive), in_planner), surface_pressure, dive, true); else - current_ceiling = entry->ceiling; + current_ceiling = entry.ceiling; last_ceiling = current_ceiling; /* If using VPM-B, take first_ceiling_pressure as the deepest ceiling */ if (decoMode(in_planner) == VPMB) { if (current_ceiling >= first_ceiling || - (time_deep_ceiling == t0 && entry->depth == (entry - 1)->depth)) { + (time_deep_ceiling == t0 && entry.depth == prev.depth)) { time_deep_ceiling = t1; first_ceiling = current_ceiling; ds->first_ceiling_pressure.mbar = depth_to_mbar(first_ceiling, dive); @@ -1013,7 +1005,7 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ but we want to over-estimate deco_time for the first iteration so it converges correctly, so add 30min*/ if (!in_planner) - ds->deco_time = pi->maxtime - t1 + 1800; + ds->deco_time = pi.maxtime - t1 + 1800; vpmb_next_gradient(ds, ds->deco_time, surface_pressure / 1000.0, in_planner); } } @@ -1024,23 +1016,23 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ time_clear_ceiling = t1; } } - entry->surface_gf = 0.0; - entry->current_gf = 0.0; + entry.surface_gf = 0.0; + entry.current_gf = 0.0; for (j = 0; j < 16; j++) { - double m_value = ds->buehlmann_inertgas_a[j] + entry->ambpressure / ds->buehlmann_inertgas_b[j]; + double m_value = ds->buehlmann_inertgas_a[j] + entry.ambpressure / ds->buehlmann_inertgas_b[j]; double surface_m_value = ds->buehlmann_inertgas_a[j] + surface_pressure / ds->buehlmann_inertgas_b[j]; - entry->ceilings[j] = deco_allowed_depth(ds->tolerated_by_tissue[j], surface_pressure, dive, 1); - if (entry->ceilings[j] > max_ceiling) - max_ceiling = entry->ceilings[j]; - double current_gf = (ds->tissue_inertgas_saturation[j] - entry->ambpressure) / (m_value - entry->ambpressure); - entry->percentages[j] = ds->tissue_inertgas_saturation[j] < entry->ambpressure ? - lrint(ds->tissue_inertgas_saturation[j] / entry->ambpressure * AMB_PERCENTAGE) : + entry.ceilings[j] = deco_allowed_depth(ds->tolerated_by_tissue[j], surface_pressure, dive, 1); + if (entry.ceilings[j] > max_ceiling) + max_ceiling = entry.ceilings[j]; + double current_gf = (ds->tissue_inertgas_saturation[j] - entry.ambpressure) / (m_value - entry.ambpressure); + entry.percentages[j] = ds->tissue_inertgas_saturation[j] < entry.ambpressure ? + lrint(ds->tissue_inertgas_saturation[j] / entry.ambpressure * AMB_PERCENTAGE) : lrint(AMB_PERCENTAGE + current_gf * (100.0 - AMB_PERCENTAGE)); - if (current_gf > entry->current_gf) - entry->current_gf = current_gf; + if (current_gf > entry.current_gf) + entry.current_gf = current_gf; double surface_gf = 100.0 * (ds->tissue_inertgas_saturation[j] - surface_pressure) / (surface_m_value - surface_pressure); - if (surface_gf > entry->surface_gf) - entry->surface_gf = surface_gf; + if (surface_gf > entry.surface_gf) + entry.surface_gf = surface_gf; } // In the planner, if the ceiling is violated, add an event. @@ -1049,12 +1041,12 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ // that can be trampled upon. But ultimately, the ceiling-violation // marker should be handled differently! // Don't scream if we violate the ceiling by a few cm. - if (in_planner && !pi->waypoint_above_ceiling && - entry->depth < max_ceiling - 100 && entry->sec > 0) { + if (in_planner && !pi.waypoint_above_ceiling && + entry.depth < max_ceiling - 100 && entry.sec > 0) { struct dive *non_const_dive = (struct dive *)dive; // cast away const! - add_event(&non_const_dive->dc, entry->sec, SAMPLE_EVENT_CEILING, -1, max_ceiling / 1000, + add_event(&non_const_dive->dc, entry.sec, SAMPLE_EVENT_CEILING, -1, max_ceiling / 1000, translate("gettextFromC", "planned waypoint above ceiling")); - pi->waypoint_above_ceiling = true; + pi.waypoint_above_ceiling = true; } /* should we do more calculations? @@ -1062,24 +1054,24 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ * If the ceiling hasn't cleared by the last data point, we need tts for VPM-B CVA calculation * It is not necessary to do these calculation on the first VPMB iteration, except for the last data point */ if ((prefs.calcndltts && (decoMode(in_planner) != VPMB || in_planner || !first_iteration)) || - (decoMode(in_planner) == VPMB && !in_planner && i == pi->nr - 1)) { + (decoMode(in_planner) == VPMB && !in_planner && i == pi.nr - 1)) { /* only calculate ndl/tts on every 30 seconds */ - if ((entry->sec - last_ndl_tts_calc_time) < 30 && i != pi->nr - 1) { - struct plot_data *prev_entry = (entry - 1); - entry->stoptime_calc = prev_entry->stoptime_calc; - entry->stopdepth_calc = prev_entry->stopdepth_calc; - entry->tts_calc = prev_entry->tts_calc; - entry->ndl_calc = prev_entry->ndl_calc; + if ((entry.sec - last_ndl_tts_calc_time) < 30 && i != pi.nr - 1) { + struct plot_data &prev_entry = pi.entry[i - 1]; + entry.stoptime_calc = prev_entry.stoptime_calc; + entry.stopdepth_calc = prev_entry.stopdepth_calc; + entry.tts_calc = prev_entry.tts_calc; + entry.ndl_calc = prev_entry.ndl_calc; continue; } - last_ndl_tts_calc_time = entry->sec; + last_ndl_tts_calc_time = entry.sec; /* We are going to mess up deco state, so store it for later restore */ deco_state_cache cache_data; cache_data.cache(ds); calculate_ndl_tts(ds, dive, entry, gasmix, surface_pressure, current_divemode, in_planner); - if (decoMode(in_planner) == VPMB && !in_planner && i == pi->nr - 1) - final_tts = entry->tts_calc; + if (decoMode(in_planner) == VPMB && !in_planner && i == pi.nr - 1) + final_tts = entry.tts_calc; /* Restore "real" deco state for next real time step */ cache_data.restore(ds, decoMode(in_planner) == VPMB); } @@ -1120,17 +1112,17 @@ static void calculate_deco_information(struct deco_state *ds, const struct deco_ /* Sort the o2 pressure values. There are so few that a simple bubble sort * will do */ -extern "C" void sort_o2_pressures(int *sensorn, int np, struct plot_data *entry) +void sort_o2_pressures(int *sensorn, int np, const struct plot_data &entry) { int smallest, position, old; for (int i = 0; i < np - 1; i++) { position = i; - smallest = entry->o2sensor[sensorn[i]].mbar; + smallest = entry.o2sensor[sensorn[i]].mbar; for (int j = i+1; j < np; j++) - if (entry->o2sensor[sensorn[j]].mbar < smallest) { + if (entry.o2sensor[sensorn[j]].mbar < smallest) { position = j; - smallest = entry->o2sensor[sensorn[j]].mbar; + smallest = entry.o2sensor[sensorn[j]].mbar; } old = sensorn[i]; sensorn[i] = position; @@ -1143,21 +1135,21 @@ extern "C" void sort_o2_pressures(int *sensorn, int np, struct plot_data *entry) * calculates the po2 value from the sensor data. If there are at least 3 sensors, sensors are voted out until * their span is within diff_limit. */ -static int calculate_ccr_po2(struct plot_data *entry, const struct divecomputer *dc) +static int calculate_ccr_po2(struct plot_data &entry, const struct divecomputer *dc) { int sump = 0, minp = 0, maxp = 0; int sensorn[MAX_O2_SENSORS]; int i, np = 0; for (i = 0; i < dc->no_o2sensors && i < MAX_O2_SENSORS; i++) - if (entry->o2sensor[i].mbar) { // Valid reading + if (entry.o2sensor[i].mbar) { // Valid reading sensorn[np++] = i; - sump += entry->o2sensor[i].mbar; + sump += entry.o2sensor[i].mbar; } if (np == 0) - return entry->o2pressure.mbar; + return entry.o2pressure.mbar; else if (np == 1) - return entry->o2sensor[sensorn[0]].mbar; + return entry.o2sensor[sensorn[0]].mbar; maxp = np - 1; sort_o2_pressures(sensorn, np, entry); @@ -1165,15 +1157,15 @@ static int calculate_ccr_po2(struct plot_data *entry, const struct divecomputer // This is the Shearwater voting logic: If there are still at least three sensors and one // differs by more than 20% from the closest it is voted out. while (maxp - minp > 1) { - if (entry->o2sensor[sensorn[minp + 1]].mbar - entry->o2sensor[sensorn[minp]].mbar > + if (entry.o2sensor[sensorn[minp + 1]].mbar - entry.o2sensor[sensorn[minp]].mbar > sump / (maxp - minp + 1) / 5) { - sump -= entry->o2sensor[sensorn[minp]].mbar; + sump -= entry.o2sensor[sensorn[minp]].mbar; ++minp; continue; } - if (entry->o2sensor[sensorn[maxp]].mbar - entry->o2sensor[sensorn[maxp - 1]].mbar > + if (entry.o2sensor[sensorn[maxp]].mbar - entry.o2sensor[sensorn[maxp - 1]].mbar > sump / (maxp - minp +1) / 5) { - sump -= entry->o2sensor[sensorn[maxp]].mbar; + sump -= entry.o2sensor[sensorn[maxp]].mbar; --maxp; continue; } @@ -1184,12 +1176,12 @@ static int calculate_ccr_po2(struct plot_data *entry, const struct divecomputer } -static double gas_density(const struct gas_pressures *pressures) +static double gas_density(const struct gas_pressures &pressures) { - return (pressures->o2 * O2_DENSITY + pressures->he * HE_DENSITY + pressures->n2 * N2_DENSITY) / 1000.0; + return (pressures.o2 * O2_DENSITY + pressures.he * HE_DENSITY + pressures.n2 * N2_DENSITY) / 1000.0; } -static void calculate_gas_information_new(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi) +static void calculate_gas_information_new(const struct dive *dive, const struct divecomputer *dc, struct plot_info &pi) { int i; double amb_pressure; @@ -1197,19 +1189,19 @@ static void calculate_gas_information_new(const struct dive *dive, const struct const struct event *evg = NULL, *evd = NULL; enum divemode_t current_divemode = UNDEF_COMP_TYPE; - for (i = 1; i < pi->nr; i++) { + for (i = 1; i < pi.nr; i++) { double fn2, fhe; - struct plot_data *entry = pi->entry + i; + struct plot_data &entry = pi.entry[i]; - gasmix = get_gasmix(dive, dc, entry->sec, &evg, gasmix); - amb_pressure = depth_to_bar(entry->depth, dive); - current_divemode = get_current_divemode(dc, entry->sec, &evd, ¤t_divemode); - fill_pressures(&entry->pressures, amb_pressure, gasmix, (current_divemode == OC) ? 0.0 : entry->o2pressure.mbar / 1000.0, current_divemode); - fn2 = 1000.0 * entry->pressures.n2 / amb_pressure; - fhe = 1000.0 * entry->pressures.he / amb_pressure; + gasmix = get_gasmix(dive, dc, entry.sec, &evg, gasmix); + amb_pressure = depth_to_bar(entry.depth, dive); + current_divemode = get_current_divemode(dc, entry.sec, &evd, ¤t_divemode); + fill_pressures(&entry.pressures, amb_pressure, gasmix, (current_divemode == OC) ? 0.0 : entry.o2pressure.mbar / 1000.0, current_divemode); + fn2 = 1000.0 * entry.pressures.n2 / amb_pressure; + fhe = 1000.0 * entry.pressures.he / amb_pressure; if (dc->divemode == PSCR) { // OC pO2 is calulated for PSCR with or without external PO2 monitoring. - struct gasmix gasmix2 = get_gasmix(dive, dc, entry->sec, &evg, gasmix); - entry->scr_OC_pO2.mbar = (int) depth_to_mbar(entry->depth, dive) * get_o2(gasmix2) / 1000; + struct gasmix gasmix2 = get_gasmix(dive, dc, entry.sec, &evg, gasmix); + entry.scr_OC_pO2.mbar = (int) depth_to_mbar(entry.depth, dive) * get_o2(gasmix2) / 1000; } /* Calculate MOD, EAD, END and EADD based on partial pressures calculated before @@ -1217,27 +1209,27 @@ static void calculate_gas_information_new(const struct dive *dive, const struct * END takes O₂ + N₂ (air) into account ("Narcotic" for trimix dives) * EAD just uses N₂ ("Air" for nitrox dives) */ pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) }; - entry->mod = gas_mod(gasmix, modpO2, dive, 1).mm; - entry->end = mbar_to_depth(lrint(depth_to_mbarf(entry->depth, dive) * (1000 - fhe) / 1000.0), dive); - entry->ead = mbar_to_depth(lrint(depth_to_mbarf(entry->depth, dive) * fn2 / (double)N2_IN_AIR), dive); - entry->eadd = mbar_to_depth(lrint(depth_to_mbarf(entry->depth, dive) * - (entry->pressures.o2 / amb_pressure * O2_DENSITY + - entry->pressures.n2 / amb_pressure * N2_DENSITY + - entry->pressures.he / amb_pressure * HE_DENSITY) / + entry.mod = gas_mod(gasmix, modpO2, dive, 1).mm; + entry.end = mbar_to_depth(lrint(depth_to_mbarf(entry.depth, dive) * (1000 - fhe) / 1000.0), dive); + entry.ead = mbar_to_depth(lrint(depth_to_mbarf(entry.depth, dive) * fn2 / (double)N2_IN_AIR), dive); + entry.eadd = mbar_to_depth(lrint(depth_to_mbarf(entry.depth, dive) * + (entry.pressures.o2 / amb_pressure * O2_DENSITY + + entry.pressures.n2 / amb_pressure * N2_DENSITY + + entry.pressures.he / amb_pressure * HE_DENSITY) / (O2_IN_AIR * O2_DENSITY + N2_IN_AIR * N2_DENSITY) * 1000), dive); - entry->density = gas_density(&entry->pressures); - if (entry->mod < 0) - entry->mod = 0; - if (entry->ead < 0) - entry->ead = 0; - if (entry->end < 0) - entry->end = 0; - if (entry->eadd < 0) - entry->eadd = 0; + entry.density = gas_density(entry.pressures); + if (entry.mod < 0) + entry.mod = 0; + if (entry.ead < 0) + entry.ead = 0; + if (entry.end < 0) + entry.end = 0; + if (entry.eadd < 0) + entry.eadd = 0; } } -static void fill_o2_values(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi) +static void fill_o2_values(const struct dive *dive, const struct divecomputer *dc, struct plot_info &pi) /* In the samples from each dive computer, there may be uninitialised oxygen * sensor or setpoint values, e.g. when events were inserted into the dive log * or if the dive computer does not report o2 values with every sample. But @@ -1251,25 +1243,25 @@ static void fill_o2_values(const struct dive *dive, const struct divecomputer *d pressure_t last_sensor[3], o2pressure; pressure_t amb_pressure; - for (i = 0; i < pi->nr; i++) { - struct plot_data *entry = pi->entry + i; + for (i = 0; i < pi.nr; i++) { + struct plot_data &entry = pi.entry[i]; if (dc->divemode == CCR || (dc->divemode == PSCR && dc->no_o2sensors)) { if (i == 0) { // For 1st iteration, initialise the last_sensor values for (j = 0; j < dc->no_o2sensors; j++) - last_sensor[j].mbar = pi->entry->o2sensor[j].mbar; + last_sensor[j].mbar = entry.o2sensor[j].mbar; } else { // Now re-insert the missing oxygen pressure values for (j = 0; j < dc->no_o2sensors; j++) - if (entry->o2sensor[j].mbar) - last_sensor[j].mbar = entry->o2sensor[j].mbar; + if (entry.o2sensor[j].mbar) + last_sensor[j].mbar = entry.o2sensor[j].mbar; else - entry->o2sensor[j].mbar = last_sensor[j].mbar; + entry.o2sensor[j].mbar = last_sensor[j].mbar; } // having initialised the empty o2 sensor values for this point on the profile, - amb_pressure.mbar = depth_to_mbar(entry->depth, dive); + amb_pressure.mbar = depth_to_mbar(entry.depth, dive); o2pressure.mbar = calculate_ccr_po2(entry, dc); // ...calculate the po2 based on the sensor data - entry->o2pressure.mbar = std::min(o2pressure.mbar, amb_pressure.mbar); + entry.o2pressure.mbar = std::min(o2pressure.mbar, amb_pressure.mbar); } else { - entry->o2pressure.mbar = 0; // initialise po2 to zero for dctype = OC + entry.o2pressure.mbar = 0; // initialise po2 to zero for dctype = OC } } } @@ -1278,7 +1270,7 @@ static void fill_o2_values(const struct dive *dive, const struct divecomputer *d /* A CCR debug function that writes the cylinder pressure and the oxygen values to the file debug_print_profiledata.dat: * Called in create_plot_info_new() */ -static void debug_print_profiledata(struct plot_info *pi) +static void debug_print_profiledata(struct plot_info &pi) { FILE *f1; struct plot_data *entry; @@ -1287,25 +1279,17 @@ static void debug_print_profiledata(struct plot_info *pi) printf("File open error for: debug_print_profiledata.dat\n"); } else { fprintf(f1, "id t1 gas gasint t2 t3 dil dilint t4 t5 setpoint sensor1 sensor2 sensor3 t6 po2 fo2\n"); - for (i = 0; i < pi->nr; i++) { - entry = pi->entry + i; + for (i = 0; i < pi.nr; i++) { + struct plot_data &entry = pi.entry[i]; fprintf(f1, "%d gas=%8d %8d ; dil=%8d %8d ; o2_sp= %d %d %d %d PO2= %f\n", i, get_plot_sensor_pressure(pi, i), get_plot_interpolated_pressure(pi, i), O2CYLINDER_PRESSURE(entry), INTERPOLATED_O2CYLINDER_PRESSURE(entry), - entry->o2pressure.mbar, entry->o2sensor[0].mbar, entry->o2sensor[1].mbar, entry->o2sensor[2].mbar, entry->pressures.o2); + entry.o2pressure.mbar, entry.o2sensor[0].mbar, entry.o2sensor[1].mbar, entry.o2sensor[2].mbar, entry.pressures.o2); } fclose(f1); } } #endif -/* - * Initialize a plot_info structure to all-zeroes - */ -extern "C" void init_plot_info(struct plot_info *pi) -{ - memset(pi, 0, sizeof(*pi)); -} - /* * Create a plot-info with smoothing and ranged min/max * @@ -1313,34 +1297,33 @@ extern "C" void init_plot_info(struct plot_info *pi) * sides, so that you can do end-points without having to worry * about it. * - * The old data will be freed. Before the first call, the plot - * info must be initialized with init_plot_info(). + * The old data will be freed. */ -extern "C" void create_plot_info_new(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi, const struct deco_state *planner_ds) +struct plot_info create_plot_info_new(const struct dive *dive, const struct divecomputer *dc, const struct deco_state *planner_ds) { int o2, he, o2max; struct deco_state plot_deco_state; bool in_planner = planner_ds != NULL; init_decompression(&plot_deco_state, dive, in_planner); - free_plot_info_data(pi); + plot_info pi; calculate_max_limits_new(dive, dc, pi, in_planner); get_dive_gas(dive, &o2, &he, &o2max); if (dc->divemode == FREEDIVE) { - pi->dive_type = plot_info::FREEDIVING; + pi.dive_type = plot_info::FREEDIVING; } else if (he > 0) { - pi->dive_type = plot_info::TRIMIX; + pi.dive_type = plot_info::TRIMIX; } else { if (o2) - pi->dive_type = plot_info::NITROX; + pi.dive_type = plot_info::NITROX; else - pi->dive_type = plot_info::AIR; + pi.dive_type = plot_info::AIR; } populate_plot_entries(dive, dc, pi); check_setpoint_events(dive, dc, pi); /* Populate setpoints */ setup_gas_sensor_pressure(dive, dc, pi); /* Try to populate our gas pressure knowledge */ - for (int cyl = 0; cyl < pi->nr_cylinders; cyl++) + for (int cyl = 0; cyl < pi.nr_cylinders; cyl++) populate_pressure_information(dive, dc, pi, cyl); fill_o2_values(dive, dc, pi); /* .. and insert the O2 sensor data having 0 values. */ calculate_sac(dive, dc, pi); /* Calculate sac */ @@ -1353,24 +1336,25 @@ extern "C" void create_plot_info_new(const struct dive *dive, const struct divec debug_print_profiledata(pi); #endif - pi->meandepth = dive->dc.meandepth.mm; + pi.meandepth = dive->dc.meandepth.mm; analyze_plot_info(pi); + return pi; } -static std::vector plot_string(const struct dive *d, const struct plot_info *pi, int idx) +static std::vector plot_string(const struct dive *d, const struct plot_info &pi, int idx) { int pressurevalue, mod, ead, end, eadd; const char *depth_unit, *pressure_unit, *temp_unit, *vertical_speed_unit; double depthvalue, tempvalue, speedvalue, sacvalue; int decimals, cyl; const char *unit; - const struct plot_data *entry = pi->entry + idx; + const struct plot_data &entry = pi.entry[idx]; std::vector res; - depthvalue = get_depth_units(entry->depth, NULL, &depth_unit); - res.push_back(casprintf_loc(translate("gettextFromC", "@: %d:%02d"), FRACTION_TUPLE(entry->sec, 60))); + depthvalue = get_depth_units(entry.depth, NULL, &depth_unit); + res.push_back(casprintf_loc(translate("gettextFromC", "@: %d:%02d"), FRACTION_TUPLE(entry.sec, 60))); res.push_back(casprintf_loc(translate("gettextFromC", "D: %.1f%s"), depthvalue, depth_unit)); - for (cyl = 0; cyl < pi->nr_cylinders; cyl++) { + for (cyl = 0; cyl < pi.nr_cylinders; cyl++) { int mbar = get_plot_pressure(pi, idx, cyl); if (!mbar) continue; @@ -1378,162 +1362,162 @@ static std::vector plot_string(const struct dive *d, const struct p pressurevalue = get_pressure_units(mbar, &pressure_unit); res.push_back(casprintf_loc(translate("gettextFromC", "P: %d%s (%s)"), pressurevalue, pressure_unit, gasname(mix))); } - if (entry->temperature) { - tempvalue = get_temp_units(entry->temperature, &temp_unit); + if (entry.temperature) { + tempvalue = get_temp_units(entry.temperature, &temp_unit); res.push_back(casprintf_loc(translate("gettextFromC", "T: %.1f%s"), tempvalue, temp_unit)); } - speedvalue = get_vertical_speed_units(abs(entry->speed), NULL, &vertical_speed_unit); + speedvalue = get_vertical_speed_units(abs(entry.speed), NULL, &vertical_speed_unit); /* Ascending speeds are positive, descending are negative */ - if (entry->speed > 0) + if (entry.speed > 0) speedvalue *= -1; res.push_back(casprintf_loc(translate("gettextFromC", "V: %.1f%s"), speedvalue, vertical_speed_unit)); - sacvalue = get_volume_units(entry->sac, &decimals, &unit); - if (entry->sac && prefs.show_sac) + sacvalue = get_volume_units(entry.sac, &decimals, &unit); + if (entry.sac && prefs.show_sac) res.push_back(casprintf_loc(translate("gettextFromC", "SAC: %.*f%s/min"), decimals, sacvalue, unit)); - if (entry->cns) - res.push_back(casprintf_loc(translate("gettextFromC", "CNS: %u%%"), entry->cns)); - if (prefs.pp_graphs.po2 && entry->pressures.o2 > 0) { - res.push_back(casprintf_loc(translate("gettextFromC", "pO₂: %.2fbar"), entry->pressures.o2)); - if (entry->scr_OC_pO2.mbar) - res.push_back(casprintf_loc(translate("gettextFromC", "SCR ΔpO₂: %.2fbar"), entry->scr_OC_pO2.mbar/1000.0 - entry->pressures.o2)); + if (entry.cns) + res.push_back(casprintf_loc(translate("gettextFromC", "CNS: %u%%"), entry.cns)); + if (prefs.pp_graphs.po2 && entry.pressures.o2 > 0) { + res.push_back(casprintf_loc(translate("gettextFromC", "pO₂: %.2fbar"), entry.pressures.o2)); + if (entry.scr_OC_pO2.mbar) + res.push_back(casprintf_loc(translate("gettextFromC", "SCR ΔpO₂: %.2fbar"), entry.scr_OC_pO2.mbar/1000.0 - entry.pressures.o2)); } - if (prefs.pp_graphs.pn2 && entry->pressures.n2 > 0) - res.push_back(casprintf_loc(translate("gettextFromC", "pN₂: %.2fbar"), entry->pressures.n2)); - if (prefs.pp_graphs.phe && entry->pressures.he > 0) - res.push_back(casprintf_loc(translate("gettextFromC", "pHe: %.2fbar"), entry->pressures.he)); - if (prefs.mod && entry->mod > 0) { - mod = lrint(get_depth_units(entry->mod, NULL, &depth_unit)); + if (prefs.pp_graphs.pn2 && entry.pressures.n2 > 0) + res.push_back(casprintf_loc(translate("gettextFromC", "pN₂: %.2fbar"), entry.pressures.n2)); + if (prefs.pp_graphs.phe && entry.pressures.he > 0) + res.push_back(casprintf_loc(translate("gettextFromC", "pHe: %.2fbar"), entry.pressures.he)); + if (prefs.mod && entry.mod > 0) { + mod = lrint(get_depth_units(entry.mod, NULL, &depth_unit)); res.push_back(casprintf_loc(translate("gettextFromC", "MOD: %d%s"), mod, depth_unit)); } - eadd = lrint(get_depth_units(entry->eadd, NULL, &depth_unit)); + eadd = lrint(get_depth_units(entry.eadd, NULL, &depth_unit)); if (prefs.ead) { - switch (pi->dive_type) { + switch (pi.dive_type) { case plot_info::NITROX: - if (entry->ead > 0) { - ead = lrint(get_depth_units(entry->ead, NULL, &depth_unit)); + if (entry.ead > 0) { + ead = lrint(get_depth_units(entry.ead, NULL, &depth_unit)); res.push_back(casprintf_loc(translate("gettextFromC", "EAD: %d%s"), ead, depth_unit)); - res.push_back(casprintf_loc(translate("gettextFromC", "EADD: %d%s / %.1fg/ℓ"), eadd, depth_unit, entry->density)); + res.push_back(casprintf_loc(translate("gettextFromC", "EADD: %d%s / %.1fg/ℓ"), eadd, depth_unit, entry.density)); break; } case plot_info::TRIMIX: - if (entry->end > 0) { - end = lrint(get_depth_units(entry->end, NULL, &depth_unit)); + if (entry.end > 0) { + end = lrint(get_depth_units(entry.end, NULL, &depth_unit)); res.push_back(casprintf_loc(translate("gettextFromC", "END: %d%s"), end, depth_unit)); - res.push_back(casprintf_loc(translate("gettextFromC", "EADD: %d%s / %.1fg/ℓ"), eadd, depth_unit, entry->density)); + res.push_back(casprintf_loc(translate("gettextFromC", "EADD: %d%s / %.1fg/ℓ"), eadd, depth_unit, entry.density)); break; } case plot_info::AIR: - if (entry->density > 0) { - res.push_back(casprintf_loc(translate("gettextFromC", "Density: %.1fg/ℓ"), entry->density)); + if (entry.density > 0) { + res.push_back(casprintf_loc(translate("gettextFromC", "Density: %.1fg/ℓ"), entry.density)); } case plot_info::FREEDIVING: /* nothing */ break; } } - if (entry->stopdepth) { - depthvalue = get_depth_units(entry->stopdepth, NULL, &depth_unit); - if (entry->ndl > 0) { + if (entry.stopdepth) { + depthvalue = get_depth_units(entry.stopdepth, NULL, &depth_unit); + if (entry.ndl > 0) { /* this is a safety stop as we still have ndl */ - if (entry->stoptime) - res.push_back(casprintf_loc(translate("gettextFromC", "Safety stop: %umin @ %.0f%s"), div_up(entry->stoptime, 60), + if (entry.stoptime) + res.push_back(casprintf_loc(translate("gettextFromC", "Safety stop: %umin @ %.0f%s"), div_up(entry.stoptime, 60), depthvalue, depth_unit)); else res.push_back(casprintf_loc(translate("gettextFromC", "Safety stop: unknown time @ %.0f%s"), depthvalue, depth_unit)); } else { /* actual deco stop */ - if (entry->stoptime) - res.push_back(casprintf_loc(translate("gettextFromC", "Deco: %umin @ %.0f%s"), div_up(entry->stoptime, 60), + if (entry.stoptime) + res.push_back(casprintf_loc(translate("gettextFromC", "Deco: %umin @ %.0f%s"), div_up(entry.stoptime, 60), depthvalue, depth_unit)); else res.push_back(casprintf_loc(translate("gettextFromC", "Deco: unknown time @ %.0f%s"), depthvalue, depth_unit)); } - } else if (entry->in_deco) { + } else if (entry.in_deco) { res.push_back(translate("gettextFromC", "In deco")); - } else if (entry->ndl >= 0) { - res.push_back(casprintf_loc(translate("gettextFromC", "NDL: %umin"), div_up(entry->ndl, 60))); + } else if (entry.ndl >= 0) { + res.push_back(casprintf_loc(translate("gettextFromC", "NDL: %umin"), div_up(entry.ndl, 60))); } - if (entry->tts) - res.push_back(casprintf_loc(translate("gettextFromC", "TTS: %umin"), div_up(entry->tts, 60))); - if (entry->stopdepth_calc && entry->stoptime_calc) { - depthvalue = get_depth_units(entry->stopdepth_calc, NULL, &depth_unit); - res.push_back(casprintf_loc(translate("gettextFromC", "Deco: %umin @ %.0f%s (calc)"), div_up(entry->stoptime_calc, 60), + if (entry.tts) + res.push_back(casprintf_loc(translate("gettextFromC", "TTS: %umin"), div_up(entry.tts, 60))); + if (entry.stopdepth_calc && entry.stoptime_calc) { + depthvalue = get_depth_units(entry.stopdepth_calc, NULL, &depth_unit); + res.push_back(casprintf_loc(translate("gettextFromC", "Deco: %umin @ %.0f%s (calc)"), div_up(entry.stoptime_calc, 60), depthvalue, depth_unit)); - } else if (entry->in_deco_calc) { + } else if (entry.in_deco_calc) { /* This means that we have no NDL left, * and we have no deco stop, * so if we just accend to the surface slowly * (ascent_mm_per_step / ascent_s_per_step) * everything will be ok. */ res.push_back(translate("gettextFromC", "In deco (calc)")); - } else if (prefs.calcndltts && entry->ndl_calc != 0) { - if(entry->ndl_calc < MAX_PROFILE_DECO) - res.push_back(casprintf_loc(translate("gettextFromC", "NDL: %umin (calc)"), div_up(entry->ndl_calc, 60))); + } else if (prefs.calcndltts && entry.ndl_calc != 0) { + if(entry.ndl_calc < MAX_PROFILE_DECO) + res.push_back(casprintf_loc(translate("gettextFromC", "NDL: %umin (calc)"), div_up(entry.ndl_calc, 60))); else res.push_back(translate("gettextFromC", "NDL: >2h (calc)")); } - if (entry->tts_calc) { - if (entry->tts_calc < MAX_PROFILE_DECO) - res.push_back(casprintf_loc(translate("gettextFromC", "TTS: %umin (calc)"), div_up(entry->tts_calc, 60))); + if (entry.tts_calc) { + if (entry.tts_calc < MAX_PROFILE_DECO) + res.push_back(casprintf_loc(translate("gettextFromC", "TTS: %umin (calc)"), div_up(entry.tts_calc, 60))); else res.push_back(translate("gettextFromC", "TTS: >2h (calc)")); } - if (entry->rbt) - res.push_back(casprintf_loc(translate("gettextFromC", "RBT: %umin"), div_up(entry->rbt, 60))); + if (entry.rbt) + res.push_back(casprintf_loc(translate("gettextFromC", "RBT: %umin"), div_up(entry.rbt, 60))); if (prefs.decoinfo) { - if (entry->current_gf > 0.0) - res.push_back(casprintf_loc(translate("gettextFromC", "GF %d%%"), (int)(100.0 * entry->current_gf))); - if (entry->surface_gf > 0.0) - res.push_back(casprintf_loc(translate("gettextFromC", "Surface GF %.0f%%"), entry->surface_gf)); - if (entry->ceiling) { - depthvalue = get_depth_units(entry->ceiling, NULL, &depth_unit); + if (entry.current_gf > 0.0) + res.push_back(casprintf_loc(translate("gettextFromC", "GF %d%%"), (int)(100.0 * entry.current_gf))); + if (entry.surface_gf > 0.0) + res.push_back(casprintf_loc(translate("gettextFromC", "Surface GF %.0f%%"), entry.surface_gf)); + if (entry.ceiling) { + depthvalue = get_depth_units(entry.ceiling, NULL, &depth_unit); res.push_back(casprintf_loc(translate("gettextFromC", "Calculated ceiling %.1f%s"), depthvalue, depth_unit)); if (prefs.calcalltissues) { int k; for (k = 0; k < 16; k++) { - if (entry->ceilings[k]) { - depthvalue = get_depth_units(entry->ceilings[k], NULL, &depth_unit); + if (entry.ceilings[k]) { + depthvalue = get_depth_units(entry.ceilings[k], NULL, &depth_unit); res.push_back(casprintf_loc(translate("gettextFromC", "Tissue %.0fmin: %.1f%s"), buehlmann_N2_t_halflife[k], depthvalue, depth_unit)); } } } } } - if (entry->icd_warning) + if (entry.icd_warning) res.push_back(translate("gettextFromC", "ICD in leading tissue")); - if (entry->heartbeat && prefs.hrgraph) - res.push_back(casprintf_loc(translate("gettextFromC", "heart rate: %d"), entry->heartbeat)); - if (entry->bearing >= 0) - res.push_back(casprintf_loc(translate("gettextFromC", "bearing: %d"), entry->bearing)); - if (entry->running_sum) { - depthvalue = get_depth_units(entry->running_sum / entry->sec, NULL, &depth_unit); + if (entry.heartbeat && prefs.hrgraph) + res.push_back(casprintf_loc(translate("gettextFromC", "heart rate: %d"), entry.heartbeat)); + if (entry.bearing >= 0) + res.push_back(casprintf_loc(translate("gettextFromC", "bearing: %d"), entry.bearing)); + if (entry.running_sum) { + depthvalue = get_depth_units(entry.running_sum / entry.sec, NULL, &depth_unit); res.push_back(casprintf_loc(translate("gettextFromC", "mean depth to here %.1f%s"), depthvalue, depth_unit)); } return res; } -std::pair> get_plot_details_new(const struct dive *d, const struct plot_info *pi, int time) +std::pair> get_plot_details_new(const struct dive *d, const struct plot_info &pi, int time) { /* The two first and the two last plot entries do not have useful data */ - if (pi->nr <= 4) + if (pi.entry.size() <= 4) return { 0, {} }; // binary search for sample index - auto it = std::lower_bound(pi->entry + 2, pi->entry + pi->nr - 3, time, + auto it = std::lower_bound(pi.entry.begin() + 2, pi.entry.end() - 3, time, [] (const plot_data &d, int time) { return d.sec < time; }); - int idx = it - pi->entry; + int idx = it - pi.entry.begin(); auto strings = plot_string(d, pi, idx); return std::make_pair(idx, strings); } /* Compare two plot_data entries and writes the results into a set of strings */ -std::vector compare_samples(const struct dive *d, const struct plot_info *pi, int idx1, int idx2, bool sum) +std::vector compare_samples(const struct dive *d, const struct plot_info &pi, int idx1, int idx2, bool sum) { std::string space(" "); const char *depth_unit, *pressure_unit, *vertical_speed_unit; @@ -1543,55 +1527,54 @@ std::vector compare_samples(const struct dive *d, const struct plot if (idx1 < 0 || idx2 < 0) return res; - if (pi->entry[idx1].sec > pi->entry[idx2].sec) { + if (pi.entry[idx1].sec > pi.entry[idx2].sec) { int tmp = idx2; idx2 = idx1; idx1 = tmp; - } else if (pi->entry[idx1].sec == pi->entry[idx2].sec) { + } else if (pi.entry[idx1].sec == pi.entry[idx2].sec) { return res; } - struct plot_data *start = pi->entry + idx1; - struct plot_data *stop = pi->entry + idx2; + const struct plot_data &start = pi.entry[idx1]; + const struct plot_data &stop = pi.entry[idx2]; int avg_speed = 0; int max_asc_speed = 0; int max_desc_speed = 0; - int delta_depth = abs(start->depth - stop->depth); - int delta_time = abs(start->sec - stop->sec); + int delta_depth = abs(start.depth - stop.depth); + int delta_time = abs(start.sec - stop.sec); int avg_depth = 0; int max_depth = 0; int min_depth = INT_MAX; - int last_sec = start->sec; + int last_sec = start.sec; volume_t cylinder_volume = { .mliter = 0, }; - std::vector start_pressures(pi->nr_cylinders, 0); - std::vector last_pressures(pi->nr_cylinders, 0); - std::vector bar_used(pi->nr_cylinders, 0); - std::vector volumes_used(pi->nr_cylinders, 0); - std::vector cylinder_is_used(pi->nr_cylinders, false); + std::vector start_pressures(pi.nr_cylinders, 0); + std::vector last_pressures(pi.nr_cylinders, 0); + std::vector bar_used(pi.nr_cylinders, 0); + std::vector volumes_used(pi.nr_cylinders, 0); + std::vector cylinder_is_used(pi.nr_cylinders, false); - struct plot_data *data = start; for (int i = idx1; i < idx2; ++i) { - data = pi->entry + i; + const struct plot_data &data = pi.entry[i]; if (sum) - avg_speed += abs(data->speed) * (data->sec - last_sec); + avg_speed += abs(data.speed) * (data.sec - last_sec); else - avg_speed += data->speed * (data->sec - last_sec); - avg_depth += data->depth * (data->sec - last_sec); + avg_speed += data.speed * (data.sec - last_sec); + avg_depth += data.depth * (data.sec - last_sec); - if (data->speed > max_desc_speed) - max_desc_speed = data->speed; - if (data->speed < max_asc_speed) - max_asc_speed = data->speed; + if (data.speed > max_desc_speed) + max_desc_speed = data.speed; + if (data.speed < max_asc_speed) + max_asc_speed = data.speed; - if (data->depth < min_depth) - min_depth = data->depth; - if (data->depth > max_depth) - max_depth = data->depth; + if (data.depth < min_depth) + min_depth = data.depth; + if (data.depth > max_depth) + max_depth = data.depth; - for (int cylinder_index = 0; cylinder_index < pi->nr_cylinders; cylinder_index++) { + for (int cylinder_index = 0; cylinder_index < pi.nr_cylinders; cylinder_index++) { int next_pressure = get_plot_pressure(pi, i, cylinder_index); if (next_pressure && !start_pressures[cylinder_index]) start_pressures[cylinder_index] = next_pressure; @@ -1614,11 +1597,11 @@ std::vector compare_samples(const struct dive *d, const struct plot last_pressures[cylinder_index] = next_pressure; } - last_sec = data->sec; + last_sec = data.sec; } - avg_depth /= stop->sec - start->sec; - avg_speed /= stop->sec - start->sec; + avg_depth /= stop.sec - start.sec; + avg_speed /= stop.sec - start.sec; std::string l = casprintf_loc(translate("gettextFromC", "ΔT:%d:%02dmin"), delta_time / 60, delta_time % 60); @@ -1648,7 +1631,7 @@ std::vector compare_samples(const struct dive *d, const struct plot int total_volume_used = 0; bool cylindersizes_are_identical = true; bool sac_is_determinable = true; - for (int cylinder_index = 0; cylinder_index < pi->nr_cylinders; cylinder_index++) { + for (int cylinder_index = 0; cylinder_index < pi.nr_cylinders; cylinder_index++) { if (cylinder_is_used[cylinder_index]) { total_bar_used += bar_used[cylinder_index]; total_volume_used += volumes_used[cylinder_index]; diff --git a/core/profile.h b/core/profile.h index 8812937d9..378014250 100644 --- a/core/profile.h +++ b/core/profile.h @@ -2,20 +2,22 @@ #ifndef PROFILE_H #define PROFILE_H -#include "dive.h" -#include "sample.h" - #ifdef __cplusplus -extern "C" { -#endif -typedef enum { +#include "gas.h" // gas_pressures +#include "sample.h" // MAX_O2_SENSORS + +#include +#include +#include + +enum velocity_t { STABLE, SLOW, MODERATE, FAST, CRAZY -} velocity_t; +}; enum plot_pressure { SENSOR_PR = 0, @@ -25,6 +27,7 @@ enum plot_pressure { struct membuffer; struct deco_state; +struct dive; struct divecomputer; /* @@ -35,72 +38,74 @@ struct plot_pressure_data { }; struct plot_data { - unsigned int in_deco : 1; - int sec; - int temperature; + bool in_deco = false; + int sec = 0; + int temperature = 0; /* Depth info */ - int depth; - int ceiling; - int ceilings[16]; - int percentages[16]; - int ndl; - int tts; - int rbt; - int stoptime; - int stopdepth; - int cns; - int smoothed; - int sac; - int running_sum; + int depth = 0; + int ceiling = 0; + int ceilings[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + int percentages[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + int ndl = 0; + int tts = 0; + int rbt = 0; + int stoptime = 0; + int stopdepth = 0; + int cns = 0; + int smoothed = 0; + int sac = 0; + int running_sum = 0; struct gas_pressures pressures; - pressure_t o2pressure; // for rebreathers, this is consensus measured po2, or setpoint otherwise. 0 for OC. - pressure_t o2sensor[MAX_O2_SENSORS]; //for rebreathers with several sensors - pressure_t o2setpoint; - pressure_t scr_OC_pO2; - int mod, ead, end, eadd; - velocity_t velocity; - int speed; + // TODO: make pressure_t default to 0 + pressure_t o2pressure = { 0 }; // for rebreathers, this is consensus measured po2, or setpoint otherwise. 0 for OC. + pressure_t o2sensor[MAX_O2_SENSORS] = {{ 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 }}; //for rebreathers with several sensors + pressure_t o2setpoint = { 0 }; + pressure_t scr_OC_pO2 = { 0 }; + int mod = 0, ead = 0, end = 0, eadd = 0; + velocity_t velocity = STABLE; + int speed = 0; /* values calculated by us */ - unsigned int in_deco_calc : 1; - int ndl_calc; - int tts_calc; - int stoptime_calc; - int stopdepth_calc; - int pressure_time; - int heartbeat; - int bearing; - double ambpressure; - double gfline; - double surface_gf; - double current_gf; - double density; - bool icd_warning; + bool in_deco_calc = false; + int ndl_calc = 0; + int tts_calc = 0; + int stoptime_calc = 0; + int stopdepth_calc = 0; + int pressure_time = 0; + int heartbeat = 0; + int bearing = 0; + double ambpressure = 0.0; + double gfline = 0.0; + double surface_gf = 0.0; + double current_gf = 0.0; + double density = 0.0; + bool icd_warning = false; }; /* Plot info with smoothing, velocity indication * and one-, two- and three-minute minimums and maximums */ struct plot_info { - int nr; - int nr_cylinders; - int maxtime; - int meandepth, maxdepth; - int minpressure, maxpressure; - int minhr, maxhr; - int mintemp, maxtemp; - enum {AIR, NITROX, TRIMIX, FREEDIVING} dive_type; - double endtempcoord; - double maxpp; - bool waypoint_above_ceiling; - struct plot_data *entry; - struct plot_pressure_data *pressures; /* cylinders.nr blocks of nr entries. */ + int nr = 0; // TODO: remove - redundant with entry.size() + int nr_cylinders = 0; + int maxtime = 0; + int meandepth = 0, maxdepth = 0; + int minpressure = 0, maxpressure = 0; + int minhr = 0, maxhr = 0; + int mintemp = 0, maxtemp = 0; + enum {AIR, NITROX, TRIMIX, FREEDIVING} dive_type = AIR; + double endtempcoord = 0.0; + double maxpp = 0.0; + bool waypoint_above_ceiling = false; + std::vector entry; + std::vector pressures; /* cylinders.nr blocks of nr entries. */ + + plot_info(); + ~plot_info(); }; #define AMB_PERCENTAGE 50.0 -extern void init_plot_info(struct plot_info *pi); /* when planner_dc is non-null, this is called in planner mode. */ -extern void create_plot_info_new(const struct dive *dive, const struct divecomputer *dc, struct plot_info *pi, const struct deco_state *planner_ds); -extern void free_plot_info_data(struct plot_info *pi); +extern struct plot_info create_plot_info_new(const struct dive *dive, const struct divecomputer *dc, const struct deco_state *planner_ds); /* * When showing dive profiles, we scale things to the @@ -110,46 +115,40 @@ extern void free_plot_info_data(struct plot_info *pi); * We also need to add 180 seconds at the end so the min/max * plots correctly */ -extern int get_maxtime(const struct plot_info *pi); +extern int get_maxtime(const struct plot_info &pi); /* get the maximum depth to which we want to plot */ -extern int get_maxdepth(const struct plot_info *pi); +extern int get_maxdepth(const struct plot_info &pi); -static inline int get_plot_pressure_data(const struct plot_info *pi, int idx, enum plot_pressure sensor, int cylinder) +static inline int get_plot_pressure_data(const struct plot_info &pi, int idx, enum plot_pressure sensor, int cylinder) { - return pi->pressures[cylinder + idx * pi->nr_cylinders].data[sensor]; + return pi.pressures[cylinder + idx * pi.nr_cylinders].data[sensor]; } -static inline void set_plot_pressure_data(struct plot_info *pi, int idx, enum plot_pressure sensor, int cylinder, int value) +static inline void set_plot_pressure_data(struct plot_info &pi, int idx, enum plot_pressure sensor, int cylinder, int value) { - pi->pressures[cylinder + idx * pi->nr_cylinders].data[sensor] = value; + pi.pressures[cylinder + idx * pi.nr_cylinders].data[sensor] = value; } -static inline int get_plot_sensor_pressure(const struct plot_info *pi, int idx, int cylinder) +static inline int get_plot_sensor_pressure(const struct plot_info &pi, int idx, int cylinder) { return get_plot_pressure_data(pi, idx, SENSOR_PR, cylinder); } -static inline int get_plot_interpolated_pressure(const struct plot_info *pi, int idx, int cylinder) +static inline int get_plot_interpolated_pressure(const struct plot_info &pi, int idx, int cylinder) { return get_plot_pressure_data(pi, idx, INTERPOLATED_PR, cylinder); } -static inline int get_plot_pressure(const struct plot_info *pi, int idx, int cylinder) +static inline int get_plot_pressure(const struct plot_info &pi, int idx, int cylinder) { int res = get_plot_sensor_pressure(pi, idx, cylinder); return res ? res : get_plot_interpolated_pressure(pi, idx, cylinder); } -#ifdef __cplusplus -} - -// C++ only formatting functions -#include -#include // Returns index of sample and array of strings describing the dive details at given time -std::pair> get_plot_details_new(const struct dive *d, const struct plot_info *pi, int time); -std::vector compare_samples(const struct dive *d, const struct plot_info *pi, int idx1, int idx2, bool sum); +std::pair> get_plot_details_new(const struct dive *d, const struct plot_info &pi, int time); +std::vector compare_samples(const struct dive *d, const struct plot_info &pi, int idx1, int idx2, bool sum); #endif #endif // PROFILE_H diff --git a/core/save-profiledata.cpp b/core/save-profiledata.cpp index a560eb19d..c5cc63035 100644 --- a/core/save-profiledata.cpp +++ b/core/save-profiledata.cpp @@ -1,9 +1,10 @@ +#include "core/save-profiledata.h" +#include "core/dive.h" #include "core/profile.h" #include "core/errorhelper.h" #include "core/file.h" #include "core/membuffer.h" #include "core/subsurface-string.h" -#include "core/save-profiledata.h" #include "core/version.h" #include @@ -41,45 +42,45 @@ static void put_video_time(struct membuffer *b, int secs) put_format(b, "%d:%02d:%02d.000,", hours, mins, secs); } -static void put_pd(struct membuffer *b, const struct plot_info *pi, int idx) +static void put_pd(struct membuffer *b, const struct plot_info &pi, int idx) { - const struct plot_data *entry = pi->entry + idx; + const struct plot_data &entry = pi.entry[idx]; - put_int(b, entry->in_deco); - put_int(b, entry->sec); - for (int c = 0; c < pi->nr_cylinders; c++) { + put_int(b, entry.in_deco); + put_int(b, entry.sec); + for (int c = 0; c < pi.nr_cylinders; c++) { put_int(b, get_plot_sensor_pressure(pi, idx, c)); put_int(b, get_plot_interpolated_pressure(pi, idx, c)); } - put_int(b, entry->temperature); - put_int(b, entry->depth); - put_int(b, entry->ceiling); + put_int(b, entry.temperature); + put_int(b, entry.depth); + put_int(b, entry.ceiling); for (int i = 0; i < 16; i++) - put_int(b, entry->ceilings[i]); + put_int(b, entry.ceilings[i]); for (int i = 0; i < 16; i++) - put_int(b, entry->percentages[i]); - put_int(b, entry->ndl); - put_int(b, entry->tts); - put_int(b, entry->rbt); - put_int(b, entry->stoptime); - put_int(b, entry->stopdepth); - put_int(b, entry->cns); - put_int(b, entry->smoothed); - put_int(b, entry->sac); - put_int(b, entry->running_sum); - put_double(b, entry->pressures.o2); - put_double(b, entry->pressures.n2); - put_double(b, entry->pressures.he); - put_int(b, entry->o2pressure.mbar); + put_int(b, entry.percentages[i]); + put_int(b, entry.ndl); + put_int(b, entry.tts); + put_int(b, entry.rbt); + put_int(b, entry.stoptime); + put_int(b, entry.stopdepth); + put_int(b, entry.cns); + put_int(b, entry.smoothed); + put_int(b, entry.sac); + put_int(b, entry.running_sum); + put_double(b, entry.pressures.o2); + put_double(b, entry.pressures.n2); + put_double(b, entry.pressures.he); + put_int(b, entry.o2pressure.mbar); for (int i = 0; i < MAX_O2_SENSORS; i++) - put_int(b, entry->o2sensor[i].mbar); - put_int(b, entry->o2setpoint.mbar); - put_int(b, entry->scr_OC_pO2.mbar); - put_int(b, entry->mod); - put_int(b, entry->ead); - put_int(b, entry->end); - put_int(b, entry->eadd); - switch (entry->velocity) { + put_int(b, entry.o2sensor[i].mbar); + put_int(b, entry.o2setpoint.mbar); + put_int(b, entry.scr_OC_pO2.mbar); + put_int(b, entry.mod); + put_int(b, entry.ead); + put_int(b, entry.end); + put_int(b, entry.eadd); + switch (entry.velocity) { case STABLE: put_csv_string(b, "STABLE"); break; @@ -96,20 +97,20 @@ static void put_pd(struct membuffer *b, const struct plot_info *pi, int idx) put_csv_string(b, "CRAZY"); break; } - put_int(b, entry->speed); - put_int(b, entry->in_deco_calc); - put_int(b, entry->ndl_calc); - put_int(b, entry->tts_calc); - put_int(b, entry->stoptime_calc); - put_int(b, entry->stopdepth_calc); - put_int(b, entry->pressure_time); - put_int(b, entry->heartbeat); - put_int(b, entry->bearing); - put_double(b, entry->ambpressure); - put_double(b, entry->gfline); - put_double(b, entry->surface_gf); - put_double(b, entry->density); - put_int_with_nl(b, entry->icd_warning ? 1 : 0); + put_int(b, entry.speed); + put_int(b, entry.in_deco_calc); + put_int(b, entry.ndl_calc); + put_int(b, entry.tts_calc); + put_int(b, entry.stoptime_calc); + put_int(b, entry.stopdepth_calc); + put_int(b, entry.pressure_time); + put_int(b, entry.heartbeat); + put_int(b, entry.bearing); + put_double(b, entry.ambpressure); + put_double(b, entry.gfline); + put_double(b, entry.surface_gf); + put_double(b, entry.density); + put_int_with_nl(b, entry.icd_warning ? 1 : 0); } static void put_headers(struct membuffer *b, int nr_cylinders) @@ -166,36 +167,36 @@ static void put_headers(struct membuffer *b, int nr_cylinders) put_csv_string_with_nl(b, "icd_warning"); } -static void put_st_event(struct membuffer *b, struct plot_data *entry, int offset, int length) +static void put_st_event(struct membuffer *b, const plot_data &entry, const plot_data &next_entry, int offset, int length) { double value; int decimals; const char *unit; - if (entry->sec < offset || entry->sec > offset + length) + if (entry.sec < offset || entry.sec > offset + length) return; put_format(b, "Dialogue: 0,"); - put_video_time(b, entry->sec - offset); - put_video_time(b, (entry+1)->sec - offset < length ? (entry+1)->sec - offset : length); + put_video_time(b, entry.sec - offset); + put_video_time(b, next_entry.sec - offset < length ? next_entry.sec - offset : length); put_format(b, "Default,,0,0,0,,"); - put_format(b, "%d:%02d ", FRACTION_TUPLE(entry->sec, 60)); - value = get_depth_units(entry->depth, &decimals, &unit); + put_format(b, "%d:%02d ", FRACTION_TUPLE(entry.sec, 60)); + value = get_depth_units(entry.depth, &decimals, &unit); put_format(b, "D=%02.2f %s ", value, unit); - if (entry->temperature) { - value = get_temp_units(entry->temperature, &unit); + if (entry.temperature) { + value = get_temp_units(entry.temperature, &unit); put_format(b, "T=%.1f%s ", value, unit); } // Only show NDL if it is not essentially infinite, show TTS for mandatory stops. - if (entry->ndl_calc < 3600) { - if (entry->ndl_calc > 0) - put_format(b, "NDL=%d:%02d ", FRACTION_TUPLE(entry->ndl_calc, 60)); + if (entry.ndl_calc < 3600) { + if (entry.ndl_calc > 0) + put_format(b, "NDL=%d:%02d ", FRACTION_TUPLE(entry.ndl_calc, 60)); else - if (entry->tts_calc > 0) - put_format(b, "TTS=%d:%02d ", FRACTION_TUPLE(entry->tts_calc, 60)); + if (entry.tts_calc > 0) + put_format(b, "TTS=%d:%02d ", FRACTION_TUPLE(entry.tts_calc, 60)); } - if (entry->surface_gf > 0.0) { - put_format(b, "sGF=%.1f%% ", entry->surface_gf); + if (entry.surface_gf > 0.0) { + put_format(b, "sGF=%.1f%% ", entry.surface_gf); } put_format(b, "\n"); } @@ -204,30 +205,25 @@ static void save_profiles_buffer(struct membuffer *b, bool select_only) { int i; struct dive *dive; - struct plot_info pi; struct deco_state *planner_deco_state = NULL; - init_plot_info(&pi); for_each_dive(i, dive) { if (select_only && !dive->selected) continue; - create_plot_info_new(dive, &dive->dc, &pi, planner_deco_state); + plot_info pi = create_plot_info_new(dive, &dive->dc, planner_deco_state); put_headers(b, pi.nr_cylinders); for (int i = 0; i < pi.nr; i++) - put_pd(b, &pi, i); + put_pd(b, pi, i); put_format(b, "\n"); - free_plot_info_data(&pi); } } void save_subtitles_buffer(struct membuffer *b, struct dive *dive, int offset, int length) { - struct plot_info pi; struct deco_state *planner_deco_state = NULL; - init_plot_info(&pi); - create_plot_info_new(dive, &dive->dc, &pi, planner_deco_state); + plot_info pi = create_plot_info_new(dive, &dive->dc, planner_deco_state); put_format(b, "[Script Info]\n"); put_format(b, "; Script generated by Subsurface %s\n", subsurface_canonical_version()); @@ -236,12 +232,9 @@ void save_subtitles_buffer(struct membuffer *b, struct dive *dive, int offset, i put_format(b, "Style: Default,Arial,12,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,7,10,10,10,0\n\n"); put_format(b, "[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n"); - for (int i = 0; i < pi.nr; i++) { - put_st_event(b, &pi.entry[i], offset, length); - } + for (int i = 0; i + 1 < pi.nr; i++) + put_st_event(b, pi.entry[i], pi.entry[i + 1], offset, length); put_format(b, "\n"); - - free_plot_info_data(&pi); } int save_profiledata(const char *filename, bool select_only) diff --git a/profile-widget/diveeventitem.cpp b/profile-widget/diveeventitem.cpp index c5fc5f975..0c8dc04fc 100644 --- a/profile-widget/diveeventitem.cpp +++ b/profile-widget/diveeventitem.cpp @@ -3,6 +3,7 @@ #include "profile-widget/divecartesianaxis.h" #include "profile-widget/divepixmapcache.h" #include "profile-widget/animationfunctions.h" +#include "core/dive.h" #include "core/event.h" #include "core/eventtype.h" #include "core/format.h" @@ -177,9 +178,9 @@ void DiveEventItem::eventVisibilityChanged(const QString&, bool) static int depthAtTime(const plot_info &pi, duration_t time) { // Do a binary search for the timestamp - auto it = std::lower_bound(pi.entry, pi.entry + pi.nr, time, + auto it = std::lower_bound(pi.entry.begin(), pi.entry.end(), time, [](const plot_data &d1, duration_t t) { return d1.sec < t.seconds; }); - if (it == pi.entry + pi.nr || it->sec != time.seconds) { + if (it == pi.entry.end() || it->sec != time.seconds) { qWarning("can't find a spot in the dataModel"); return DEPTH_NOT_FOUND; } diff --git a/profile-widget/diveprofileitem.cpp b/profile-widget/diveprofileitem.cpp index f4fd29a54..4b75bda82 100644 --- a/profile-widget/diveprofileitem.cpp +++ b/profile-widget/diveprofileitem.cpp @@ -49,7 +49,7 @@ void AbstractProfilePolygonItem::clipStop(double &x, double &y, double prev_x, d std::pair AbstractProfilePolygonItem::getPoint(int i) const { - const struct plot_data *data = pInfo.entry; + const auto &data = pInfo.entry; double x = data[i].sec; double y = accessor(data[i]); @@ -121,7 +121,7 @@ void DiveProfileItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o pen.setCosmetic(true); pen.setWidth(2); QPolygonF poly = polygon(); - const struct plot_data *data = pInfo.entry; + const auto &data = pInfo.entry; // This paints the colors of the velocities. for (int i = from + 1; i < to; i++) { QColor color = getColor((color_index_t)(VELOCITY_COLORS_START_IDX + data[i].velocity)); @@ -150,7 +150,7 @@ void DiveProfileItem::replot(const dive *d, int from, int to, bool in_planner) /* Show any ceiling we may have encountered */ if (prefs.dcceiling && !prefs.redceiling) { QPolygonF p = polygon(); - plot_data *entry = pInfo.entry + to - 1; + auto entry = pInfo.entry.begin() + (to - 1); for (int i = to - 1; i >= from; i--, entry--) { if (!entry->in_deco) { /* not in deco implies this is a safety stop, no ceiling */ @@ -176,7 +176,7 @@ void DiveProfileItem::replot(const dive *d, int from, int to, bool in_planner) const int half_interval = vAxis.getMinLabelDistance(hAxis); const int min_depth = 2000; // in mm const int min_prominence = 2000; // in mm (should this adapt to depth range?) - const plot_data *data = pInfo.entry; + const auto &data = pInfo.entry; const int max_peaks = (data[to - 1].sec - data[from].sec) / half_interval + 1; struct Peak { int range_from; @@ -185,7 +185,7 @@ void DiveProfileItem::replot(const dive *d, int from, int to, bool in_planner) }; std::vector stack; stack.reserve(max_peaks); - int highest_peak = std::max_element(data + from, data + to, comp_depth) - data; + int highest_peak = std::max_element(data.begin() + from, data.begin() + to, comp_depth) - data.begin(); if (data[highest_peak].depth < min_depth) return; stack.push_back(Peak{ from, to, highest_peak }); @@ -210,7 +210,7 @@ void DiveProfileItem::replot(const dive *d, int from, int to, bool in_planner) // Continue search until peaks reach the minimum prominence (height from valley). for ( ; new_from + 3 < act_peak.range_to; ++new_from) { if (data[new_from].depth >= data[valley].depth + min_prominence) { - int new_peak = std::max_element(data + new_from, data + act_peak.range_to, comp_depth) - data; + int new_peak = std::max_element(data.begin() + new_from, data.begin() + act_peak.range_to, comp_depth) - data.begin(); if (data[new_peak].depth < min_depth) break; stack.push_back(Peak{ new_from, act_peak.range_to, new_peak }); @@ -236,7 +236,7 @@ void DiveProfileItem::replot(const dive *d, int from, int to, bool in_planner) // Continue search until peaks reach the minimum prominence (height from valley). for ( ; new_to >= act_peak.range_from + 3; --new_to) { if (data[new_to].depth >= data[valley].depth + min_prominence) { - int new_peak = std::max_element(data + act_peak.range_from, data + new_to, comp_depth) - data; + int new_peak = std::max_element(data.begin() + act_peak.range_from, data.begin() + new_to, comp_depth) - data.begin(); if (data[new_peak].depth < min_depth) break; stack.push_back(Peak{ act_peak.range_from, new_to, new_peak }); @@ -533,17 +533,17 @@ void DiveGasPressureItem::replot(const dive *d, int fromIn, int toIn, bool in_pl segments.clear(); for (int i = from; i < to; i++) { - const struct plot_data *entry = pInfo.entry + i; + auto entry = pInfo.entry.begin() + i; for (int cyl = 0; cyl < pInfo.nr_cylinders; cyl++) { - double mbar = static_cast(get_plot_pressure(&pInfo, i, cyl)); + double mbar = static_cast(get_plot_pressure(pInfo, i, cyl)); double time = static_cast(entry->sec); if (mbar < 1.0) continue; if (i == from && i < to - 1) { - double mbar2 = static_cast(get_plot_pressure(&pInfo, i+1, cyl)); + double mbar2 = static_cast(get_plot_pressure(pInfo, i+1, cyl)); double time2 = static_cast(entry[1].sec); if (mbar2 < 1.0) continue; @@ -551,7 +551,7 @@ void DiveGasPressureItem::replot(const dive *d, int fromIn, int toIn, bool in_pl } if (i == to - 1 && i > from) { - double mbar2 = static_cast(get_plot_pressure(&pInfo, i-1, cyl)); + double mbar2 = static_cast(get_plot_pressure(pInfo, i-1, cyl)); double time2 = static_cast(entry[-1].sec); if (mbar2 < 1.0) continue; diff --git a/profile-widget/divetooltipitem.cpp b/profile-widget/divetooltipitem.cpp index 403e252d7..1b6b22b2d 100644 --- a/profile-widget/divetooltipitem.cpp +++ b/profile-widget/divetooltipitem.cpp @@ -212,7 +212,7 @@ void ToolTipItem::setPlotInfo(const plot_info &plot) void ToolTipItem::clearPlotInfo() { - memset(&pInfo, 0, sizeof(pInfo)); + pInfo = plot_info(); } void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis) @@ -231,7 +231,7 @@ void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner) lastTime = time; clear(); - auto [idx, lines] = get_plot_details_new(d, &pInfo, time); + auto [idx, lines] = get_plot_details_new(d, pInfo, time); tissues.fill(); painter.setPen(QColor(0, 0, 0, 0)); diff --git a/profile-widget/profilescene.cpp b/profile-widget/profilescene.cpp index b1e19ed1c..37b0e7e00 100644 --- a/profile-widget/profilescene.cpp +++ b/profile-widget/profilescene.cpp @@ -7,6 +7,7 @@ #include "diveprofileitem.h" #include "divetextitem.h" #include "tankitem.h" +#include "core/dive.h" #include "core/device.h" #include "core/divecomputer.h" #include "core/event.h" @@ -151,8 +152,6 @@ ProfileScene::ProfileScene(double dpr, bool printMode, bool isGrayscale) : tankItem(new TankItem(*timeAxis, dpr)), pixmaps(getDivePixmaps(dpr)) { - init_plot_info(&plotInfo); - setSceneRect(0, 0, 100, 100); setItemIndexMethod(QGraphicsScene::NoIndex); @@ -188,7 +187,6 @@ ProfileScene::ProfileScene(double dpr, bool printMode, bool isGrayscale) : ProfileScene::~ProfileScene() { - free_plot_info_data(&plotInfo); } void ProfileScene::clear() @@ -201,7 +199,7 @@ void ProfileScene::clear() // the DiveEventItems qDeleteAll(eventItems); eventItems.clear(); - free_plot_info_data(&plotInfo); + plotInfo = plot_info(); empty = true; } @@ -454,7 +452,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM * create_plot_info_new() automatically frees old plot data. */ if (!keepPlotInfo) - create_plot_info_new(d, currentdc, &plotInfo, planner_ds); + plotInfo = create_plot_info_new(d, currentdc, planner_ds); bool hasHeartBeat = plotInfo.maxhr; // For mobile we might want to turn of some features that are normally shown. @@ -466,7 +464,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM updateVisibility(hasHeartBeat, simplified); updateAxes(hasHeartBeat, simplified); - int newMaxtime = get_maxtime(&plotInfo); + int newMaxtime = get_maxtime(plotInfo); if (calcMax || newMaxtime > maxtime) maxtime = newMaxtime; @@ -474,7 +472,7 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM * when we are dragging the handler to plan / add dive. * otherwhise, update normally. */ - int newMaxDepth = get_maxdepth(&plotInfo); + int newMaxDepth = get_maxdepth(plotInfo); if (!calcMax) { if (maxdepth < newMaxDepth) maxdepth = newMaxDepth; @@ -509,18 +507,18 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM // Find first and last plotInfo entry int firstSecond = lrint(timeAxis->minimum()); int lastSecond = lrint(timeAxis->maximum()); - auto it1 = std::lower_bound(plotInfo.entry, plotInfo.entry + plotInfo.nr, firstSecond, + auto it1 = std::lower_bound(plotInfo.entry.begin(), plotInfo.entry.end(), firstSecond, [](const plot_data &d, int s) { return d.sec < s; }); - auto it2 = std::lower_bound(it1, plotInfo.entry + plotInfo.nr, lastSecond, + auto it2 = std::lower_bound(it1, plotInfo.entry.end(), lastSecond, [](const plot_data &d, int s) { return d.sec < s; }); - if (it1 > plotInfo.entry && it1->sec > firstSecond) + if (it1 > plotInfo.entry.begin() && it1->sec > firstSecond) --it1; - if (it2 < plotInfo.entry + plotInfo.nr) + if (it2 < plotInfo.entry.end()) ++it2; - int from = it1 - plotInfo.entry; - int to = it2 - plotInfo.entry; + int from = it1 - plotInfo.entry.begin(); + int to = it2 - plotInfo.entry.begin(); timeAxis->updateTicks(animSpeed); animatedAxes.push_back(timeAxis); diff --git a/profile-widget/ruleritem.cpp b/profile-widget/ruleritem.cpp index b431749c6..755d24b12 100644 --- a/profile-widget/ruleritem.cpp +++ b/profile-widget/ruleritem.cpp @@ -113,7 +113,7 @@ void RulerItem2::recalculate() setLine(line); QString text; - for (const std::string &s: compare_samples(dive, pInfo, source->idx, dest->idx, 1)) { + for (const std::string &s: compare_samples(dive, *pInfo, source->idx, dest->idx, 1)) { if (!text.isEmpty()) text += '\n'; text += QString::fromStdString(s);