11 int selected_dive = 0;
13 /* Plot info with smoothing and one-, two- and three-minute minimums and maximums */
17 int meandepth, maxdepth;
18 int minpressure, maxpressure;
22 int pressure, temperature;
26 struct plot_data *min[3];
27 struct plot_data *max[3];
31 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
33 /* Scale to 0,0 -> maxx,maxy */
34 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
35 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
36 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
38 static void move_to(struct graphics_context *gc, double x, double y)
40 cairo_move_to(gc->cr, SCALE(gc, x, y));
43 static void line_to(struct graphics_context *gc, double x, double y)
45 cairo_line_to(gc->cr, SCALE(gc, x, y));
48 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
51 /* Black is white and white is black */
58 cairo_set_source_rgba(gc->cr, r, g, b, a);
61 static void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
63 set_source_rgba(gc, r, g, b, 1);
66 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
69 * When showing dive profiles, we scale things to the
70 * current dive. However, we don't scale past less than
71 * 30 minutes or 90 ft, just so that small dives show
74 static int get_maxtime(struct plot_info *pi)
76 int seconds = pi->maxtime;
77 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
78 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
81 static int get_maxdepth(struct plot_info *pi)
83 unsigned mm = pi->maxdepth;
84 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
85 return MAX(30000, ROUND_UP(mm+3000, 10000));
92 } text_render_options_t;
102 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
103 double x, double y, const char *fmt, ...)
105 cairo_t *cr = gc->cr;
106 cairo_font_extents_t fe;
107 cairo_text_extents_t extents;
113 vsnprintf(buffer, sizeof(buffer), fmt, args);
116 cairo_set_font_size(cr, tro->size);
117 cairo_font_extents(cr, &fe);
118 cairo_text_extents(cr, buffer, &extents);
119 dx = tro->hpos * extents.width + extents.x_bearing;
120 dy = tro->vpos * extents.height + fe.descent;
123 cairo_rel_move_to(cr, dx, dy);
125 cairo_text_path(cr, buffer);
126 set_source_rgb(gc, 0, 0, 0);
130 cairo_rel_move_to(cr, dx, dy);
132 set_source_rgb(gc, tro->r, tro->g, tro->b);
133 cairo_show_text(cr, buffer);
136 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
138 int sec = entry->sec;
139 depth_t depth = { entry->val };
143 switch (output_units.length) {
145 d = depth.mm / 1000.0;
153 plot_text(gc, tro, sec, depth.mm, fmt, d);
156 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
158 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
159 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
162 for (i = 0; i < pi->nr; i++) {
163 struct plot_data *entry = pi->entry + i;
165 if (entry->val < 2000)
168 if (entry == entry->max[2])
169 render_depth_sample(gc, entry, &deep);
171 if (entry == entry->min[2])
172 render_depth_sample(gc, entry, &shallow);
176 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
178 int maxtime, maxdepth;
180 /* Get plot scaling limits */
181 maxtime = get_maxtime(pi);
182 maxdepth = get_maxdepth(pi);
184 gc->leftx = 0; gc->rightx = maxtime;
185 gc->topy = 0; gc->bottomy = maxdepth;
187 plot_text_samples(gc, pi);
190 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
193 struct plot_data *entry = pi->entry;
195 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
196 move_to(gc, entry->sec, entry->smoothed);
197 for (i = 1; i < pi->nr; i++) {
199 line_to(gc, entry->sec, entry->smoothed);
201 cairo_stroke(gc->cr);
204 static void plot_minmax_profile_minute(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, 1, a);
211 move_to(gc, entry->sec, entry->min[index]->val);
212 for (i = 1; i < pi->nr; i++) {
214 line_to(gc, entry->sec, entry->min[index]->val);
216 for (i = 1; i < pi->nr; i++) {
217 line_to(gc, entry->sec, entry->max[index]->val);
220 cairo_close_path(gc->cr);
224 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
228 plot_minmax_profile_minute(gc, pi, 2, 0.1);
229 plot_minmax_profile_minute(gc, pi, 1, 0.1);
230 plot_minmax_profile_minute(gc, pi, 0, 0.1);
233 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
236 cairo_t *cr = gc->cr;
237 int begins, sec, depth;
238 struct plot_data *entry;
239 int maxtime, maxdepth, marker;
241 /* Get plot scaling limits */
242 maxtime = get_maxtime(pi);
243 maxdepth = get_maxdepth(pi);
245 /* Time markers: every 5 min */
246 gc->leftx = 0; gc->rightx = maxtime;
247 gc->topy = 0; gc->bottomy = 1.0;
248 for (i = 5*60; i < maxtime; i += 5*60) {
253 /* Depth markers: every 30 ft or 10 m*/
254 gc->leftx = 0; gc->rightx = 1.0;
255 gc->topy = 0; gc->bottomy = maxdepth;
256 switch (output_units.length) {
257 case METERS: marker = 10000; break;
258 case FEET: marker = 9144; break; /* 30 ft */
261 set_source_rgba(gc, 1, 1, 1, 0.5);
262 for (i = marker; i < maxdepth; i += marker) {
268 /* Show mean depth */
269 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
270 move_to(gc, 0, pi->meandepth);
271 line_to(gc, 1, pi->meandepth);
274 gc->leftx = 0; gc->rightx = maxtime;
276 plot_smoothed_profile(gc, pi);
277 plot_minmax_profile(gc, pi);
280 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
282 move_to(gc, entry->sec, entry->val);
283 for (i = 1; i < pi->nr; i++) {
286 if (sec <= maxtime) {
288 line_to(gc, sec, depth);
291 gc->topy = 0; gc->bottomy = 1.0;
292 line_to(gc, MIN(sec,maxtime), 0);
293 line_to(gc, begins, 0);
294 cairo_close_path(cr);
295 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
296 cairo_fill_preserve(cr);
297 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
301 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
303 int maxtime, mintemp, maxtemp;
305 /* Get plot scaling limits */
306 maxtime = get_maxtime(pi);
307 mintemp = pi->mintemp;
308 maxtemp = pi->maxtemp;
310 gc->leftx = 0; gc->rightx = maxtime;
311 /* Show temperatures in roughly the lower third */
312 gc->topy = maxtemp + (maxtemp - mintemp)*2;
313 gc->bottomy = mintemp - (maxtemp - mintemp)/2;
315 return maxtemp > mintemp;
318 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
322 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
323 temperature_t temperature = { mkelvin };
325 if (output_units.temperature == FAHRENHEIT) {
326 deg = to_F(temperature);
329 deg = to_C(temperature);
332 plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
335 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
338 int last = 0, sec = 0;
339 int last_temperature = 0, last_printed_temp = 0;
341 if (!setup_temperature_limits(gc, pi))
344 for (i = 0; i < pi->nr; i++) {
345 struct plot_data *entry = pi->entry+i;
346 int mkelvin = entry->temperature;
350 last_temperature = mkelvin;
352 if (sec < last + 300)
355 plot_single_temp_text(gc,sec,mkelvin);
356 last_printed_temp = mkelvin;
358 /* it would be nice to print the end temperature, if it's different */
359 if (abs(last_temperature - last_printed_temp) > 500)
360 plot_single_temp_text(gc, sec, last_temperature);
363 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
366 cairo_t *cr = gc->cr;
369 if (!setup_temperature_limits(gc, pi))
372 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
373 for (i = 0; i < pi->nr; i++) {
374 struct plot_data *entry = pi->entry + i;
375 int mkelvin = entry->temperature;
376 int sec = entry->sec;
383 line_to(gc, sec, mkelvin);
385 move_to(gc, sec, mkelvin);
391 /* gets both the actual start and end pressure as well as the scaling factors */
392 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
395 gc->rightx = get_maxtime(pi);
397 gc->topy = 0; gc->bottomy = pi->maxpressure * 1.5;
398 return pi->maxpressure != 0;
401 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
405 if (!get_cylinder_pressure_range(gc, pi))
408 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
410 move_to(gc, 0, pi->maxpressure);
411 for (i = 1; i < pi->nr; i++) {
413 struct plot_data *entry = pi->entry + i;
415 mbar = entry->pressure;
418 line_to(gc, entry->sec, mbar);
420 line_to(gc, pi->maxtime, pi->minpressure);
421 cairo_stroke(gc->cr);
425 * Return air usage (in liters).
427 static double calculate_airuse(struct dive *dive)
432 for (i = 0; i < MAX_CYLINDERS; i++) {
433 cylinder_t *cyl = dive->cylinder + i;
434 int size = cyl->type.size.mliter;
440 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
442 /* Liters of air at 1 atm == milliliters at 1k atm*/
443 airuse += kilo_atm * size;
448 static void plot_info(struct dive *dive, struct graphics_context *gc)
450 text_render_options_t tro = {10, 0.2, 1.0, 0.2, RIGHT, BOTTOM};
451 const double liters_per_cuft = 28.317;
452 const char *unit, *desc;
455 airuse = calculate_airuse(dive);
459 /* I really need to start addign some unit setting thing */
460 switch (output_units.volume) {
466 airuse /= liters_per_cuft;
470 plot_text(gc, &tro, 0.98, 0.98, "vol: %4.2f %s", airuse, unit);
473 if (dive->duration.seconds) {
474 double pressure = 1 + (dive->meandepth.mm / 10000.0);
475 double sac = airuse / pressure * 60 / dive->duration.seconds;
476 plot_text(gc, &tro, 0.98, 0.98, "SAC: %4.2f %s/min", sac, unit);
480 desc = dive->cylinder[0].type.description;
481 if (desc || dive->cylinder[0].gasmix.o2.permille) {
482 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
487 plot_text(gc, &tro, 0.98, 0.98, "%s (%d%%)", desc, o2);
491 static int mbar_to_PSI(int mbar)
493 pressure_t p = {mbar};
497 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
499 if (get_cylinder_pressure_range(gc, pi)) {
501 const char *unit = "bar";
503 switch (output_units.pressure) {
505 start = pi->maxpressure * 100;
506 end = pi->minpressure * 100;
510 start = (pi->maxpressure + 500) / 1000;
511 end = (pi->minpressure + 500) / 1000;
515 start = mbar_to_PSI(pi->maxpressure);
516 end = mbar_to_PSI(pi->minpressure);
521 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
522 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
523 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
528 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
530 struct plot_data *p = entry;
531 int time = entry->sec;
532 int seconds = 90*(index+1);
533 struct plot_data *min, *max;
536 /* Go back 'seconds' in time */
538 if (p[-1].sec < time - seconds)
543 /* Then go forward until we hit an entry past the time */
549 if (p->sec > time + seconds)
558 entry->min[index] = min;
559 entry->max[index] = max;
560 entry->avg[index] = (avg + nr/2) / nr;
563 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
565 analyze_plot_info_minmax_minute(entry, first, last, 0);
566 analyze_plot_info_minmax_minute(entry, first, last, 1);
567 analyze_plot_info_minmax_minute(entry, first, last, 2);
570 static struct plot_info *analyze_plot_info(struct plot_info *pi)
575 /* Do pressure min/max based on the non-surface data */
576 for (i = 0; i < nr; i++) {
577 struct plot_data *entry = pi->entry+i;
578 int pressure = entry->pressure;
579 int temperature = entry->temperature;
582 if (!pi->minpressure || pressure < pi->minpressure)
583 pi->minpressure = pressure;
584 if (pressure > pi->maxpressure)
585 pi->maxpressure = pressure;
589 if (!pi->mintemp || temperature < pi->mintemp)
590 pi->mintemp = temperature;
591 if (temperature > pi->maxtemp)
592 pi->maxtemp = temperature;
596 /* Smoothing function: 5-point triangular smooth */
597 for (i = 2; i < nr-2; i++) {
598 struct plot_data *entry = pi->entry+i;
601 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
602 entry->smoothed = (val+4) / 9;
605 /* One-, two- and three-minute minmax data */
606 for (i = 0; i < nr; i++) {
607 struct plot_data *entry = pi->entry +i;
608 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
615 * Create a plot-info with smoothing and ranged min/max
617 * This also makes sure that we have extra empty events on both
618 * sides, so that you can do end-points without having to worry
621 static struct plot_info *create_plot_info(struct dive *dive)
623 int lastdepth, lastindex;
624 int i, nr = dive->samples + 4, sec;
625 size_t alloc_size = plot_info_size(nr);
626 struct plot_info *pi;
628 pi = malloc(alloc_size);
631 memset(pi, 0, alloc_size);
636 for (i = 0; i < dive->samples; i++) {
638 struct sample *sample = dive->sample+i;
639 struct plot_data *entry = pi->entry + i + 2;
641 sec = entry->sec = sample->time.seconds;
642 depth = entry->val = sample->depth.mm;
643 entry->pressure = sample->cylinderpressure.mbar;
644 entry->temperature = sample->temperature.mkelvin;
646 if (depth || lastdepth)
650 if (depth > pi->maxdepth)
651 pi->maxdepth = depth;
655 /* Fill in the last two entries with empty values but valid times */
656 i = dive->samples + 2;
657 pi->entry[i].sec = sec + 20;
658 pi->entry[i+1].sec = sec + 40;
660 pi->nr = lastindex+1;
661 pi->maxtime = pi->entry[lastindex].sec;
663 pi->minpressure = dive->cylinder[0].end.mbar;
664 pi->maxpressure = dive->cylinder[0].start.mbar;
666 pi->meandepth = dive->meandepth.mm;
668 return analyze_plot_info(pi);
671 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
674 struct plot_info *pi = create_plot_info(dive);
678 cairo_translate(gc->cr, topx, topy);
679 cairo_set_line_width(gc->cr, 2);
680 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
681 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
684 * We can use "cairo_translate()" because that doesn't
685 * scale line width etc. But the actual scaling we need
686 * do set up ourselves..
690 gc->maxx = (w - 2*topx);
691 gc->maxy = (h - 2*topy);
693 /* Temperature profile */
694 plot_temperature_profile(gc, pi);
696 /* Cylinder pressure plot */
697 plot_cylinder_pressure(gc, pi);
700 plot_depth_profile(gc, pi);
702 /* Text on top of all graphs.. */
703 plot_temperature_text(gc, pi);
704 plot_depth_text(gc, pi);
705 plot_cylinder_pressure_text(gc, pi);
707 /* And info box in the lower right corner.. */
708 gc->leftx = 0; gc->rightx = 1.0;
709 gc->topy = 0; gc->bottomy = 1.0;
712 /* Bounding box last */
713 set_source_rgb(gc, 1, 1, 1);
718 cairo_close_path(gc->cr);
719 cairo_stroke(gc->cr);
723 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
725 struct dive *dive = current_dive;
726 struct graphics_context gc = { .printer = 0 };
729 w = widget->allocation.width;
730 h = widget->allocation.height;
732 gc.cr = gdk_cairo_create(widget->window);
733 set_source_rgb(&gc, 0, 0, 0);
737 plot(&gc, w, h, dive);
739 cairo_destroy(gc.cr);
744 GtkWidget *dive_profile_widget(void)
748 da = gtk_drawing_area_new();
749 gtk_widget_set_size_request(da, 350, 250);
750 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);