+ gc->leftx = 0; gc->rightx = maxtime;
+
+ /*
+ * These are good for debugging text placement etc,
+ * but not for actual display..
+ */
+ if (0) {
+ plot_smoothed_profile(gc, pi);
+ plot_minmax_profile(gc, pi);
+ }
+
+ set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
+
+ /* Do the depth profile for the neat fill */
+ gc->topy = 0; gc->bottomy = maxdepth;
+ set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
+
+ entry = pi->entry;
+ move_to(gc, 0, 0);
+ for (i = 0; i < pi->nr; i++, entry++)
+ line_to(gc, entry->sec, entry->depth);
+ cairo_close_path(gc->cr);
+ if (gc->printer) {
+ set_source_rgba(gc, 1, 1, 1, 0.2);
+ cairo_fill_preserve(cr);
+ set_source_rgb(gc, 1, 1, 1);
+ cairo_stroke(cr);
+ return;
+ }
+ cairo_fill(gc->cr);
+
+ /* Now do it again for the velocity colors */
+ entry = pi->entry;
+ for (i = 1; i < pi->nr; i++) {
+ entry++;
+ sec = entry->sec;
+ /* we want to draw the segments in different colors
+ * representing the vertical velocity, so we need to
+ * chop this into short segments */
+ rgb_t color = rgb[entry->velocity];
+ depth = entry->depth;
+ set_source_rgb(gc, color.r, color.g, color.b);
+ move_to(gc, entry[-1].sec, entry[-1].depth);
+ line_to(gc, sec, depth);
+ cairo_stroke(cr);
+ }
+}
+
+static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
+{
+ int maxtime, mintemp, maxtemp, delta;
+
+ /* Get plot scaling limits */
+ maxtime = get_maxtime(pi);
+ mintemp = pi->mintemp;
+ maxtemp = pi->maxtemp;
+
+ gc->leftx = 0; gc->rightx = maxtime;
+ /* Show temperatures in roughly the lower third, but make sure the scale
+ is at least somewhat reasonable */
+ delta = maxtemp - mintemp;
+ if (delta > 3000) { /* more than 3K in fluctuation */
+ gc->topy = maxtemp + delta*2;
+ gc->bottomy = mintemp - delta/2;
+ } else {
+ gc->topy = maxtemp + 1500 + delta*2;
+ gc->bottomy = mintemp - delta/2;
+ }
+
+ return maxtemp > mintemp;
+}
+
+static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
+{
+ double deg;
+ const char *unit;
+ static const text_render_options_t tro = {12, 0.6, 0.6, 1.0, LEFT, TOP};
+
+ deg = get_temp_units(mkelvin, &unit);
+
+ plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
+}
+
+static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
+{
+ int i;
+ int last = -300, sec = 0;
+ int last_temperature = 0, last_printed_temp = 0;
+
+ if (!setup_temperature_limits(gc, pi))
+ return;
+
+ for (i = 0; i < pi->nr; i++) {
+ struct plot_data *entry = pi->entry+i;
+ int mkelvin = entry->temperature;
+
+ if (!mkelvin)
+ continue;
+ last_temperature = mkelvin;
+ sec = entry->sec;
+ /* don't print a temperature
+ * if it's been less than 5min and less than a 2K change OR
+ * if it's been less than 2min OR if the change from the
+ * last print is less than .4K (and therefore less than 1F */
+ if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
+ (sec < last + 120) ||
+ (abs(mkelvin - last_printed_temp) < 400))
+ continue;
+ last = sec;
+ plot_single_temp_text(gc,sec,mkelvin);
+ last_printed_temp = mkelvin;
+ }
+ /* it would be nice to print the end temperature, if it's
+ * different or if the last temperature print has been more
+ * than a quarter of the dive back */
+ if ((abs(last_temperature - last_printed_temp) > 500) ||
+ ((double)last / (double)sec < 0.75))
+ plot_single_temp_text(gc, sec, last_temperature);
+}
+
+static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
+{
+ int i;
+ cairo_t *cr = gc->cr;
+ int last = 0;
+
+ if (!setup_temperature_limits(gc, pi))
+ return;
+
+ set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
+ for (i = 0; i < pi->nr; i++) {
+ struct plot_data *entry = pi->entry + i;
+ int mkelvin = entry->temperature;
+ int sec = entry->sec;
+ if (!mkelvin) {
+ if (!last)
+ continue;
+ mkelvin = last;
+ }
+ if (last)
+ line_to(gc, sec, mkelvin);
+ else
+ move_to(gc, sec, mkelvin);
+ last = mkelvin;
+ }
+ cairo_stroke(cr);
+}
+
+/* gets both the actual start and end pressure as well as the scaling factors */
+static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
+{
+ gc->leftx = 0;
+ gc->rightx = get_maxtime(pi);
+
+ gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
+ return pi->maxpressure != 0;
+}
+
+static void plot_pressure_helper(struct graphics_context *gc, struct plot_info *pi, int type)
+{
+ int i;
+ int lift_pen = FALSE;
+
+ for (i = 0; i < pi->nr; i++) {
+ int mbar;
+ struct plot_data *entry = pi->entry + i;
+
+ mbar = entry->pressure[type];
+ if (!entry->same_cylinder)
+ lift_pen = TRUE;
+ if (!mbar) {
+ lift_pen = TRUE;
+ continue;
+ }
+ if (lift_pen) {
+ if (i > 0 && entry->same_cylinder) {
+ /* if we have a previous event from the same tank,
+ * draw at least a short line .
+ * This uses the implementation detail that the
+ * type is either 0 or 1 */
+ int prev_pr;
+ prev_pr = (entry-1)->pressure[type] ? : (entry-1)->pressure[1 - type];
+ move_to(gc, (entry-1)->sec, prev_pr);
+ line_to(gc, entry->sec, mbar);
+ } else
+ move_to(gc, entry->sec, mbar);
+ lift_pen = FALSE;
+ }
+ else
+ line_to(gc, entry->sec, mbar);
+ }
+ cairo_stroke(gc->cr);
+
+}
+
+static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
+{
+ if (!get_cylinder_pressure_range(gc, pi))
+ return;
+
+ /* first plot the pressure readings we have from the dive computer */
+ set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
+ plot_pressure_helper(gc, pi, SENSOR_PR);
+
+ /* then, in a different color, the interpolated values */
+ set_source_rgba(gc, 1.0, 1.0, 0.2, 0.80);
+ plot_pressure_helper(gc, pi, INTERPOLATED_PR);
+}
+
+static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
+ int xalign, int yalign)
+{
+ int pressure;
+ const char *unit;
+
+ pressure = get_pressure_units(mbar, &unit);
+ text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
+ plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
+}
+
+static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
+{
+ int i;
+ int mbar, cyl;
+ int seen_cyl[MAX_CYLINDERS] = { FALSE, };
+ int last_pressure[MAX_CYLINDERS] = { 0, };
+ int last_time[MAX_CYLINDERS] = { 0, };
+ struct plot_data *entry;
+
+ if (!get_cylinder_pressure_range(gc, pi))
+ return;
+
+ /* only loop over the actual events from the dive computer
+ * plus the second synthetic event at the start (to make sure
+ * we get "time=0" right) */
+ for (i = 1; i < pi->nr; i++) {
+ entry = pi->entry + i;
+
+ if (!entry->same_cylinder) {
+ cyl = entry->cylinderindex;
+ if (!seen_cyl[cyl]) {
+ mbar = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
+ plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
+ seen_cyl[cyl] = TRUE;
+ }
+ if (i > 2) {
+ /* remember the last pressure and time of
+ * the previous cylinder */
+ cyl = (entry - 1)->cylinderindex;
+ last_pressure[cyl] =
+ SENSOR_PRESSURE(entry - 1) ? : INTERPOLATED_PRESSURE(entry - 1);
+ last_time[cyl] = (entry - 1)->sec;
+ }
+ }
+ }
+ cyl = entry->cylinderindex;
+ last_pressure[cyl] = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
+ last_time[cyl] = entry->sec;
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ if (last_time[cyl]) {
+ plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
+ }
+ }
+}
+
+static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
+{
+ struct plot_data *p = entry;
+ int time = entry->sec;
+ int seconds = 90*(index+1);
+ struct plot_data *min, *max;
+ int avg, nr;
+
+ /* Go back 'seconds' in time */
+ while (p > first) {
+ if (p[-1].sec < time - seconds)
+ break;
+ p--;
+ }
+
+ /* Then go forward until we hit an entry past the time */
+ min = max = p;
+ avg = p->depth;
+ nr = 1;
+ while (++p < last) {
+ int depth = p->depth;
+ if (p->sec > time + seconds)
+ break;
+ avg += depth;
+ nr ++;
+ if (depth < min->depth)
+ min = p;
+ if (depth > max->depth)
+ max = p;
+ }
+ entry->min[index] = min;
+ entry->max[index] = max;
+ entry->avg[index] = (avg + nr/2) / nr;
+}
+
+static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
+{
+ analyze_plot_info_minmax_minute(entry, first, last, 0);
+ analyze_plot_info_minmax_minute(entry, first, last, 1);
+ analyze_plot_info_minmax_minute(entry, first, last, 2);
+}
+
+static velocity_t velocity(int speed)
+{
+ velocity_t v;
+
+ if (speed < -304) /* ascent faster than -60ft/min */
+ v = CRAZY;
+ else if (speed < -152) /* above -30ft/min */
+ v = FAST;
+ else if (speed < -76) /* -15ft/min */
+ v = MODERATE;
+ else if (speed < -25) /* -5ft/min */
+ v = SLOW;
+ else if (speed < 25) /* very hard to find data, but it appears that the recommendations
+ for descent are usually about 2x ascent rate; still, we want
+ stable to mean stable */
+ v = STABLE;
+ else if (speed < 152) /* between 5 and 30ft/min is considered slow */
+ v = SLOW;
+ else if (speed < 304) /* up to 60ft/min is moderate */
+ v = MODERATE;
+ else if (speed < 507) /* up to 100ft/min is fast */
+ v = FAST;
+ else /* more than that is just crazy - you'll blow your ears out */
+ v = CRAZY;
+
+ return v;
+}
+static struct plot_info *analyze_plot_info(struct plot_info *pi)
+{
+ int i;
+ int nr = pi->nr;
+
+ /* Do pressure min/max based on the non-surface data */
+ for (i = 0; i < nr; i++) {
+ struct plot_data *entry = pi->entry+i;
+ int pressure = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
+ int temperature = entry->temperature;
+
+ if (pressure) {
+ if (!pi->minpressure || pressure < pi->minpressure)
+ pi->minpressure = pressure;
+ if (pressure > pi->maxpressure)
+ pi->maxpressure = pressure;
+ }
+
+ if (temperature) {
+ if (!pi->mintemp || temperature < pi->mintemp)
+ pi->mintemp = temperature;
+ if (temperature > pi->maxtemp)
+ pi->maxtemp = temperature;
+ }
+ }
+
+ /* Smoothing function: 5-point triangular smooth */
+ for (i = 2; i < nr; 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;
+ }
+ /* 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->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
+ /* if our samples are short and we aren't too FAST*/
+ if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
+ int past = -2;
+ while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
+ past--;
+ entry->velocity = velocity((entry[0].depth - entry[past].depth) /
+ (entry[0].sec - entry[past].sec));
+ }
+ } else
+ entry->velocity = STABLE;
+ }
+
+ /* One-, two- and three-minute minmax data */
+ for (i = 0; i < nr; i++) {
+ struct plot_data *entry = pi->entry +i;
+ analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
+ }
+
+ return pi;
+}
+
+/*
+ * simple structure to track the beginning and end tank pressure as
+ * well as the integral of depth over time spent while we have no
+ * pressure reading from the tank */
+typedef struct pr_track_struct pr_track_t;
+struct pr_track_struct {
+ int start;
+ int end;
+ int t_start;
+ int t_end;
+ double pressure_time;
+ pr_track_t *next;
+};
+
+static pr_track_t *pr_track_alloc(int start, int t_start) {
+ pr_track_t *pt = malloc(sizeof(pr_track_t));
+ pt->start = start;
+ pt->t_start = t_start;
+ pt->end = 0;
+ pt->t_end = 0;
+ pt->pressure_time = 0.0;
+ pt->next = NULL;
+ return pt;
+}
+
+/* poor man's linked list */
+static pr_track_t *list_last(pr_track_t *list)
+{
+ pr_track_t *tail = list;
+ if (!tail)
+ return NULL;
+ while (tail->next) {
+ tail = tail->next;
+ }
+ return tail;
+}
+
+static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
+{
+ pr_track_t *tail = list_last(list);
+ if (!tail)
+ return element;
+ tail->next = element;
+ return list;
+}
+
+static void list_free(pr_track_t *list)
+{
+ if (!list)
+ return;
+ list_free(list->next);
+ free(list);
+}
+
+static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
+ pr_track_t **track_pr)
+{
+ pr_track_t *list = NULL;
+ pr_track_t *nlist = NULL;
+ double pt, magic;
+ int cyl, i;
+ struct plot_data *entry;
+ int cur_pr[MAX_CYLINDERS];
+
+ for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
+ cur_pr[cyl] = track_pr[cyl]->start;
+ }
+
+ /* The first two are "fillers", but in case we don't have a sample
+ * at time 0 we need to process the second of them here */
+ for (i = 1; i < pi->nr; i++) {
+ entry = pi->entry + i;
+ if (SENSOR_PRESSURE(entry)) {
+ cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
+ } else {
+ if(!list || list->t_end < entry->sec) {
+ nlist = track_pr[entry->cylinderindex];
+ list = NULL;
+ while (nlist && nlist->t_start <= entry->sec) {
+ list = nlist;
+ nlist = list->next;
+ }
+ /* there may be multiple segments - so
+ * let's assemble the length */
+ nlist = list;
+ pt = list->pressure_time;
+ while (!nlist->end) {
+ nlist = nlist->next;
+ if (!nlist) {
+ /* oops - we have no end pressure,
+ * so this means this is a tank without
+ * gas consumption information */
+ break;
+ }
+ pt += nlist->pressure_time;
+ }
+ if (!nlist) {
+ /* just continue without calculating
+ * interpolated values */
+ list = NULL;
+ continue;
+ }
+ magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt; }
+ if (pt != 0.0) {
+ double cur_pt = (entry->sec - (entry-1)->sec) *
+ (1 + entry->depth / 10000.0);
+ INTERPOLATED_PRESSURE(entry) =
+ cur_pr[entry->cylinderindex] + cur_pt * magic;
+ cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
+ } else
+ INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
+ }
+ }
+}
+
+static int get_cylinder_index(struct dive *dive, struct event *ev)
+{
+ int i;
+
+ /*
+ * Try to find a cylinder that matches the O2 percentage
+ * in the gas change event 'value' field.
+ *
+ * Crazy suunto gas change events. We really should do
+ * this in libdivecomputer or something.
+ */
+ for (i = 0; i < MAX_CYLINDERS; i++) {
+ cylinder_t *cyl = dive->cylinder+i;
+ int o2 = (cyl->gasmix.o2.permille + 5) / 10;
+ if (o2 == ev->value)
+ return i;
+ }
+
+ return 0;
+}
+
+static struct event *get_next_gaschange(struct event *event)
+{
+ while (event) {
+ if (!strcmp(event->name, "gaschange"))
+ return event;
+ event = event->next;
+ }
+ return event;
+}
+
+static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
+{
+ while (i < pi->nr) {
+ struct plot_data *entry = pi->entry+i;
+ if (entry->sec > end)
+ break;
+ if (entry->cylinderindex != cylinderindex) {
+ entry->cylinderindex = cylinderindex;
+ entry->pressure[0] = 0;
+ }
+ i++;
+ }
+ return i;
+}
+
+static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
+{
+ int i = 0, cylinderindex = 0;
+ struct event *ev = get_next_gaschange(dive->events);
+
+ if (!ev)
+ return;
+
+ do {
+ i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
+ cylinderindex = get_cylinder_index(dive, ev);
+ ev = get_next_gaschange(ev->next);
+ } while (ev);
+ set_cylinder_index(pi, i, cylinderindex, ~0u);
+}
+
+/* for computers that track gas changes through events */
+static int count_gas_change_events(struct dive *dive)
+{
+ int count = 0;
+ struct event *ev = get_next_gaschange(dive->events);
+
+ while (ev) {
+ count++;
+ ev = get_next_gaschange(ev->next);
+ }
+ return count;
+}
+
+/*
+ * Create a plot-info with smoothing and ranged min/max
+ *
+ * This also makes sure that we have extra empty events on both
+ * sides, so that you can do end-points without having to worry
+ * about it.
+ */
+static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
+{
+ int cylinderindex = -1;
+ int lastdepth, lastindex;
+ int i, pi_idx, nr, sec, cyl;
+ size_t alloc_size;
+ struct plot_info *pi;
+ pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
+ pr_track_t *pr_track, *current;
+ gboolean missing_pr = FALSE;
+ struct plot_data *entry = NULL;
+ struct event *ev;
+
+ /* we want to potentially add synthetic plot_info elements for the gas changes */
+ nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
+ alloc_size = plot_info_size(nr);
+ pi = malloc(alloc_size);
+ if (!pi)
+ return pi;
+ memset(pi, 0, alloc_size);
+ pi->nr = nr;
+ pi_idx = 2; /* the two extra events at the start */
+ /* check for gas changes before the samples start */
+ ev = get_next_gaschange(dive->events);
+ while (ev && ev->time.seconds < dive_sample->time.seconds) {
+ entry = pi->entry + pi_idx;
+ entry->sec = ev->time.seconds;
+ entry->depth = 0; /* is that always correct ? */
+ pi_idx++;
+ ev = get_next_gaschange(ev->next);
+ }
+ if (ev && ev->time.seconds == dive_sample->time.seconds) {
+ /* we already have a sample at the time of the event */
+ ev = get_next_gaschange(ev->next);
+ }
+ sec = 0;
+ lastindex = 0;
+ lastdepth = -1;
+ for (i = 0; i < nr_samples; i++) {
+ int depth;
+ int delay = 0;
+ struct sample *sample = dive_sample+i;