11 int selected_dive = 0;
13 /* Plot info with smoothing, velocity indication
14 * and one-, two- and three-minute minimums and maximums */
18 int meandepth, maxdepth;
19 int minpressure, maxpressure;
23 int pressure, temperature;
27 enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity;
28 struct plot_data *min[3];
29 struct plot_data *max[3];
34 /* convert velocity to colors */
35 typedef struct { double r, g, b; } rgb_t;
36 static const rgb_t rgb[] = {
37 [STABLE] = {0.0, 0.4, 0.0},
38 [SLOW] = {0.4, 0.8, 0.0},
39 [MODERATE] = {0.8, 0.8, 0.0},
40 [FAST] = {0.8, 0.5, 0.0},
41 [CRAZY] = {1.0, 0.0, 0.0},
44 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
46 /* Scale to 0,0 -> maxx,maxy */
47 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
48 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
49 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
51 static void move_to(struct graphics_context *gc, double x, double y)
53 cairo_move_to(gc->cr, SCALE(gc, x, y));
56 static void line_to(struct graphics_context *gc, double x, double y)
58 cairo_line_to(gc->cr, SCALE(gc, x, y));
61 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
64 /* Black is white and white is black */
71 cairo_set_source_rgba(gc->cr, r, g, b, a);
74 static void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
76 set_source_rgba(gc, r, g, b, 1);
79 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
82 * When showing dive profiles, we scale things to the
83 * current dive. However, we don't scale past less than
84 * 30 minutes or 90 ft, just so that small dives show
86 * we also need to add 180 seconds at the end so the min/max
89 static int get_maxtime(struct plot_info *pi)
91 int seconds = pi->maxtime;
92 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
93 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
96 static int get_maxdepth(struct plot_info *pi)
98 unsigned mm = pi->maxdepth;
99 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
100 return MAX(30000, ROUND_UP(mm+3000, 10000));
107 } text_render_options_t;
110 #define CENTER (-0.5)
117 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
118 double x, double y, const char *fmt, ...)
120 cairo_t *cr = gc->cr;
121 cairo_font_extents_t fe;
122 cairo_text_extents_t extents;
128 vsnprintf(buffer, sizeof(buffer), fmt, args);
131 cairo_set_font_size(cr, tro->size);
132 cairo_font_extents(cr, &fe);
133 cairo_text_extents(cr, buffer, &extents);
134 dx = tro->hpos * extents.width + extents.x_bearing;
135 dy = tro->vpos * extents.height + fe.descent;
138 cairo_rel_move_to(cr, dx, dy);
140 cairo_text_path(cr, buffer);
141 set_source_rgb(gc, 0, 0, 0);
145 cairo_rel_move_to(cr, dx, dy);
147 set_source_rgb(gc, tro->r, tro->g, tro->b);
148 cairo_show_text(cr, buffer);
151 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
153 int sec = entry->sec;
154 depth_t depth = { entry->val };
158 switch (output_units.length) {
160 d = depth.mm / 1000.0;
168 plot_text(gc, tro, sec, depth.mm, fmt, d);
171 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
173 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
174 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
177 for (i = 0; i < pi->nr; i++) {
178 struct plot_data *entry = pi->entry + i;
180 if (entry->val < 2000)
183 if (entry == entry->max[2])
184 render_depth_sample(gc, entry, &deep);
186 if (entry == entry->min[2])
187 render_depth_sample(gc, entry, &shallow);
191 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
193 int maxtime, maxdepth;
195 /* Get plot scaling limits */
196 maxtime = get_maxtime(pi);
197 maxdepth = get_maxdepth(pi);
199 gc->leftx = 0; gc->rightx = maxtime;
200 gc->topy = 0; gc->bottomy = maxdepth;
202 plot_text_samples(gc, pi);
205 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
208 struct plot_data *entry = pi->entry;
210 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
211 move_to(gc, entry->sec, entry->smoothed);
212 for (i = 1; i < pi->nr; i++) {
214 line_to(gc, entry->sec, entry->smoothed);
216 cairo_stroke(gc->cr);
219 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
223 struct plot_data *entry = pi->entry;
225 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
226 move_to(gc, entry->sec, entry->min[index]->val);
227 for (i = 1; i < pi->nr; i++) {
229 line_to(gc, entry->sec, entry->min[index]->val);
231 for (i = 1; i < pi->nr; i++) {
232 line_to(gc, entry->sec, entry->max[index]->val);
235 cairo_close_path(gc->cr);
239 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
243 plot_minmax_profile_minute(gc, pi, 2, 0.1);
244 plot_minmax_profile_minute(gc, pi, 1, 0.1);
245 plot_minmax_profile_minute(gc, pi, 0, 0.1);
248 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
251 cairo_t *cr = gc->cr;
252 int ends, sec, depth;
255 struct plot_data *entry;
256 int maxtime, maxdepth, marker;
258 /* Get plot scaling limits */
259 maxtime = get_maxtime(pi);
260 maxdepth = get_maxdepth(pi);
262 /* Time markers: every 5 min */
263 gc->leftx = 0; gc->rightx = maxtime;
264 gc->topy = 0; gc->bottomy = 1.0;
265 for (i = 5*60; i < maxtime; i += 5*60) {
270 /* Depth markers: every 30 ft or 10 m*/
271 gc->leftx = 0; gc->rightx = 1.0;
272 gc->topy = 0; gc->bottomy = maxdepth;
273 switch (output_units.length) {
274 case METERS: marker = 10000; break;
275 case FEET: marker = 9144; break; /* 30 ft */
278 set_source_rgba(gc, 1, 1, 1, 0.5);
279 for (i = marker; i < maxdepth; i += marker) {
285 /* Show mean depth */
286 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
287 move_to(gc, 0, pi->meandepth);
288 line_to(gc, 1, pi->meandepth);
291 gc->leftx = 0; gc->rightx = maxtime;
293 plot_smoothed_profile(gc, pi);
294 plot_minmax_profile(gc, pi);
297 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
298 secs = (int *) malloc(sizeof(int) * pi->nr);
299 depths = (int *) malloc(sizeof(int) * pi->nr);
300 secs[0] = entry->sec;
301 depths[0] = entry->val;
302 for (i = 1; i < pi->nr; i++) {
305 if (sec <= maxtime || entry->val > 0) {
306 /* we want to draw the segments in different colors
307 * representing the vertical velocity, so we need to
308 * chop this into short segments */
309 rgb_t color = rgb[entry->velocity];
311 set_source_rgb(gc, color.r, color.g, color.b);
312 move_to(gc, secs[i-1], depths[i-1]);
313 line_to(gc, sec, depth);
320 move_to(gc, secs[ends], depths[ends]);
321 gc->topy = 0; gc->bottomy = 1.0;
322 line_to(gc, secs[ends], 0);
323 line_to(gc, secs[0], 0);
324 cairo_close_path(cr);
325 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
327 /* now do it again for the neat fill */
328 gc->topy = 0; gc->bottomy = maxdepth;
329 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
330 move_to(gc, secs[0], depths[0]);
331 for (i = 1; i <= ends; i++) {
332 line_to(gc, secs[i],depths[i]);
334 gc->topy = 0; gc->bottomy = 1.0;
335 line_to(gc, secs[ends], 0);
336 line_to(gc, secs[0], 0);
337 cairo_close_path(gc->cr);
341 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
343 int maxtime, mintemp, maxtemp, delta;
345 /* Get plot scaling limits */
346 maxtime = get_maxtime(pi);
347 mintemp = pi->mintemp;
348 maxtemp = pi->maxtemp;
350 gc->leftx = 0; gc->rightx = maxtime;
351 /* Show temperatures in roughly the lower third, but make sure the scale
352 is at least somewhat reasonable */
353 delta = maxtemp - mintemp;
354 if (delta > 3000) { /* more than 3K in fluctuation */
355 gc->topy = maxtemp + delta*2;
356 gc->bottomy = mintemp - delta/2;
358 gc->topy = maxtemp + 1500 + delta*2;
359 gc->bottomy = mintemp - delta/2;
362 return maxtemp > mintemp;
365 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
369 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
370 temperature_t temperature = { mkelvin };
372 if (output_units.temperature == FAHRENHEIT) {
373 deg = to_F(temperature);
376 deg = to_C(temperature);
379 plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
382 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
385 int last = 0, sec = 0;
386 int last_temperature = 0, last_printed_temp = 0;
388 if (!setup_temperature_limits(gc, pi))
391 for (i = 0; i < pi->nr; i++) {
392 struct plot_data *entry = pi->entry+i;
393 int mkelvin = entry->temperature;
397 last_temperature = mkelvin;
399 if (sec < last + 300)
402 plot_single_temp_text(gc,sec,mkelvin);
403 last_printed_temp = mkelvin;
405 /* it would be nice to print the end temperature, if it's different */
406 if (abs(last_temperature - last_printed_temp) > 500)
407 plot_single_temp_text(gc, sec, last_temperature);
410 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
413 cairo_t *cr = gc->cr;
416 if (!setup_temperature_limits(gc, pi))
419 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
420 for (i = 0; i < pi->nr; i++) {
421 struct plot_data *entry = pi->entry + i;
422 int mkelvin = entry->temperature;
423 int sec = entry->sec;
430 line_to(gc, sec, mkelvin);
432 move_to(gc, sec, mkelvin);
438 /* gets both the actual start and end pressure as well as the scaling factors */
439 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
442 gc->rightx = get_maxtime(pi);
444 gc->topy = 0; gc->bottomy = pi->maxpressure * 1.5;
445 return pi->maxpressure != 0;
448 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
452 if (!get_cylinder_pressure_range(gc, pi))
455 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
457 move_to(gc, 0, pi->maxpressure);
458 for (i = 1; i < pi->nr; i++) {
460 struct plot_data *entry = pi->entry + i;
462 mbar = entry->pressure;
465 line_to(gc, entry->sec, mbar);
467 line_to(gc, pi->maxtime, pi->minpressure);
468 cairo_stroke(gc->cr);
472 * Return air usage (in liters).
474 static double calculate_airuse(struct dive *dive)
479 for (i = 0; i < MAX_CYLINDERS; i++) {
480 cylinder_t *cyl = dive->cylinder + i;
481 int size = cyl->type.size.mliter;
487 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
489 /* Liters of air at 1 atm == milliliters at 1k atm*/
490 airuse += kilo_atm * size;
495 static void plot_info(struct dive *dive, struct graphics_context *gc)
497 text_render_options_t tro = {10, 0.2, 1.0, 0.2, RIGHT, BOTTOM};
498 const double liters_per_cuft = 28.317;
499 const char *unit, *format, *desc;
505 airuse = calculate_airuse(dive);
507 update_air_info(" \n ");
510 switch (output_units.volume) {
513 format = "vol: %4.0f %s";
517 format = "vol: %4.2f %s";
518 airuse /= liters_per_cuft;
522 plot_text(gc, &tro, 0.98, 0.98, format, airuse, unit);
523 len = snprintf(buffer1, sizeof(buffer1), format, airuse, unit);
525 if (dive->duration.seconds) {
526 double pressure = 1 + (dive->meandepth.mm / 10000.0);
527 double sac = airuse / pressure * 60 / dive->duration.seconds;
528 plot_text(gc, &tro, 0.98, 0.98, "SAC: %4.2f %s/min", sac, unit);
529 snprintf(buffer1+len, sizeof(buffer1)-len,
530 "\nSAC: %4.2f %s/min", sac, unit);
534 desc = dive->cylinder[0].type.description;
535 if (desc || dive->cylinder[0].gasmix.o2.permille) {
536 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
541 plot_text(gc, &tro, 0.98, 0.98, "%s (%d%%)", desc, o2);
542 len = snprintf(buffer2, sizeof(buffer2), "%s (%d%%): used ", desc, o2);
544 snprintf(buffer2+len, sizeof(buffer2)-len, buffer1);
545 update_air_info(buffer2);
548 static int mbar_to_PSI(int mbar)
550 pressure_t p = {mbar};
554 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
556 if (get_cylinder_pressure_range(gc, pi)) {
558 const char *unit = "bar";
560 switch (output_units.pressure) {
562 start = pi->maxpressure * 100;
563 end = pi->minpressure * 100;
567 start = (pi->maxpressure + 500) / 1000;
568 end = (pi->minpressure + 500) / 1000;
572 start = mbar_to_PSI(pi->maxpressure);
573 end = mbar_to_PSI(pi->minpressure);
578 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
579 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
580 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
585 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
587 struct plot_data *p = entry;
588 int time = entry->sec;
589 int seconds = 90*(index+1);
590 struct plot_data *min, *max;
593 /* Go back 'seconds' in time */
595 if (p[-1].sec < time - seconds)
600 /* Then go forward until we hit an entry past the time */
606 if (p->sec > time + seconds)
615 entry->min[index] = min;
616 entry->max[index] = max;
617 entry->avg[index] = (avg + nr/2) / nr;
620 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
622 analyze_plot_info_minmax_minute(entry, first, last, 0);
623 analyze_plot_info_minmax_minute(entry, first, last, 1);
624 analyze_plot_info_minmax_minute(entry, first, last, 2);
627 static struct plot_info *analyze_plot_info(struct plot_info *pi)
632 /* Do pressure min/max based on the non-surface data */
633 for (i = 0; i < nr; i++) {
634 struct plot_data *entry = pi->entry+i;
635 int pressure = entry->pressure;
636 int temperature = entry->temperature;
639 if (!pi->minpressure || pressure < pi->minpressure)
640 pi->minpressure = pressure;
641 if (pressure > pi->maxpressure)
642 pi->maxpressure = pressure;
646 if (!pi->mintemp || temperature < pi->mintemp)
647 pi->mintemp = temperature;
648 if (temperature > pi->maxtemp)
649 pi->maxtemp = temperature;
653 /* Smoothing function: 5-point triangular smooth */
654 for (i = 2; i < nr-1; i++) {
655 struct plot_data *entry = pi->entry+i;
659 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
660 entry->smoothed = (val+4) / 9;
662 /* vertical velocity in mm/sec */
663 if (entry[0].sec - entry[-1].sec) {
664 val = (entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec);
665 if (val < -304) /* ascent faster than -60ft/min */
666 entry->velocity = CRAZY;
667 else if (val < -152) /* above -30ft/min */
668 entry->velocity = FAST;
669 else if (val < -76) /* -15ft/min */
670 entry->velocity = MODERATE;
671 else if (val < -25) /* -5ft/min */
672 entry->velocity = SLOW;
673 else if (val < 25) /* very hard to find data, but it appears that the recommendations
674 for descent are usually about 2x ascent rate; still, we want
675 stable to mean stable */
676 entry->velocity = STABLE;
677 else if (val < 152) /* between 5 and 30ft/min is considered slow */
678 entry->velocity = SLOW;
679 else if (val < 304) /* up to 60ft/min is moderate */
680 entry->velocity = MODERATE;
681 else if (val < 507) /* up to 100ft/min is fast */
682 entry->velocity = FAST;
683 else /* more than that is just crazy - you'll blow your ears out */
684 entry->velocity = CRAZY;
686 entry->velocity = STABLE;
689 /* One-, two- and three-minute minmax data */
690 for (i = 0; i < nr; i++) {
691 struct plot_data *entry = pi->entry +i;
692 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
699 * Create a plot-info with smoothing and ranged min/max
701 * This also makes sure that we have extra empty events on both
702 * sides, so that you can do end-points without having to worry
705 static struct plot_info *create_plot_info(struct dive *dive)
707 int lastdepth, lastindex;
708 int i, nr = dive->samples + 4, sec;
709 size_t alloc_size = plot_info_size(nr);
710 struct plot_info *pi;
712 pi = malloc(alloc_size);
715 memset(pi, 0, alloc_size);
720 for (i = 0; i < dive->samples; i++) {
722 struct sample *sample = dive->sample+i;
723 struct plot_data *entry = pi->entry + i + 2;
725 sec = entry->sec = sample->time.seconds;
726 depth = entry->val = sample->depth.mm;
727 entry->pressure = sample->cylinderpressure.mbar;
728 entry->temperature = sample->temperature.mkelvin;
730 if (depth || lastdepth)
734 if (depth > pi->maxdepth)
735 pi->maxdepth = depth;
739 /* Fill in the last two entries with empty values but valid times */
740 i = dive->samples + 2;
741 pi->entry[i].sec = sec + 20;
742 pi->entry[i+1].sec = sec + 40;
744 pi->nr = lastindex+1;
745 pi->maxtime = pi->entry[lastindex].sec;
747 pi->minpressure = dive->cylinder[0].end.mbar;
748 pi->maxpressure = dive->cylinder[0].start.mbar;
750 pi->meandepth = dive->meandepth.mm;
752 return analyze_plot_info(pi);
755 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
758 struct plot_info *pi = create_plot_info(dive);
762 cairo_translate(gc->cr, topx, topy);
763 cairo_set_line_width(gc->cr, 2);
764 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
765 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
768 * We can use "cairo_translate()" because that doesn't
769 * scale line width etc. But the actual scaling we need
770 * do set up ourselves..
774 gc->maxx = (w - 2*topx);
775 gc->maxy = (h - 2*topy);
777 /* Temperature profile */
778 plot_temperature_profile(gc, pi);
780 /* Cylinder pressure plot */
781 plot_cylinder_pressure(gc, pi);
784 plot_depth_profile(gc, pi);
786 /* Text on top of all graphs.. */
787 plot_temperature_text(gc, pi);
788 plot_depth_text(gc, pi);
789 plot_cylinder_pressure_text(gc, pi);
791 /* And info box in the lower right corner.. */
792 gc->leftx = 0; gc->rightx = 1.0;
793 gc->topy = 0; gc->bottomy = 1.0;
796 /* Bounding box last */
797 set_source_rgb(gc, 1, 1, 1);
802 cairo_close_path(gc->cr);
803 cairo_stroke(gc->cr);
807 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
809 struct dive *dive = current_dive;
810 struct graphics_context gc = { .printer = 0 };
813 w = widget->allocation.width;
814 h = widget->allocation.height;
816 gc.cr = gdk_cairo_create(widget->window);
817 set_source_rgb(&gc, 0, 0, 0);
821 plot(&gc, w, h, dive);
823 cairo_destroy(gc.cr);
828 GtkWidget *dive_profile_widget(void)
832 da = gtk_drawing_area_new();
833 gtk_widget_set_size_request(da, 350, 250);
834 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);