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 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
46 * When showing dive profiles, we scale things to the
47 * current dive. However, we don't scale past less than
48 * 30 minutes or 90 ft, just so that small dives show
51 static int round_seconds_up(int seconds)
53 return MAX(30*60, ROUND_UP(seconds, 60*10));
56 static int round_depth_up(depth_t depth)
58 unsigned mm = depth.mm;
60 return MAX(30000, ROUND_UP(mm+3000, 10000));
66 enum {CENTER,LEFT} halign;
67 enum {MIDDLE,TOP,BOTTOM} valign;
68 } text_render_options_t;
70 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
71 double x, double y, const char *fmt, ...)
74 cairo_text_extents_t extents;
80 vsnprintf(buffer, sizeof(buffer), fmt, args);
83 cairo_set_font_size(cr, tro->size);
84 cairo_text_extents(cr, buffer, &extents);
86 switch (tro->halign) {
88 dx = -(extents.width/2 + extents.x_bearing);
94 switch (tro->valign) {
96 dy = extents.height * 1.2;
99 dy = -extents.height * 0.8;
107 cairo_rel_move_to(cr, dx, dy);
109 cairo_text_path(cr, buffer);
110 cairo_set_source_rgb(cr, 0, 0, 0);
114 cairo_rel_move_to(cr, dx, dy);
116 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
117 cairo_show_text(cr, buffer);
120 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
122 int sec = entry->sec;
123 depth_t depth = { entry->val };
127 switch (output_units.length) {
129 d = depth.mm / 1000.0;
137 plot_text(gc, tro, sec, depth.mm, fmt, d);
140 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
142 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
143 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
146 for (i = 0; i < pi->nr; i++) {
147 struct plot_data *entry = pi->entry + i;
149 if (entry->val < 2000)
152 if (entry == entry->max[2])
153 render_depth_sample(gc, entry, &deep);
155 if (entry == entry->min[2])
156 render_depth_sample(gc, entry, &shallow);
160 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
162 int maxtime, maxdepth;
164 /* Get plot scaling limits */
165 maxtime = round_seconds_up(dive->duration.seconds);
166 maxdepth = round_depth_up(dive->maxdepth);
168 gc->leftx = 0; gc->rightx = maxtime;
169 gc->topy = 0; gc->bottomy = maxdepth;
171 plot_text_samples(gc, pi);
174 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
177 struct plot_data *entry = pi->entry;
179 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
180 move_to(gc, entry->sec, entry->smoothed);
181 for (i = 1; i < pi->nr; i++) {
183 line_to(gc, entry->sec, entry->smoothed);
185 cairo_stroke(gc->cr);
188 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
192 struct plot_data *entry = pi->entry;
194 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
195 move_to(gc, entry->sec, entry->min[index]->val);
196 for (i = 1; i < pi->nr; i++) {
198 line_to(gc, entry->sec, entry->min[index]->val);
200 for (i = 1; i < pi->nr; i++) {
201 line_to(gc, entry->sec, entry->max[index]->val);
204 cairo_close_path(gc->cr);
208 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
210 plot_minmax_profile_minute(gc, pi, 2, 0.1);
211 plot_minmax_profile_minute(gc, pi, 1, 0.1);
212 plot_minmax_profile_minute(gc, pi, 0, 0.1);
215 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
218 cairo_t *cr = gc->cr;
219 int begins, sec, depth;
220 struct plot_data *entry;
221 int maxtime, maxdepth, marker;
223 /* Get plot scaling limits */
224 maxtime = round_seconds_up(dive->duration.seconds);
225 maxdepth = round_depth_up(dive->maxdepth);
227 /* Time markers: every 5 min */
228 gc->leftx = 0; gc->rightx = maxtime;
229 gc->topy = 0; gc->bottomy = 1.0;
230 for (i = 5*60; i < maxtime; i += 5*60) {
235 /* Depth markers: every 30 ft or 10 m*/
236 gc->leftx = 0; gc->rightx = 1.0;
237 gc->topy = 0; gc->bottomy = maxdepth;
238 switch (output_units.length) {
239 case METERS: marker = 10000; break;
240 case FEET: marker = 9144; break; /* 30 ft */
243 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
244 for (i = marker; i < maxdepth; i += marker) {
250 /* Show mean depth */
251 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
252 move_to(gc, 0, dive->meandepth.mm);
253 line_to(gc, 1, dive->meandepth.mm);
256 gc->leftx = 0; gc->rightx = maxtime;
258 plot_smoothed_profile(gc, pi);
259 plot_minmax_profile(gc, pi);
262 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
264 move_to(gc, entry->sec, entry->val);
265 for (i = 1; i < pi->nr; i++) {
268 if (sec <= maxtime) {
270 line_to(gc, sec, depth);
273 gc->topy = 0; gc->bottomy = 1.0;
274 line_to(gc, MIN(sec,maxtime), 0);
275 line_to(gc, begins, 0);
276 cairo_close_path(cr);
277 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
278 cairo_fill_preserve(cr);
279 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
283 static int setup_temperature_limits(struct dive *dive, struct graphics_context *gc)
286 int maxtime, mintemp, maxtemp;
288 /* Get plot scaling limits */
289 maxtime = round_seconds_up(dive->duration.seconds);
292 for (i = 0; i < dive->samples; i++) {
293 struct sample *sample = dive->sample+i;
294 int mkelvin = sample->temperature.mkelvin;
297 if (mkelvin > maxtemp)
299 if (mkelvin < mintemp)
303 gc->leftx = 0; gc->rightx = maxtime;
304 /* Show temperatures in roughly the lower third */
305 gc->topy = maxtemp + (maxtemp - mintemp)*2;
306 gc->bottomy = mintemp - (maxtemp - mintemp)/2;
308 return maxtemp > mintemp;
311 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc)
314 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
318 if (!setup_temperature_limits(dive, gc))
321 for (i = 0; i < dive->samples; i++) {
323 struct sample *sample = dive->sample+i;
324 int mkelvin = sample->temperature.mkelvin;
328 sec = sample->time.seconds;
332 if (output_units.temperature == FAHRENHEIT) {
333 deg = to_F(sample->temperature);
336 deg = to_C(sample->temperature);
339 plot_text(gc, &tro, sec, mkelvin, "%d %s", deg, unit);
343 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
346 cairo_t *cr = gc->cr;
349 if (!setup_temperature_limits(dive, gc))
352 cairo_set_source_rgba(cr, 0.2, 0.2, 1.0, 0.8);
353 for (i = 0; i < dive->samples; i++) {
354 struct sample *sample = dive->sample+i;
355 int mkelvin = sample->temperature.mkelvin;
362 line_to(gc, sample->time.seconds, mkelvin);
364 move_to(gc, sample->time.seconds, mkelvin);
370 /* gets both the actual start and end pressure as well as the scaling factors */
371 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
372 pressure_t *startp, pressure_t *endp)
377 gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
382 startp->mbar = endp->mbar = 0;
384 for (i = 0; i < dive->samples; i++) {
386 struct sample *sample = dive->sample + i;
388 /* FIXME! We only track cylinder 0 right now */
389 if (sample->cylinderindex)
391 mbar = sample->cylinderpressure.mbar;
405 gc->topy = 0; gc->bottomy = max * 1.5;
409 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
413 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
416 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
418 move_to(gc, 0, dive->cylinder[0].start.mbar);
419 for (i = 1; i < dive->samples; i++) {
421 struct sample *sample = dive->sample + i;
423 mbar = sample->cylinderpressure.mbar;
426 sec = sample->time.seconds;
427 if (sec <= dive->duration.seconds)
428 line_to(gc, sec, mbar);
431 * We may have "surface time" events, in which case we don't go
432 * back to dive duration
434 if (sec < dive->duration.seconds)
435 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
436 cairo_stroke(gc->cr);
440 * Return air usage (in liters).
442 static double calculate_airuse(struct dive *dive)
447 for (i = 0; i < MAX_CYLINDERS; i++) {
448 cylinder_t *cyl = dive->cylinder + i;
449 int size = cyl->type.size.mliter;
455 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
457 /* Liters of air at 1 atm == milliliters at 1k atm*/
458 airuse += kilo_atm * size;
463 static void plot_info(struct dive *dive, struct graphics_context *gc)
465 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
466 const double liters_per_cuft = 28.317;
467 const char *unit, *desc;
470 airuse = calculate_airuse(dive);
474 /* I really need to start addign some unit setting thing */
475 switch (output_units.volume) {
481 airuse /= liters_per_cuft;
484 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
485 if (dive->duration.seconds) {
486 double pressure = 1 + (dive->meandepth.mm / 10000.0);
487 double sac = airuse / pressure * 60 / dive->duration.seconds;
488 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
490 desc = dive->cylinder[0].type.description;
491 if (desc || dive->cylinder[0].gasmix.o2.permille) {
492 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
497 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
501 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
503 pressure_t startp, endp;
505 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
507 const char *unit = "bar";
509 switch (output_units.pressure) {
511 start = startp.mbar * 100;
512 end = startp.mbar * 100;
516 start = (startp.mbar + 500) / 1000;
517 end = (endp.mbar + 500) / 1000;
521 start = to_PSI(startp);
527 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
528 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
529 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
534 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
536 struct plot_data *p = entry;
537 int time = entry->sec;
538 int seconds = 90*(index+1);
539 struct plot_data *min, *max;
542 /* Go back 'seconds' in time */
544 if (p[-1].sec < time - seconds)
549 /* Then go forward until we hit an entry past the time */
555 if (p->sec > time + seconds)
564 entry->min[index] = min;
565 entry->max[index] = max;
566 entry->avg[index] = (avg + nr/2) / nr;
569 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
571 analyze_plot_info_minmax_minute(entry, first, last, 0);
572 analyze_plot_info_minmax_minute(entry, first, last, 1);
573 analyze_plot_info_minmax_minute(entry, first, last, 2);
576 static struct plot_info *analyze_plot_info(struct plot_info *pi)
581 /* Smoothing function: 5-point triangular smooth */
582 for (i = 2; i < nr-2; i++) {
583 struct plot_data *entry = pi->entry+i;
586 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
587 entry->smoothed = (val+4) / 9;
590 /* One-, two- and three-minute minmax data */
591 for (i = 0; i < nr; i++) {
592 struct plot_data *entry = pi->entry +i;
593 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
600 * Create a plot-info with smoothing and ranged min/max
602 * This also makes sure that we have extra empty events on both
603 * sides, so that you can do end-points without having to worry
606 static struct plot_info *depth_plot_info(struct dive *dive)
608 int i, nr = dive->samples + 4, sec;
609 size_t alloc_size = plot_info_size(nr);
610 struct plot_info *pi;
612 pi = malloc(alloc_size);
615 memset(pi, 0, alloc_size);
618 for (i = 0; i < dive->samples; i++) {
619 struct sample *sample = dive->sample+i;
620 struct plot_data *entry = pi->entry + i + 2;
622 sec = entry->sec = sample->time.seconds;
623 entry->val = sample->depth.mm;
625 /* Fill in the last two entries with empty values but valid times */
626 i = dive->samples + 2;
627 pi->entry[i].sec = sec + 20;
628 pi->entry[i+1].sec = sec + 40;
630 return analyze_plot_info(pi);
633 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
636 struct plot_info *pi = depth_plot_info(dive);
640 cairo_translate(gc->cr, topx, topy);
641 cairo_set_line_width(gc->cr, 2);
642 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
643 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
646 * We can use "cairo_translate()" because that doesn't
647 * scale line width etc. But the actual scaling we need
648 * do set up ourselves..
652 gc->maxx = (w - 2*topx);
653 gc->maxy = (h - 2*topy);
655 /* Temperature profile */
656 plot_temperature_profile(dive, gc);
658 /* Cylinder pressure plot */
659 plot_cylinder_pressure(dive, gc);
662 plot_depth_profile(dive, gc, pi);
664 /* Text on top of all graphs.. */
665 plot_temperature_text(dive, gc);
666 plot_depth_text(dive, gc, pi);
667 plot_cylinder_pressure_text(dive, gc);
669 /* And info box in the lower right corner.. */
670 gc->leftx = 0; gc->rightx = 1.0;
671 gc->topy = 0; gc->bottomy = 1.0;
674 /* Bounding box last */
675 cairo_set_source_rgb(gc->cr, 1, 1, 1);
680 cairo_close_path(gc->cr);
681 cairo_stroke(gc->cr);
685 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
687 struct dive *dive = current_dive;
688 struct graphics_context gc;
691 w = widget->allocation.width;
692 h = widget->allocation.height;
694 gc.cr = gdk_cairo_create(widget->window);
695 cairo_set_source_rgb(gc.cr, 0, 0, 0);
699 plot(&gc, w, h, dive);
701 cairo_destroy(gc.cr);
706 GtkWidget *dive_profile_widget(void)
710 da = gtk_drawing_area_new();
711 gtk_widget_set_size_request(da, 350, 250);
712 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);