Profile: fix SAC calculation for air dives

Commit f5b11daffd changed gasmix
arguments and return values to be passed by value instead of
using pointers.

Notably, get_gasmix() is fed a default-value and returns a
new value. In the old code, NULL was passed in in a first
loop iteration and non-NULL was always returned in the first
iteration. Thus, an equality comparison of passed-in an
returned gasmix would always fail in the first loop iteration.

The new code passed in air as default. Now if air was also
returned, then the matching gases were not calculated in
calculate_sac(). To revert to the old behavior, pass in
an invalid gasmix.

Moreover, give names to the invalid and air gasmixes.

Reported-by: tormento <turment@gmail.com>
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-09-10 20:40:25 +02:00 committed by Dirk Hohndel
parent c75f53a7a8
commit 6e4a253896
6 changed files with 18 additions and 7 deletions

View file

@ -2205,8 +2205,15 @@ void cylinder_renumber(struct dive *dive, int mapping[])
dc_cylinder_renumber(dive, dc, mapping);
}
static bool gasmix_is_invalid(struct gasmix mix)
{
return mix.o2.permille < 0;
}
int same_gasmix(struct gasmix a, struct gasmix b)
{
if (gasmix_is_invalid(a) || gasmix_is_invalid(b))
return 0;
if (gasmix_is_air(a) && gasmix_is_air(b))
return 1;
return a.o2.permille == b.o2.permille && a.he.permille == b.he.permille;
@ -4314,6 +4321,6 @@ struct gasmix get_gasmix(const struct dive *dive, const struct divecomputer *dc,
struct gasmix get_gasmix_at_time(const struct dive *d, const struct divecomputer *dc, duration_t time)
{
const struct event *ev = NULL;
struct gasmix gasmix = { 0 };
struct gasmix gasmix = gasmix_air;
return get_gasmix(d, dc, time.seconds, &ev, gasmix);
}

View file

@ -34,10 +34,14 @@ extern const char *cylinderuse_text[];
extern const char *divemode_text_ui[];
extern const char *divemode_text[];
// o2 == 0 && he == 0 -> air
// o2 < 0 -> invalid
struct gasmix {
fraction_t o2;
fraction_t he;
};
static const struct gasmix gasmix_invalid = { { -1 }, { -1 } };
static const struct gasmix gasmix_air = { { 0 }, { 0 } };
typedef struct
{

View file

@ -405,7 +405,7 @@ static int calculate_sac(const struct dive *dive)
static void add_dive_to_deco(struct deco_state *ds, struct dive *dive)
{
struct divecomputer *dc = &dive->dc;
struct gasmix gasmix = { 0 };
struct gasmix gasmix = gasmix_air;
int i;
const struct event *ev = NULL, *evd = NULL;
enum divemode_t current_divemode = UNDEF_COMP_TYPE;

View file

@ -76,7 +76,7 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d
struct membuffer icdbuf = { 0 };
const char *deco, *segmentsymbol;
int lastdepth = 0, lasttime = 0, lastsetpoint = -1, newdepth = 0, lastprintdepth = 0, lastprintsetpoint = -1;
struct gasmix lastprintgasmix = {{ -1 }, { -1 }};
struct gasmix lastprintgasmix = gasmix_invalid;
struct divedatapoint *dp = diveplan->dp;
bool plan_verbatim = prefs.verbatim_plan;
bool plan_display_runtime = prefs.display_runtime;

View file

@ -780,7 +780,7 @@ static unsigned int matching_gases(struct dive *dive, struct gasmix gasmix)
static void calculate_sac(struct dive *dive, struct divecomputer *dc, struct plot_info *pi)
{
struct gasmix gasmix = { 0 };
struct gasmix gasmix = gasmix_invalid;
const struct event *ev = NULL;
unsigned int gases = 0;
@ -1021,7 +1021,7 @@ void calculate_deco_information(struct deco_state *ds, const struct deco_state *
int last_ndl_tts_calc_time = 0, first_ceiling = 0, current_ceiling, last_ceiling = 0, final_tts = 0 , time_clear_ceiling = 0;
if (decoMode() == VPMB)
ds->first_ceiling_pressure.mbar = depth_to_mbar(first_ceiling, dive);
struct gasmix gasmix = { 0 };
struct gasmix gasmix = gasmix_invalid;
const struct event *ev = NULL, *evd = NULL;
enum divemode_t current_divemode = UNDEF_COMP_TYPE;
@ -1210,7 +1210,7 @@ static void calculate_gas_information_new(struct dive *dive, struct divecomputer
{
int i;
double amb_pressure;
struct gasmix gasmix = { 0 };
struct gasmix gasmix = gasmix_invalid;
const struct event *evg = NULL, *evd = NULL;
enum divemode_t current_divemode = UNDEF_COMP_TYPE;

View file

@ -410,7 +410,7 @@ void DivePercentageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
for (int i = 1, modelDataCount = dataModel->rowCount(); i < modelDataCount; i++) {
if (i < poly.count()) {
double value = dataModel->index(i, vDataColumn).data().toDouble();
struct gasmix gasmix = { 0 };
struct gasmix gasmix = gasmix_air;
const struct event *ev = NULL;
int sec = dataModel->index(i, DivePlotDataModel::TIME).data().toInt();
gasmix = get_gasmix(&displayed_dive, displayed_dc, sec, &ev, gasmix);