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;
27 unsigned int same_cylinder:1;
28 unsigned int cylinderindex;
30 /* pressure[0] is sensor pressure
31 * pressure[1] is interpolated pressure */
38 struct plot_data *min[3];
39 struct plot_data *max[3];
44 #define INTERPOLATED_PR 1
45 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
46 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
47 #define GET_PRESSURE(_entry) (SENSOR_PRESSURE(_entry) ? : INTERPOLATED_PRESSURE(_entry))
49 typedef enum { TEXT_BACKGROUND, TRIANGULAR_BG, TRIANGULAR_FG, EVENTS, SAMPLE_DEEP, SAMPLE_SHALLOW,
50 SMOOTHED, MINUTE, TIME_MARKER, TIME_TEXT, DEPTH_MARKER, MEAN_DEPTH, DEPTH_TOP,
51 DEPTH_BOTTOM, DEPTH_FILL_PRINTER, DEPTH_STROKE_PRINTER, TEMP_TEXT, TEMP_PLOT, SAC_DEFAULT,
52 BOUNDING_BOX, PRESSURE_TEXT, VELO_STABLE, VELO_SLOW, VELO_MODERATE, VELO_FAST, VELO_CRAZY,
53 SAC_1, SAC_2, SAC_3, SAC_4, SAC_5, SAC_6, SAC_7, SAC_8, SAC_9, BACKGROUND } color_t;
55 /* convert velocity to colors */
56 typedef struct { double r, g, b; } rgb_t;
57 static const color_t velocity_color[] = {
58 [STABLE] = VELO_STABLE,
60 [MODERATE] = VELO_MODERATE,
65 static const rgb_t profile_color[] = {
66 [TEXT_BACKGROUND] = {0.95, 0.95, 0.95},
67 [TRIANGULAR_BG] = {1.0, 1.0, 0.1},
68 [TRIANGULAR_FG] = {0.0, 0.0, 0.0},
69 [EVENTS] = {1.0, 0.2, 0.2},
70 [SAMPLE_DEEP] = {0.8, 0.2, 0.2},
71 [SAMPLE_SHALLOW] = {0.8, 0.2, 0.2},
72 [SMOOTHED] = {1.0, 0.2, 0.2},
73 [MINUTE] = {0.7, 0.2, 0.7},
74 [TIME_MARKER] = {1.0, 1.0, 1.0},
75 [TIME_TEXT] = {0.1, 0.5, 0.1},
76 [DEPTH_MARKER] = {1.0, 1.0, 1.0},
77 [MEAN_DEPTH] = {1.0, 0.2, 0.2},
78 [DEPTH_TOP] = {0.2, 0.2, 0.8},
79 [DEPTH_BOTTOM] = {0.9, 0.9, 0.9},
80 [DEPTH_FILL_PRINTER] = {1.0, 1.0, 1.0},
81 [DEPTH_STROKE_PRINTER] = {1.0, 1.0, 1.0},
82 [TEMP_TEXT] = {0.2, 0.2, 0.7},
83 [TEMP_PLOT] = {0.2, 0.2, 0.9},
84 [SAC_DEFAULT] = {1.0, 1.0, 1.0},
85 [BOUNDING_BOX] = {1.0, 1.0, 1.0},
86 [PRESSURE_TEXT] = {0.2, 0.4, 0.2},
87 [VELO_STABLE] = {0.0, 0.4, 0.0},
88 [VELO_SLOW] = {0.4, 0.8, 0.0},
89 [VELO_MODERATE] = {0.8, 0.8, 0.0},
90 [VELO_FAST] = {0.8, 0.5, 0.0},
91 [VELO_CRAZY] = {1.0, 0.0, 0.0},
92 [SAC_1] = {0.0, 0.4, 0.2},
93 [SAC_2] = {0.2, 0.6, 0.2},
94 [SAC_3] = {0.4, 0.8, 0.2},
95 [SAC_4] = {0.6, 0.8, 0.2},
96 [SAC_5] = {0.8, 0.8, 0.2},
97 [SAC_6] = {0.8, 0.6, 0.2},
98 [SAC_7] = {0.8, 0.4, 0.2},
99 [SAC_8] = {0.9, 0.3, 0.2},
100 [SAC_9] = {1.0, 0.2, 0.2},
101 [BACKGROUND] = {0.95, 0.95, 0.9},
105 static const color_t sac_color[SAC_COLORS] = {
106 SAC_1, SAC_2, SAC_3, SAC_4, SAC_5, SAC_6, SAC_7, SAC_8, SAC_9
109 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
111 /* Scale to 0,0 -> maxx,maxy */
112 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
113 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
114 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
116 static void move_to(struct graphics_context *gc, double x, double y)
118 cairo_move_to(gc->cr, SCALE(gc, x, y));
121 static void line_to(struct graphics_context *gc, double x, double y)
123 cairo_line_to(gc->cr, SCALE(gc, x, y));
126 static void set_source_rgba(struct graphics_context *gc, color_t c, double a)
128 const rgb_t *rgb = &profile_color[c];
133 * For printers, we still honor 'a', but ignore colors
134 * for now. Black is white and white is black
143 cairo_set_source_rgba(gc->cr, r, g, b, a);
146 static void set_source_rgb(struct graphics_context *gc, color_t c)
148 set_source_rgba(gc, c, 1);
151 void init_profile_background(struct graphics_context *gc)
153 set_source_rgb(gc, BACKGROUND);
156 void pattern_add_color_stop_rgba(cairo_pattern_t *pat, double o, color_t c, double a)
158 const rgb_t *rgb = &profile_color[c];
159 cairo_pattern_add_color_stop_rgba (pat, o, rgb->r, rgb->g, rgb->b, a);
162 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
164 /* debugging tool - not normally used */
165 static void dump_pi (struct plot_info *pi)
169 printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
170 " maxpressure:%d mintemp:%d maxtemp:%d\n",
171 pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
172 pi->maxpressure, pi->mintemp, pi->maxtemp);
173 for (i = 0; i < pi->nr; i++)
174 printf(" entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
175 " temperature:%d depth:%d smoothed:%d}\n",
176 i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
177 pi->entry[i].pressure[0], pi->entry[i].pressure[1],
178 pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
183 * When showing dive profiles, we scale things to the
184 * current dive. However, we don't scale past less than
185 * 30 minutes or 90 ft, just so that small dives show
187 * we also need to add 180 seconds at the end so the min/max
190 static int get_maxtime(struct plot_info *pi)
192 int seconds = pi->maxtime;
193 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
194 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
197 static int get_maxdepth(struct plot_info *pi)
199 unsigned mm = pi->maxdepth;
200 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
201 return MAX(30000, ROUND_UP(mm+3000, 10000));
208 } text_render_options_t;
211 #define CENTER (-0.5)
218 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
219 double x, double y, const char *fmt, ...)
221 cairo_t *cr = gc->cr;
222 cairo_font_extents_t fe;
223 cairo_text_extents_t extents;
229 vsnprintf(buffer, sizeof(buffer), fmt, args);
232 cairo_set_font_size(cr, tro->size);
233 cairo_font_extents(cr, &fe);
234 cairo_text_extents(cr, buffer, &extents);
235 dx = tro->hpos * extents.width + extents.x_bearing;
236 dy = tro->vpos * extents.height + fe.descent;
239 cairo_rel_move_to(cr, dx, dy);
241 cairo_text_path(cr, buffer);
242 set_source_rgba(gc, TEXT_BACKGROUND, 0.95);
246 cairo_rel_move_to(cr, dx, dy);
248 set_source_rgb(gc, tro->color);
249 cairo_show_text(cr, buffer);
256 static struct ev_select *ev_namelist;
257 static int evn_allocated;
260 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
264 for (i = 0; i < evn_used; i++) {
265 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
269 void remember_event(const char *eventname)
273 if (!eventname || (len = strlen(eventname)) == 0)
275 while (i < evn_used) {
276 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
280 if (evn_used == evn_allocated) {
282 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
284 /* we are screwed, but let's just bail out */
287 ev_namelist[evn_used].ev_name = strdup(eventname);
288 ev_namelist[evn_used].plot_ev = TRUE;
292 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
297 /* is plotting this event disabled? */
299 for (i = 0; i < evn_used; i++) {
300 if (! strcmp(event->name, ev_namelist[i].ev_name)) {
301 if (ev_namelist[i].plot_ev)
308 for (i = 0; i < pi->nr; i++) {
309 struct plot_data *data = pi->entry + i;
310 if (event->time.seconds < data->sec)
314 /* draw a little tirangular marker and attach tooltip */
315 x = SCALEX(gc, event->time.seconds);
316 y = SCALEY(gc, depth);
317 set_source_rgba(gc, TRIANGULAR_BG, 0.8);
318 cairo_move_to(gc->cr, x-15, y+6);
319 cairo_line_to(gc->cr, x-3 , y+6);
320 cairo_line_to(gc->cr, x-9, y-6);
321 cairo_line_to(gc->cr, x-15, y+6);
322 cairo_stroke_preserve(gc->cr);
324 set_source_rgba(gc, TRIANGULAR_FG, 0.8);
325 cairo_move_to(gc->cr, x-9, y-3);
326 cairo_line_to(gc->cr, x-9, y+1);
327 cairo_move_to(gc->cr, x-9, y+4);
328 cairo_line_to(gc->cr, x-9, y+4);
329 cairo_stroke(gc->cr);
330 attach_tooltip(x-15, y-6, 12, 12, event->name);
333 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
335 static const text_render_options_t tro = {14, EVENTS, CENTER, TOP};
336 struct event *event = dive->events;
342 plot_one_event(gc, pi, event, &tro);
347 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
349 int sec = entry->sec, decimals;
352 d = get_depth_units(entry->depth, &decimals, NULL);
354 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
357 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
359 static const text_render_options_t deep = {14, SAMPLE_DEEP, CENTER, TOP};
360 static const text_render_options_t shallow = {14, SAMPLE_SHALLOW, CENTER, BOTTOM};
364 for (i = 0; i < pi->nr; i++) {
365 struct plot_data *entry = pi->entry + i;
367 if (entry->depth < 2000)
370 if ((entry == entry->max[2]) && entry->depth != last) {
371 render_depth_sample(gc, entry, &deep);
375 if ((entry == entry->min[2]) && entry->depth != last) {
376 render_depth_sample(gc, entry, &shallow);
380 if (entry->depth != last)
385 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
387 int maxtime, maxdepth;
389 /* Get plot scaling limits */
390 maxtime = get_maxtime(pi);
391 maxdepth = get_maxdepth(pi);
393 gc->leftx = 0; gc->rightx = maxtime;
394 gc->topy = 0; gc->bottomy = maxdepth;
396 plot_text_samples(gc, pi);
399 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
402 struct plot_data *entry = pi->entry;
404 set_source_rgba(gc, SMOOTHED, 0.20);
405 move_to(gc, entry->sec, entry->smoothed);
406 for (i = 1; i < pi->nr; i++) {
408 line_to(gc, entry->sec, entry->smoothed);
410 cairo_stroke(gc->cr);
413 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
417 struct plot_data *entry = pi->entry;
419 set_source_rgba(gc, MINUTE, a);
420 move_to(gc, entry->sec, entry->min[index]->depth);
421 for (i = 1; i < pi->nr; i++) {
423 line_to(gc, entry->sec, entry->min[index]->depth);
425 for (i = 1; i < pi->nr; i++) {
426 line_to(gc, entry->sec, entry->max[index]->depth);
429 cairo_close_path(gc->cr);
433 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
437 plot_minmax_profile_minute(gc, pi, 2, 0.1);
438 plot_minmax_profile_minute(gc, pi, 1, 0.1);
439 plot_minmax_profile_minute(gc, pi, 0, 0.1);
442 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
445 cairo_t *cr = gc->cr;
447 struct plot_data *entry;
448 int maxtime, maxdepth, marker;
449 int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
451 /* Get plot scaling limits */
452 maxtime = get_maxtime(pi);
453 maxdepth = get_maxdepth(pi);
455 /* Time markers: at most every 5 min, but no more than 12 markers
456 * and for convenience we do 5, 10, 15 or 30 min intervals.
457 * This allows for 6h dives - enough (I hope) for even the craziest
458 * divers - but just in case, for those 8h depth-record-breaking dives,
459 * we double the interval if this still doesn't get us to 12 or fewer
462 while (maxtime / increments[i] > 12 && i < 4)
464 incr = increments[i];
465 while (maxtime / incr > 12)
468 gc->leftx = 0; gc->rightx = maxtime;
469 gc->topy = 0; gc->bottomy = 1.0;
470 set_source_rgba(gc, TIME_MARKER, 0.5);
471 cairo_set_line_width(gc->cr, 1);
473 for (i = incr; i < maxtime; i += incr) {
479 /* now the text on every second time marker */
480 text_render_options_t tro = {10, TIME_TEXT, CENTER, TOP};
481 for (i = incr; i < maxtime; i += 2 * incr)
482 plot_text(gc, &tro, i, 1, "%d", i/60);
484 /* Depth markers: every 30 ft or 10 m*/
485 gc->leftx = 0; gc->rightx = 1.0;
486 gc->topy = 0; gc->bottomy = maxdepth;
487 switch (output_units.length) {
488 case METERS: marker = 10000; break;
489 case FEET: marker = 9144; break; /* 30 ft */
492 set_source_rgba(gc, DEPTH_MARKER, 0.5);
493 for (i = marker; i < maxdepth; i += marker) {
499 /* Show mean depth */
501 set_source_rgba(gc, MEAN_DEPTH, 0.40);
502 move_to(gc, 0, pi->meandepth);
503 line_to(gc, 1, pi->meandepth);
507 gc->leftx = 0; gc->rightx = maxtime;
510 * These are good for debugging text placement etc,
511 * but not for actual display..
514 plot_smoothed_profile(gc, pi);
515 plot_minmax_profile(gc, pi);
518 /* Do the depth profile for the neat fill */
519 gc->topy = 0; gc->bottomy = maxdepth;
521 cairo_pattern_t *pat;
522 pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, 256.0);
523 pattern_add_color_stop_rgba (pat, 1, DEPTH_TOP, 0.6);
524 pattern_add_color_stop_rgba (pat, 0, DEPTH_BOTTOM, 0.6);
526 cairo_set_source(gc->cr, pat);
527 cairo_pattern_destroy(pat);
528 cairo_set_line_width(gc->cr, 2);
532 for (i = 0; i < pi->nr; i++, entry++)
533 line_to(gc, entry->sec, entry->depth);
534 cairo_close_path(gc->cr);
536 set_source_rgba(gc, DEPTH_FILL_PRINTER, 0.2);
537 cairo_fill_preserve(cr);
538 set_source_rgb(gc, DEPTH_STROKE_PRINTER);
544 /* Now do it again for the velocity colors */
546 for (i = 1; i < pi->nr; i++) {
549 /* we want to draw the segments in different colors
550 * representing the vertical velocity, so we need to
551 * chop this into short segments */
552 depth = entry->depth;
553 set_source_rgb(gc, velocity_color[entry->velocity]);
554 move_to(gc, entry[-1].sec, entry[-1].depth);
555 line_to(gc, sec, depth);
560 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
562 int maxtime, mintemp, maxtemp, delta;
564 /* Get plot scaling limits */
565 maxtime = get_maxtime(pi);
566 mintemp = pi->mintemp;
567 maxtemp = pi->maxtemp;
569 gc->leftx = 0; gc->rightx = maxtime;
570 /* Show temperatures in roughly the lower third, but make sure the scale
571 is at least somewhat reasonable */
572 delta = maxtemp - mintemp;
573 if (delta > 3000) { /* more than 3K in fluctuation */
574 gc->topy = maxtemp + delta*2;
575 gc->bottomy = mintemp - delta/2;
577 gc->topy = maxtemp + 1500 + delta*2;
578 gc->bottomy = mintemp - delta/2;
581 return maxtemp > mintemp;
584 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
588 static const text_render_options_t tro = {12, TEMP_TEXT, LEFT, TOP};
590 deg = get_temp_units(mkelvin, &unit);
592 plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
595 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
598 int last = -300, sec = 0;
599 int last_temperature = 0, last_printed_temp = 0;
601 if (!setup_temperature_limits(gc, pi))
604 for (i = 0; i < pi->nr; i++) {
605 struct plot_data *entry = pi->entry+i;
606 int mkelvin = entry->temperature;
610 last_temperature = mkelvin;
612 /* don't print a temperature
613 * if it's been less than 5min and less than a 2K change OR
614 * if it's been less than 2min OR if the change from the
615 * last print is less than .4K (and therefore less than 1F */
616 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
617 (sec < last + 120) ||
618 (abs(mkelvin - last_printed_temp) < 400))
621 plot_single_temp_text(gc,sec,mkelvin);
622 last_printed_temp = mkelvin;
624 /* it would be nice to print the end temperature, if it's
625 * different or if the last temperature print has been more
626 * than a quarter of the dive back */
627 if ((abs(last_temperature - last_printed_temp) > 500) ||
628 ((double)last / (double)sec < 0.75))
629 plot_single_temp_text(gc, sec, last_temperature);
632 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
635 cairo_t *cr = gc->cr;
638 if (!setup_temperature_limits(gc, pi))
641 cairo_set_line_width(gc->cr, 2);
642 set_source_rgba(gc, TEMP_PLOT, 0.8);
643 for (i = 0; i < pi->nr; i++) {
644 struct plot_data *entry = pi->entry + i;
645 int mkelvin = entry->temperature;
646 int sec = entry->sec;
653 line_to(gc, sec, mkelvin);
655 move_to(gc, sec, mkelvin);
661 /* gets both the actual start and end pressure as well as the scaling factors */
662 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
665 gc->rightx = get_maxtime(pi);
667 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
668 return pi->maxpressure != 0;
671 /* set the color for the pressure plot according to temporary sac rate
672 * as compared to avg_sac; the calculation simply maps the delta between
673 * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
674 * more than 6000 ml/min below avg_sac mapped to 0 */
676 static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
679 int delta = sac - avg_sac + 7000;
682 sac_index = delta / 2000;
685 if (sac_index > SAC_COLORS - 1)
686 sac_index = SAC_COLORS - 1;
687 set_source_rgb(gc, sac_color[sac_index]);
689 set_source_rgb(gc, SAC_DEFAULT);
693 /* calculate the current SAC in ml/min and convert to int */
694 #define GET_LOCAL_SAC(_entry1, _entry2, _dive) (int) \
695 ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) * \
696 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter / \
697 (((_entry2)->sec - (_entry1)->sec) / 60.0) / \
698 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) / \
701 #define SAC_WINDOW 45 /* sliding window in seconds for current SAC calculation */
703 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
708 int lift_pen = FALSE;
709 int first_plot = TRUE;
711 struct plot_data *last_entry = NULL;
713 if (!get_cylinder_pressure_range(gc, pi))
716 cairo_set_line_width(gc->cr, 2);
718 for (i = 0; i < pi->nr; i++) {
720 struct plot_data *entry = pi->entry + i;
722 mbar = GET_PRESSURE(entry);
723 if (!entry->same_cylinder) {
734 sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
738 for (j = last; j < i; j++)
739 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
741 if (entry->sec - last_entry->sec >= SAC_WINDOW) {
743 last_entry = pi->entry + last;
746 set_sac_color(gc, sac, dive->sac);
748 if (!first_plot && entry->same_cylinder) {
749 /* if we have a previous event from the same tank,
750 * draw at least a short line */
752 prev_pr = GET_PRESSURE(entry - 1);
753 move_to(gc, (entry-1)->sec, prev_pr);
754 line_to(gc, entry->sec, mbar);
757 move_to(gc, entry->sec, mbar);
761 line_to(gc, entry->sec, mbar);
763 cairo_stroke(gc->cr);
764 move_to(gc, entry->sec, mbar);
768 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
769 int xalign, int yalign)
774 pressure = get_pressure_units(mbar, &unit);
775 text_render_options_t tro = {10, PRESSURE_TEXT, xalign, yalign};
776 plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
779 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
783 int seen_cyl[MAX_CYLINDERS] = { FALSE, };
784 int last_pressure[MAX_CYLINDERS] = { 0, };
785 int last_time[MAX_CYLINDERS] = { 0, };
786 struct plot_data *entry;
788 if (!get_cylinder_pressure_range(gc, pi))
791 /* only loop over the actual events from the dive computer
792 * plus the second synthetic event at the start (to make sure
793 * we get "time=0" right)
794 * sadly with a recent change that first entry may no longer
795 * have any pressure reading - in that case just grab the
796 * pressure from the second entry */
797 if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
798 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
799 for (i = 1; i < pi->nr; i++) {
800 entry = pi->entry + i;
802 if (!entry->same_cylinder) {
803 cyl = entry->cylinderindex;
804 if (!seen_cyl[cyl]) {
805 mbar = GET_PRESSURE(entry);
806 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
807 seen_cyl[cyl] = TRUE;
810 /* remember the last pressure and time of
811 * the previous cylinder */
812 cyl = (entry - 1)->cylinderindex;
813 last_pressure[cyl] = GET_PRESSURE(entry - 1);
814 last_time[cyl] = (entry - 1)->sec;
818 cyl = entry->cylinderindex;
819 if (GET_PRESSURE(entry))
820 last_pressure[cyl] = GET_PRESSURE(entry);
821 last_time[cyl] = entry->sec;
823 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
824 if (last_time[cyl]) {
825 plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
830 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
832 struct plot_data *p = entry;
833 int time = entry->sec;
834 int seconds = 90*(index+1);
835 struct plot_data *min, *max;
838 /* Go back 'seconds' in time */
840 if (p[-1].sec < time - seconds)
845 /* Then go forward until we hit an entry past the time */
850 int depth = p->depth;
851 if (p->sec > time + seconds)
855 if (depth < min->depth)
857 if (depth > max->depth)
860 entry->min[index] = min;
861 entry->max[index] = max;
862 entry->avg[index] = (avg + nr/2) / nr;
865 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
867 analyze_plot_info_minmax_minute(entry, first, last, 0);
868 analyze_plot_info_minmax_minute(entry, first, last, 1);
869 analyze_plot_info_minmax_minute(entry, first, last, 2);
872 static velocity_t velocity(int speed)
876 if (speed < -304) /* ascent faster than -60ft/min */
878 else if (speed < -152) /* above -30ft/min */
880 else if (speed < -76) /* -15ft/min */
882 else if (speed < -25) /* -5ft/min */
884 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
885 for descent are usually about 2x ascent rate; still, we want
886 stable to mean stable */
888 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
890 else if (speed < 304) /* up to 60ft/min is moderate */
892 else if (speed < 507) /* up to 100ft/min is fast */
894 else /* more than that is just crazy - you'll blow your ears out */
899 static struct plot_info *analyze_plot_info(struct plot_info *pi)
904 /* Do pressure min/max based on the non-surface data */
905 for (i = 0; i < nr; i++) {
906 struct plot_data *entry = pi->entry+i;
907 int pressure = GET_PRESSURE(entry);
908 int temperature = entry->temperature;
911 if (pressure > pi->maxpressure)
912 pi->maxpressure = pressure;
916 if (!pi->mintemp || temperature < pi->mintemp)
917 pi->mintemp = temperature;
918 if (temperature > pi->maxtemp)
919 pi->maxtemp = temperature;
923 /* Smoothing function: 5-point triangular smooth */
924 for (i = 2; i < nr; i++) {
925 struct plot_data *entry = pi->entry+i;
929 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
930 entry->smoothed = (depth+4) / 9;
932 /* vertical velocity in mm/sec */
933 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
934 if (entry[0].sec - entry[-1].sec) {
935 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
936 /* if our samples are short and we aren't too FAST*/
937 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
939 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
941 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
942 (entry[0].sec - entry[past].sec));
945 entry->velocity = STABLE;
948 /* One-, two- and three-minute minmax data */
949 for (i = 0; i < nr; i++) {
950 struct plot_data *entry = pi->entry +i;
951 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
958 * simple structure to track the beginning and end tank pressure as
959 * well as the integral of depth over time spent while we have no
960 * pressure reading from the tank */
961 typedef struct pr_track_struct pr_track_t;
962 struct pr_track_struct {
967 double pressure_time;
971 static pr_track_t *pr_track_alloc(int start, int t_start) {
972 pr_track_t *pt = malloc(sizeof(pr_track_t));
974 pt->t_start = t_start;
977 pt->pressure_time = 0.0;
982 /* poor man's linked list */
983 static pr_track_t *list_last(pr_track_t *list)
985 pr_track_t *tail = list;
994 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
996 pr_track_t *tail = list_last(list);
999 tail->next = element;
1003 static void list_free(pr_track_t *list)
1007 list_free(list->next);
1011 static void dump_pr_track(pr_track_t **track_pr)
1016 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1017 list = track_pr[cyl];
1019 printf("cyl%d: start %d end %d t_start %d t_end %d pt %6.3f\n", cyl,
1020 list->start, list->end, list->t_start, list->t_end, list->pressure_time);
1026 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
1027 pr_track_t **track_pr)
1029 pr_track_t *list = NULL;
1030 pr_track_t *nlist = NULL;
1033 struct plot_data *entry;
1034 int cur_pr[MAX_CYLINDERS];
1037 /* another great debugging tool */
1038 dump_pr_track(track_pr);
1040 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1041 cur_pr[cyl] = track_pr[cyl]->start;
1044 /* The first two are "fillers", but in case we don't have a sample
1045 * at time 0 we need to process the second of them here */
1046 for (i = 1; i < pi->nr; i++) {
1047 entry = pi->entry + i;
1048 if (SENSOR_PRESSURE(entry)) {
1049 cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
1051 if(!list || list->t_end < entry->sec) {
1052 nlist = track_pr[entry->cylinderindex];
1054 while (nlist && nlist->t_start <= entry->sec) {
1058 /* there may be multiple segments - so
1059 * let's assemble the length */
1061 pt = list->pressure_time;
1062 while (!nlist->end) {
1063 nlist = nlist->next;
1065 /* oops - we have no end pressure,
1066 * so this means this is a tank without
1067 * gas consumption information */
1070 pt += nlist->pressure_time;
1073 /* just continue without calculating
1074 * interpolated values */
1075 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1079 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
1082 double cur_pt = (entry->sec - (entry-1)->sec) *
1083 (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1084 INTERPOLATED_PRESSURE(entry) =
1085 cur_pr[entry->cylinderindex] + cur_pt * magic + 0.5;
1086 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1088 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1093 static int get_cylinder_index(struct dive *dive, struct event *ev)
1098 * Try to find a cylinder that matches the O2 percentage
1099 * in the gas change event 'value' field.
1101 * Crazy suunto gas change events. We really should do
1102 * this in libdivecomputer or something.
1104 for (i = 0; i < MAX_CYLINDERS; i++) {
1105 cylinder_t *cyl = dive->cylinder+i;
1106 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1107 if (o2 == ev->value)
1114 static struct event *get_next_gaschange(struct event *event)
1117 if (!strcmp(event->name, "gaschange"))
1119 event = event->next;
1124 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1126 while (i < pi->nr) {
1127 struct plot_data *entry = pi->entry+i;
1128 if (entry->sec > end)
1130 if (entry->cylinderindex != cylinderindex) {
1131 entry->cylinderindex = cylinderindex;
1132 entry->pressure[0] = 0;
1139 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1141 int i = 0, cylinderindex = 0;
1142 struct event *ev = get_next_gaschange(dive->events);
1148 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1149 cylinderindex = get_cylinder_index(dive, ev);
1150 ev = get_next_gaschange(ev->next);
1152 set_cylinder_index(pi, i, cylinderindex, ~0u);
1155 /* for computers that track gas changes through events */
1156 static int count_gas_change_events(struct dive *dive)
1159 struct event *ev = get_next_gaschange(dive->events);
1163 ev = get_next_gaschange(ev->next);
1169 * Create a plot-info with smoothing and ranged min/max
1171 * This also makes sure that we have extra empty events on both
1172 * sides, so that you can do end-points without having to worry
1175 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1177 int cylinderindex = -1;
1178 int lastdepth, lastindex;
1179 int i, pi_idx, nr, sec, cyl;
1181 struct plot_info *pi;
1182 pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1183 pr_track_t *pr_track, *current;
1184 gboolean missing_pr = FALSE;
1185 struct plot_data *entry = NULL;
1188 /* we want to potentially add synthetic plot_info elements for the gas changes */
1189 nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1190 alloc_size = plot_info_size(nr);
1191 pi = malloc(alloc_size);
1194 memset(pi, 0, alloc_size);
1196 pi_idx = 2; /* the two extra events at the start */
1197 /* check for gas changes before the samples start */
1198 ev = get_next_gaschange(dive->events);
1199 while (ev && ev->time.seconds < dive_sample->time.seconds) {
1200 entry = pi->entry + pi_idx;
1201 entry->sec = ev->time.seconds;
1202 entry->depth = 0; /* is that always correct ? */
1204 ev = get_next_gaschange(ev->next);
1206 if (ev && ev->time.seconds == dive_sample->time.seconds) {
1207 /* we already have a sample at the time of the event */
1208 ev = get_next_gaschange(ev->next);
1213 for (i = 0; i < nr_samples; i++) {
1216 struct sample *sample = dive_sample+i;
1218 entry = pi->entry + i + pi_idx;
1219 while (ev && ev->time.seconds < sample->time.seconds) {
1220 /* insert two fake plot info structures for the end of
1221 * the old tank and the start of the new tank */
1222 if (ev->time.seconds == sample->time.seconds - 1) {
1223 entry->sec = ev->time.seconds - 1;
1224 (entry+1)->sec = ev->time.seconds;
1226 entry->sec = ev->time.seconds;
1227 (entry+1)->sec = ev->time.seconds + 1;
1229 /* we need a fake depth - let's interpolate */
1231 entry->depth = sample->depth.mm -
1232 (sample->depth.mm - (sample-1)->depth.mm) / 2;
1234 entry->depth = sample->depth.mm;
1235 (entry+1)->depth = entry->depth;
1237 entry = pi->entry + i + pi_idx;
1238 ev = get_next_gaschange(ev->next);
1240 if (ev && ev->time.seconds == sample->time.seconds) {
1241 /* we already have a sample at the time of the event
1242 * just add a new one for the old tank and delay the
1243 * real even by one second (to keep time monotonous) */
1244 entry->sec = ev->time.seconds;
1245 entry->depth = sample->depth.mm;
1247 entry = pi->entry + i + pi_idx;
1248 ev = get_next_gaschange(ev->next);
1251 sec = entry->sec = sample->time.seconds + delay;
1252 depth = entry->depth = sample->depth.mm;
1253 entry->cylinderindex = sample->cylinderindex;
1254 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1255 entry->temperature = sample->temperature.mkelvin;
1257 if (depth || lastdepth)
1258 lastindex = i + pi_idx;
1261 if (depth > pi->maxdepth)
1262 pi->maxdepth = depth;
1264 entry = pi->entry + i + pi_idx;
1265 /* are there still unprocessed gas changes? that would be very strange */
1267 entry->sec = ev->time.seconds;
1268 entry->depth = 0; /* why are there gas changes after the dive is over? */
1270 entry = pi->entry + i + pi_idx;
1271 ev = get_next_gaschange(ev->next);
1273 nr = nr_samples + pi_idx - 2;
1274 check_gas_change_events(dive, pi);
1276 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1277 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1278 current = track_pr[pi->entry[2].cylinderindex];
1279 for (i = 0; i < nr + 1; i++) {
1280 entry = pi->entry + i + 1;
1282 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1283 cylinderindex = entry->cylinderindex;
1285 /* track the segments per cylinder and their pressure/time integral */
1286 if (!entry->same_cylinder) {
1287 current->end = SENSOR_PRESSURE(entry-1);
1288 current->t_end = (entry-1)->sec;
1289 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1290 track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1291 } else { /* same cylinder */
1292 if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1293 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1294 /* transmitter changed its working status */
1295 current->end = SENSOR_PRESSURE(entry-1);
1296 current->t_end = (entry-1)->sec;
1297 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1298 track_pr[cylinderindex] =
1299 list_add(track_pr[cylinderindex], current);
1302 /* finally, do the discrete integration to get the SAC rate equivalent */
1303 current->pressure_time += (entry->sec - (entry-1)->sec) *
1304 (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1305 missing_pr |= !SENSOR_PRESSURE(entry);
1309 current->t_end = entry->sec;
1311 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1312 int pr = dive->cylinder[cyl].end.mbar;
1313 if (pr && track_pr[cyl]) {
1314 pr_track = list_last(track_pr[cyl]);
1318 /* Fill in the last two entries with empty values but valid times
1319 * without creating a false cylinder change event */
1321 pi->entry[i].sec = sec + 20;
1322 pi->entry[i].same_cylinder = 1;
1323 pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1324 INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1325 pi->entry[i+1].sec = sec + 40;
1326 pi->entry[i+1].same_cylinder = 1;
1327 pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1328 INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1329 /* the number of actual entries - some computers have lots of
1330 * depth 0 samples at the end of a dive, we want to make sure
1331 * we have exactly one of them at the end */
1332 pi->nr = lastindex+1;
1333 while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1335 pi->maxtime = pi->entry[lastindex].sec;
1337 /* Analyze_plot_info() will do the sample max pressures,
1338 * this handles the manual pressures
1340 pi->maxpressure = 0;
1341 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1342 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1343 if (mbar > pi->maxpressure)
1344 pi->maxpressure = mbar;
1347 pi->meandepth = dive->meandepth.mm;
1350 fill_missing_tank_pressures(dive, pi, track_pr);
1352 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1353 list_free(track_pr[cyl]);
1354 if (0) /* awesome for debugging - not useful otherwise */
1356 return analyze_plot_info(pi);
1359 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1361 struct plot_info *pi;
1362 static struct sample fake[4];
1363 struct sample *sample = dive->sample;
1364 int nr = dive->samples;
1367 int duration = dive->duration.seconds;
1368 int maxdepth = dive->maxdepth.mm;
1370 fake[1].time.seconds = duration * 0.05;
1371 fake[1].depth.mm = maxdepth;
1372 fake[2].time.seconds = duration * 0.95;
1373 fake[2].depth.mm = maxdepth;
1374 fake[3].time.seconds = duration * 1.00;
1378 pi = create_plot_info(dive, nr, sample);
1380 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1381 cairo_set_line_width(gc->cr, 1);
1382 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1383 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1386 * We can use "cairo_translate()" because that doesn't
1387 * scale line width etc. But the actual scaling we need
1388 * do set up ourselves..
1390 * Snif. What a pity.
1392 gc->maxx = (drawing_area->width - 2*drawing_area->x);
1393 gc->maxy = (drawing_area->height - 2*drawing_area->y);
1395 /* Temperature profile */
1396 plot_temperature_profile(gc, pi);
1399 plot_depth_profile(gc, pi);
1400 plot_events(gc, pi, dive);
1402 /* Cylinder pressure plot */
1403 plot_cylinder_pressure(gc, pi, dive);
1405 /* Text on top of all graphs.. */
1406 plot_temperature_text(gc, pi);
1407 plot_depth_text(gc, pi);
1408 plot_cylinder_pressure_text(gc, pi);
1410 /* Bounding box last */
1411 gc->leftx = 0; gc->rightx = 1.0;
1412 gc->topy = 0; gc->bottomy = 1.0;
1414 set_source_rgb(gc, BOUNDING_BOX);
1415 cairo_set_line_width(gc->cr, 1);
1420 cairo_close_path(gc->cr);
1421 cairo_stroke(gc->cr);