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 remember_event(const char *eventname)
181 if (!eventname || (len = strlen(eventname)) == 0)
183 while (i < evn_used) {
184 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
188 if (evn_used == evn_allocated) {
190 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
192 /* we are screwed, but let's just bail out */
195 ev_namelist[evn_used].ev_name = strdup(eventname);
196 ev_namelist[evn_used].plot_ev = TRUE;
200 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
205 /* is plotting this event disabled? */
207 for (i = 0; i < evn_used; i++) {
208 if (! strcmp(event->name, ev_namelist[i].ev_name)) {
209 if (ev_namelist[i].plot_ev)
216 for (i = 0; i < pi->nr; i++) {
217 struct plot_data *data = pi->entry + i;
218 if (event->time.seconds < data->sec)
222 /* draw a little tirangular marker and attach tooltip */
223 x = SCALEX(gc, event->time.seconds);
224 y = SCALEY(gc, depth);
225 set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
226 cairo_move_to(gc->cr, x-15, y+6);
227 cairo_line_to(gc->cr, x-3 , y+6);
228 cairo_line_to(gc->cr, x-9, y-6);
229 cairo_line_to(gc->cr, x-15, y+6);
230 cairo_stroke_preserve(gc->cr);
232 set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
233 cairo_move_to(gc->cr, x-9, y-3);
234 cairo_line_to(gc->cr, x-9, y+1);
235 cairo_move_to(gc->cr, x-9, y+4);
236 cairo_line_to(gc->cr, x-9, y+4);
237 cairo_stroke(gc->cr);
238 attach_tooltip(x-15, y-6, 12, 12, event->name);
241 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
243 static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
244 struct event *event = dive->events;
250 plot_one_event(gc, pi, event, &tro);
255 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
257 int sec = entry->sec, decimals;
260 d = get_depth_units(entry->depth, &decimals, NULL);
262 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
265 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
267 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
268 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
271 for (i = 0; i < pi->nr; i++) {
272 struct plot_data *entry = pi->entry + i;
274 if (entry->depth < 2000)
277 if (entry == entry->max[2])
278 render_depth_sample(gc, entry, &deep);
280 if (entry == entry->min[2])
281 render_depth_sample(gc, entry, &shallow);
285 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
287 int maxtime, maxdepth;
289 /* Get plot scaling limits */
290 maxtime = get_maxtime(pi);
291 maxdepth = get_maxdepth(pi);
293 gc->leftx = 0; gc->rightx = maxtime;
294 gc->topy = 0; gc->bottomy = maxdepth;
296 plot_text_samples(gc, pi);
299 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
302 struct plot_data *entry = pi->entry;
304 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
305 move_to(gc, entry->sec, entry->smoothed);
306 for (i = 1; i < pi->nr; i++) {
308 line_to(gc, entry->sec, entry->smoothed);
310 cairo_stroke(gc->cr);
313 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
317 struct plot_data *entry = pi->entry;
319 set_source_rgba(gc, 1, 0.2, 1, a);
320 move_to(gc, entry->sec, entry->min[index]->depth);
321 for (i = 1; i < pi->nr; i++) {
323 line_to(gc, entry->sec, entry->min[index]->depth);
325 for (i = 1; i < pi->nr; i++) {
326 line_to(gc, entry->sec, entry->max[index]->depth);
329 cairo_close_path(gc->cr);
333 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
337 plot_minmax_profile_minute(gc, pi, 2, 0.1);
338 plot_minmax_profile_minute(gc, pi, 1, 0.1);
339 plot_minmax_profile_minute(gc, pi, 0, 0.1);
342 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
345 cairo_t *cr = gc->cr;
347 struct plot_data *entry;
348 int maxtime, maxdepth, marker;
349 int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
351 /* Get plot scaling limits */
352 maxtime = get_maxtime(pi);
353 maxdepth = get_maxdepth(pi);
355 /* Time markers: at most every 5 min, but no more than 12 markers
356 * and for convenience we do 5, 10, 15 or 30 min intervals.
357 * This allows for 6h dives - enough (I hope) for even the craziest
358 * divers - but just in case, for those 8h depth-record-breaking dives,
359 * we double the interval if this still doesn't get us to 12 or fewer
362 while (maxtime / increments[i] > 12 && i < 4)
364 incr = increments[i];
365 while (maxtime / incr > 12)
368 gc->leftx = 0; gc->rightx = maxtime;
369 gc->topy = 0; gc->bottomy = 1.0;
370 set_source_rgba(gc, 1, 1, 1, 0.5);
371 for (i = incr; i < maxtime; i += incr) {
377 /* now the text on every second time marker */
378 text_render_options_t tro = {10, 0.2, 1.0, 0.2, CENTER, TOP};
379 for (i = incr; i < maxtime; i += 2 * incr)
380 plot_text(gc, &tro, i, 1, "%d", i/60);
382 /* Depth markers: every 30 ft or 10 m*/
383 gc->leftx = 0; gc->rightx = 1.0;
384 gc->topy = 0; gc->bottomy = maxdepth;
385 switch (output_units.length) {
386 case METERS: marker = 10000; break;
387 case FEET: marker = 9144; break; /* 30 ft */
390 set_source_rgba(gc, 1, 1, 1, 0.5);
391 for (i = marker; i < maxdepth; i += marker) {
397 /* Show mean depth */
399 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
400 move_to(gc, 0, pi->meandepth);
401 line_to(gc, 1, pi->meandepth);
405 gc->leftx = 0; gc->rightx = maxtime;
408 * These are good for debugging text placement etc,
409 * but not for actual display..
412 plot_smoothed_profile(gc, pi);
413 plot_minmax_profile(gc, pi);
416 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
418 /* Do the depth profile for the neat fill */
419 gc->topy = 0; gc->bottomy = maxdepth;
420 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
424 for (i = 0; i < pi->nr; i++, entry++)
425 line_to(gc, entry->sec, entry->depth);
426 cairo_close_path(gc->cr);
428 set_source_rgba(gc, 1, 1, 1, 0.2);
429 cairo_fill_preserve(cr);
430 set_source_rgb(gc, 1, 1, 1);
436 /* Now do it again for the velocity colors */
438 for (i = 1; i < pi->nr; i++) {
441 /* we want to draw the segments in different colors
442 * representing the vertical velocity, so we need to
443 * chop this into short segments */
444 rgb_t color = rgb[entry->velocity];
445 depth = entry->depth;
446 set_source_rgb(gc, color.r, color.g, color.b);
447 move_to(gc, entry[-1].sec, entry[-1].depth);
448 line_to(gc, sec, depth);
453 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
455 int maxtime, mintemp, maxtemp, delta;
457 /* Get plot scaling limits */
458 maxtime = get_maxtime(pi);
459 mintemp = pi->mintemp;
460 maxtemp = pi->maxtemp;
462 gc->leftx = 0; gc->rightx = maxtime;
463 /* Show temperatures in roughly the lower third, but make sure the scale
464 is at least somewhat reasonable */
465 delta = maxtemp - mintemp;
466 if (delta > 3000) { /* more than 3K in fluctuation */
467 gc->topy = maxtemp + delta*2;
468 gc->bottomy = mintemp - delta/2;
470 gc->topy = maxtemp + 1500 + delta*2;
471 gc->bottomy = mintemp - delta/2;
474 return maxtemp > mintemp;
477 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
481 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
482 temperature_t temperature = { mkelvin };
484 if (output_units.temperature == FAHRENHEIT) {
485 deg = to_F(temperature);
486 unit = UTF8_DEGREE "F";
488 deg = to_C(temperature);
489 unit = UTF8_DEGREE "C";
491 plot_text(gc, &tro, sec, temperature.mkelvin, "%d%s", deg, unit);
494 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
497 int last = 0, sec = 0;
498 int last_temperature = 0, last_printed_temp = 0;
500 if (!setup_temperature_limits(gc, pi))
503 for (i = 0; i < pi->nr; i++) {
504 struct plot_data *entry = pi->entry+i;
505 int mkelvin = entry->temperature;
509 last_temperature = mkelvin;
511 if (sec < last + 300)
514 plot_single_temp_text(gc,sec,mkelvin);
515 last_printed_temp = mkelvin;
517 /* it would be nice to print the end temperature, if it's different */
518 if (abs(last_temperature - last_printed_temp) > 500)
519 plot_single_temp_text(gc, sec, last_temperature);
522 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
525 cairo_t *cr = gc->cr;
528 if (!setup_temperature_limits(gc, pi))
531 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
532 for (i = 0; i < pi->nr; i++) {
533 struct plot_data *entry = pi->entry + i;
534 int mkelvin = entry->temperature;
535 int sec = entry->sec;
542 line_to(gc, sec, mkelvin);
544 move_to(gc, sec, mkelvin);
550 /* gets both the actual start and end pressure as well as the scaling factors */
551 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
554 gc->rightx = get_maxtime(pi);
556 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
557 return pi->maxpressure != 0;
560 static void plot_pressure_helper(struct graphics_context *gc, struct plot_info *pi, int type)
563 int lift_pen = FALSE;
565 for (i = 0; i < pi->nr; i++) {
567 struct plot_data *entry = pi->entry + i;
569 mbar = entry->pressure[type];
570 if (!entry->same_cylinder)
577 if (i > 0 && entry->same_cylinder) {
578 /* if we have a previous event from the same tank,
579 * draw at least a short line .
580 * This uses the implementation detail that the
581 * type is either 0 or 1 */
583 prev_pr = (entry-1)->pressure[type] ? : (entry-1)->pressure[1 - type];
584 move_to(gc, (entry-1)->sec, prev_pr);
585 line_to(gc, entry->sec, mbar);
587 move_to(gc, entry->sec, mbar);
591 line_to(gc, entry->sec, mbar);
593 cairo_stroke(gc->cr);
597 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
599 if (!get_cylinder_pressure_range(gc, pi))
602 /* first plot the pressure readings we have from the dive computer */
603 set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
604 plot_pressure_helper(gc, pi, SENSOR_PR);
606 /* then, in a different color, the interpolated values */
607 set_source_rgba(gc, 1.0, 1.0, 0.2, 0.80);
608 plot_pressure_helper(gc, pi, INTERPOLATED_PR);
611 static int mbar_to_PSI(int mbar)
613 pressure_t p = {mbar};
617 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
618 int xalign, int yalign)
623 switch (output_units.pressure) {
625 pressure = mbar * 100;
629 pressure = (mbar + 500) / 1000;
633 pressure = mbar_to_PSI(mbar);
637 text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
638 plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
641 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
645 int seen_cyl[MAX_CYLINDERS] = { FALSE, };
646 int last_pressure[MAX_CYLINDERS] = { 0, };
647 int last_time[MAX_CYLINDERS] = { 0, };
648 struct plot_data *entry;
650 if (!get_cylinder_pressure_range(gc, pi))
653 /* only loop over the actual events from the dive computer */
654 for (i = 2; i < pi->nr - 2; i++) {
655 entry = pi->entry + i;
657 if (!entry->same_cylinder) {
658 cyl = entry->cylinderindex;
659 if (!seen_cyl[cyl]) {
660 mbar = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
661 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
662 seen_cyl[cyl] = TRUE;
665 /* remember the last pressure and time of
666 * the previous cylinder */
667 cyl = (entry - 1)->cylinderindex;
669 SENSOR_PRESSURE(entry - 1) ? : INTERPOLATED_PRESSURE(entry - 1);
670 last_time[cyl] = (entry - 1)->sec;
674 cyl = entry->cylinderindex;
675 last_pressure[cyl] = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
676 last_time[cyl] = entry->sec;
678 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
679 if (last_time[cyl]) {
680 plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
685 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
687 struct plot_data *p = entry;
688 int time = entry->sec;
689 int seconds = 90*(index+1);
690 struct plot_data *min, *max;
693 /* Go back 'seconds' in time */
695 if (p[-1].sec < time - seconds)
700 /* Then go forward until we hit an entry past the time */
705 int depth = p->depth;
706 if (p->sec > time + seconds)
710 if (depth < min->depth)
712 if (depth > max->depth)
715 entry->min[index] = min;
716 entry->max[index] = max;
717 entry->avg[index] = (avg + nr/2) / nr;
720 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
722 analyze_plot_info_minmax_minute(entry, first, last, 0);
723 analyze_plot_info_minmax_minute(entry, first, last, 1);
724 analyze_plot_info_minmax_minute(entry, first, last, 2);
727 static velocity_t velocity(int speed)
731 if (speed < -304) /* ascent faster than -60ft/min */
733 else if (speed < -152) /* above -30ft/min */
735 else if (speed < -76) /* -15ft/min */
737 else if (speed < -25) /* -5ft/min */
739 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
740 for descent are usually about 2x ascent rate; still, we want
741 stable to mean stable */
743 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
745 else if (speed < 304) /* up to 60ft/min is moderate */
747 else if (speed < 507) /* up to 100ft/min is fast */
749 else /* more than that is just crazy - you'll blow your ears out */
754 static struct plot_info *analyze_plot_info(struct plot_info *pi)
759 /* Do pressure min/max based on the non-surface data */
760 for (i = 0; i < nr; i++) {
761 struct plot_data *entry = pi->entry+i;
762 int pressure = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
763 int temperature = entry->temperature;
766 if (!pi->minpressure || pressure < pi->minpressure)
767 pi->minpressure = pressure;
768 if (pressure > pi->maxpressure)
769 pi->maxpressure = pressure;
773 if (!pi->mintemp || temperature < pi->mintemp)
774 pi->mintemp = temperature;
775 if (temperature > pi->maxtemp)
776 pi->maxtemp = temperature;
780 /* Smoothing function: 5-point triangular smooth */
781 for (i = 2; i < nr; i++) {
782 struct plot_data *entry = pi->entry+i;
786 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
787 entry->smoothed = (depth+4) / 9;
789 /* vertical velocity in mm/sec */
790 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
791 if (entry[0].sec - entry[-1].sec) {
792 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
793 /* if our samples are short and we aren't too FAST*/
794 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
796 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
798 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
799 (entry[0].sec - entry[past].sec));
802 entry->velocity = STABLE;
805 /* One-, two- and three-minute minmax data */
806 for (i = 0; i < nr; i++) {
807 struct plot_data *entry = pi->entry +i;
808 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
815 * simple structure to track the beginning and end tank pressure as
816 * well as the integral of depth over time spent while we have no
817 * pressure reading from the tank */
818 typedef struct pr_track_struct pr_track_t;
819 struct pr_track_struct {
824 double pressure_time;
828 static pr_track_t *pr_track_alloc(int start, int t_start) {
829 pr_track_t *pt = malloc(sizeof(pr_track_t));
831 pt->t_start = t_start;
834 pt->pressure_time = 0.0;
839 /* poor man's linked list */
840 static pr_track_t *list_last(pr_track_t *list)
842 pr_track_t *tail = list;
851 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
853 pr_track_t *tail = list_last(list);
856 tail->next = element;
860 static void list_free(pr_track_t *list)
864 list_free(list->next);
868 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
869 pr_track_t **track_pr)
871 pr_track_t *list = NULL;
872 pr_track_t *nlist = NULL;
875 struct plot_data *entry;
876 int cur_pr[MAX_CYLINDERS];
878 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
879 cur_pr[cyl] = track_pr[cyl]->start;
881 for (i = 0; i < dive->samples; i++) {
882 entry = pi->entry + i + 2;
883 if (SENSOR_PRESSURE(entry)) {
884 cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
886 if(!list || list->t_end < entry->sec) {
887 nlist = track_pr[entry->cylinderindex];
889 while (nlist && nlist->t_start <= entry->sec) {
893 /* there may be multiple segments - so
894 * let's assemble the length */
896 pt = list->pressure_time;
897 while (!nlist->end) {
900 /* oops - we have no end pressure,
901 * so this means this is a tank without
902 * gas consumption information */
905 pt += nlist->pressure_time;
908 /* just continue without calculating
909 * interpolated values */
913 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt; }
915 double cur_pt = (entry->sec - (entry-1)->sec) *
916 (1 + entry->depth / 10000.0);
917 INTERPOLATED_PRESSURE(entry) =
918 cur_pr[entry->cylinderindex] + cur_pt * magic;
919 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
925 static int get_cylinder_index(struct dive *dive, struct event *ev)
930 * Try to find a cylinder that matches the O2 percentage
931 * in the gas change event 'value' field.
933 * Crazy suunto gas change events. We really should do
934 * this in libdivecomputer or something.
936 for (i = 0; i < MAX_CYLINDERS; i++) {
937 cylinder_t *cyl = dive->cylinder+i;
938 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
946 static struct event *get_next_gaschange(struct event *event)
949 if (!strcmp(event->name, "gaschange"))
956 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
959 struct plot_data *entry = pi->entry+i;
960 if (entry->sec > end)
962 if (entry->cylinderindex != cylinderindex) {
963 entry->cylinderindex = cylinderindex;
964 entry->pressure[0] = 0;
971 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
973 int i = 0, cylinderindex = 0;
974 struct event *ev = get_next_gaschange(dive->events);
980 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
981 cylinderindex = get_cylinder_index(dive, ev);
982 ev = get_next_gaschange(ev->next);
984 set_cylinder_index(pi, i, cylinderindex, ~0u);
988 * Create a plot-info with smoothing and ranged min/max
990 * This also makes sure that we have extra empty events on both
991 * sides, so that you can do end-points without having to worry
994 static struct plot_info *create_plot_info(struct dive *dive)
996 int cylinderindex = -1;
997 int lastdepth, lastindex;
998 int i, nr = dive->samples + 4, sec, cyl;
999 size_t alloc_size = plot_info_size(nr);
1000 struct plot_info *pi;
1001 pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1002 pr_track_t *pr_track, *current;
1003 gboolean missing_pr = FALSE;
1004 struct plot_data *entry;
1006 pi = malloc(alloc_size);
1009 memset(pi, 0, alloc_size);
1014 for (i = 0; i < dive->samples; i++) {
1016 struct sample *sample = dive->sample+i;
1018 entry = pi->entry + i + 2;
1019 sec = entry->sec = sample->time.seconds;
1020 depth = entry->depth = sample->depth.mm;
1021 entry->cylinderindex = sample->cylinderindex;
1022 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1023 entry->temperature = sample->temperature.mkelvin;
1025 if (depth || lastdepth)
1029 if (depth > pi->maxdepth)
1030 pi->maxdepth = depth;
1033 check_gas_change_events(dive, pi);
1035 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1036 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1037 current = track_pr[dive->sample[0].cylinderindex];
1038 for (i = 0; i < dive->samples; i++) {
1039 entry = pi->entry + i + 2;
1041 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1042 cylinderindex = entry->cylinderindex;
1044 /* track the segments per cylinder and their pressure/time integral */
1045 if (!entry->same_cylinder) {
1046 current->end = SENSOR_PRESSURE(entry-1);
1047 current->t_end = (entry-1)->sec;
1048 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1049 track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1050 } else { /* same cylinder */
1051 if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1052 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1053 /* transmitter changed its working status */
1054 current->end = SENSOR_PRESSURE(entry-1);
1055 current->t_end = (entry-1)->sec;
1056 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1057 track_pr[cylinderindex] =
1058 list_add(track_pr[cylinderindex], current);
1061 /* finally, do the discrete integration to get the SAC rate equivalent */
1062 current->pressure_time += (entry->sec - (entry-1)->sec) *
1063 (1 + entry->depth / 10000.0);
1064 missing_pr |= !SENSOR_PRESSURE(entry);
1067 current->t_end = entry->sec;
1068 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1069 int pr = dive->cylinder[cyl].end.mbar;
1070 if (pr && track_pr[cyl]) {
1071 pr_track = list_last(track_pr[cyl]);
1077 /* Fill in the last two entries with empty values but valid times */
1078 i = dive->samples + 2;
1079 pi->entry[i].sec = sec + 20;
1080 pi->entry[i+1].sec = sec + 40;
1081 pi->nr = lastindex+1;
1082 pi->maxtime = pi->entry[lastindex].sec;
1084 pi->endpressure = pi->minpressure = dive->cylinder[0].end.mbar;
1085 pi->maxpressure = dive->cylinder[0].start.mbar;
1087 pi->meandepth = dive->meandepth.mm;
1090 fill_missing_tank_pressures(dive, pi, track_pr);
1092 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1093 list_free(track_pr[cyl]);
1094 return analyze_plot_info(pi);
1097 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1099 struct plot_info *pi = create_plot_info(dive);
1101 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1102 cairo_set_line_width(gc->cr, 2);
1103 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1104 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1107 * We can use "cairo_translate()" because that doesn't
1108 * scale line width etc. But the actual scaling we need
1109 * do set up ourselves..
1111 * Snif. What a pity.
1113 gc->maxx = (drawing_area->width - 2*drawing_area->x);
1114 gc->maxy = (drawing_area->height - 2*drawing_area->y);
1116 /* Temperature profile */
1117 plot_temperature_profile(gc, pi);
1119 /* Cylinder pressure plot */
1120 plot_cylinder_pressure(gc, pi);
1123 plot_depth_profile(gc, pi);
1124 plot_events(gc, pi, dive);
1126 /* Text on top of all graphs.. */
1127 plot_temperature_text(gc, pi);
1128 plot_depth_text(gc, pi);
1129 plot_cylinder_pressure_text(gc, pi);
1131 /* Bounding box last */
1132 gc->leftx = 0; gc->rightx = 1.0;
1133 gc->topy = 0; gc->bottomy = 1.0;
1135 set_source_rgb(gc, 1, 1, 1);
1140 cairo_close_path(gc->cr);
1141 cairo_stroke(gc->cr);