11 int selected_dive = 0;
13 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
14 /* Plot info with smoothing, velocity indication
15 * and one-, two- and three-minute minimums and maximums */
19 int meandepth, maxdepth;
20 int minpressure, maxpressure;
24 int pressure, temperature;
29 struct plot_data *min[3];
30 struct plot_data *max[3];
35 /* convert velocity to colors */
36 typedef struct { double r, g, b; } rgb_t;
37 static const rgb_t rgb[] = {
38 [STABLE] = {0.0, 0.4, 0.0},
39 [SLOW] = {0.4, 0.8, 0.0},
40 [MODERATE] = {0.8, 0.8, 0.0},
41 [FAST] = {0.8, 0.5, 0.0},
42 [CRAZY] = {1.0, 0.0, 0.0},
45 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
47 /* Scale to 0,0 -> maxx,maxy */
48 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
49 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
50 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
52 static void move_to(struct graphics_context *gc, double x, double y)
54 cairo_move_to(gc->cr, SCALE(gc, x, y));
57 static void line_to(struct graphics_context *gc, double x, double y)
59 cairo_line_to(gc->cr, SCALE(gc, x, y));
62 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
65 /* Black is white and white is black */
72 cairo_set_source_rgba(gc->cr, r, g, b, a);
75 static void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
77 set_source_rgba(gc, r, g, b, 1);
80 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
83 * When showing dive profiles, we scale things to the
84 * current dive. However, we don't scale past less than
85 * 30 minutes or 90 ft, just so that small dives show
87 * we also need to add 180 seconds at the end so the min/max
90 static int get_maxtime(struct plot_info *pi)
92 int seconds = pi->maxtime;
93 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
94 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
97 static int get_maxdepth(struct plot_info *pi)
99 unsigned mm = pi->maxdepth;
100 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
101 return MAX(30000, ROUND_UP(mm+3000, 10000));
108 } text_render_options_t;
111 #define CENTER (-0.5)
118 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
119 double x, double y, const char *fmt, ...)
121 cairo_t *cr = gc->cr;
122 cairo_font_extents_t fe;
123 cairo_text_extents_t extents;
129 vsnprintf(buffer, sizeof(buffer), fmt, args);
132 cairo_set_font_size(cr, tro->size);
133 cairo_font_extents(cr, &fe);
134 cairo_text_extents(cr, buffer, &extents);
135 dx = tro->hpos * extents.width + extents.x_bearing;
136 dy = tro->vpos * extents.height + fe.descent;
139 cairo_rel_move_to(cr, dx, dy);
141 cairo_text_path(cr, buffer);
142 set_source_rgb(gc, 0, 0, 0);
146 cairo_rel_move_to(cr, dx, dy);
148 set_source_rgb(gc, tro->r, tro->g, tro->b);
149 cairo_show_text(cr, buffer);
152 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
154 int sec = entry->sec;
155 depth_t depth = { entry->val };
159 switch (output_units.length) {
161 d = depth.mm / 1000.0;
169 plot_text(gc, tro, sec, depth.mm, fmt, d);
172 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
174 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
175 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
178 for (i = 0; i < pi->nr; i++) {
179 struct plot_data *entry = pi->entry + i;
181 if (entry->val < 2000)
184 if (entry == entry->max[2])
185 render_depth_sample(gc, entry, &deep);
187 if (entry == entry->min[2])
188 render_depth_sample(gc, entry, &shallow);
192 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
194 int maxtime, maxdepth;
196 /* Get plot scaling limits */
197 maxtime = get_maxtime(pi);
198 maxdepth = get_maxdepth(pi);
200 gc->leftx = 0; gc->rightx = maxtime;
201 gc->topy = 0; gc->bottomy = maxdepth;
203 plot_text_samples(gc, pi);
206 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
209 struct plot_data *entry = pi->entry;
211 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
212 move_to(gc, entry->sec, entry->smoothed);
213 for (i = 1; i < pi->nr; i++) {
215 line_to(gc, entry->sec, entry->smoothed);
217 cairo_stroke(gc->cr);
220 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
224 struct plot_data *entry = pi->entry;
226 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
227 move_to(gc, entry->sec, entry->min[index]->val);
228 for (i = 1; i < pi->nr; i++) {
230 line_to(gc, entry->sec, entry->min[index]->val);
232 for (i = 1; i < pi->nr; i++) {
233 line_to(gc, entry->sec, entry->max[index]->val);
236 cairo_close_path(gc->cr);
240 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
244 plot_minmax_profile_minute(gc, pi, 2, 0.1);
245 plot_minmax_profile_minute(gc, pi, 1, 0.1);
246 plot_minmax_profile_minute(gc, pi, 0, 0.1);
249 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
252 cairo_t *cr = gc->cr;
253 int ends, sec, depth;
256 struct plot_data *entry;
257 int maxtime, maxdepth, marker;
259 /* Get plot scaling limits */
260 maxtime = get_maxtime(pi);
261 maxdepth = get_maxdepth(pi);
263 /* Time markers: every 5 min */
264 gc->leftx = 0; gc->rightx = maxtime;
265 gc->topy = 0; gc->bottomy = 1.0;
266 for (i = 5*60; i < maxtime; i += 5*60) {
271 /* Depth markers: every 30 ft or 10 m*/
272 gc->leftx = 0; gc->rightx = 1.0;
273 gc->topy = 0; gc->bottomy = maxdepth;
274 switch (output_units.length) {
275 case METERS: marker = 10000; break;
276 case FEET: marker = 9144; break; /* 30 ft */
279 set_source_rgba(gc, 1, 1, 1, 0.5);
280 for (i = marker; i < maxdepth; i += marker) {
286 /* Show mean depth */
287 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
288 move_to(gc, 0, pi->meandepth);
289 line_to(gc, 1, pi->meandepth);
292 gc->leftx = 0; gc->rightx = maxtime;
294 plot_smoothed_profile(gc, pi);
295 plot_minmax_profile(gc, pi);
298 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
299 secs = (int *) malloc(sizeof(int) * pi->nr);
300 depths = (int *) malloc(sizeof(int) * pi->nr);
301 secs[0] = entry->sec;
302 depths[0] = entry->val;
303 for (i = 1; i < pi->nr; i++) {
306 if (sec <= maxtime || entry->val > 0) {
307 /* we want to draw the segments in different colors
308 * representing the vertical velocity, so we need to
309 * chop this into short segments */
310 rgb_t color = rgb[entry->velocity];
312 set_source_rgb(gc, color.r, color.g, color.b);
313 move_to(gc, secs[i-1], depths[i-1]);
314 line_to(gc, sec, depth);
321 move_to(gc, secs[ends], depths[ends]);
322 gc->topy = 0; gc->bottomy = 1.0;
323 line_to(gc, secs[ends], 0);
324 line_to(gc, secs[0], 0);
325 cairo_close_path(cr);
326 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
328 /* now do it again for the neat fill */
329 gc->topy = 0; gc->bottomy = maxdepth;
330 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
331 move_to(gc, secs[0], depths[0]);
332 for (i = 1; i <= ends; i++) {
333 line_to(gc, secs[i],depths[i]);
335 gc->topy = 0; gc->bottomy = 1.0;
336 line_to(gc, secs[ends], 0);
337 line_to(gc, secs[0], 0);
338 cairo_close_path(gc->cr);
342 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
344 int maxtime, mintemp, maxtemp, delta;
346 /* Get plot scaling limits */
347 maxtime = get_maxtime(pi);
348 mintemp = pi->mintemp;
349 maxtemp = pi->maxtemp;
351 gc->leftx = 0; gc->rightx = maxtime;
352 /* Show temperatures in roughly the lower third, but make sure the scale
353 is at least somewhat reasonable */
354 delta = maxtemp - mintemp;
355 if (delta > 3000) { /* more than 3K in fluctuation */
356 gc->topy = maxtemp + delta*2;
357 gc->bottomy = mintemp - delta/2;
359 gc->topy = maxtemp + 1500 + delta*2;
360 gc->bottomy = mintemp - delta/2;
363 return maxtemp > mintemp;
366 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
370 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
371 temperature_t temperature = { mkelvin };
373 if (output_units.temperature == FAHRENHEIT) {
374 deg = to_F(temperature);
377 deg = to_C(temperature);
380 plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
383 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
386 int last = 0, sec = 0;
387 int last_temperature = 0, last_printed_temp = 0;
389 if (!setup_temperature_limits(gc, pi))
392 for (i = 0; i < pi->nr; i++) {
393 struct plot_data *entry = pi->entry+i;
394 int mkelvin = entry->temperature;
398 last_temperature = mkelvin;
400 if (sec < last + 300)
403 plot_single_temp_text(gc,sec,mkelvin);
404 last_printed_temp = mkelvin;
406 /* it would be nice to print the end temperature, if it's different */
407 if (abs(last_temperature - last_printed_temp) > 500)
408 plot_single_temp_text(gc, sec, last_temperature);
411 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
414 cairo_t *cr = gc->cr;
417 if (!setup_temperature_limits(gc, pi))
420 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
421 for (i = 0; i < pi->nr; i++) {
422 struct plot_data *entry = pi->entry + i;
423 int mkelvin = entry->temperature;
424 int sec = entry->sec;
431 line_to(gc, sec, mkelvin);
433 move_to(gc, sec, mkelvin);
439 /* gets both the actual start and end pressure as well as the scaling factors */
440 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
443 gc->rightx = get_maxtime(pi);
445 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
446 return pi->maxpressure != 0;
449 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
453 if (!get_cylinder_pressure_range(gc, pi))
456 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
458 move_to(gc, 0, pi->maxpressure);
459 for (i = 1; i < pi->nr; i++) {
461 struct plot_data *entry = pi->entry + i;
463 mbar = entry->pressure;
466 line_to(gc, entry->sec, mbar);
468 line_to(gc, pi->maxtime, pi->minpressure);
469 cairo_stroke(gc->cr);
472 static int mbar_to_PSI(int mbar)
474 pressure_t p = {mbar};
478 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
480 if (get_cylinder_pressure_range(gc, pi)) {
482 const char *unit = "bar";
484 switch (output_units.pressure) {
486 start = pi->maxpressure * 100;
487 end = pi->minpressure * 100;
491 start = (pi->maxpressure + 500) / 1000;
492 end = (pi->minpressure + 500) / 1000;
496 start = mbar_to_PSI(pi->maxpressure);
497 end = mbar_to_PSI(pi->minpressure);
502 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
503 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
504 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
509 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
511 struct plot_data *p = entry;
512 int time = entry->sec;
513 int seconds = 90*(index+1);
514 struct plot_data *min, *max;
517 /* Go back 'seconds' in time */
519 if (p[-1].sec < time - seconds)
524 /* Then go forward until we hit an entry past the time */
530 if (p->sec > time + seconds)
539 entry->min[index] = min;
540 entry->max[index] = max;
541 entry->avg[index] = (avg + nr/2) / nr;
544 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
546 analyze_plot_info_minmax_minute(entry, first, last, 0);
547 analyze_plot_info_minmax_minute(entry, first, last, 1);
548 analyze_plot_info_minmax_minute(entry, first, last, 2);
551 static velocity_t velocity(int speed)
555 if (speed < -304) /* ascent faster than -60ft/min */
557 else if (speed < -152) /* above -30ft/min */
559 else if (speed < -76) /* -15ft/min */
561 else if (speed < -25) /* -5ft/min */
563 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
564 for descent are usually about 2x ascent rate; still, we want
565 stable to mean stable */
567 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
569 else if (speed < 304) /* up to 60ft/min is moderate */
571 else if (speed < 507) /* up to 100ft/min is fast */
573 else /* more than that is just crazy - you'll blow your ears out */
578 static struct plot_info *analyze_plot_info(struct plot_info *pi)
583 /* Do pressure min/max based on the non-surface data */
584 for (i = 0; i < nr; i++) {
585 struct plot_data *entry = pi->entry+i;
586 int pressure = entry->pressure;
587 int temperature = entry->temperature;
590 if (!pi->minpressure || pressure < pi->minpressure)
591 pi->minpressure = pressure;
592 if (pressure > pi->maxpressure)
593 pi->maxpressure = pressure;
597 if (!pi->mintemp || temperature < pi->mintemp)
598 pi->mintemp = temperature;
599 if (temperature > pi->maxtemp)
600 pi->maxtemp = temperature;
604 /* Smoothing function: 5-point triangular smooth */
605 for (i = 2; i < nr-1; i++) {
606 struct plot_data *entry = pi->entry+i;
610 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
611 entry->smoothed = (val+4) / 9;
613 /* vertical velocity in mm/sec */
614 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
615 if (entry[0].sec - entry[-1].sec) {
616 entry->velocity = velocity((entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec));
617 /* if our samples are short and we aren't too FAST*/
618 if (entry[0].sec - entry[-1].sec < 30 && entry->velocity < FAST) {
620 while (pi->entry <= entry-past && entry[0].sec - entry[past].sec < 30)
622 entry->velocity = velocity((entry[0].val - entry[past].val) /
623 (entry[0].sec - entry[past].sec));
626 entry->velocity = STABLE;
629 /* One-, two- and three-minute minmax data */
630 for (i = 0; i < nr; i++) {
631 struct plot_data *entry = pi->entry +i;
632 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
639 * Create a plot-info with smoothing and ranged min/max
641 * This also makes sure that we have extra empty events on both
642 * sides, so that you can do end-points without having to worry
645 static struct plot_info *create_plot_info(struct dive *dive)
647 int lastdepth, lastindex;
648 int i, nr = dive->samples + 4, sec;
649 size_t alloc_size = plot_info_size(nr);
650 struct plot_info *pi;
652 pi = malloc(alloc_size);
655 memset(pi, 0, alloc_size);
660 for (i = 0; i < dive->samples; i++) {
662 struct sample *sample = dive->sample+i;
663 struct plot_data *entry = pi->entry + i + 2;
665 sec = entry->sec = sample->time.seconds;
666 depth = entry->val = sample->depth.mm;
667 entry->pressure = sample->cylinderpressure.mbar;
668 entry->temperature = sample->temperature.mkelvin;
670 if (depth || lastdepth)
674 if (depth > pi->maxdepth)
675 pi->maxdepth = depth;
679 /* Fill in the last two entries with empty values but valid times */
680 i = dive->samples + 2;
681 pi->entry[i].sec = sec + 20;
682 pi->entry[i+1].sec = sec + 40;
684 pi->nr = lastindex+1;
685 pi->maxtime = pi->entry[lastindex].sec;
687 pi->minpressure = dive->cylinder[0].end.mbar;
688 pi->maxpressure = dive->cylinder[0].start.mbar;
690 pi->meandepth = dive->meandepth.mm;
692 return analyze_plot_info(pi);
695 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
698 struct plot_info *pi = create_plot_info(dive);
702 cairo_translate(gc->cr, topx, topy);
703 cairo_set_line_width(gc->cr, 2);
704 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
705 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
708 * We can use "cairo_translate()" because that doesn't
709 * scale line width etc. But the actual scaling we need
710 * do set up ourselves..
714 gc->maxx = (w - 2*topx);
715 gc->maxy = (h - 2*topy);
717 /* Temperature profile */
718 plot_temperature_profile(gc, pi);
720 /* Cylinder pressure plot */
721 plot_cylinder_pressure(gc, pi);
724 plot_depth_profile(gc, pi);
726 /* Text on top of all graphs.. */
727 plot_temperature_text(gc, pi);
728 plot_depth_text(gc, pi);
729 plot_cylinder_pressure_text(gc, pi);
731 /* Bounding box last */
732 gc->leftx = 0; gc->rightx = 1.0;
733 gc->topy = 0; gc->bottomy = 1.0;
735 set_source_rgb(gc, 1, 1, 1);
740 cairo_close_path(gc->cr);
741 cairo_stroke(gc->cr);
745 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
747 struct dive *dive = current_dive;
748 struct graphics_context gc = { .printer = 0 };
751 w = widget->allocation.width;
752 h = widget->allocation.height;
754 gc.cr = gdk_cairo_create(widget->window);
755 set_source_rgb(&gc, 0, 0, 0);
759 plot(&gc, w, h, dive);
761 cairo_destroy(gc.cr);
766 GtkWidget *dive_profile_widget(void)
770 da = gtk_drawing_area_new();
771 gtk_widget_set_size_request(da, 350, 250);
772 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);