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);
471 static int mbar_to_PSI(int mbar)
473 pressure_t p = {mbar};
477 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
479 if (get_cylinder_pressure_range(gc, pi)) {
481 const char *unit = "bar";
483 switch (output_units.pressure) {
485 start = pi->maxpressure * 100;
486 end = pi->minpressure * 100;
490 start = (pi->maxpressure + 500) / 1000;
491 end = (pi->minpressure + 500) / 1000;
495 start = mbar_to_PSI(pi->maxpressure);
496 end = mbar_to_PSI(pi->minpressure);
501 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
502 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
503 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
508 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
510 struct plot_data *p = entry;
511 int time = entry->sec;
512 int seconds = 90*(index+1);
513 struct plot_data *min, *max;
516 /* Go back 'seconds' in time */
518 if (p[-1].sec < time - seconds)
523 /* Then go forward until we hit an entry past the time */
529 if (p->sec > time + seconds)
538 entry->min[index] = min;
539 entry->max[index] = max;
540 entry->avg[index] = (avg + nr/2) / nr;
543 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
545 analyze_plot_info_minmax_minute(entry, first, last, 0);
546 analyze_plot_info_minmax_minute(entry, first, last, 1);
547 analyze_plot_info_minmax_minute(entry, first, last, 2);
550 static struct plot_info *analyze_plot_info(struct plot_info *pi)
555 /* Do pressure min/max based on the non-surface data */
556 for (i = 0; i < nr; i++) {
557 struct plot_data *entry = pi->entry+i;
558 int pressure = entry->pressure;
559 int temperature = entry->temperature;
562 if (!pi->minpressure || pressure < pi->minpressure)
563 pi->minpressure = pressure;
564 if (pressure > pi->maxpressure)
565 pi->maxpressure = pressure;
569 if (!pi->mintemp || temperature < pi->mintemp)
570 pi->mintemp = temperature;
571 if (temperature > pi->maxtemp)
572 pi->maxtemp = temperature;
576 /* Smoothing function: 5-point triangular smooth */
577 for (i = 2; i < nr-1; i++) {
578 struct plot_data *entry = pi->entry+i;
582 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
583 entry->smoothed = (val+4) / 9;
585 /* vertical velocity in mm/sec */
586 if (entry[0].sec - entry[-1].sec) {
587 val = (entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec);
588 if (val < -304) /* ascent faster than -60ft/min */
589 entry->velocity = CRAZY;
590 else if (val < -152) /* above -30ft/min */
591 entry->velocity = FAST;
592 else if (val < -76) /* -15ft/min */
593 entry->velocity = MODERATE;
594 else if (val < -25) /* -5ft/min */
595 entry->velocity = SLOW;
596 else if (val < 25) /* very hard to find data, but it appears that the recommendations
597 for descent are usually about 2x ascent rate; still, we want
598 stable to mean stable */
599 entry->velocity = STABLE;
600 else if (val < 152) /* between 5 and 30ft/min is considered slow */
601 entry->velocity = SLOW;
602 else if (val < 304) /* up to 60ft/min is moderate */
603 entry->velocity = MODERATE;
604 else if (val < 507) /* up to 100ft/min is fast */
605 entry->velocity = FAST;
606 else /* more than that is just crazy - you'll blow your ears out */
607 entry->velocity = CRAZY;
609 entry->velocity = STABLE;
612 /* One-, two- and three-minute minmax data */
613 for (i = 0; i < nr; i++) {
614 struct plot_data *entry = pi->entry +i;
615 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
622 * Create a plot-info with smoothing and ranged min/max
624 * This also makes sure that we have extra empty events on both
625 * sides, so that you can do end-points without having to worry
628 static struct plot_info *create_plot_info(struct dive *dive)
630 int lastdepth, lastindex;
631 int i, nr = dive->samples + 4, sec;
632 size_t alloc_size = plot_info_size(nr);
633 struct plot_info *pi;
635 pi = malloc(alloc_size);
638 memset(pi, 0, alloc_size);
643 for (i = 0; i < dive->samples; i++) {
645 struct sample *sample = dive->sample+i;
646 struct plot_data *entry = pi->entry + i + 2;
648 sec = entry->sec = sample->time.seconds;
649 depth = entry->val = sample->depth.mm;
650 entry->pressure = sample->cylinderpressure.mbar;
651 entry->temperature = sample->temperature.mkelvin;
653 if (depth || lastdepth)
657 if (depth > pi->maxdepth)
658 pi->maxdepth = depth;
662 /* Fill in the last two entries with empty values but valid times */
663 i = dive->samples + 2;
664 pi->entry[i].sec = sec + 20;
665 pi->entry[i+1].sec = sec + 40;
667 pi->nr = lastindex+1;
668 pi->maxtime = pi->entry[lastindex].sec;
670 pi->minpressure = dive->cylinder[0].end.mbar;
671 pi->maxpressure = dive->cylinder[0].start.mbar;
673 pi->meandepth = dive->meandepth.mm;
675 return analyze_plot_info(pi);
678 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
681 struct plot_info *pi = create_plot_info(dive);
685 cairo_translate(gc->cr, topx, topy);
686 cairo_set_line_width(gc->cr, 2);
687 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
688 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
691 * We can use "cairo_translate()" because that doesn't
692 * scale line width etc. But the actual scaling we need
693 * do set up ourselves..
697 gc->maxx = (w - 2*topx);
698 gc->maxy = (h - 2*topy);
700 /* Temperature profile */
701 plot_temperature_profile(gc, pi);
703 /* Cylinder pressure plot */
704 plot_cylinder_pressure(gc, pi);
707 plot_depth_profile(gc, pi);
709 /* Text on top of all graphs.. */
710 plot_temperature_text(gc, pi);
711 plot_depth_text(gc, pi);
712 plot_cylinder_pressure_text(gc, pi);
714 /* Bounding box last */
715 gc->leftx = 0; gc->rightx = 1.0;
716 gc->topy = 0; gc->bottomy = 1.0;
718 set_source_rgb(gc, 1, 1, 1);
723 cairo_close_path(gc->cr);
724 cairo_stroke(gc->cr);
728 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
730 struct dive *dive = current_dive;
731 struct graphics_context gc = { .printer = 0 };
734 w = widget->allocation.width;
735 h = widget->allocation.height;
737 gc.cr = gdk_cairo_create(widget->window);
738 set_source_rgb(&gc, 0, 0, 0);
742 plot(&gc, w, h, dive);
744 cairo_destroy(gc.cr);
749 GtkWidget *dive_profile_widget(void)
753 da = gtk_drawing_area_new();
754 gtk_widget_set_size_request(da, 350, 250);
755 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);