11 int selected_dive = 0;
13 /* Plot info with smoothing and one-, two- and three-minute minimums and maximums */
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 return MAX(30*60, ROUND_UP(seconds, 60*10));
80 static int round_depth_up(depth_t depth)
82 unsigned mm = depth.mm;
84 return MAX(30000, ROUND_UP(mm+3000, 10000));
90 enum {CENTER,LEFT} halign;
91 enum {MIDDLE,TOP,BOTTOM} valign;
92 } text_render_options_t;
94 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
95 double x, double y, const char *fmt, ...)
98 cairo_text_extents_t extents;
104 vsnprintf(buffer, sizeof(buffer), fmt, args);
107 cairo_set_font_size(cr, tro->size);
108 cairo_text_extents(cr, buffer, &extents);
110 switch (tro->halign) {
112 dx = -(extents.width/2 + extents.x_bearing);
118 switch (tro->valign) {
120 dy = extents.height * 1.2;
123 dy = -extents.height * 0.8;
131 cairo_rel_move_to(cr, dx, dy);
133 cairo_text_path(cr, buffer);
134 set_source_rgb(gc, 0, 0, 0);
138 cairo_rel_move_to(cr, dx, dy);
140 set_source_rgb(gc, tro->r, tro->g, tro->b);
141 cairo_show_text(cr, buffer);
144 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
146 int sec = entry->sec;
147 depth_t depth = { entry->val };
151 switch (output_units.length) {
153 d = depth.mm / 1000.0;
161 plot_text(gc, tro, sec, depth.mm, fmt, d);
164 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
166 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
167 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
170 for (i = 0; i < pi->nr; i++) {
171 struct plot_data *entry = pi->entry + i;
173 if (entry->val < 2000)
176 if (entry == entry->max[2])
177 render_depth_sample(gc, entry, &deep);
179 if (entry == entry->min[2])
180 render_depth_sample(gc, entry, &shallow);
184 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
186 int maxtime, maxdepth;
188 /* Get plot scaling limits */
189 maxtime = get_maxtime(pi);
190 maxdepth = round_depth_up(dive->maxdepth);
192 gc->leftx = 0; gc->rightx = maxtime;
193 gc->topy = 0; gc->bottomy = maxdepth;
195 plot_text_samples(gc, pi);
198 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
201 struct plot_data *entry = pi->entry;
203 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
204 move_to(gc, entry->sec, entry->smoothed);
205 for (i = 1; i < pi->nr; i++) {
207 line_to(gc, entry->sec, entry->smoothed);
209 cairo_stroke(gc->cr);
212 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
216 struct plot_data *entry = pi->entry;
218 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
219 move_to(gc, entry->sec, entry->min[index]->val);
220 for (i = 1; i < pi->nr; i++) {
222 line_to(gc, entry->sec, entry->min[index]->val);
224 for (i = 1; i < pi->nr; i++) {
225 line_to(gc, entry->sec, entry->max[index]->val);
228 cairo_close_path(gc->cr);
232 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
236 plot_minmax_profile_minute(gc, pi, 2, 0.1);
237 plot_minmax_profile_minute(gc, pi, 1, 0.1);
238 plot_minmax_profile_minute(gc, pi, 0, 0.1);
241 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
244 cairo_t *cr = gc->cr;
245 int begins, sec, depth;
246 struct plot_data *entry;
247 int maxtime, maxdepth, marker;
249 /* Get plot scaling limits */
250 maxtime = get_maxtime(pi);
251 maxdepth = round_depth_up(dive->maxdepth);
253 /* Time markers: every 5 min */
254 gc->leftx = 0; gc->rightx = maxtime;
255 gc->topy = 0; gc->bottomy = 1.0;
256 for (i = 5*60; i < maxtime; i += 5*60) {
261 /* Depth markers: every 30 ft or 10 m*/
262 gc->leftx = 0; gc->rightx = 1.0;
263 gc->topy = 0; gc->bottomy = maxdepth;
264 switch (output_units.length) {
265 case METERS: marker = 10000; break;
266 case FEET: marker = 9144; break; /* 30 ft */
269 set_source_rgba(gc, 1, 1, 1, 0.5);
270 for (i = marker; i < maxdepth; i += marker) {
276 /* Show mean depth */
277 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
278 move_to(gc, 0, dive->meandepth.mm);
279 line_to(gc, 1, dive->meandepth.mm);
282 gc->leftx = 0; gc->rightx = maxtime;
284 plot_smoothed_profile(gc, pi);
285 plot_minmax_profile(gc, pi);
288 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
290 move_to(gc, entry->sec, entry->val);
291 for (i = 1; i < pi->nr; i++) {
294 if (sec <= maxtime) {
296 line_to(gc, sec, depth);
299 gc->topy = 0; gc->bottomy = 1.0;
300 line_to(gc, MIN(sec,maxtime), 0);
301 line_to(gc, begins, 0);
302 cairo_close_path(cr);
303 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
304 cairo_fill_preserve(cr);
305 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
309 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
311 int maxtime, mintemp, maxtemp;
313 /* Get plot scaling limits */
314 maxtime = get_maxtime(pi);
315 mintemp = pi->mintemp;
316 maxtemp = pi->maxtemp;
318 gc->leftx = 0; gc->rightx = maxtime;
319 /* Show temperatures in roughly the lower third */
320 gc->topy = maxtemp + (maxtemp - mintemp)*2;
321 gc->bottomy = mintemp - (maxtemp - mintemp)/2;
323 return maxtemp > mintemp;
326 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
330 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
331 temperature_t temperature = { mkelvin };
333 if (output_units.temperature == FAHRENHEIT) {
334 deg = to_F(temperature);
337 deg = to_C(temperature);
340 plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
343 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
347 int last_temperature = 0, last_printed_temp = 0;
349 if (!setup_temperature_limits(gc, pi))
352 for (i = 0; i < pi->nr; i++) {
354 struct plot_data *entry = pi->entry+i;
355 int mkelvin = entry->temperature;
359 last_temperature = mkelvin;
361 if (sec < last + 300)
364 plot_single_temp_text(gc,sec,mkelvin);
365 last_printed_temp = mkelvin;
367 /* it would be nice to print the end temperature, if it's different */
368 if (last_temperature != last_printed_temp)
369 plot_single_temp_text(gc, last, last_temperature);
372 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc,
373 struct plot_info *pi)
376 cairo_t *cr = gc->cr;
379 if (!setup_temperature_limits(gc, pi))
382 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
383 for (i = 0; i < dive->samples; i++) {
384 struct sample *sample = dive->sample+i;
385 if (sample->time.seconds > dive->duration.seconds)
386 break; /* let's not plot surface temp events */
387 int mkelvin = sample->temperature.mkelvin;
394 line_to(gc, sample->time.seconds, mkelvin);
396 move_to(gc, sample->time.seconds, mkelvin);
402 /* gets both the actual start and end pressure as well as the scaling factors */
403 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
404 struct plot_info *pi,
405 pressure_t *startp, pressure_t *endp)
411 gc->rightx = get_maxtime(pi);
416 startp->mbar = endp->mbar = 0;
418 for (i = 0; i < dive->samples; i++) {
420 struct sample *sample = dive->sample + i;
422 /* FIXME! We only track cylinder 0 right now */
423 if (sample->cylinderindex)
425 mbar = sample->cylinderpressure.mbar;
439 gc->topy = 0; gc->bottomy = max * 1.5;
443 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc,
444 struct plot_info *pi)
448 if (!get_cylinder_pressure_range(dive, gc, pi, NULL, NULL))
451 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
453 move_to(gc, 0, dive->cylinder[0].start.mbar);
454 for (i = 1; i < dive->samples; i++) {
456 struct sample *sample = dive->sample + i;
458 mbar = sample->cylinderpressure.mbar;
461 sec = sample->time.seconds;
462 if (sec <= dive->duration.seconds)
463 line_to(gc, sec, mbar);
466 * We may have "surface time" events, in which case we don't go
467 * back to dive duration
469 if (sec < dive->duration.seconds)
470 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
471 cairo_stroke(gc->cr);
475 * Return air usage (in liters).
477 static double calculate_airuse(struct dive *dive)
482 for (i = 0; i < MAX_CYLINDERS; i++) {
483 cylinder_t *cyl = dive->cylinder + i;
484 int size = cyl->type.size.mliter;
490 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
492 /* Liters of air at 1 atm == milliliters at 1k atm*/
493 airuse += kilo_atm * size;
498 static void plot_info(struct dive *dive, struct graphics_context *gc)
500 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
501 const double liters_per_cuft = 28.317;
502 const char *unit, *desc;
505 airuse = calculate_airuse(dive);
509 /* I really need to start addign some unit setting thing */
510 switch (output_units.volume) {
516 airuse /= liters_per_cuft;
519 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
520 if (dive->duration.seconds) {
521 double pressure = 1 + (dive->meandepth.mm / 10000.0);
522 double sac = airuse / pressure * 60 / dive->duration.seconds;
523 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
525 desc = dive->cylinder[0].type.description;
526 if (desc || dive->cylinder[0].gasmix.o2.permille) {
527 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
532 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
536 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc,
537 struct plot_info *pi)
539 pressure_t startp, endp;
541 if (get_cylinder_pressure_range(dive, gc, pi, &startp, &endp)) {
543 const char *unit = "bar";
545 switch (output_units.pressure) {
547 start = startp.mbar * 100;
548 end = startp.mbar * 100;
552 start = (startp.mbar + 500) / 1000;
553 end = (endp.mbar + 500) / 1000;
557 start = to_PSI(startp);
563 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
564 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
565 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
570 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
572 struct plot_data *p = entry;
573 int time = entry->sec;
574 int seconds = 90*(index+1);
575 struct plot_data *min, *max;
578 /* Go back 'seconds' in time */
580 if (p[-1].sec < time - seconds)
585 /* Then go forward until we hit an entry past the time */
591 if (p->sec > time + seconds)
600 entry->min[index] = min;
601 entry->max[index] = max;
602 entry->avg[index] = (avg + nr/2) / nr;
605 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
607 analyze_plot_info_minmax_minute(entry, first, last, 0);
608 analyze_plot_info_minmax_minute(entry, first, last, 1);
609 analyze_plot_info_minmax_minute(entry, first, last, 2);
612 static struct plot_info *analyze_plot_info(struct plot_info *pi)
617 /* Do pressure min/max based on the non-surface data */
618 for (i = 0; i < nr; i++) {
619 struct plot_data *entry = pi->entry+i;
620 int pressure = entry->pressure;
621 int temperature = entry->temperature;
624 if (!pi->minpressure || pressure < pi->minpressure)
625 pi->minpressure = pressure;
626 if (pressure > pi->maxpressure)
627 pi->maxpressure = pressure;
631 if (!pi->mintemp || temperature < pi->mintemp)
632 pi->mintemp = temperature;
633 if (temperature > pi->maxtemp)
634 pi->maxtemp = temperature;
638 /* Smoothing function: 5-point triangular smooth */
639 for (i = 2; i < nr-2; i++) {
640 struct plot_data *entry = pi->entry+i;
643 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
644 entry->smoothed = (val+4) / 9;
647 /* One-, two- and three-minute minmax data */
648 for (i = 0; i < nr; i++) {
649 struct plot_data *entry = pi->entry +i;
650 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
657 * Create a plot-info with smoothing and ranged min/max
659 * This also makes sure that we have extra empty events on both
660 * sides, so that you can do end-points without having to worry
663 static struct plot_info *create_plot_info(struct dive *dive)
665 int lastdepth, lastindex;
666 int i, nr = dive->samples + 4, sec;
667 size_t alloc_size = plot_info_size(nr);
668 struct plot_info *pi;
670 pi = malloc(alloc_size);
673 memset(pi, 0, alloc_size);
678 for (i = 0; i < dive->samples; i++) {
680 struct sample *sample = dive->sample+i;
681 struct plot_data *entry = pi->entry + i + 2;
683 sec = entry->sec = sample->time.seconds;
684 depth = entry->val = sample->depth.mm;
685 entry->pressure = sample->cylinderpressure.mbar;
686 entry->temperature = sample->temperature.mkelvin;
688 if (depth || lastdepth)
692 if (depth > pi->maxdepth)
693 pi->maxdepth = depth;
697 /* Fill in the last two entries with empty values but valid times */
698 i = dive->samples + 2;
699 pi->entry[i].sec = sec + 20;
700 pi->entry[i+1].sec = sec + 40;
702 pi->nr = lastindex+1;
703 pi->maxtime = pi->entry[lastindex].sec;
705 return analyze_plot_info(pi);
708 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
711 struct plot_info *pi = create_plot_info(dive);
715 cairo_translate(gc->cr, topx, topy);
716 cairo_set_line_width(gc->cr, 2);
717 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
718 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
721 * We can use "cairo_translate()" because that doesn't
722 * scale line width etc. But the actual scaling we need
723 * do set up ourselves..
727 gc->maxx = (w - 2*topx);
728 gc->maxy = (h - 2*topy);
730 /* Temperature profile */
731 plot_temperature_profile(dive, gc, pi);
733 /* Cylinder pressure plot */
734 plot_cylinder_pressure(dive, gc, pi);
737 plot_depth_profile(dive, gc, pi);
739 /* Text on top of all graphs.. */
740 plot_temperature_text(gc, pi);
741 plot_depth_text(dive, gc, pi);
742 plot_cylinder_pressure_text(dive, gc, pi);
744 /* And info box in the lower right corner.. */
745 gc->leftx = 0; gc->rightx = 1.0;
746 gc->topy = 0; gc->bottomy = 1.0;
749 /* Bounding box last */
750 set_source_rgb(gc, 1, 1, 1);
755 cairo_close_path(gc->cr);
756 cairo_stroke(gc->cr);
760 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
762 struct dive *dive = current_dive;
763 struct graphics_context gc = { .printer = 0 };
766 w = widget->allocation.width;
767 h = widget->allocation.height;
769 gc.cr = gdk_cairo_create(widget->window);
770 set_source_rgb(&gc, 0, 0, 0);
774 plot(&gc, w, h, dive);
776 cairo_destroy(gc.cr);
781 GtkWidget *dive_profile_widget(void)
785 da = gtk_drawing_area_new();
786 gtk_widget_set_size_request(da, 350, 250);
787 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);