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;
25 int endpressure; /* start pressure better be max pressure */
28 unsigned int same_cylinder:1;
30 int pressure, temperature;
35 struct plot_data *min[3];
36 struct plot_data *max[3];
41 /* convert velocity to colors */
42 typedef struct { double r, g, b; } rgb_t;
43 static const rgb_t rgb[] = {
44 [STABLE] = {0.0, 0.4, 0.0},
45 [SLOW] = {0.4, 0.8, 0.0},
46 [MODERATE] = {0.8, 0.8, 0.0},
47 [FAST] = {0.8, 0.5, 0.0},
48 [CRAZY] = {1.0, 0.0, 0.0},
51 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
53 /* Scale to 0,0 -> maxx,maxy */
54 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
55 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
56 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
58 static void move_to(struct graphics_context *gc, double x, double y)
60 cairo_move_to(gc->cr, SCALE(gc, x, y));
63 static void line_to(struct graphics_context *gc, double x, double y)
65 cairo_line_to(gc->cr, SCALE(gc, x, y));
68 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
71 * For printers, we still honor 'a', but ignore colors
72 * for now. Black is white and white is black
81 cairo_set_source_rgba(gc->cr, r, g, b, a);
84 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
86 set_source_rgba(gc, r, g, b, 1);
89 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
92 * When showing dive profiles, we scale things to the
93 * current dive. However, we don't scale past less than
94 * 30 minutes or 90 ft, just so that small dives show
96 * we also need to add 180 seconds at the end so the min/max
99 static int get_maxtime(struct plot_info *pi)
101 int seconds = pi->maxtime;
102 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
103 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
106 static int get_maxdepth(struct plot_info *pi)
108 unsigned mm = pi->maxdepth;
109 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
110 return MAX(30000, ROUND_UP(mm+3000, 10000));
117 } text_render_options_t;
120 #define CENTER (-0.5)
127 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
128 double x, double y, const char *fmt, ...)
130 cairo_t *cr = gc->cr;
131 cairo_font_extents_t fe;
132 cairo_text_extents_t extents;
138 vsnprintf(buffer, sizeof(buffer), fmt, args);
141 cairo_set_font_size(cr, tro->size);
142 cairo_font_extents(cr, &fe);
143 cairo_text_extents(cr, buffer, &extents);
144 dx = tro->hpos * extents.width + extents.x_bearing;
145 dy = tro->vpos * extents.height + fe.descent;
148 cairo_rel_move_to(cr, dx, dy);
150 cairo_text_path(cr, buffer);
151 set_source_rgb(gc, 0, 0, 0);
155 cairo_rel_move_to(cr, dx, dy);
157 set_source_rgb(gc, tro->r, tro->g, tro->b);
158 cairo_show_text(cr, buffer);
161 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
166 for (i = 0; i < pi->nr; i++) {
167 struct plot_data *data = pi->entry + i;
168 if (event->time.seconds < data->sec)
172 /* draw a little tirangular marker and attach tooltip */
173 x = SCALEX(gc, event->time.seconds);
174 y = SCALEY(gc, depth);
175 set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
176 cairo_move_to(gc->cr, x-15, y+6);
177 cairo_line_to(gc->cr, x-3 , y+6);
178 cairo_line_to(gc->cr, x-9, y-6);
179 cairo_line_to(gc->cr, x-15, y+6);
180 cairo_stroke_preserve(gc->cr);
182 set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
183 cairo_move_to(gc->cr, x-9, y-3);
184 cairo_line_to(gc->cr, x-9, y+1);
185 cairo_move_to(gc->cr, x-9, y+4);
186 cairo_line_to(gc->cr, x-9, y+4);
187 cairo_stroke(gc->cr);
188 attach_tooltip(x-15, y-6, 12, 12, event->name);
191 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
193 static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
194 struct event *event = dive->events;
200 plot_one_event(gc, pi, event, &tro);
205 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
207 int sec = entry->sec, decimals;
210 d = get_depth_units(entry->depth, &decimals, NULL);
212 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
215 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
217 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
218 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
221 for (i = 0; i < pi->nr; i++) {
222 struct plot_data *entry = pi->entry + i;
224 if (entry->depth < 2000)
227 if (entry == entry->max[2])
228 render_depth_sample(gc, entry, &deep);
230 if (entry == entry->min[2])
231 render_depth_sample(gc, entry, &shallow);
235 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
237 int maxtime, maxdepth;
239 /* Get plot scaling limits */
240 maxtime = get_maxtime(pi);
241 maxdepth = get_maxdepth(pi);
243 gc->leftx = 0; gc->rightx = maxtime;
244 gc->topy = 0; gc->bottomy = maxdepth;
246 plot_text_samples(gc, pi);
249 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
252 struct plot_data *entry = pi->entry;
254 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
255 move_to(gc, entry->sec, entry->smoothed);
256 for (i = 1; i < pi->nr; i++) {
258 line_to(gc, entry->sec, entry->smoothed);
260 cairo_stroke(gc->cr);
263 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
267 struct plot_data *entry = pi->entry;
269 set_source_rgba(gc, 1, 0.2, 1, a);
270 move_to(gc, entry->sec, entry->min[index]->depth);
271 for (i = 1; i < pi->nr; i++) {
273 line_to(gc, entry->sec, entry->min[index]->depth);
275 for (i = 1; i < pi->nr; i++) {
276 line_to(gc, entry->sec, entry->max[index]->depth);
279 cairo_close_path(gc->cr);
283 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
287 plot_minmax_profile_minute(gc, pi, 2, 0.1);
288 plot_minmax_profile_minute(gc, pi, 1, 0.1);
289 plot_minmax_profile_minute(gc, pi, 0, 0.1);
292 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
295 cairo_t *cr = gc->cr;
297 struct plot_data *entry;
298 int maxtime, maxdepth, marker;
300 /* Get plot scaling limits */
301 maxtime = get_maxtime(pi);
302 maxdepth = get_maxdepth(pi);
304 /* Time markers: every 5 min */
305 gc->leftx = 0; gc->rightx = maxtime;
306 gc->topy = 0; gc->bottomy = 1.0;
307 for (i = 5*60; i < maxtime; i += 5*60) {
312 /* Depth markers: every 30 ft or 10 m*/
313 gc->leftx = 0; gc->rightx = 1.0;
314 gc->topy = 0; gc->bottomy = maxdepth;
315 switch (output_units.length) {
316 case METERS: marker = 10000; break;
317 case FEET: marker = 9144; break; /* 30 ft */
320 set_source_rgba(gc, 1, 1, 1, 0.5);
321 for (i = marker; i < maxdepth; i += marker) {
327 /* Show mean depth */
329 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
330 move_to(gc, 0, pi->meandepth);
331 line_to(gc, 1, pi->meandepth);
335 gc->leftx = 0; gc->rightx = maxtime;
338 * These are good for debugging text placement etc,
339 * but not for actual display..
342 plot_smoothed_profile(gc, pi);
343 plot_minmax_profile(gc, pi);
346 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
348 /* Do the depth profile for the neat fill */
349 gc->topy = 0; gc->bottomy = maxdepth;
350 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
354 for (i = 0; i < pi->nr; i++, entry++)
355 line_to(gc, entry->sec, entry->depth);
356 cairo_close_path(gc->cr);
358 set_source_rgba(gc, 1, 1, 1, 0.2);
359 cairo_fill_preserve(cr);
360 set_source_rgb(gc, 1, 1, 1);
366 /* Now do it again for the velocity colors */
368 for (i = 1; i < pi->nr; i++) {
371 /* we want to draw the segments in different colors
372 * representing the vertical velocity, so we need to
373 * chop this into short segments */
374 rgb_t color = rgb[entry->velocity];
375 depth = entry->depth;
376 set_source_rgb(gc, color.r, color.g, color.b);
377 move_to(gc, entry[-1].sec, entry[-1].depth);
378 line_to(gc, sec, depth);
383 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
385 int maxtime, mintemp, maxtemp, delta;
387 /* Get plot scaling limits */
388 maxtime = get_maxtime(pi);
389 mintemp = pi->mintemp;
390 maxtemp = pi->maxtemp;
392 gc->leftx = 0; gc->rightx = maxtime;
393 /* Show temperatures in roughly the lower third, but make sure the scale
394 is at least somewhat reasonable */
395 delta = maxtemp - mintemp;
396 if (delta > 3000) { /* more than 3K in fluctuation */
397 gc->topy = maxtemp + delta*2;
398 gc->bottomy = mintemp - delta/2;
400 gc->topy = maxtemp + 1500 + delta*2;
401 gc->bottomy = mintemp - delta/2;
404 return maxtemp > mintemp;
407 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
411 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
412 temperature_t temperature = { mkelvin };
414 if (output_units.temperature == FAHRENHEIT) {
415 deg = to_F(temperature);
416 unit = UTF8_DEGREE "F";
418 deg = to_C(temperature);
419 unit = UTF8_DEGREE "C";
421 plot_text(gc, &tro, sec, temperature.mkelvin, "%d%s", deg, unit);
424 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
427 int last = 0, sec = 0;
428 int last_temperature = 0, last_printed_temp = 0;
430 if (!setup_temperature_limits(gc, pi))
433 for (i = 0; i < pi->nr; i++) {
434 struct plot_data *entry = pi->entry+i;
435 int mkelvin = entry->temperature;
439 last_temperature = mkelvin;
441 if (sec < last + 300)
444 plot_single_temp_text(gc,sec,mkelvin);
445 last_printed_temp = mkelvin;
447 /* it would be nice to print the end temperature, if it's different */
448 if (abs(last_temperature - last_printed_temp) > 500)
449 plot_single_temp_text(gc, sec, last_temperature);
452 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
455 cairo_t *cr = gc->cr;
458 if (!setup_temperature_limits(gc, pi))
461 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
462 for (i = 0; i < pi->nr; i++) {
463 struct plot_data *entry = pi->entry + i;
464 int mkelvin = entry->temperature;
465 int sec = entry->sec;
472 line_to(gc, sec, mkelvin);
474 move_to(gc, sec, mkelvin);
480 /* gets both the actual start and end pressure as well as the scaling factors */
481 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
484 gc->rightx = get_maxtime(pi);
486 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
487 return pi->maxpressure != 0;
490 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
493 int have_pressure = FALSE;
495 if (!get_cylinder_pressure_range(gc, pi))
498 set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
500 for (i = 0; i < pi->nr; i++) {
502 struct plot_data *entry = pi->entry + i;
504 mbar = entry->pressure;
507 have_pressure = TRUE;
508 if (entry->same_cylinder)
509 line_to(gc, entry->sec, mbar);
511 move_to(gc, entry->sec, mbar);
513 /* if we have valid samples, we don't want to draw a line to the minpressure
514 * but just end wherever the dive ended (think valve shutdowns during dive)
515 * but that doesn't work so well if we have only max and min
518 line_to(gc, pi->maxtime, pi->minpressure);
519 cairo_stroke(gc->cr);
522 static int mbar_to_PSI(int mbar)
524 pressure_t p = {mbar};
528 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
530 if (get_cylinder_pressure_range(gc, pi)) {
532 const char *unit = "bar";
534 switch (output_units.pressure) {
536 start = pi->maxpressure * 100;
537 end = pi->endpressure * 100;
541 start = (pi->maxpressure + 500) / 1000;
542 end = (pi->endpressure + 500) / 1000;
546 start = mbar_to_PSI(pi->maxpressure);
547 end = mbar_to_PSI(pi->endpressure);
552 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
553 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
554 plot_text(gc, &tro, pi->maxtime, pi->endpressure,
559 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
561 struct plot_data *p = entry;
562 int time = entry->sec;
563 int seconds = 90*(index+1);
564 struct plot_data *min, *max;
567 /* Go back 'seconds' in time */
569 if (p[-1].sec < time - seconds)
574 /* Then go forward until we hit an entry past the time */
579 int depth = p->depth;
580 if (p->sec > time + seconds)
584 if (depth < min->depth)
586 if (depth > max->depth)
589 entry->min[index] = min;
590 entry->max[index] = max;
591 entry->avg[index] = (avg + nr/2) / nr;
594 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
596 analyze_plot_info_minmax_minute(entry, first, last, 0);
597 analyze_plot_info_minmax_minute(entry, first, last, 1);
598 analyze_plot_info_minmax_minute(entry, first, last, 2);
601 static velocity_t velocity(int speed)
605 if (speed < -304) /* ascent faster than -60ft/min */
607 else if (speed < -152) /* above -30ft/min */
609 else if (speed < -76) /* -15ft/min */
611 else if (speed < -25) /* -5ft/min */
613 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
614 for descent are usually about 2x ascent rate; still, we want
615 stable to mean stable */
617 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
619 else if (speed < 304) /* up to 60ft/min is moderate */
621 else if (speed < 507) /* up to 100ft/min is fast */
623 else /* more than that is just crazy - you'll blow your ears out */
628 static struct plot_info *analyze_plot_info(struct plot_info *pi)
633 /* Do pressure min/max based on the non-surface data */
634 for (i = 0; i < nr; i++) {
635 struct plot_data *entry = pi->entry+i;
636 int pressure = entry->pressure;
637 int temperature = entry->temperature;
640 if (!pi->minpressure || pressure < pi->minpressure)
641 pi->minpressure = pressure;
642 if (pressure > pi->maxpressure)
643 pi->maxpressure = pressure;
647 if (!pi->mintemp || temperature < pi->mintemp)
648 pi->mintemp = temperature;
649 if (temperature > pi->maxtemp)
650 pi->maxtemp = temperature;
654 /* Smoothing function: 5-point triangular smooth */
655 for (i = 2; i < nr; i++) {
656 struct plot_data *entry = pi->entry+i;
660 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
661 entry->smoothed = (depth+4) / 9;
663 /* vertical velocity in mm/sec */
664 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
665 if (entry[0].sec - entry[-1].sec) {
666 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
667 /* if our samples are short and we aren't too FAST*/
668 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
670 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
672 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
673 (entry[0].sec - entry[past].sec));
676 entry->velocity = STABLE;
679 /* One-, two- and three-minute minmax data */
680 for (i = 0; i < nr; i++) {
681 struct plot_data *entry = pi->entry +i;
682 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
689 * Create a plot-info with smoothing and ranged min/max
691 * This also makes sure that we have extra empty events on both
692 * sides, so that you can do end-points without having to worry
695 static struct plot_info *create_plot_info(struct dive *dive)
697 int cylinderindex = -1;
698 int lastdepth, lastindex;
699 int i, nr = dive->samples + 4, sec;
700 size_t alloc_size = plot_info_size(nr);
701 struct plot_info *pi;
703 pi = malloc(alloc_size);
706 memset(pi, 0, alloc_size);
711 for (i = 0; i < dive->samples; i++) {
713 struct sample *sample = dive->sample+i;
714 struct plot_data *entry = pi->entry + i + 2;
716 sec = entry->sec = sample->time.seconds;
717 depth = entry->depth = sample->depth.mm;
719 entry->same_cylinder = sample->cylinderindex == cylinderindex;
720 cylinderindex = sample->cylinderindex;
721 entry->pressure = sample->cylinderpressure.mbar;
722 if (!entry->same_cylinder && !entry->pressure)
723 entry->pressure = dive->cylinder[cylinderindex].start.mbar;
724 entry->temperature = sample->temperature.mkelvin;
726 if (depth || lastdepth)
730 if (depth > pi->maxdepth)
731 pi->maxdepth = depth;
735 /* Fill in the last two entries with empty values but valid times */
736 i = dive->samples + 2;
737 pi->entry[i].sec = sec + 20;
738 pi->entry[i+1].sec = sec + 40;
739 if (cylinderindex >= 0) {
740 pi->entry[i].pressure = dive->cylinder[cylinderindex].end.mbar;
741 pi->entry[i].same_cylinder = 1;
744 pi->nr = lastindex+1;
745 pi->maxtime = pi->entry[lastindex].sec;
747 pi->endpressure = 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, cairo_rectangle_int_t *drawing_area, struct dive *dive)
757 struct plot_info *pi = create_plot_info(dive);
759 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
760 cairo_set_line_width(gc->cr, 2);
761 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
762 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
765 * We can use "cairo_translate()" because that doesn't
766 * scale line width etc. But the actual scaling we need
767 * do set up ourselves..
771 gc->maxx = (drawing_area->width - 2*drawing_area->x);
772 gc->maxy = (drawing_area->height - 2*drawing_area->y);
774 /* Temperature profile */
775 plot_temperature_profile(gc, pi);
777 /* Cylinder pressure plot */
778 plot_cylinder_pressure(gc, pi);
781 plot_depth_profile(gc, pi);
782 plot_events(gc, pi, dive);
784 /* Text on top of all graphs.. */
785 plot_temperature_text(gc, pi);
786 plot_depth_text(gc, pi);
787 plot_cylinder_pressure_text(gc, pi);
789 /* Bounding box last */
790 gc->leftx = 0; gc->rightx = 1.0;
791 gc->topy = 0; gc->bottomy = 1.0;
793 set_source_rgb(gc, 1, 1, 1);
798 cairo_close_path(gc->cr);
799 cairo_stroke(gc->cr);