2 /* creates all the necessary data for drawing the dive profile
3 * uses cairo to draw it
15 int selected_dive = 0;
17 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
18 /* Plot info with smoothing, velocity indication
19 * and one-, two- and three-minute minimums and maximums */
23 int meandepth, maxdepth;
24 int minpressure, maxpressure;
25 int endpressure; /* start pressure better be max pressure */
28 unsigned int same_cylinder:1;
29 unsigned int cylinderindex;
31 /* pressure[0] is sensor pressure
32 * pressure[1] is interpolated pressure */
39 struct plot_data *min[3];
40 struct plot_data *max[3];
45 #define INTERPOLATED_PR 1
46 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
47 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
49 /* convert velocity to colors */
50 typedef struct { double r, g, b; } rgb_t;
51 static const rgb_t rgb[] = {
52 [STABLE] = {0.0, 0.4, 0.0},
53 [SLOW] = {0.4, 0.8, 0.0},
54 [MODERATE] = {0.8, 0.8, 0.0},
55 [FAST] = {0.8, 0.5, 0.0},
56 [CRAZY] = {1.0, 0.0, 0.0},
59 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
61 /* Scale to 0,0 -> maxx,maxy */
62 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
63 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
64 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
66 static void move_to(struct graphics_context *gc, double x, double y)
68 cairo_move_to(gc->cr, SCALE(gc, x, y));
71 static void line_to(struct graphics_context *gc, double x, double y)
73 cairo_line_to(gc->cr, SCALE(gc, x, y));
76 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
79 * For printers, we still honor 'a', but ignore colors
80 * for now. Black is white and white is black
89 cairo_set_source_rgba(gc->cr, r, g, b, a);
92 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
94 set_source_rgba(gc, r, g, b, 1);
97 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
100 * When showing dive profiles, we scale things to the
101 * current dive. However, we don't scale past less than
102 * 30 minutes or 90 ft, just so that small dives show
104 * we also need to add 180 seconds at the end so the min/max
107 static int get_maxtime(struct plot_info *pi)
109 int seconds = pi->maxtime;
110 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
111 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
114 static int get_maxdepth(struct plot_info *pi)
116 unsigned mm = pi->maxdepth;
117 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
118 return MAX(30000, ROUND_UP(mm+3000, 10000));
125 } text_render_options_t;
128 #define CENTER (-0.5)
135 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
136 double x, double y, const char *fmt, ...)
138 cairo_t *cr = gc->cr;
139 cairo_font_extents_t fe;
140 cairo_text_extents_t extents;
146 vsnprintf(buffer, sizeof(buffer), fmt, args);
149 cairo_set_font_size(cr, tro->size);
150 cairo_font_extents(cr, &fe);
151 cairo_text_extents(cr, buffer, &extents);
152 dx = tro->hpos * extents.width + extents.x_bearing;
153 dy = tro->vpos * extents.height + fe.descent;
156 cairo_rel_move_to(cr, dx, dy);
158 cairo_text_path(cr, buffer);
159 set_source_rgb(gc, 0, 0, 0);
163 cairo_rel_move_to(cr, dx, dy);
165 set_source_rgb(gc, tro->r, tro->g, tro->b);
166 cairo_show_text(cr, buffer);
173 static struct ev_select *ev_namelist;
174 static int evn_allocated;
177 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
181 for (i = 0; i < evn_used; i++) {
182 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
186 void remember_event(const char *eventname)
190 if (!eventname || (len = strlen(eventname)) == 0)
192 while (i < evn_used) {
193 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
197 if (evn_used == evn_allocated) {
199 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
201 /* we are screwed, but let's just bail out */
204 ev_namelist[evn_used].ev_name = strdup(eventname);
205 ev_namelist[evn_used].plot_ev = TRUE;
209 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
214 /* is plotting this event disabled? */
216 for (i = 0; i < evn_used; i++) {
217 if (! strcmp(event->name, ev_namelist[i].ev_name)) {
218 if (ev_namelist[i].plot_ev)
225 for (i = 0; i < pi->nr; i++) {
226 struct plot_data *data = pi->entry + i;
227 if (event->time.seconds < data->sec)
231 /* draw a little tirangular marker and attach tooltip */
232 x = SCALEX(gc, event->time.seconds);
233 y = SCALEY(gc, depth);
234 set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
235 cairo_move_to(gc->cr, x-15, y+6);
236 cairo_line_to(gc->cr, x-3 , y+6);
237 cairo_line_to(gc->cr, x-9, y-6);
238 cairo_line_to(gc->cr, x-15, y+6);
239 cairo_stroke_preserve(gc->cr);
241 set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
242 cairo_move_to(gc->cr, x-9, y-3);
243 cairo_line_to(gc->cr, x-9, y+1);
244 cairo_move_to(gc->cr, x-9, y+4);
245 cairo_line_to(gc->cr, x-9, y+4);
246 cairo_stroke(gc->cr);
247 attach_tooltip(x-15, y-6, 12, 12, event->name);
250 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
252 static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
253 struct event *event = dive->events;
259 plot_one_event(gc, pi, event, &tro);
264 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
266 int sec = entry->sec, decimals;
269 d = get_depth_units(entry->depth, &decimals, NULL);
271 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
274 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
276 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
277 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
280 for (i = 0; i < pi->nr; i++) {
281 struct plot_data *entry = pi->entry + i;
283 if (entry->depth < 2000)
286 if (entry == entry->max[2])
287 render_depth_sample(gc, entry, &deep);
289 if (entry == entry->min[2])
290 render_depth_sample(gc, entry, &shallow);
294 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
296 int maxtime, maxdepth;
298 /* Get plot scaling limits */
299 maxtime = get_maxtime(pi);
300 maxdepth = get_maxdepth(pi);
302 gc->leftx = 0; gc->rightx = maxtime;
303 gc->topy = 0; gc->bottomy = maxdepth;
305 plot_text_samples(gc, pi);
308 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
311 struct plot_data *entry = pi->entry;
313 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
314 move_to(gc, entry->sec, entry->smoothed);
315 for (i = 1; i < pi->nr; i++) {
317 line_to(gc, entry->sec, entry->smoothed);
319 cairo_stroke(gc->cr);
322 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
326 struct plot_data *entry = pi->entry;
328 set_source_rgba(gc, 1, 0.2, 1, a);
329 move_to(gc, entry->sec, entry->min[index]->depth);
330 for (i = 1; i < pi->nr; i++) {
332 line_to(gc, entry->sec, entry->min[index]->depth);
334 for (i = 1; i < pi->nr; i++) {
335 line_to(gc, entry->sec, entry->max[index]->depth);
338 cairo_close_path(gc->cr);
342 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
346 plot_minmax_profile_minute(gc, pi, 2, 0.1);
347 plot_minmax_profile_minute(gc, pi, 1, 0.1);
348 plot_minmax_profile_minute(gc, pi, 0, 0.1);
351 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
354 cairo_t *cr = gc->cr;
356 struct plot_data *entry;
357 int maxtime, maxdepth, marker;
358 int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
360 /* Get plot scaling limits */
361 maxtime = get_maxtime(pi);
362 maxdepth = get_maxdepth(pi);
364 /* Time markers: at most every 5 min, but no more than 12 markers
365 * and for convenience we do 5, 10, 15 or 30 min intervals.
366 * This allows for 6h dives - enough (I hope) for even the craziest
367 * divers - but just in case, for those 8h depth-record-breaking dives,
368 * we double the interval if this still doesn't get us to 12 or fewer
371 while (maxtime / increments[i] > 12 && i < 4)
373 incr = increments[i];
374 while (maxtime / incr > 12)
377 gc->leftx = 0; gc->rightx = maxtime;
378 gc->topy = 0; gc->bottomy = 1.0;
379 set_source_rgba(gc, 1, 1, 1, 0.5);
380 for (i = incr; i < maxtime; i += incr) {
386 /* now the text on every second time marker */
387 text_render_options_t tro = {10, 0.2, 1.0, 0.2, CENTER, TOP};
388 for (i = incr; i < maxtime; i += 2 * incr)
389 plot_text(gc, &tro, i, 1, "%d", i/60);
391 /* Depth markers: every 30 ft or 10 m*/
392 gc->leftx = 0; gc->rightx = 1.0;
393 gc->topy = 0; gc->bottomy = maxdepth;
394 switch (output_units.length) {
395 case METERS: marker = 10000; break;
396 case FEET: marker = 9144; break; /* 30 ft */
399 set_source_rgba(gc, 1, 1, 1, 0.5);
400 for (i = marker; i < maxdepth; i += marker) {
406 /* Show mean depth */
408 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
409 move_to(gc, 0, pi->meandepth);
410 line_to(gc, 1, pi->meandepth);
414 gc->leftx = 0; gc->rightx = maxtime;
417 * These are good for debugging text placement etc,
418 * but not for actual display..
421 plot_smoothed_profile(gc, pi);
422 plot_minmax_profile(gc, pi);
425 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
427 /* Do the depth profile for the neat fill */
428 gc->topy = 0; gc->bottomy = maxdepth;
429 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
433 for (i = 0; i < pi->nr; i++, entry++)
434 line_to(gc, entry->sec, entry->depth);
435 cairo_close_path(gc->cr);
437 set_source_rgba(gc, 1, 1, 1, 0.2);
438 cairo_fill_preserve(cr);
439 set_source_rgb(gc, 1, 1, 1);
445 /* Now do it again for the velocity colors */
447 for (i = 1; i < pi->nr; i++) {
450 /* we want to draw the segments in different colors
451 * representing the vertical velocity, so we need to
452 * chop this into short segments */
453 rgb_t color = rgb[entry->velocity];
454 depth = entry->depth;
455 set_source_rgb(gc, color.r, color.g, color.b);
456 move_to(gc, entry[-1].sec, entry[-1].depth);
457 line_to(gc, sec, depth);
462 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
464 int maxtime, mintemp, maxtemp, delta;
466 /* Get plot scaling limits */
467 maxtime = get_maxtime(pi);
468 mintemp = pi->mintemp;
469 maxtemp = pi->maxtemp;
471 gc->leftx = 0; gc->rightx = maxtime;
472 /* Show temperatures in roughly the lower third, but make sure the scale
473 is at least somewhat reasonable */
474 delta = maxtemp - mintemp;
475 if (delta > 3000) { /* more than 3K in fluctuation */
476 gc->topy = maxtemp + delta*2;
477 gc->bottomy = mintemp - delta/2;
479 gc->topy = maxtemp + 1500 + delta*2;
480 gc->bottomy = mintemp - delta/2;
483 return maxtemp > mintemp;
486 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
490 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
491 temperature_t temperature = { mkelvin };
493 if (output_units.temperature == FAHRENHEIT) {
494 deg = to_F(temperature);
495 unit = UTF8_DEGREE "F";
497 deg = to_C(temperature);
498 unit = UTF8_DEGREE "C";
500 plot_text(gc, &tro, sec, temperature.mkelvin, "%d%s", deg, unit);
503 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
506 int last = 0, sec = 0;
507 int last_temperature = 0, last_printed_temp = 0;
509 if (!setup_temperature_limits(gc, pi))
512 for (i = 0; i < pi->nr; i++) {
513 struct plot_data *entry = pi->entry+i;
514 int mkelvin = entry->temperature;
518 last_temperature = mkelvin;
520 if (sec < last + 300)
523 plot_single_temp_text(gc,sec,mkelvin);
524 last_printed_temp = mkelvin;
526 /* it would be nice to print the end temperature, if it's different */
527 if (abs(last_temperature - last_printed_temp) > 500)
528 plot_single_temp_text(gc, sec, last_temperature);
531 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
534 cairo_t *cr = gc->cr;
537 if (!setup_temperature_limits(gc, pi))
540 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
541 for (i = 0; i < pi->nr; i++) {
542 struct plot_data *entry = pi->entry + i;
543 int mkelvin = entry->temperature;
544 int sec = entry->sec;
551 line_to(gc, sec, mkelvin);
553 move_to(gc, sec, mkelvin);
559 /* gets both the actual start and end pressure as well as the scaling factors */
560 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
563 gc->rightx = get_maxtime(pi);
565 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
566 return pi->maxpressure != 0;
569 static void plot_pressure_helper(struct graphics_context *gc, struct plot_info *pi, int type)
572 int lift_pen = FALSE;
574 for (i = 0; i < pi->nr; i++) {
576 struct plot_data *entry = pi->entry + i;
578 mbar = entry->pressure[type];
579 if (!entry->same_cylinder)
586 if (i > 0 && entry->same_cylinder) {
587 /* if we have a previous event from the same tank,
588 * draw at least a short line .
589 * This uses the implementation detail that the
590 * type is either 0 or 1 */
592 prev_pr = (entry-1)->pressure[type] ? : (entry-1)->pressure[1 - type];
593 move_to(gc, (entry-1)->sec, prev_pr);
594 line_to(gc, entry->sec, mbar);
596 move_to(gc, entry->sec, mbar);
600 line_to(gc, entry->sec, mbar);
602 cairo_stroke(gc->cr);
606 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
608 if (!get_cylinder_pressure_range(gc, pi))
611 /* first plot the pressure readings we have from the dive computer */
612 set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
613 plot_pressure_helper(gc, pi, SENSOR_PR);
615 /* then, in a different color, the interpolated values */
616 set_source_rgba(gc, 1.0, 1.0, 0.2, 0.80);
617 plot_pressure_helper(gc, pi, INTERPOLATED_PR);
620 static int mbar_to_PSI(int mbar)
622 pressure_t p = {mbar};
626 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
627 int xalign, int yalign)
632 switch (output_units.pressure) {
634 pressure = mbar * 100;
638 pressure = (mbar + 500) / 1000;
642 pressure = mbar_to_PSI(mbar);
646 text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
647 plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
650 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
654 int seen_cyl[MAX_CYLINDERS] = { FALSE, };
655 int last_pressure[MAX_CYLINDERS] = { 0, };
656 int last_time[MAX_CYLINDERS] = { 0, };
657 struct plot_data *entry;
659 if (!get_cylinder_pressure_range(gc, pi))
662 /* only loop over the actual events from the dive computer */
663 for (i = 2; i < pi->nr - 2; i++) {
664 entry = pi->entry + i;
666 if (!entry->same_cylinder) {
667 cyl = entry->cylinderindex;
668 if (!seen_cyl[cyl]) {
669 mbar = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
670 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
671 seen_cyl[cyl] = TRUE;
674 /* remember the last pressure and time of
675 * the previous cylinder */
676 cyl = (entry - 1)->cylinderindex;
678 SENSOR_PRESSURE(entry - 1) ? : INTERPOLATED_PRESSURE(entry - 1);
679 last_time[cyl] = (entry - 1)->sec;
683 cyl = entry->cylinderindex;
684 last_pressure[cyl] = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
685 last_time[cyl] = entry->sec;
687 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
688 if (last_time[cyl]) {
689 plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
694 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
696 struct plot_data *p = entry;
697 int time = entry->sec;
698 int seconds = 90*(index+1);
699 struct plot_data *min, *max;
702 /* Go back 'seconds' in time */
704 if (p[-1].sec < time - seconds)
709 /* Then go forward until we hit an entry past the time */
714 int depth = p->depth;
715 if (p->sec > time + seconds)
719 if (depth < min->depth)
721 if (depth > max->depth)
724 entry->min[index] = min;
725 entry->max[index] = max;
726 entry->avg[index] = (avg + nr/2) / nr;
729 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
731 analyze_plot_info_minmax_minute(entry, first, last, 0);
732 analyze_plot_info_minmax_minute(entry, first, last, 1);
733 analyze_plot_info_minmax_minute(entry, first, last, 2);
736 static velocity_t velocity(int speed)
740 if (speed < -304) /* ascent faster than -60ft/min */
742 else if (speed < -152) /* above -30ft/min */
744 else if (speed < -76) /* -15ft/min */
746 else if (speed < -25) /* -5ft/min */
748 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
749 for descent are usually about 2x ascent rate; still, we want
750 stable to mean stable */
752 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
754 else if (speed < 304) /* up to 60ft/min is moderate */
756 else if (speed < 507) /* up to 100ft/min is fast */
758 else /* more than that is just crazy - you'll blow your ears out */
763 static struct plot_info *analyze_plot_info(struct plot_info *pi)
768 /* Do pressure min/max based on the non-surface data */
769 for (i = 0; i < nr; i++) {
770 struct plot_data *entry = pi->entry+i;
771 int pressure = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
772 int temperature = entry->temperature;
775 if (!pi->minpressure || pressure < pi->minpressure)
776 pi->minpressure = pressure;
777 if (pressure > pi->maxpressure)
778 pi->maxpressure = pressure;
782 if (!pi->mintemp || temperature < pi->mintemp)
783 pi->mintemp = temperature;
784 if (temperature > pi->maxtemp)
785 pi->maxtemp = temperature;
789 /* Smoothing function: 5-point triangular smooth */
790 for (i = 2; i < nr; i++) {
791 struct plot_data *entry = pi->entry+i;
795 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
796 entry->smoothed = (depth+4) / 9;
798 /* vertical velocity in mm/sec */
799 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
800 if (entry[0].sec - entry[-1].sec) {
801 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
802 /* if our samples are short and we aren't too FAST*/
803 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
805 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
807 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
808 (entry[0].sec - entry[past].sec));
811 entry->velocity = STABLE;
814 /* One-, two- and three-minute minmax data */
815 for (i = 0; i < nr; i++) {
816 struct plot_data *entry = pi->entry +i;
817 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
824 * simple structure to track the beginning and end tank pressure as
825 * well as the integral of depth over time spent while we have no
826 * pressure reading from the tank */
827 typedef struct pr_track_struct pr_track_t;
828 struct pr_track_struct {
833 double pressure_time;
837 static pr_track_t *pr_track_alloc(int start, int t_start) {
838 pr_track_t *pt = malloc(sizeof(pr_track_t));
840 pt->t_start = t_start;
843 pt->pressure_time = 0.0;
848 /* poor man's linked list */
849 static pr_track_t *list_last(pr_track_t *list)
851 pr_track_t *tail = list;
860 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
862 pr_track_t *tail = list_last(list);
865 tail->next = element;
869 static void list_free(pr_track_t *list)
873 list_free(list->next);
877 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
878 pr_track_t **track_pr)
880 pr_track_t *list = NULL;
881 pr_track_t *nlist = NULL;
884 struct plot_data *entry;
885 int cur_pr[MAX_CYLINDERS];
887 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
888 cur_pr[cyl] = track_pr[cyl]->start;
890 for (i = 0; i < dive->samples; i++) {
891 entry = pi->entry + i + 2;
892 if (SENSOR_PRESSURE(entry)) {
893 cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
895 if(!list || list->t_end < entry->sec) {
896 nlist = track_pr[entry->cylinderindex];
898 while (nlist && nlist->t_start <= entry->sec) {
902 /* there may be multiple segments - so
903 * let's assemble the length */
905 pt = list->pressure_time;
906 while (!nlist->end) {
909 /* oops - we have no end pressure,
910 * so this means this is a tank without
911 * gas consumption information */
914 pt += nlist->pressure_time;
917 /* just continue without calculating
918 * interpolated values */
922 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt; }
924 double cur_pt = (entry->sec - (entry-1)->sec) *
925 (1 + entry->depth / 10000.0);
926 INTERPOLATED_PRESSURE(entry) =
927 cur_pr[entry->cylinderindex] + cur_pt * magic;
928 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
934 static int get_cylinder_index(struct dive *dive, struct event *ev)
939 * Try to find a cylinder that matches the O2 percentage
940 * in the gas change event 'value' field.
942 * Crazy suunto gas change events. We really should do
943 * this in libdivecomputer or something.
945 for (i = 0; i < MAX_CYLINDERS; i++) {
946 cylinder_t *cyl = dive->cylinder+i;
947 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
955 static struct event *get_next_gaschange(struct event *event)
958 if (!strcmp(event->name, "gaschange"))
965 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
968 struct plot_data *entry = pi->entry+i;
969 if (entry->sec > end)
971 if (entry->cylinderindex != cylinderindex) {
972 entry->cylinderindex = cylinderindex;
973 entry->pressure[0] = 0;
980 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
982 int i = 0, cylinderindex = 0;
983 struct event *ev = get_next_gaschange(dive->events);
989 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
990 cylinderindex = get_cylinder_index(dive, ev);
991 ev = get_next_gaschange(ev->next);
993 set_cylinder_index(pi, i, cylinderindex, ~0u);
997 * Create a plot-info with smoothing and ranged min/max
999 * This also makes sure that we have extra empty events on both
1000 * sides, so that you can do end-points without having to worry
1003 static struct plot_info *create_plot_info(struct dive *dive)
1005 int cylinderindex = -1;
1006 int lastdepth, lastindex;
1007 int i, nr = dive->samples + 4, sec, cyl;
1008 size_t alloc_size = plot_info_size(nr);
1009 struct plot_info *pi;
1010 pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1011 pr_track_t *pr_track, *current;
1012 gboolean missing_pr = FALSE;
1013 struct plot_data *entry;
1015 pi = malloc(alloc_size);
1018 memset(pi, 0, alloc_size);
1023 for (i = 0; i < dive->samples; i++) {
1025 struct sample *sample = dive->sample+i;
1027 entry = pi->entry + i + 2;
1028 sec = entry->sec = sample->time.seconds;
1029 depth = entry->depth = sample->depth.mm;
1030 entry->cylinderindex = sample->cylinderindex;
1031 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1032 entry->temperature = sample->temperature.mkelvin;
1034 if (depth || lastdepth)
1038 if (depth > pi->maxdepth)
1039 pi->maxdepth = depth;
1042 check_gas_change_events(dive, pi);
1044 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1045 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1046 current = track_pr[dive->sample[0].cylinderindex];
1047 for (i = 0; i < dive->samples; i++) {
1048 entry = pi->entry + i + 2;
1050 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1051 cylinderindex = entry->cylinderindex;
1053 /* track the segments per cylinder and their pressure/time integral */
1054 if (!entry->same_cylinder) {
1055 current->end = SENSOR_PRESSURE(entry-1);
1056 current->t_end = (entry-1)->sec;
1057 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1058 track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1059 } else { /* same cylinder */
1060 if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1061 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1062 /* transmitter changed its working status */
1063 current->end = SENSOR_PRESSURE(entry-1);
1064 current->t_end = (entry-1)->sec;
1065 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1066 track_pr[cylinderindex] =
1067 list_add(track_pr[cylinderindex], current);
1070 /* finally, do the discrete integration to get the SAC rate equivalent */
1071 current->pressure_time += (entry->sec - (entry-1)->sec) *
1072 (1 + entry->depth / 10000.0);
1073 missing_pr |= !SENSOR_PRESSURE(entry);
1076 current->t_end = entry->sec;
1077 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1078 int pr = dive->cylinder[cyl].end.mbar;
1079 if (pr && track_pr[cyl]) {
1080 pr_track = list_last(track_pr[cyl]);
1086 /* Fill in the last two entries with empty values but valid times */
1087 i = dive->samples + 2;
1088 pi->entry[i].sec = sec + 20;
1089 pi->entry[i+1].sec = sec + 40;
1090 pi->nr = lastindex+1;
1091 pi->maxtime = pi->entry[lastindex].sec;
1093 pi->endpressure = pi->minpressure = dive->cylinder[0].end.mbar;
1094 pi->maxpressure = dive->cylinder[0].start.mbar;
1096 pi->meandepth = dive->meandepth.mm;
1099 fill_missing_tank_pressures(dive, pi, track_pr);
1101 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1102 list_free(track_pr[cyl]);
1103 return analyze_plot_info(pi);
1106 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1108 struct plot_info *pi = create_plot_info(dive);
1110 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1111 cairo_set_line_width(gc->cr, 2);
1112 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1113 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1116 * We can use "cairo_translate()" because that doesn't
1117 * scale line width etc. But the actual scaling we need
1118 * do set up ourselves..
1120 * Snif. What a pity.
1122 gc->maxx = (drawing_area->width - 2*drawing_area->x);
1123 gc->maxy = (drawing_area->height - 2*drawing_area->y);
1125 /* Temperature profile */
1126 plot_temperature_profile(gc, pi);
1128 /* Cylinder pressure plot */
1129 plot_cylinder_pressure(gc, pi);
1132 plot_depth_profile(gc, pi);
1133 plot_events(gc, pi, dive);
1135 /* Text on top of all graphs.. */
1136 plot_temperature_text(gc, pi);
1137 plot_depth_text(gc, pi);
1138 plot_cylinder_pressure_text(gc, pi);
1140 /* Bounding box last */
1141 gc->leftx = 0; gc->rightx = 1.0;
1142 gc->topy = 0; gc->bottomy = 1.0;
1144 set_source_rgb(gc, 1, 1, 1);
1149 cairo_close_path(gc->cr);
1150 cairo_stroke(gc->cr);