planner: don't use fixed size deco stop table

This was quite ominous: a 60-element fixed size table was
passed as argument to plan(). But there was no check for 60
anywhere? Use a dynamic vector instead.

The whole thing is weird, as the depth of the decostop table
doesn't seem to be used.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-09-07 14:56:34 +02:00 committed by Michael Keller
parent 0745c50e58
commit 3e006e678a
5 changed files with 17 additions and 38 deletions

View file

@ -602,7 +602,7 @@ static void average_max_depth(const struct diveplan &dive, int *avg_depth, int *
*avg_depth = *max_depth = 0; *avg_depth = *max_depth = 0;
} }
bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, int dcNr, int timestep, struct decostop *decostoptable, deco_state_cache &cache, bool is_planner, bool show_disclaimer) bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, int dcNr, int timestep, std::vector<decostop> &decostoptable, deco_state_cache &cache, bool is_planner, bool show_disclaimer)
{ {
int bottom_depth; int bottom_depth;
@ -635,7 +635,6 @@ bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, i
int first_stop_depth = 0; int first_stop_depth = 0;
int laststoptime = timestep; int laststoptime = timestep;
bool o2breaking = false; bool o2breaking = false;
int decostopcounter = 0;
struct divecomputer *dc = dive->get_dc(dcNr); struct divecomputer *dc = dive->get_dc(dcNr);
enum divemode_t divemode = dc->divemode; enum divemode_t divemode = dc->divemode;
@ -819,7 +818,7 @@ bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, i
//CVA //CVA
do { do {
decostopcounter = 0; decostoptable.clear();
is_final_plan = (decoMode(true) == BUEHLMANN) || (previous_deco_time - ds->deco_time < 10); // CVA time converges is_final_plan = (decoMode(true) == BUEHLMANN) || (previous_deco_time - ds->deco_time < 10); // CVA time converges
if (ds->deco_time != 10000000) if (ds->deco_time != 10000000)
vpmb_next_gradient(ds, ds->deco_time, diveplan.surface_pressure / 1000.0, true); vpmb_next_gradient(ds, ds->deco_time, diveplan.surface_pressure / 1000.0, true);
@ -931,9 +930,7 @@ bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, i
/* Check if ascending to next stop is clear, go back and wait if we hit the ceiling on the way */ /* Check if ascending to next stop is clear, go back and wait if we hit the ceiling on the way */
if (trial_ascent(ds, 0, depth, stoplevels[stopidx], avg_depth, bottom_time, if (trial_ascent(ds, 0, depth, stoplevels[stopidx], avg_depth, bottom_time,
dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure / 1000.0, dive, divemode)) { dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure / 1000.0, dive, divemode)) {
decostoptable[decostopcounter].depth = depth; decostoptable.push_back( decostop { depth, 0 });
decostoptable[decostopcounter].time = 0;
decostopcounter++;
break; /* We did not hit the ceiling */ break; /* We did not hit the ceiling */
} }
@ -1023,9 +1020,7 @@ bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, i
add_segment(ds, dive->depth_to_bar(depth), dive->get_cylinder(stop_cylinder)->gasmix, add_segment(ds, dive->depth_to_bar(depth), dive->get_cylinder(stop_cylinder)->gasmix,
laststoptime, po2, divemode, prefs.decosac, true); laststoptime, po2, divemode, prefs.decosac, true);
last_segment_min_switch = false; last_segment_min_switch = false;
decostoptable[decostopcounter].depth = depth; decostoptable.push_back(decostop { depth, laststoptime } );
decostoptable[decostopcounter].time = laststoptime;
++decostopcounter;
clock += laststoptime; clock += laststoptime;
if (!o2breaking) if (!o2breaking)
@ -1045,7 +1040,6 @@ bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, i
* Assume final ascent takes 20s, which is the time taken to ascend at 9m/min from 3m */ * Assume final ascent takes 20s, which is the time taken to ascend at 9m/min from 3m */
ds->deco_time = clock - bottom_time - (M_OR_FT(3,10) * ( prefs.last_stop ? 2 : 1)) / last_ascend_rate + 20; ds->deco_time = clock - bottom_time - (M_OR_FT(3,10) * ( prefs.last_stop ? 2 : 1)) / last_ascend_rate + 20;
} while (!is_final_plan && error == PLAN_OK); } while (!is_final_plan && error == PLAN_OK);
decostoptable[decostopcounter].depth = 0;
plan_add_segment(diveplan, clock - previous_point_time, 0, current_cylinder, po2, false, divemode); plan_add_segment(diveplan, clock - previous_point_time, 0, current_cylinder, po2, false, divemode);
if (decoMode(true) == VPMB) { if (decoMode(true) == VPMB) {

View file

@ -66,5 +66,5 @@ struct decostop {
}; };
extern std::string get_planner_disclaimer_formatted(); extern std::string get_planner_disclaimer_formatted();
extern bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, int dcNr, int timestep, struct decostop *decostoptable, deco_state_cache &cache, bool is_planner, bool show_disclaimer); extern bool plan(struct deco_state *ds, struct diveplan &diveplan, struct dive *dive, int dcNr, int timestep, std::vector<decostop> &decostoptable, deco_state_cache &cache, bool is_planner, bool show_disclaimer);
#endif // PLANNER_H #endif // PLANNER_H

View file

@ -1119,7 +1119,7 @@ void DivePlannerPointsModel::updateDiveProfile()
return; return;
deco_state_cache cache; deco_state_cache cache;
struct decostop stoptable[60]; std::vector<decostop> stoptable;
struct deco_state plan_deco_state; struct deco_state plan_deco_state;
plan(&plan_deco_state, diveplan, d, dcNr, decotimestep, stoptable, cache, isPlanner(), false); plan(&plan_deco_state, diveplan, d, dcNr, decotimestep, stoptable, cache, isPlanner(), false);
@ -1182,29 +1182,14 @@ void DivePlannerPointsModel::cloneDiveplan(const struct diveplan &plan_src, stru
plan_copy = plan_src; plan_copy = plan_src;
} }
int DivePlannerPointsModel::analyzeVariations(struct decostop *min, struct decostop *mid, struct decostop *max, const char *unit) int DivePlannerPointsModel::analyzeVariations(const std::vector<decostop> &min, const std::vector<decostop> &mid, const std::vector<decostop> &max, const char *unit)
{ {
int minsum = 0; auto sum_time = [](int time, const decostop &ds) { return ds.time + time; };
int midsum = 0; int minsum = std::accumulate(min.begin(), min.end(), 0, sum_time);
int maxsum = 0; int midsum = std::accumulate(mid.begin(), mid.end(), 0, sum_time);
int leftsum = 0; int maxsum = std::accumulate(max.begin(), max.end(), 0, sum_time);
int rightsum = 0; int leftsum = midsum - minsum;
int rightsum = maxsum - midsum;
while (min->depth) {
minsum += min->time;
++min;
}
while (mid->depth) {
midsum += mid->time;
++mid;
}
while (max->depth) {
maxsum += max->time;
++max;
}
leftsum = midsum - minsum;
rightsum = maxsum - midsum;
#ifdef DEBUG_STOPVAR #ifdef DEBUG_STOPVAR
printf("Total + %d:%02d/%s +- %d s/%s\n\n", FRACTION_TUPLE((leftsum + rightsum) / 2, 60), unit, printf("Total + %d:%02d/%s +- %d s/%s\n\n", FRACTION_TUPLE((leftsum + rightsum) / 2, 60), unit,
@ -1237,7 +1222,7 @@ void DivePlannerPointsModel::computeVariations(std::unique_ptr<struct diveplan>
auto dive = std::make_unique<struct dive>(); auto dive = std::make_unique<struct dive>();
copy_dive(d, dive.get()); copy_dive(d, dive.get());
struct decostop original[60], deeper[60], shallower[60], shorter[60], longer[60]; std::vector<decostop> original, deeper, shallower, shorter, longer;
deco_state_cache cache, save; deco_state_cache cache, save;
struct diveplan plan_copy; struct diveplan plan_copy;
struct deco_state ds = *previous_ds; struct deco_state ds = *previous_ds;
@ -1331,7 +1316,7 @@ void DivePlannerPointsModel::createPlan(bool saveAsNew)
removeDeco(); removeDeco();
createTemporaryPlan(); createTemporaryPlan();
struct decostop stoptable[60]; std::vector<decostop> stoptable;
plan(&ds_after_previous_dives, diveplan, d, dcNr, decotimestep, stoptable, cache, isPlanner(), true); plan(&ds_after_previous_dives, diveplan, d, dcNr, decotimestep, stoptable, cache, isPlanner(), true);
if (shouldComputeVariations()) { if (shouldComputeVariations()) {

View file

@ -134,7 +134,7 @@ private:
void computeVariationsDone(QString text); void computeVariationsDone(QString text);
void computeVariations(std::unique_ptr<struct diveplan> plan, const struct deco_state *ds); void computeVariations(std::unique_ptr<struct diveplan> plan, const struct deco_state *ds);
void computeVariationsFreeDeco(std::unique_ptr<struct diveplan> plan, std::unique_ptr<struct deco_state> ds); void computeVariationsFreeDeco(std::unique_ptr<struct diveplan> plan, std::unique_ptr<struct deco_state> ds);
int analyzeVariations(struct decostop *min, struct decostop *mid, struct decostop *max, const char *unit); int analyzeVariations(const std::vector<decostop> &min, const std::vector<decostop> &mid, const std::vector<decostop> &max, const char *unit);
struct dive *d; struct dive *d;
int dcNr; int dcNr;
CylindersModel cylinders; CylindersModel cylinders;

View file

@ -13,7 +13,7 @@
// testing the dive plan algorithm // testing the dive plan algorithm
static struct dive dive; static struct dive dive;
static struct decostop stoptable[60]; static std::vector<decostop> stoptable;
static struct deco_state test_deco_state; static struct deco_state test_deco_state;
void setupPrefs() void setupPrefs()
{ {