11 int selected_dive = 0;
13 /* Plot info with smoothing and one-, two- and three-minute minimums and maximums */
21 struct plot_data *min[3];
22 struct plot_data *max[3];
26 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
28 /* Scale to 0,0 -> maxx,maxy */
29 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
30 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
31 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
33 static void move_to(struct graphics_context *gc, double x, double y)
35 cairo_move_to(gc->cr, SCALE(gc, x, y));
38 static void line_to(struct graphics_context *gc, double x, double y)
40 cairo_line_to(gc->cr, SCALE(gc, x, y));
43 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
46 /* Black is white and white is black */
53 cairo_set_source_rgba(gc->cr, r, g, b, a);
56 static void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
58 set_source_rgba(gc, r, g, b, 1);
61 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
64 * When showing dive profiles, we scale things to the
65 * current dive. However, we don't scale past less than
66 * 30 minutes or 90 ft, just so that small dives show
69 static int get_maxtime(struct plot_info *pi)
71 int seconds = pi->maxtime;
72 return MAX(30*60, ROUND_UP(seconds, 60*10));
75 static int round_depth_up(depth_t depth)
77 unsigned mm = depth.mm;
79 return MAX(30000, ROUND_UP(mm+3000, 10000));
85 enum {CENTER,LEFT} halign;
86 enum {MIDDLE,TOP,BOTTOM} valign;
87 } text_render_options_t;
89 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
90 double x, double y, const char *fmt, ...)
93 cairo_text_extents_t extents;
99 vsnprintf(buffer, sizeof(buffer), fmt, args);
102 cairo_set_font_size(cr, tro->size);
103 cairo_text_extents(cr, buffer, &extents);
105 switch (tro->halign) {
107 dx = -(extents.width/2 + extents.x_bearing);
113 switch (tro->valign) {
115 dy = extents.height * 1.2;
118 dy = -extents.height * 0.8;
126 cairo_rel_move_to(cr, dx, dy);
128 cairo_text_path(cr, buffer);
129 set_source_rgb(gc, 0, 0, 0);
133 cairo_rel_move_to(cr, dx, dy);
135 set_source_rgb(gc, tro->r, tro->g, tro->b);
136 cairo_show_text(cr, buffer);
139 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
141 int sec = entry->sec;
142 depth_t depth = { entry->val };
146 switch (output_units.length) {
148 d = depth.mm / 1000.0;
156 plot_text(gc, tro, sec, depth.mm, fmt, d);
159 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
161 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
162 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
165 for (i = 0; i < pi->nr; i++) {
166 struct plot_data *entry = pi->entry + i;
168 if (entry->val < 2000)
171 if (entry == entry->max[2])
172 render_depth_sample(gc, entry, &deep);
174 if (entry == entry->min[2])
175 render_depth_sample(gc, entry, &shallow);
179 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
181 int maxtime, maxdepth;
183 /* Get plot scaling limits */
184 maxtime = get_maxtime(pi);
185 maxdepth = round_depth_up(dive->maxdepth);
187 gc->leftx = 0; gc->rightx = maxtime;
188 gc->topy = 0; gc->bottomy = maxdepth;
190 plot_text_samples(gc, pi);
193 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
196 struct plot_data *entry = pi->entry;
198 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
199 move_to(gc, entry->sec, entry->smoothed);
200 for (i = 1; i < pi->nr; i++) {
202 line_to(gc, entry->sec, entry->smoothed);
204 cairo_stroke(gc->cr);
207 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
211 struct plot_data *entry = pi->entry;
213 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
214 move_to(gc, entry->sec, entry->min[index]->val);
215 for (i = 1; i < pi->nr; i++) {
217 line_to(gc, entry->sec, entry->min[index]->val);
219 for (i = 1; i < pi->nr; i++) {
220 line_to(gc, entry->sec, entry->max[index]->val);
223 cairo_close_path(gc->cr);
227 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
231 plot_minmax_profile_minute(gc, pi, 2, 0.1);
232 plot_minmax_profile_minute(gc, pi, 1, 0.1);
233 plot_minmax_profile_minute(gc, pi, 0, 0.1);
236 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
239 cairo_t *cr = gc->cr;
240 int begins, sec, depth;
241 struct plot_data *entry;
242 int maxtime, maxdepth, marker;
244 /* Get plot scaling limits */
245 maxtime = get_maxtime(pi);
246 maxdepth = round_depth_up(dive->maxdepth);
248 /* Time markers: every 5 min */
249 gc->leftx = 0; gc->rightx = maxtime;
250 gc->topy = 0; gc->bottomy = 1.0;
251 for (i = 5*60; i < maxtime; i += 5*60) {
256 /* Depth markers: every 30 ft or 10 m*/
257 gc->leftx = 0; gc->rightx = 1.0;
258 gc->topy = 0; gc->bottomy = maxdepth;
259 switch (output_units.length) {
260 case METERS: marker = 10000; break;
261 case FEET: marker = 9144; break; /* 30 ft */
264 set_source_rgba(gc, 1, 1, 1, 0.5);
265 for (i = marker; i < maxdepth; i += marker) {
271 /* Show mean depth */
272 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
273 move_to(gc, 0, dive->meandepth.mm);
274 line_to(gc, 1, dive->meandepth.mm);
277 gc->leftx = 0; gc->rightx = maxtime;
279 plot_smoothed_profile(gc, pi);
280 plot_minmax_profile(gc, pi);
283 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
285 move_to(gc, entry->sec, entry->val);
286 for (i = 1; i < pi->nr; i++) {
289 if (sec <= maxtime) {
291 line_to(gc, sec, depth);
294 gc->topy = 0; gc->bottomy = 1.0;
295 line_to(gc, MIN(sec,maxtime), 0);
296 line_to(gc, begins, 0);
297 cairo_close_path(cr);
298 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
299 cairo_fill_preserve(cr);
300 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
304 static int setup_temperature_limits(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
307 int maxtime, mintemp, maxtemp;
309 /* Get plot scaling limits */
310 maxtime = get_maxtime(pi);
313 for (i = 0; i < dive->samples; i++) {
314 struct sample *sample = dive->sample+i;
315 int mkelvin = sample->temperature.mkelvin;
318 if (mkelvin > maxtemp)
320 if (mkelvin < mintemp)
324 gc->leftx = 0; gc->rightx = maxtime;
325 /* Show temperatures in roughly the lower third */
326 gc->topy = maxtemp + (maxtemp - mintemp)*2;
327 gc->bottomy = mintemp - (maxtemp - mintemp)/2;
329 return maxtemp > mintemp;
332 static void plot_single_temp_text(struct graphics_context *gc, int sec, temperature_t temperature)
336 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
338 if (output_units.temperature == FAHRENHEIT) {
339 deg = to_F(temperature);
342 deg = to_C(temperature);
345 plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
348 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc,
349 struct plot_info *pi)
353 temperature_t last_temperature, last_printed_temp;
355 if (!setup_temperature_limits(dive, gc, pi))
358 for (i = 0; i < dive->samples; i++) {
359 struct sample *sample = dive->sample+i;
360 if (sample->time.seconds > dive->duration.seconds)
361 break; /* let's not plot surface temp events */
362 int mkelvin = sample->temperature.mkelvin;
366 last_temperature = sample->temperature;
367 sec = sample->time.seconds;
371 plot_single_temp_text(gc,sec,sample->temperature);
372 last_printed_temp = last_temperature ;
374 /* it would be nice to print the end temperature, if it's different */
375 if (last_temperature.mkelvin != last_printed_temp.mkelvin) {
376 plot_single_temp_text(gc,dive->duration.seconds,last_temperature);
380 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc,
381 struct plot_info *pi)
384 cairo_t *cr = gc->cr;
387 if (!setup_temperature_limits(dive, gc, pi))
390 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
391 for (i = 0; i < dive->samples; i++) {
392 struct sample *sample = dive->sample+i;
393 if (sample->time.seconds > dive->duration.seconds)
394 break; /* let's not plot surface temp events */
395 int mkelvin = sample->temperature.mkelvin;
402 line_to(gc, sample->time.seconds, mkelvin);
404 move_to(gc, sample->time.seconds, mkelvin);
410 /* gets both the actual start and end pressure as well as the scaling factors */
411 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
412 struct plot_info *pi,
413 pressure_t *startp, pressure_t *endp)
419 gc->rightx = get_maxtime(pi);
424 startp->mbar = endp->mbar = 0;
426 for (i = 0; i < dive->samples; i++) {
428 struct sample *sample = dive->sample + i;
430 /* FIXME! We only track cylinder 0 right now */
431 if (sample->cylinderindex)
433 mbar = sample->cylinderpressure.mbar;
447 gc->topy = 0; gc->bottomy = max * 1.5;
451 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc,
452 struct plot_info *pi)
456 if (!get_cylinder_pressure_range(dive, gc, pi, NULL, NULL))
459 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
461 move_to(gc, 0, dive->cylinder[0].start.mbar);
462 for (i = 1; i < dive->samples; i++) {
464 struct sample *sample = dive->sample + i;
466 mbar = sample->cylinderpressure.mbar;
469 sec = sample->time.seconds;
470 if (sec <= dive->duration.seconds)
471 line_to(gc, sec, mbar);
474 * We may have "surface time" events, in which case we don't go
475 * back to dive duration
477 if (sec < dive->duration.seconds)
478 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
479 cairo_stroke(gc->cr);
483 * Return air usage (in liters).
485 static double calculate_airuse(struct dive *dive)
490 for (i = 0; i < MAX_CYLINDERS; i++) {
491 cylinder_t *cyl = dive->cylinder + i;
492 int size = cyl->type.size.mliter;
498 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
500 /* Liters of air at 1 atm == milliliters at 1k atm*/
501 airuse += kilo_atm * size;
506 static void plot_info(struct dive *dive, struct graphics_context *gc)
508 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
509 const double liters_per_cuft = 28.317;
510 const char *unit, *desc;
513 airuse = calculate_airuse(dive);
517 /* I really need to start addign some unit setting thing */
518 switch (output_units.volume) {
524 airuse /= liters_per_cuft;
527 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
528 if (dive->duration.seconds) {
529 double pressure = 1 + (dive->meandepth.mm / 10000.0);
530 double sac = airuse / pressure * 60 / dive->duration.seconds;
531 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
533 desc = dive->cylinder[0].type.description;
534 if (desc || dive->cylinder[0].gasmix.o2.permille) {
535 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
540 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
544 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc,
545 struct plot_info *pi)
547 pressure_t startp, endp;
549 if (get_cylinder_pressure_range(dive, gc, pi, &startp, &endp)) {
551 const char *unit = "bar";
553 switch (output_units.pressure) {
555 start = startp.mbar * 100;
556 end = startp.mbar * 100;
560 start = (startp.mbar + 500) / 1000;
561 end = (endp.mbar + 500) / 1000;
565 start = to_PSI(startp);
571 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
572 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
573 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
578 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
580 struct plot_data *p = entry;
581 int time = entry->sec;
582 int seconds = 90*(index+1);
583 struct plot_data *min, *max;
586 /* Go back 'seconds' in time */
588 if (p[-1].sec < time - seconds)
593 /* Then go forward until we hit an entry past the time */
599 if (p->sec > time + seconds)
608 entry->min[index] = min;
609 entry->max[index] = max;
610 entry->avg[index] = (avg + nr/2) / nr;
613 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
615 analyze_plot_info_minmax_minute(entry, first, last, 0);
616 analyze_plot_info_minmax_minute(entry, first, last, 1);
617 analyze_plot_info_minmax_minute(entry, first, last, 2);
620 static struct plot_info *analyze_plot_info(struct plot_info *pi)
625 /* Smoothing function: 5-point triangular smooth */
626 for (i = 2; i < nr-2; i++) {
627 struct plot_data *entry = pi->entry+i;
630 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
631 entry->smoothed = (val+4) / 9;
634 /* One-, two- and three-minute minmax data */
635 for (i = 0; i < nr; i++) {
636 struct plot_data *entry = pi->entry +i;
637 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
644 * Create a plot-info with smoothing and ranged min/max
646 * This also makes sure that we have extra empty events on both
647 * sides, so that you can do end-points without having to worry
650 static struct plot_info *depth_plot_info(struct dive *dive)
652 int lastdepth, maxtime;
653 int i, nr = dive->samples + 4, sec;
654 size_t alloc_size = plot_info_size(nr);
655 struct plot_info *pi;
657 pi = malloc(alloc_size);
660 memset(pi, 0, alloc_size);
665 for (i = 0; i < dive->samples; i++) {
667 struct sample *sample = dive->sample+i;
668 struct plot_data *entry = pi->entry + i + 2;
670 sec = entry->sec = sample->time.seconds;
671 depth = entry->val = sample->depth.mm;
672 if (depth || lastdepth)
678 /* Fill in the last two entries with empty values but valid times */
679 i = dive->samples + 2;
680 pi->entry[i].sec = sec + 20;
681 pi->entry[i+1].sec = sec + 40;
682 pi->maxtime = maxtime;
684 return analyze_plot_info(pi);
687 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
690 struct plot_info *pi = depth_plot_info(dive);
694 cairo_translate(gc->cr, topx, topy);
695 cairo_set_line_width(gc->cr, 2);
696 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
697 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
700 * We can use "cairo_translate()" because that doesn't
701 * scale line width etc. But the actual scaling we need
702 * do set up ourselves..
706 gc->maxx = (w - 2*topx);
707 gc->maxy = (h - 2*topy);
709 /* Temperature profile */
710 plot_temperature_profile(dive, gc, pi);
712 /* Cylinder pressure plot */
713 plot_cylinder_pressure(dive, gc, pi);
716 plot_depth_profile(dive, gc, pi);
718 /* Text on top of all graphs.. */
719 plot_temperature_text(dive, gc, pi);
720 plot_depth_text(dive, gc, pi);
721 plot_cylinder_pressure_text(dive, gc, pi);
723 /* And info box in the lower right corner.. */
724 gc->leftx = 0; gc->rightx = 1.0;
725 gc->topy = 0; gc->bottomy = 1.0;
728 /* Bounding box last */
729 set_source_rgb(gc, 1, 1, 1);
734 cairo_close_path(gc->cr);
735 cairo_stroke(gc->cr);
739 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
741 struct dive *dive = current_dive;
742 struct graphics_context gc = { .printer = 0 };
745 w = widget->allocation.width;
746 h = widget->allocation.height;
748 gc.cr = gdk_cairo_create(widget->window);
749 set_source_rgb(&gc, 0, 0, 0);
753 plot(&gc, w, h, dive);
755 cairo_destroy(gc.cr);
760 GtkWidget *dive_profile_widget(void)
764 da = gtk_drawing_area_new();
765 gtk_widget_set_size_request(da, 350, 250);
766 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);