11 int selected_dive = 0;
14 * Cairo scaling really is horribly horribly mis-designed.
16 * Which is sad, because I really like Cairo otherwise. But
17 * the fact that the line width is scaled with the same scale
18 * as the coordinate system is a f*&%ing disaster. So we
19 * can't use it, and instead have this butt-ugly wrapper thing..
21 struct graphics_context {
24 double scalex, scaley;
27 /* Plot info with smoothing and one-, two- and three-minute minimums and maximums */
35 struct plot_data *min[3];
36 struct plot_data *max[3];
40 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
42 /* Scale to 0,0 -> maxx,maxy */
43 #define SCALE(gc,x,y) (x)*gc->maxx/gc->scalex,(y)*gc->maxy/gc->scaley
45 static void move_to(struct graphics_context *gc, double x, double y)
47 cairo_move_to(gc->cr, SCALE(gc, x, y));
50 static void line_to(struct graphics_context *gc, double x, double y)
52 cairo_line_to(gc->cr, SCALE(gc, x, y));
55 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
58 * When showing dive profiles, we scale things to the
59 * current dive. However, we don't scale past less than
60 * 30 minutes or 90 ft, just so that small dives show
63 static int round_seconds_up(int seconds)
65 return MAX(30*60, ROUND_UP(seconds, 60*10));
68 static int round_depth_up(depth_t depth)
70 unsigned mm = depth.mm;
72 return MAX(30000, ROUND_UP(mm+3000, 10000));
78 enum {CENTER,LEFT} halign;
79 enum {MIDDLE,TOP,BOTTOM} valign;
80 } text_render_options_t;
82 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
83 double x, double y, const char *fmt, ...)
86 cairo_text_extents_t extents;
92 vsnprintf(buffer, sizeof(buffer), fmt, args);
95 cairo_set_font_size(cr, tro->size);
96 cairo_text_extents(cr, buffer, &extents);
98 switch (tro->halign) {
100 dx = -(extents.width/2 + extents.x_bearing);
106 switch (tro->valign) {
108 dy = extents.height * 1.2;
111 dy = -extents.height * 0.8;
119 cairo_rel_move_to(cr, dx, dy);
121 cairo_text_path(cr, buffer);
122 cairo_set_source_rgb(cr, 0, 0, 0);
126 cairo_rel_move_to(cr, dx, dy);
128 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
129 cairo_show_text(cr, buffer);
132 static void render_depth_sample(struct graphics_context *gc, struct sample *sample, const text_render_options_t *tro)
134 int sec = sample->time.seconds;
135 depth_t depth = sample->depth;
139 switch (output_units.length) {
141 d = depth.mm / 1000.0;
149 plot_text(gc, tro, sec, depth.mm, fmt, d);
153 * Find the next minimum/maximum point.
155 * We exit early if we hit "enough" of a depth reversal,
156 * which is roughly 10 feet.
158 static struct sample *next_minmax(struct sample *sample, struct sample *end, int minmax)
160 const int enough = 3000;
161 struct sample *result;
167 depthlimit = sample->depth.mm;
176 depth = sample->depth.mm;
179 if (depth <= depthlimit) {
180 if (depthlimit - depth > enough)
185 if (depth >= depthlimit) {
186 if (depth - depthlimit > enough)
198 static void plot_text_samples(struct graphics_context *gc, struct sample *a, struct sample *b)
200 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
201 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
206 a = next_minmax(a, b, 1);
209 render_depth_sample(gc, a, &deep);
210 a = next_minmax(a, b, 0);
213 if (a->depth.mm < 2500)
215 render_depth_sample(gc, a, &shallow);
219 static void plot_depth_text(struct dive *dive, struct graphics_context *gc)
221 struct sample *sample, *end;
222 int maxtime, maxdepth;
224 /* Get plot scaling limits */
225 maxtime = round_seconds_up(dive->duration.seconds);
226 maxdepth = round_depth_up(dive->maxdepth);
228 gc->scalex = maxtime;
229 gc->scaley = maxdepth;
231 sample = dive->sample;
232 end = dive->sample + dive->samples;
234 plot_text_samples(gc, sample, end);
237 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
240 struct plot_data *entry = pi->entry;
242 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
243 move_to(gc, entry->sec, entry->smoothed);
244 for (i = 1; i < pi->nr; i++) {
246 line_to(gc, entry->sec, entry->smoothed);
248 cairo_stroke(gc->cr);
251 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
255 struct plot_data *entry = pi->entry;
257 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
258 move_to(gc, entry->sec, entry->min[index]->val);
259 for (i = 1; i < pi->nr; i++) {
261 line_to(gc, entry->sec, entry->min[index]->val);
263 for (i = 1; i < pi->nr; i++) {
264 line_to(gc, entry->sec, entry->max[index]->val);
267 cairo_close_path(gc->cr);
271 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
273 plot_minmax_profile_minute(gc, pi, 2, 0.1);
274 plot_minmax_profile_minute(gc, pi, 1, 0.1);
275 plot_minmax_profile_minute(gc, pi, 0, 0.1);
278 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
281 cairo_t *cr = gc->cr;
282 int begins, sec, depth;
283 struct plot_data *entry;
284 int maxtime, maxdepth, marker;
286 cairo_set_line_width(gc->cr, 2);
288 /* Get plot scaling limits */
289 maxtime = round_seconds_up(dive->duration.seconds);
290 maxdepth = round_depth_up(dive->maxdepth);
292 /* Time markers: every 5 min */
293 gc->scalex = maxtime;
295 for (i = 5*60; i < maxtime; i += 5*60) {
300 /* Depth markers: every 30 ft or 10 m*/
302 gc->scaley = maxdepth;
303 switch (output_units.length) {
304 case METERS: marker = 10000; break;
305 case FEET: marker = 9144; break; /* 30 ft */
308 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
309 for (i = marker; i < maxdepth; i += marker) {
315 /* Show mean depth */
316 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
317 move_to(gc, 0, dive->meandepth.mm);
318 line_to(gc, 1, dive->meandepth.mm);
321 gc->scalex = maxtime;
323 plot_smoothed_profile(gc, pi);
324 plot_minmax_profile(gc, pi);
327 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
329 move_to(gc, entry->sec, entry->val);
330 for (i = 1; i < pi->nr; i++) {
333 if (sec <= maxtime) {
335 line_to(gc, sec, depth);
339 line_to(gc, MIN(sec,maxtime), 0);
340 line_to(gc, begins, 0);
341 cairo_close_path(cr);
342 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
343 cairo_fill_preserve(cr);
344 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
348 /* gets both the actual start and end pressure as well as the scaling factors */
349 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
350 pressure_t *startp, pressure_t *endp)
355 gc->scalex = round_seconds_up(dive->duration.seconds);
360 startp->mbar = endp->mbar = 0;
362 for (i = 0; i < dive->samples; i++) {
364 struct sample *sample = dive->sample + i;
366 /* FIXME! We only track cylinder 0 right now */
367 if (sample->cylinderindex)
369 mbar = sample->cylinderpressure.mbar;
383 gc->scaley = max * 1.5;
387 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
391 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
394 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
396 move_to(gc, 0, dive->cylinder[0].start.mbar);
397 for (i = 1; i < dive->samples; i++) {
399 struct sample *sample = dive->sample + i;
401 mbar = sample->cylinderpressure.mbar;
404 sec = sample->time.seconds;
405 if (sec <= dive->duration.seconds)
406 line_to(gc, sec, mbar);
409 * We may have "surface time" events, in which case we don't go
410 * back to dive duration
412 if (sec < dive->duration.seconds)
413 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
414 cairo_stroke(gc->cr);
418 * Return air usage (in liters).
420 static double calculate_airuse(struct dive *dive)
425 for (i = 0; i < MAX_CYLINDERS; i++) {
426 cylinder_t *cyl = dive->cylinder + i;
427 int size = cyl->type.size.mliter;
433 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
435 /* Liters of air at 1 atm == milliliters at 1k atm*/
436 airuse += kilo_atm * size;
441 static void plot_info(struct dive *dive, struct graphics_context *gc)
443 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
444 const double liters_per_cuft = 28.317;
448 airuse = calculate_airuse(dive);
452 /* I really need to start addign some unit setting thing */
453 switch (output_units.volume) {
459 airuse /= liters_per_cuft;
462 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
463 if (dive->duration.seconds) {
464 double pressure = 1 + (dive->meandepth.mm / 10000.0);
465 double sac = airuse / pressure * 60 / dive->duration.seconds;
466 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
470 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
472 pressure_t startp, endp;
474 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
476 const char *unit = "bar";
478 switch (output_units.pressure) {
480 start = startp.mbar * 100;
481 end = startp.mbar * 100;
485 start = (startp.mbar + 500) / 1000;
486 end = (endp.mbar + 500) / 1000;
490 start = to_PSI(startp);
496 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
497 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
498 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
503 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
505 struct plot_data *p = entry;
506 int time = entry->sec;
507 int seconds = 90*(index+1);
508 struct plot_data *min, *max;
511 /* Go back 'seconds' in time */
513 if (p[-1].sec < time - seconds)
518 /* Then go forward until we hit an entry past the time */
524 if (p->sec > time + seconds)
533 entry->min[index] = min;
534 entry->max[index] = max;
535 entry->avg[index] = (avg + nr/2) / nr;
538 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
540 analyze_plot_info_minmax_minute(entry, first, last, 0);
541 analyze_plot_info_minmax_minute(entry, first, last, 1);
542 analyze_plot_info_minmax_minute(entry, first, last, 2);
545 static struct plot_info *analyze_plot_info(struct plot_info *pi)
550 /* Smoothing function: 5-point triangular smooth */
551 for (i = 2; i < nr-2; i++) {
552 struct plot_data *entry = pi->entry+i;
555 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
556 entry->smoothed = (val+4) / 9;
559 /* One-, two- and three-minute minmax data */
560 for (i = 0; i < nr; i++) {
561 struct plot_data *entry = pi->entry +i;
562 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
569 * Create a plot-info with smoothing and ranged min/max
571 * This also makes sure that we have extra empty events on both
572 * sides, so that you can do end-points without having to worry
575 static struct plot_info *depth_plot_info(struct dive *dive)
577 int i, nr = dive->samples + 4, sec;
578 size_t alloc_size = plot_info_size(nr);
579 struct plot_info *pi;
581 pi = malloc(alloc_size);
584 memset(pi, 0, alloc_size);
587 for (i = 0; i < dive->samples; i++) {
588 struct sample *sample = dive->sample+i;
589 struct plot_data *entry = pi->entry + i + 2;
591 sec = entry->sec = sample->time.seconds;
592 entry->val = sample->depth.mm;
594 /* Fill in the last two entries with empty values but valid times */
595 i = dive->samples + 2;
596 pi->entry[i].sec = sec + 20;
597 pi->entry[i+1].sec = sec + 40;
599 return analyze_plot_info(pi);
602 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
605 struct plot_info *pi = depth_plot_info(dive);
609 cairo_translate(gc->cr, topx, topy);
612 * We can use "cairo_translate()" because that doesn't
613 * scale line width etc. But the actual scaling we need
614 * do set up ourselves..
618 gc->maxx = (w - 2*topx);
619 gc->maxy = (h - 2*topy);
621 /* Cylinder pressure plot */
622 plot_cylinder_pressure(dive, gc);
625 plot_depth_profile(dive, gc, pi);
627 /* Text on top of all graphs.. */
628 plot_depth_text(dive, gc);
629 plot_cylinder_pressure_text(dive, gc);
631 /* And info box in the lower right corner.. */
632 gc->scalex = gc->scaley = 1.0;
635 /* Bounding box last */
636 cairo_set_source_rgb(gc->cr, 1, 1, 1);
641 cairo_close_path(gc->cr);
642 cairo_stroke(gc->cr);
646 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
648 struct dive *dive = current_dive;
649 struct graphics_context gc;
652 w = widget->allocation.width;
653 h = widget->allocation.height;
655 gc.cr = gdk_cairo_create(widget->window);
656 cairo_set_source_rgb(gc.cr, 0, 0, 0);
660 plot(&gc, w, h, dive);
662 cairo_destroy(gc.cr);
667 GtkWidget *dive_profile_widget(void)
671 da = gtk_drawing_area_new();
672 gtk_widget_set_size_request(da, 450, 350);
673 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);