2 /* creates all the necessary data for drawing the dive profile
3 * uses cairo to draw it
16 int selected_dive = 0;
17 int *selectiontracker;
19 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
21 /* Plot info with smoothing, velocity indication
22 * and one-, two- and three-minute minimums and maximums */
26 int meandepth, maxdepth;
30 unsigned int same_cylinder:1;
31 unsigned int cylinderindex;
33 /* pressure[0] is sensor pressure
34 * pressure[1] is interpolated pressure */
41 struct plot_data *min[3];
42 struct plot_data *max[3];
48 #define INTERPOLATED_PR 1
49 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
50 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
51 #define GET_PRESSURE(_entry) (SENSOR_PRESSURE(_entry) ? : INTERPOLATED_PRESSURE(_entry))
53 #define SAC_COLORS_START_IDX SAC_1
55 #define VELOCITY_COLORS_START_IDX VELO_STABLE
56 #define VELOCITY_COLORS 5
59 /* SAC colors. Order is important, the SAC_COLORS_START_IDX define above. */
60 SAC_1, SAC_2, SAC_3, SAC_4, SAC_5, SAC_6, SAC_7, SAC_8, SAC_9,
62 /* Velocity colors. Order is still important, ref VELOCITY_COLORS_START_IDX. */
63 VELO_STABLE, VELO_SLOW, VELO_MODERATE, VELO_FAST, VELO_CRAZY,
66 TEXT_BACKGROUND, ALERT_BG, ALERT_FG, EVENTS, SAMPLE_DEEP, SAMPLE_SHALLOW,
67 SMOOTHED, MINUTE, TIME_GRID, TIME_TEXT, DEPTH_GRID, MEAN_DEPTH, DEPTH_TOP,
68 DEPTH_BOTTOM, TEMP_TEXT, TEMP_PLOT, SAC_DEFAULT, BOUNDING_BOX, PRESSURE_TEXT, BACKGROUND
72 /* media[0] is screen, and media[1] is printer */
78 /* [color indice] = {{screen color, printer color}} */
79 static const color_t profile_color[] = {
80 [SAC_1] = {{FUNGREEN1, BLACK1_LOW_TRANS}},
81 [SAC_2] = {{APPLE1, BLACK1_LOW_TRANS}},
82 [SAC_3] = {{ATLANTIS1, BLACK1_LOW_TRANS}},
83 [SAC_4] = {{ATLANTIS2, BLACK1_LOW_TRANS}},
84 [SAC_5] = {{EARLSGREEN1, BLACK1_LOW_TRANS}},
85 [SAC_6] = {{HOKEYPOKEY1, BLACK1_LOW_TRANS}},
86 [SAC_7] = {{TUSCANY1, BLACK1_LOW_TRANS}},
87 [SAC_8] = {{CINNABAR1, BLACK1_LOW_TRANS}},
88 [SAC_9] = {{REDORANGE1, BLACK1_LOW_TRANS}},
90 [VELO_STABLE] = {{CAMARONE1, BLACK1_LOW_TRANS}},
91 [VELO_SLOW] = {{LIMENADE1, BLACK1_LOW_TRANS}},
92 [VELO_MODERATE] = {{RIOGRANDE1, BLACK1_LOW_TRANS}},
93 [VELO_FAST] = {{PIRATEGOLD1, BLACK1_LOW_TRANS}},
94 [VELO_CRAZY] = {{RED1, BLACK1_LOW_TRANS}},
96 [TEXT_BACKGROUND] = {{CONCRETE1_LOWER_TRANS, WHITE1}},
97 [ALERT_BG] = {{BROOM1_LOWER_TRANS, BLACK1_LOW_TRANS}},
98 [ALERT_FG] = {{BLACK1_LOW_TRANS, BLACK1_LOW_TRANS}},
99 [EVENTS] = {{REDORANGE1, BLACK1_LOW_TRANS}},
100 [SAMPLE_DEEP] = {{PERSIANRED1, BLACK1_LOW_TRANS}},
101 [SAMPLE_SHALLOW] = {{PERSIANRED1, BLACK1_LOW_TRANS}},
102 [SMOOTHED] = {{REDORANGE1_HIGH_TRANS, BLACK1_LOW_TRANS}},
103 [MINUTE] = {{MEDIUMREDVIOLET1_HIGHER_TRANS, BLACK1_LOW_TRANS}},
104 [TIME_GRID] = {{WHITE1, TUNDORA1_MED_TRANS}},
105 [TIME_TEXT] = {{FORESTGREEN1, BLACK1_LOW_TRANS}},
106 [DEPTH_GRID] = {{WHITE1, TUNDORA1_MED_TRANS}},
107 [MEAN_DEPTH] = {{REDORANGE1_MED_TRANS, BLACK1_LOW_TRANS}},
108 [DEPTH_BOTTOM] = {{GOVERNORBAY1_MED_TRANS, TUNDORA1_MED_TRANS}},
109 [DEPTH_TOP] = {{MERCURY1_MED_TRANS, WHITE1_MED_TRANS}},
110 [TEMP_TEXT] = {{GOVERNORBAY2, BLACK1_LOW_TRANS}},
111 [TEMP_PLOT] = {{ROYALBLUE2_LOW_TRANS, BLACK1_LOW_TRANS}},
112 [SAC_DEFAULT] = {{WHITE1, BLACK1_LOW_TRANS}},
113 [BOUNDING_BOX] = {{WHITE1, BLACK1_LOW_TRANS}},
114 [PRESSURE_TEXT] = {{KILLARNEY1, BLACK1_LOW_TRANS}},
115 [BACKGROUND] = {{SPRINGWOOD1, BLACK1_LOW_TRANS}},
118 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
120 /* Scale to 0,0 -> maxx,maxy */
121 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
122 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
123 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
125 static void move_to(struct graphics_context *gc, double x, double y)
127 cairo_move_to(gc->cr, SCALE(gc, x, y));
130 static void line_to(struct graphics_context *gc, double x, double y)
132 cairo_line_to(gc->cr, SCALE(gc, x, y));
135 static void set_source_rgba(struct graphics_context *gc, color_indice_t c)
137 const color_t *col = &profile_color[c];
138 struct rgba rgb = col->media[gc->printer];
144 cairo_set_source_rgba(gc->cr, r, g, b, a);
147 void init_profile_background(struct graphics_context *gc)
149 set_source_rgba(gc, BACKGROUND);
152 void pattern_add_color_stop_rgba(struct graphics_context *gc, cairo_pattern_t *pat, double o, color_indice_t c)
154 const color_t *col = &profile_color[c];
155 struct rgba rgb = col->media[gc->printer];
156 cairo_pattern_add_color_stop_rgba(pat, o, rgb.r, rgb.g, rgb.b, rgb.a);
159 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
161 /* debugging tool - not normally used */
162 static void dump_pi (struct plot_info *pi)
166 printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
167 " maxpressure:%d mintemp:%d maxtemp:%d\n",
168 pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
169 pi->maxpressure, pi->mintemp, pi->maxtemp);
170 for (i = 0; i < pi->nr; i++)
171 printf(" entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
172 " time:%d:%02d temperature:%d depth:%d smoothed:%d}\n",
173 i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
174 pi->entry[i].pressure[0], pi->entry[i].pressure[1],
175 pi->entry[i].sec / 60, pi->entry[i].sec % 60,
176 pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
181 * When showing dive profiles, we scale things to the
182 * current dive. However, we don't scale past less than
183 * 30 minutes or 90 ft, just so that small dives show
185 * we also need to add 180 seconds at the end so the min/max
188 static int get_maxtime(struct plot_info *pi)
190 int seconds = pi->maxtime;
191 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
192 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
195 static int get_maxdepth(struct plot_info *pi)
197 unsigned mm = pi->maxdepth;
198 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
199 return MAX(30000, ROUND_UP(mm+3000, 10000));
204 color_indice_t color;
206 } text_render_options_t;
209 #define CENTER (-0.5)
216 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
217 double x, double y, const char *fmt, ...)
219 cairo_t *cr = gc->cr;
220 cairo_font_extents_t fe;
221 cairo_text_extents_t extents;
227 vsnprintf(buffer, sizeof(buffer), fmt, args);
230 cairo_set_font_size(cr, tro->size);
231 cairo_font_extents(cr, &fe);
232 cairo_text_extents(cr, buffer, &extents);
233 dx = tro->hpos * extents.width + extents.x_bearing;
234 dy = tro->vpos * extents.height + fe.descent;
237 cairo_rel_move_to(cr, dx, dy);
239 cairo_text_path(cr, buffer);
240 set_source_rgba(gc, TEXT_BACKGROUND);
244 cairo_rel_move_to(cr, dx, dy);
246 set_source_rgba(gc, tro->color);
247 cairo_show_text(cr, buffer);
254 static struct ev_select *ev_namelist;
255 static int evn_allocated;
258 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
262 for (i = 0; i < evn_used; i++) {
263 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
267 void remember_event(const char *eventname)
271 if (!eventname || (len = strlen(eventname)) == 0)
273 while (i < evn_used) {
274 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
278 if (evn_used == evn_allocated) {
280 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
282 /* we are screwed, but let's just bail out */
285 ev_namelist[evn_used].ev_name = strdup(eventname);
286 ev_namelist[evn_used].plot_ev = TRUE;
290 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
295 /* is plotting this event disabled? */
297 for (i = 0; i < evn_used; i++) {
298 if (! strcmp(event->name, ev_namelist[i].ev_name)) {
299 if (ev_namelist[i].plot_ev)
306 for (i = 0; i < pi->nr; i++) {
307 struct plot_data *data = pi->entry + i;
308 if (event->time.seconds < data->sec)
312 /* draw a little tirangular marker and attach tooltip */
313 x = SCALEX(gc, event->time.seconds);
314 y = SCALEY(gc, depth);
315 set_source_rgba(gc, ALERT_BG);
316 cairo_move_to(gc->cr, x-15, y+6);
317 cairo_line_to(gc->cr, x-3 , y+6);
318 cairo_line_to(gc->cr, x-9, y-6);
319 cairo_line_to(gc->cr, x-15, y+6);
320 cairo_stroke_preserve(gc->cr);
322 set_source_rgba(gc, ALERT_FG);
323 cairo_move_to(gc->cr, x-9, y-3);
324 cairo_line_to(gc->cr, x-9, y+1);
325 cairo_move_to(gc->cr, x-9, y+4);
326 cairo_line_to(gc->cr, x-9, y+4);
327 cairo_stroke(gc->cr);
328 attach_tooltip(x-15, y-6, 12, 12, event->name);
331 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
333 static const text_render_options_t tro = {14, EVENTS, CENTER, TOP};
334 struct event *event = dive->events;
340 plot_one_event(gc, pi, event, &tro);
345 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
347 int sec = entry->sec, decimals;
350 d = get_depth_units(entry->depth, &decimals, NULL);
352 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
355 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
357 static const text_render_options_t deep = {14, SAMPLE_DEEP, CENTER, TOP};
358 static const text_render_options_t shallow = {14, SAMPLE_SHALLOW, CENTER, BOTTOM};
362 for (i = 0; i < pi->nr; i++) {
363 struct plot_data *entry = pi->entry + i;
365 if (entry->depth < 2000)
368 if ((entry == entry->max[2]) && entry->depth != last) {
369 render_depth_sample(gc, entry, &deep);
373 if ((entry == entry->min[2]) && entry->depth != last) {
374 render_depth_sample(gc, entry, &shallow);
378 if (entry->depth != last)
383 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
385 int maxtime, maxdepth;
387 /* Get plot scaling limits */
388 maxtime = get_maxtime(pi);
389 maxdepth = get_maxdepth(pi);
391 gc->leftx = 0; gc->rightx = maxtime;
392 gc->topy = 0; gc->bottomy = maxdepth;
394 plot_text_samples(gc, pi);
397 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
400 struct plot_data *entry = pi->entry;
402 set_source_rgba(gc, SMOOTHED);
403 move_to(gc, entry->sec, entry->smoothed);
404 for (i = 1; i < pi->nr; i++) {
406 line_to(gc, entry->sec, entry->smoothed);
408 cairo_stroke(gc->cr);
411 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
415 struct plot_data *entry = pi->entry;
417 set_source_rgba(gc, MINUTE);
418 move_to(gc, entry->sec, entry->min[index]->depth);
419 for (i = 1; i < pi->nr; i++) {
421 line_to(gc, entry->sec, entry->min[index]->depth);
423 for (i = 1; i < pi->nr; i++) {
424 line_to(gc, entry->sec, entry->max[index]->depth);
427 cairo_close_path(gc->cr);
431 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
435 plot_minmax_profile_minute(gc, pi, 2);
436 plot_minmax_profile_minute(gc, pi, 1);
437 plot_minmax_profile_minute(gc, pi, 0);
440 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
443 cairo_t *cr = gc->cr;
445 struct plot_data *entry;
446 int maxtime, maxdepth, marker;
447 int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
449 /* Get plot scaling limits */
450 maxtime = get_maxtime(pi);
451 maxdepth = get_maxdepth(pi);
453 /* Time markers: at most every 5 min, but no more than 12 markers
454 * and for convenience we do 5, 10, 15 or 30 min intervals.
455 * This allows for 6h dives - enough (I hope) for even the craziest
456 * divers - but just in case, for those 8h depth-record-breaking dives,
457 * we double the interval if this still doesn't get us to 12 or fewer
460 while (maxtime / increments[i] > 12 && i < 4)
462 incr = increments[i];
463 while (maxtime / incr > 12)
466 gc->leftx = 0; gc->rightx = maxtime;
467 gc->topy = 0; gc->bottomy = 1.0;
468 set_source_rgba(gc, TIME_GRID);
469 cairo_set_line_width(gc->cr, 2);
471 for (i = incr; i < maxtime; i += incr) {
477 /* now the text on every second time marker */
478 text_render_options_t tro = {10, TIME_TEXT, CENTER, TOP};
479 for (i = incr; i < maxtime; i += 2 * incr)
480 plot_text(gc, &tro, i, 1, "%d", i/60);
482 /* Depth markers: every 30 ft or 10 m*/
483 gc->leftx = 0; gc->rightx = 1.0;
484 gc->topy = 0; gc->bottomy = maxdepth;
485 switch (output_units.length) {
486 case METERS: marker = 10000; break;
487 case FEET: marker = 9144; break; /* 30 ft */
490 set_source_rgba(gc, DEPTH_GRID);
491 for (i = marker; i < maxdepth; i += marker) {
497 /* Show mean depth */
499 set_source_rgba(gc, MEAN_DEPTH);
500 move_to(gc, 0, pi->meandepth);
501 line_to(gc, 1, pi->meandepth);
505 gc->leftx = 0; gc->rightx = maxtime;
508 * These are good for debugging text placement etc,
509 * but not for actual display..
512 plot_smoothed_profile(gc, pi);
513 plot_minmax_profile(gc, pi);
516 /* Do the depth profile for the neat fill */
517 gc->topy = 0; gc->bottomy = maxdepth;
519 cairo_pattern_t *pat;
520 pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, 256.0);
521 pattern_add_color_stop_rgba (gc, pat, 1, DEPTH_BOTTOM);
522 pattern_add_color_stop_rgba (gc, pat, 0, DEPTH_TOP);
524 cairo_set_source(gc->cr, pat);
525 cairo_pattern_destroy(pat);
526 cairo_set_line_width(gc->cr, 2);
530 for (i = 0; i < pi->nr; i++, entry++)
531 line_to(gc, entry->sec, entry->depth);
532 cairo_close_path(gc->cr);
536 /* Now do it again for the velocity colors */
538 for (i = 1; i < pi->nr; i++) {
541 /* we want to draw the segments in different colors
542 * representing the vertical velocity, so we need to
543 * chop this into short segments */
544 depth = entry->depth;
545 set_source_rgba(gc, VELOCITY_COLORS_START_IDX + entry->velocity);
546 move_to(gc, entry[-1].sec, entry[-1].depth);
547 line_to(gc, sec, depth);
552 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
554 int maxtime, mintemp, maxtemp, delta;
556 /* Get plot scaling limits */
557 maxtime = get_maxtime(pi);
558 mintemp = pi->mintemp;
559 maxtemp = pi->maxtemp;
561 gc->leftx = 0; gc->rightx = maxtime;
562 /* Show temperatures in roughly the lower third, but make sure the scale
563 is at least somewhat reasonable */
564 delta = maxtemp - mintemp;
565 if (delta > 3000) { /* more than 3K in fluctuation */
566 gc->topy = maxtemp + delta*2;
567 gc->bottomy = mintemp - delta/2;
569 gc->topy = maxtemp + 1500 + delta*2;
570 gc->bottomy = mintemp - delta/2;
573 return maxtemp > mintemp;
576 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
580 static const text_render_options_t tro = {12, TEMP_TEXT, LEFT, TOP};
582 deg = get_temp_units(mkelvin, &unit);
584 plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
587 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
590 int last = -300, sec = 0;
591 int last_temperature = 0, last_printed_temp = 0;
593 if (!setup_temperature_limits(gc, pi))
596 for (i = 0; i < pi->nr; i++) {
597 struct plot_data *entry = pi->entry+i;
598 int mkelvin = entry->temperature;
602 last_temperature = mkelvin;
604 /* don't print a temperature
605 * if it's been less than 5min and less than a 2K change OR
606 * if it's been less than 2min OR if the change from the
607 * last print is less than .4K (and therefore less than 1F */
608 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
609 (sec < last + 120) ||
610 (abs(mkelvin - last_printed_temp) < 400))
613 plot_single_temp_text(gc,sec,mkelvin);
614 last_printed_temp = mkelvin;
616 /* it would be nice to print the end temperature, if it's
617 * different or if the last temperature print has been more
618 * than a quarter of the dive back */
619 if ((abs(last_temperature - last_printed_temp) > 500) ||
620 ((double)last / (double)sec < 0.75))
621 plot_single_temp_text(gc, sec, last_temperature);
624 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
627 cairo_t *cr = gc->cr;
630 if (!setup_temperature_limits(gc, pi))
633 cairo_set_line_width(gc->cr, 2);
634 set_source_rgba(gc, TEMP_PLOT);
635 for (i = 0; i < pi->nr; i++) {
636 struct plot_data *entry = pi->entry + i;
637 int mkelvin = entry->temperature;
638 int sec = entry->sec;
645 line_to(gc, sec, mkelvin);
647 move_to(gc, sec, mkelvin);
653 /* gets both the actual start and end pressure as well as the scaling factors */
654 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
657 gc->rightx = get_maxtime(pi);
659 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
660 return pi->maxpressure != 0;
663 /* set the color for the pressure plot according to temporary sac rate
664 * as compared to avg_sac; the calculation simply maps the delta between
665 * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
666 * more than 6000 ml/min below avg_sac mapped to 0 */
668 static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
671 int delta = sac - avg_sac + 7000;
674 sac_index = delta / 2000;
677 if (sac_index > SAC_COLORS - 1)
678 sac_index = SAC_COLORS - 1;
679 set_source_rgba(gc, SAC_COLORS_START_IDX + sac_index);
681 set_source_rgba(gc, SAC_DEFAULT);
685 /* calculate the current SAC in ml/min and convert to int */
686 #define GET_LOCAL_SAC(_entry1, _entry2, _dive) (int) \
687 ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) * \
688 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter / \
689 (((_entry2)->sec - (_entry1)->sec) / 60.0) / \
690 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) / \
693 #define SAC_WINDOW 45 /* sliding window in seconds for current SAC calculation */
695 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
700 int lift_pen = FALSE;
701 int first_plot = TRUE;
703 struct plot_data *last_entry = NULL;
705 if (!get_cylinder_pressure_range(gc, pi))
708 cairo_set_line_width(gc->cr, 2);
710 for (i = 0; i < pi->nr; i++) {
712 struct plot_data *entry = pi->entry + i;
714 mbar = GET_PRESSURE(entry);
715 if (!entry->same_cylinder) {
726 sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
730 for (j = last; j < i; j++)
731 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
733 if (entry->sec - last_entry->sec >= SAC_WINDOW) {
735 last_entry = pi->entry + last;
738 set_sac_color(gc, sac, dive->sac);
740 if (!first_plot && entry->same_cylinder) {
741 /* if we have a previous event from the same tank,
742 * draw at least a short line */
744 prev_pr = GET_PRESSURE(entry - 1);
745 move_to(gc, (entry-1)->sec, prev_pr);
746 line_to(gc, entry->sec, mbar);
749 move_to(gc, entry->sec, mbar);
753 line_to(gc, entry->sec, mbar);
755 cairo_stroke(gc->cr);
756 move_to(gc, entry->sec, mbar);
760 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
761 int xalign, int yalign)
766 pressure = get_pressure_units(mbar, &unit);
767 text_render_options_t tro = {10, PRESSURE_TEXT, xalign, yalign};
768 plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
771 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
775 int seen_cyl[MAX_CYLINDERS] = { FALSE, };
776 int last_pressure[MAX_CYLINDERS] = { 0, };
777 int last_time[MAX_CYLINDERS] = { 0, };
778 struct plot_data *entry;
780 if (!get_cylinder_pressure_range(gc, pi))
783 /* only loop over the actual events from the dive computer
784 * plus the second synthetic event at the start (to make sure
785 * we get "time=0" right)
786 * sadly with a recent change that first entry may no longer
787 * have any pressure reading - in that case just grab the
788 * pressure from the second entry */
789 if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
790 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
791 for (i = 1; i < pi->nr; i++) {
792 entry = pi->entry + i;
794 if (!entry->same_cylinder) {
795 cyl = entry->cylinderindex;
796 if (!seen_cyl[cyl]) {
797 mbar = GET_PRESSURE(entry);
798 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
799 seen_cyl[cyl] = TRUE;
802 /* remember the last pressure and time of
803 * the previous cylinder */
804 cyl = (entry - 1)->cylinderindex;
805 last_pressure[cyl] = GET_PRESSURE(entry - 1);
806 last_time[cyl] = (entry - 1)->sec;
810 cyl = entry->cylinderindex;
811 if (GET_PRESSURE(entry))
812 last_pressure[cyl] = GET_PRESSURE(entry);
813 last_time[cyl] = entry->sec;
815 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
816 if (last_time[cyl]) {
817 plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
822 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
824 struct plot_data *p = entry;
825 int time = entry->sec;
826 int seconds = 90*(index+1);
827 struct plot_data *min, *max;
830 /* Go back 'seconds' in time */
832 if (p[-1].sec < time - seconds)
837 /* Then go forward until we hit an entry past the time */
842 int depth = p->depth;
843 if (p->sec > time + seconds)
847 if (depth < min->depth)
849 if (depth > max->depth)
852 entry->min[index] = min;
853 entry->max[index] = max;
854 entry->avg[index] = (avg + nr/2) / nr;
857 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
859 analyze_plot_info_minmax_minute(entry, first, last, 0);
860 analyze_plot_info_minmax_minute(entry, first, last, 1);
861 analyze_plot_info_minmax_minute(entry, first, last, 2);
864 static velocity_t velocity(int speed)
868 if (speed < -304) /* ascent faster than -60ft/min */
870 else if (speed < -152) /* above -30ft/min */
872 else if (speed < -76) /* -15ft/min */
874 else if (speed < -25) /* -5ft/min */
876 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
877 for descent are usually about 2x ascent rate; still, we want
878 stable to mean stable */
880 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
882 else if (speed < 304) /* up to 60ft/min is moderate */
884 else if (speed < 507) /* up to 100ft/min is fast */
886 else /* more than that is just crazy - you'll blow your ears out */
891 static struct plot_info *analyze_plot_info(struct plot_info *pi)
896 /* Do pressure min/max based on the non-surface data */
897 for (i = 0; i < nr; i++) {
898 struct plot_data *entry = pi->entry+i;
899 int pressure = GET_PRESSURE(entry);
900 int temperature = entry->temperature;
903 if (pressure > pi->maxpressure)
904 pi->maxpressure = pressure;
908 if (!pi->mintemp || temperature < pi->mintemp)
909 pi->mintemp = temperature;
910 if (temperature > pi->maxtemp)
911 pi->maxtemp = temperature;
915 /* Smoothing function: 5-point triangular smooth */
916 for (i = 2; i < nr; i++) {
917 struct plot_data *entry = pi->entry+i;
921 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
922 entry->smoothed = (depth+4) / 9;
924 /* vertical velocity in mm/sec */
925 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
926 if (entry[0].sec - entry[-1].sec) {
927 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
928 /* if our samples are short and we aren't too FAST*/
929 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
931 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
933 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
934 (entry[0].sec - entry[past].sec));
937 entry->velocity = STABLE;
940 /* One-, two- and three-minute minmax data */
941 for (i = 0; i < nr; i++) {
942 struct plot_data *entry = pi->entry +i;
943 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
950 * simple structure to track the beginning and end tank pressure as
951 * well as the integral of depth over time spent while we have no
952 * pressure reading from the tank */
953 typedef struct pr_track_struct pr_track_t;
954 struct pr_track_struct {
959 double pressure_time;
963 static pr_track_t *pr_track_alloc(int start, int t_start) {
964 pr_track_t *pt = malloc(sizeof(pr_track_t));
966 pt->t_start = t_start;
969 pt->pressure_time = 0.0;
974 /* poor man's linked list */
975 static pr_track_t *list_last(pr_track_t *list)
977 pr_track_t *tail = list;
986 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
988 pr_track_t *tail = list_last(list);
991 tail->next = element;
995 static void list_free(pr_track_t *list)
999 list_free(list->next);
1003 static void dump_pr_track(pr_track_t **track_pr)
1008 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1009 list = track_pr[cyl];
1011 printf("cyl%d: start %d end %d t_start %d t_end %d pt %6.3f\n", cyl,
1012 list->start, list->end, list->t_start, list->t_end, list->pressure_time);
1018 static void fill_missing_tank_pressures(struct plot_info *pi, pr_track_t **track_pr)
1020 pr_track_t *list = NULL;
1021 pr_track_t *nlist = NULL;
1024 struct plot_data *entry;
1025 int cur_pr[MAX_CYLINDERS];
1028 /* another great debugging tool */
1029 dump_pr_track(track_pr);
1031 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1032 cur_pr[cyl] = track_pr[cyl]->start;
1035 /* The first two are "fillers", but in case we don't have a sample
1036 * at time 0 we need to process the second of them here */
1037 for (i = 1; i < pi->nr; i++) {
1038 entry = pi->entry + i;
1039 if (SENSOR_PRESSURE(entry)) {
1040 cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
1042 if(!list || list->t_end < entry->sec) {
1043 nlist = track_pr[entry->cylinderindex];
1045 while (nlist && nlist->t_start <= entry->sec) {
1049 /* there may be multiple segments - so
1050 * let's assemble the length */
1053 pt = list->pressure_time;
1054 while (!nlist->end) {
1055 nlist = nlist->next;
1057 /* oops - we have no end pressure,
1058 * so this means this is a tank without
1059 * gas consumption information */
1062 pt += nlist->pressure_time;
1066 /* just continue without calculating
1067 * interpolated values */
1068 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1072 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
1075 double cur_pt = (entry->sec - (entry-1)->sec) *
1076 (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1077 INTERPOLATED_PRESSURE(entry) =
1078 cur_pr[entry->cylinderindex] + cur_pt * magic + 0.5;
1079 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1081 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1086 static int get_cylinder_index(struct dive *dive, struct event *ev)
1091 * Try to find a cylinder that matches the O2 percentage
1092 * in the gas change event 'value' field.
1094 * Crazy suunto gas change events. We really should do
1095 * this in libdivecomputer or something.
1097 for (i = 0; i < MAX_CYLINDERS; i++) {
1098 cylinder_t *cyl = dive->cylinder+i;
1099 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1100 if (o2 == ev->value)
1107 static struct event *get_next_gaschange(struct event *event)
1110 if (!strcmp(event->name, "gaschange"))
1112 event = event->next;
1117 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1119 while (i < pi->nr) {
1120 struct plot_data *entry = pi->entry+i;
1121 if (entry->sec > end)
1123 if (entry->cylinderindex != cylinderindex) {
1124 entry->cylinderindex = cylinderindex;
1125 entry->pressure[0] = 0;
1132 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1134 int i = 0, cylinderindex = 0;
1135 struct event *ev = get_next_gaschange(dive->events);
1141 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1142 cylinderindex = get_cylinder_index(dive, ev);
1143 ev = get_next_gaschange(ev->next);
1145 set_cylinder_index(pi, i, cylinderindex, ~0u);
1148 /* for computers that track gas changes through events */
1149 static int count_gas_change_events(struct dive *dive)
1152 struct event *ev = get_next_gaschange(dive->events);
1156 ev = get_next_gaschange(ev->next);
1162 * Create a plot-info with smoothing and ranged min/max
1164 * This also makes sure that we have extra empty events on both
1165 * sides, so that you can do end-points without having to worry
1168 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1170 int cylinderindex = -1;
1171 int lastdepth, lastindex;
1172 int i, pi_idx, nr, sec, cyl;
1174 struct plot_info *pi;
1175 pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1176 pr_track_t *pr_track, *current;
1177 gboolean missing_pr = FALSE;
1178 struct plot_data *entry = NULL;
1181 /* we want to potentially add synthetic plot_info elements for the gas changes */
1182 nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1183 alloc_size = plot_info_size(nr);
1184 pi = malloc(alloc_size);
1187 memset(pi, 0, alloc_size);
1189 pi_idx = 2; /* the two extra events at the start */
1190 /* check for gas changes before the samples start */
1191 ev = get_next_gaschange(dive->events);
1192 while (ev && ev->time.seconds < dive_sample->time.seconds) {
1193 entry = pi->entry + pi_idx;
1194 entry->sec = ev->time.seconds;
1195 entry->depth = 0; /* is that always correct ? */
1197 ev = get_next_gaschange(ev->next);
1199 if (ev && ev->time.seconds == dive_sample->time.seconds) {
1200 /* we already have a sample at the time of the event */
1201 ev = get_next_gaschange(ev->next);
1206 for (i = 0; i < nr_samples; i++) {
1209 struct sample *sample = dive_sample+i;
1211 entry = pi->entry + i + pi_idx;
1212 while (ev && ev->time.seconds < sample->time.seconds) {
1213 /* insert two fake plot info structures for the end of
1214 * the old tank and the start of the new tank */
1215 if (ev->time.seconds == sample->time.seconds - 1) {
1216 entry->sec = ev->time.seconds - 1;
1217 (entry+1)->sec = ev->time.seconds;
1219 entry->sec = ev->time.seconds;
1220 (entry+1)->sec = ev->time.seconds + 1;
1222 /* we need a fake depth - let's interpolate */
1224 entry->depth = sample->depth.mm -
1225 (sample->depth.mm - (sample-1)->depth.mm) / 2;
1227 entry->depth = sample->depth.mm;
1228 (entry+1)->depth = entry->depth;
1230 entry = pi->entry + i + pi_idx;
1231 ev = get_next_gaschange(ev->next);
1233 if (ev && ev->time.seconds == sample->time.seconds) {
1234 /* we already have a sample at the time of the event
1235 * just add a new one for the old tank and delay the
1236 * real even by one second (to keep time monotonous) */
1237 entry->sec = ev->time.seconds;
1238 entry->depth = sample->depth.mm;
1240 entry = pi->entry + i + pi_idx;
1241 ev = get_next_gaschange(ev->next);
1244 sec = entry->sec = sample->time.seconds + delay;
1245 depth = entry->depth = sample->depth.mm;
1246 entry->cylinderindex = sample->cylinderindex;
1247 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1248 entry->temperature = sample->temperature.mkelvin;
1250 if (depth || lastdepth)
1251 lastindex = i + pi_idx;
1254 if (depth > pi->maxdepth)
1255 pi->maxdepth = depth;
1257 entry = pi->entry + i + pi_idx;
1258 /* are there still unprocessed gas changes? that would be very strange */
1260 entry->sec = ev->time.seconds;
1261 entry->depth = 0; /* why are there gas changes after the dive is over? */
1263 entry = pi->entry + i + pi_idx;
1264 ev = get_next_gaschange(ev->next);
1266 nr = nr_samples + pi_idx - 2;
1267 check_gas_change_events(dive, pi);
1269 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1270 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1271 current = track_pr[pi->entry[2].cylinderindex];
1272 for (i = 0; i < nr + 1; i++) {
1273 entry = pi->entry + i + 1;
1275 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1276 cylinderindex = entry->cylinderindex;
1278 /* track the segments per cylinder and their pressure/time integral */
1279 if (!entry->same_cylinder) {
1280 current->end = SENSOR_PRESSURE(entry-1);
1281 current->t_end = (entry-1)->sec;
1282 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1283 track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1284 } else { /* same cylinder */
1285 if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1286 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1287 /* transmitter changed its working status */
1288 current->end = SENSOR_PRESSURE(entry-1);
1289 current->t_end = (entry-1)->sec;
1290 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1291 track_pr[cylinderindex] =
1292 list_add(track_pr[cylinderindex], current);
1295 /* finally, do the discrete integration to get the SAC rate equivalent */
1296 current->pressure_time += (entry->sec - (entry-1)->sec) *
1297 (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1298 missing_pr |= !SENSOR_PRESSURE(entry);
1302 current->t_end = entry->sec;
1304 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1305 int pr = dive->cylinder[cyl].end.mbar;
1306 if (pr && track_pr[cyl]) {
1307 pr_track = list_last(track_pr[cyl]);
1311 /* Fill in the last two entries with empty values but valid times
1312 * without creating a false cylinder change event */
1314 pi->entry[i].sec = sec + 20;
1315 pi->entry[i].same_cylinder = 1;
1316 pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1317 INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1318 pi->entry[i+1].sec = sec + 40;
1319 pi->entry[i+1].same_cylinder = 1;
1320 pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1321 INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1322 /* the number of actual entries - some computers have lots of
1323 * depth 0 samples at the end of a dive, we want to make sure
1324 * we have exactly one of them at the end */
1325 pi->nr = lastindex+1;
1326 while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1328 pi->maxtime = pi->entry[lastindex].sec;
1330 /* Analyze_plot_info() will do the sample max pressures,
1331 * this handles the manual pressures
1333 pi->maxpressure = 0;
1334 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1335 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1336 if (mbar > pi->maxpressure)
1337 pi->maxpressure = mbar;
1340 pi->meandepth = dive->meandepth.mm;
1343 fill_missing_tank_pressures(pi, track_pr);
1345 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1346 list_free(track_pr[cyl]);
1347 if (0) /* awesome for debugging - not useful otherwise */
1349 return analyze_plot_info(pi);
1352 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1354 struct plot_info *pi;
1355 static struct sample fake[4];
1356 struct sample *sample = dive->sample;
1357 int nr = dive->samples;
1360 /* The dive has no samples, so create a few fake ones. This assumes an
1361 ascent/descent rate of 9 m/min, which is just below the limit for FAST. */
1362 int duration = dive->duration.seconds;
1363 int maxdepth = dive->maxdepth.mm;
1364 int asc_desc_time = dive->maxdepth.mm*60/9000;
1366 fake[1].time.seconds = asc_desc_time;
1367 fake[1].depth.mm = maxdepth;
1368 fake[2].time.seconds = duration - asc_desc_time;
1369 fake[2].depth.mm = maxdepth;
1370 fake[3].time.seconds = duration * 1.00;
1374 pi = create_plot_info(dive, nr, sample);
1376 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1377 cairo_set_line_width(gc->cr, 1);
1378 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1379 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1382 * We can use "cairo_translate()" because that doesn't
1383 * scale line width etc. But the actual scaling we need
1384 * do set up ourselves..
1386 * Snif. What a pity.
1388 gc->maxx = (drawing_area->width - 2*drawing_area->x);
1389 gc->maxy = (drawing_area->height - 2*drawing_area->y);
1392 plot_depth_profile(gc, pi);
1393 plot_events(gc, pi, dive);
1395 /* Temperature profile */
1396 plot_temperature_profile(gc, pi);
1398 /* Cylinder pressure plot */
1399 plot_cylinder_pressure(gc, pi, dive);
1401 /* Text on top of all graphs.. */
1402 plot_temperature_text(gc, pi);
1403 plot_depth_text(gc, pi);
1404 plot_cylinder_pressure_text(gc, pi);
1406 /* Bounding box last */
1407 gc->leftx = 0; gc->rightx = 1.0;
1408 gc->topy = 0; gc->bottomy = 1.0;
1410 set_source_rgba(gc, BOUNDING_BOX);
1411 cairo_set_line_width(gc->cr, 1);
1416 cairo_close_path(gc->cr);
1417 cairo_stroke(gc->cr);