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;
24 int minpressure, maxpressure;
28 int pressure, temperature;
33 struct plot_data *min[3];
34 struct plot_data *max[3];
39 /* convert velocity to colors */
40 typedef struct { double r, g, b; } rgb_t;
41 static const rgb_t rgb[] = {
42 [STABLE] = {0.0, 0.4, 0.0},
43 [SLOW] = {0.4, 0.8, 0.0},
44 [MODERATE] = {0.8, 0.8, 0.0},
45 [FAST] = {0.8, 0.5, 0.0},
46 [CRAZY] = {1.0, 0.0, 0.0},
49 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
51 /* Scale to 0,0 -> maxx,maxy */
52 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
53 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
54 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
56 static void move_to(struct graphics_context *gc, double x, double y)
58 cairo_move_to(gc->cr, SCALE(gc, x, y));
61 static void line_to(struct graphics_context *gc, double x, double y)
63 cairo_line_to(gc->cr, SCALE(gc, x, y));
66 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
69 * For printers, we still honor 'a', but ignore colors
70 * for now. Black is white and white is black
79 cairo_set_source_rgba(gc->cr, r, g, b, a);
82 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
84 set_source_rgba(gc, r, g, b, 1);
87 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
90 * When showing dive profiles, we scale things to the
91 * current dive. However, we don't scale past less than
92 * 30 minutes or 90 ft, just so that small dives show
94 * we also need to add 180 seconds at the end so the min/max
97 static int get_maxtime(struct plot_info *pi)
99 int seconds = pi->maxtime;
100 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
101 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
104 static int get_maxdepth(struct plot_info *pi)
106 unsigned mm = pi->maxdepth;
107 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
108 return MAX(30000, ROUND_UP(mm+3000, 10000));
115 } text_render_options_t;
118 #define CENTER (-0.5)
125 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
126 double x, double y, const char *fmt, ...)
128 cairo_t *cr = gc->cr;
129 cairo_font_extents_t fe;
130 cairo_text_extents_t extents;
136 vsnprintf(buffer, sizeof(buffer), fmt, args);
139 cairo_set_font_size(cr, tro->size);
140 cairo_font_extents(cr, &fe);
141 cairo_text_extents(cr, buffer, &extents);
142 dx = tro->hpos * extents.width + extents.x_bearing;
143 dy = tro->vpos * extents.height + fe.descent;
146 cairo_rel_move_to(cr, dx, dy);
148 cairo_text_path(cr, buffer);
149 set_source_rgb(gc, 0, 0, 0);
153 cairo_rel_move_to(cr, dx, dy);
155 set_source_rgb(gc, tro->r, tro->g, tro->b);
156 cairo_show_text(cr, buffer);
159 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
163 for (i = 0; i < pi->nr; i++) {
164 struct plot_data *data = pi->entry + i;
165 if (event->time.seconds < data->sec)
169 plot_text(gc, tro, event->time.seconds, depth, "%s", event->name);
172 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
174 static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
175 struct event *event = dive->events;
181 plot_one_event(gc, pi, event, &tro);
186 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
188 int sec = entry->sec, decimals;
191 d = get_depth_units(entry->val, &decimals, NULL);
193 plot_text(gc, tro, sec, entry->val, "%.*f", decimals, d);
196 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
198 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
199 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
202 for (i = 0; i < pi->nr; i++) {
203 struct plot_data *entry = pi->entry + i;
205 if (entry->val < 2000)
208 if (entry == entry->max[2])
209 render_depth_sample(gc, entry, &deep);
211 if (entry == entry->min[2])
212 render_depth_sample(gc, entry, &shallow);
216 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
218 int maxtime, maxdepth;
220 /* Get plot scaling limits */
221 maxtime = get_maxtime(pi);
222 maxdepth = get_maxdepth(pi);
224 gc->leftx = 0; gc->rightx = maxtime;
225 gc->topy = 0; gc->bottomy = maxdepth;
227 plot_text_samples(gc, pi);
230 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
233 struct plot_data *entry = pi->entry;
235 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
236 move_to(gc, entry->sec, entry->smoothed);
237 for (i = 1; i < pi->nr; i++) {
239 line_to(gc, entry->sec, entry->smoothed);
241 cairo_stroke(gc->cr);
244 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
248 struct plot_data *entry = pi->entry;
250 set_source_rgba(gc, 1, 0.2, 1, a);
251 move_to(gc, entry->sec, entry->min[index]->val);
252 for (i = 1; i < pi->nr; i++) {
254 line_to(gc, entry->sec, entry->min[index]->val);
256 for (i = 1; i < pi->nr; i++) {
257 line_to(gc, entry->sec, entry->max[index]->val);
260 cairo_close_path(gc->cr);
264 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
268 plot_minmax_profile_minute(gc, pi, 2, 0.1);
269 plot_minmax_profile_minute(gc, pi, 1, 0.1);
270 plot_minmax_profile_minute(gc, pi, 0, 0.1);
273 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
276 cairo_t *cr = gc->cr;
278 struct plot_data *entry;
279 int maxtime, maxdepth, marker;
281 /* Get plot scaling limits */
282 maxtime = get_maxtime(pi);
283 maxdepth = get_maxdepth(pi);
285 /* Time markers: every 5 min */
286 gc->leftx = 0; gc->rightx = maxtime;
287 gc->topy = 0; gc->bottomy = 1.0;
288 for (i = 5*60; i < maxtime; i += 5*60) {
293 /* Depth markers: every 30 ft or 10 m*/
294 gc->leftx = 0; gc->rightx = 1.0;
295 gc->topy = 0; gc->bottomy = maxdepth;
296 switch (output_units.length) {
297 case METERS: marker = 10000; break;
298 case FEET: marker = 9144; break; /* 30 ft */
301 set_source_rgba(gc, 1, 1, 1, 0.5);
302 for (i = marker; i < maxdepth; i += marker) {
308 /* Show mean depth */
310 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
311 move_to(gc, 0, pi->meandepth);
312 line_to(gc, 1, pi->meandepth);
316 gc->leftx = 0; gc->rightx = maxtime;
319 * These are good for debugging text placement etc,
320 * but not for actual display..
323 plot_smoothed_profile(gc, pi);
324 plot_minmax_profile(gc, pi);
327 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
329 /* Do the depth profile for the neat fill */
330 gc->topy = 0; gc->bottomy = maxdepth;
331 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
335 for (i = 0; i < pi->nr; i++, entry++)
336 line_to(gc, entry->sec, entry->val);
337 cairo_close_path(gc->cr);
339 set_source_rgba(gc, 1, 1, 1, 0.2);
340 cairo_fill_preserve(cr);
341 set_source_rgb(gc, 1, 1, 1);
347 /* Now do it again for the velocity colors */
349 for (i = 1; i < pi->nr; i++) {
352 /* we want to draw the segments in different colors
353 * representing the vertical velocity, so we need to
354 * chop this into short segments */
355 rgb_t color = rgb[entry->velocity];
357 set_source_rgb(gc, color.r, color.g, color.b);
358 move_to(gc, entry[-1].sec, entry[-1].val);
359 line_to(gc, sec, depth);
364 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
366 int maxtime, mintemp, maxtemp, delta;
368 /* Get plot scaling limits */
369 maxtime = get_maxtime(pi);
370 mintemp = pi->mintemp;
371 maxtemp = pi->maxtemp;
373 gc->leftx = 0; gc->rightx = maxtime;
374 /* Show temperatures in roughly the lower third, but make sure the scale
375 is at least somewhat reasonable */
376 delta = maxtemp - mintemp;
377 if (delta > 3000) { /* more than 3K in fluctuation */
378 gc->topy = maxtemp + delta*2;
379 gc->bottomy = mintemp - delta/2;
381 gc->topy = maxtemp + 1500 + delta*2;
382 gc->bottomy = mintemp - delta/2;
385 return maxtemp > mintemp;
388 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
392 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
393 temperature_t temperature = { mkelvin };
395 if (output_units.temperature == FAHRENHEIT) {
396 deg = to_F(temperature);
397 unit = UTF8_DEGREE "F";
399 deg = to_C(temperature);
400 unit = UTF8_DEGREE "C";
402 plot_text(gc, &tro, sec, temperature.mkelvin, "%d%s", deg, unit);
405 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
408 int last = 0, sec = 0;
409 int last_temperature = 0, last_printed_temp = 0;
411 if (!setup_temperature_limits(gc, pi))
414 for (i = 0; i < pi->nr; i++) {
415 struct plot_data *entry = pi->entry+i;
416 int mkelvin = entry->temperature;
420 last_temperature = mkelvin;
422 if (sec < last + 300)
425 plot_single_temp_text(gc,sec,mkelvin);
426 last_printed_temp = mkelvin;
428 /* it would be nice to print the end temperature, if it's different */
429 if (abs(last_temperature - last_printed_temp) > 500)
430 plot_single_temp_text(gc, sec, last_temperature);
433 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
436 cairo_t *cr = gc->cr;
439 if (!setup_temperature_limits(gc, pi))
442 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
443 for (i = 0; i < pi->nr; i++) {
444 struct plot_data *entry = pi->entry + i;
445 int mkelvin = entry->temperature;
446 int sec = entry->sec;
453 line_to(gc, sec, mkelvin);
455 move_to(gc, sec, mkelvin);
461 /* gets both the actual start and end pressure as well as the scaling factors */
462 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
465 gc->rightx = get_maxtime(pi);
467 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
468 return pi->maxpressure != 0;
471 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
475 if (!get_cylinder_pressure_range(gc, pi))
478 set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
480 move_to(gc, 0, pi->maxpressure);
481 for (i = 1; i < pi->nr; i++) {
483 struct plot_data *entry = pi->entry + i;
485 mbar = entry->pressure;
488 line_to(gc, entry->sec, mbar);
490 line_to(gc, pi->maxtime, pi->minpressure);
491 cairo_stroke(gc->cr);
494 static int mbar_to_PSI(int mbar)
496 pressure_t p = {mbar};
500 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
502 if (get_cylinder_pressure_range(gc, pi)) {
504 const char *unit = "bar";
506 switch (output_units.pressure) {
508 start = pi->maxpressure * 100;
509 end = pi->minpressure * 100;
513 start = (pi->maxpressure + 500) / 1000;
514 end = (pi->minpressure + 500) / 1000;
518 start = mbar_to_PSI(pi->maxpressure);
519 end = mbar_to_PSI(pi->minpressure);
524 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
525 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
526 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
531 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
533 struct plot_data *p = entry;
534 int time = entry->sec;
535 int seconds = 90*(index+1);
536 struct plot_data *min, *max;
539 /* Go back 'seconds' in time */
541 if (p[-1].sec < time - seconds)
546 /* Then go forward until we hit an entry past the time */
552 if (p->sec > time + seconds)
561 entry->min[index] = min;
562 entry->max[index] = max;
563 entry->avg[index] = (avg + nr/2) / nr;
566 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
568 analyze_plot_info_minmax_minute(entry, first, last, 0);
569 analyze_plot_info_minmax_minute(entry, first, last, 1);
570 analyze_plot_info_minmax_minute(entry, first, last, 2);
573 static velocity_t velocity(int speed)
577 if (speed < -304) /* ascent faster than -60ft/min */
579 else if (speed < -152) /* above -30ft/min */
581 else if (speed < -76) /* -15ft/min */
583 else if (speed < -25) /* -5ft/min */
585 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
586 for descent are usually about 2x ascent rate; still, we want
587 stable to mean stable */
589 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
591 else if (speed < 304) /* up to 60ft/min is moderate */
593 else if (speed < 507) /* up to 100ft/min is fast */
595 else /* more than that is just crazy - you'll blow your ears out */
600 static struct plot_info *analyze_plot_info(struct plot_info *pi)
605 /* Do pressure min/max based on the non-surface data */
606 for (i = 0; i < nr; i++) {
607 struct plot_data *entry = pi->entry+i;
608 int pressure = entry->pressure;
609 int temperature = entry->temperature;
612 if (!pi->minpressure || pressure < pi->minpressure)
613 pi->minpressure = pressure;
614 if (pressure > pi->maxpressure)
615 pi->maxpressure = pressure;
619 if (!pi->mintemp || temperature < pi->mintemp)
620 pi->mintemp = temperature;
621 if (temperature > pi->maxtemp)
622 pi->maxtemp = temperature;
626 /* Smoothing function: 5-point triangular smooth */
627 for (i = 2; i < nr; i++) {
628 struct plot_data *entry = pi->entry+i;
632 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
633 entry->smoothed = (val+4) / 9;
635 /* vertical velocity in mm/sec */
636 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
637 if (entry[0].sec - entry[-1].sec) {
638 entry->velocity = velocity((entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec));
639 /* if our samples are short and we aren't too FAST*/
640 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
642 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
644 entry->velocity = velocity((entry[0].val - entry[past].val) /
645 (entry[0].sec - entry[past].sec));
648 entry->velocity = STABLE;
651 /* One-, two- and three-minute minmax data */
652 for (i = 0; i < nr; i++) {
653 struct plot_data *entry = pi->entry +i;
654 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
661 * Create a plot-info with smoothing and ranged min/max
663 * This also makes sure that we have extra empty events on both
664 * sides, so that you can do end-points without having to worry
667 static struct plot_info *create_plot_info(struct dive *dive)
669 int lastdepth, lastindex;
670 int i, nr = dive->samples + 4, sec;
671 size_t alloc_size = plot_info_size(nr);
672 struct plot_info *pi;
674 pi = malloc(alloc_size);
677 memset(pi, 0, alloc_size);
682 for (i = 0; i < dive->samples; i++) {
684 struct sample *sample = dive->sample+i;
685 struct plot_data *entry = pi->entry + i + 2;
687 sec = entry->sec = sample->time.seconds;
688 depth = entry->val = sample->depth.mm;
689 entry->pressure = sample->cylinderpressure.mbar;
690 entry->temperature = sample->temperature.mkelvin;
692 if (depth || lastdepth)
696 if (depth > pi->maxdepth)
697 pi->maxdepth = depth;
701 /* Fill in the last two entries with empty values but valid times */
702 i = dive->samples + 2;
703 pi->entry[i].sec = sec + 20;
704 pi->entry[i+1].sec = sec + 40;
706 pi->nr = lastindex+1;
707 pi->maxtime = pi->entry[lastindex].sec;
709 pi->minpressure = dive->cylinder[0].end.mbar;
710 pi->maxpressure = dive->cylinder[0].start.mbar;
712 pi->meandepth = dive->meandepth.mm;
714 return analyze_plot_info(pi);
717 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
720 struct plot_info *pi = create_plot_info(dive);
724 cairo_translate(gc->cr, topx, topy);
725 cairo_set_line_width(gc->cr, 2);
726 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
727 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
730 * We can use "cairo_translate()" because that doesn't
731 * scale line width etc. But the actual scaling we need
732 * do set up ourselves..
736 gc->maxx = (w - 2*topx);
737 gc->maxy = (h - 2*topy);
739 /* Temperature profile */
740 plot_temperature_profile(gc, pi);
742 /* Cylinder pressure plot */
743 plot_cylinder_pressure(gc, pi);
746 plot_depth_profile(gc, pi);
747 plot_events(gc, pi, dive);
749 /* Text on top of all graphs.. */
750 plot_temperature_text(gc, pi);
751 plot_depth_text(gc, pi);
752 plot_cylinder_pressure_text(gc, pi);
754 /* Bounding box last */
755 gc->leftx = 0; gc->rightx = 1.0;
756 gc->topy = 0; gc->bottomy = 1.0;
758 set_source_rgb(gc, 1, 1, 1);
763 cairo_close_path(gc->cr);
764 cairo_stroke(gc->cr);