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)
52 cairo_set_source_rgba(gc->cr, r, g, b, a);
55 static void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
57 set_source_rgba(gc, r, g, b, 1);
60 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
63 * When showing dive profiles, we scale things to the
64 * current dive. However, we don't scale past less than
65 * 30 minutes or 90 ft, just so that small dives show
68 static int round_seconds_up(int seconds)
70 return MAX(30*60, ROUND_UP(seconds, 60*10));
73 static int round_depth_up(depth_t depth)
75 unsigned mm = depth.mm;
77 return MAX(30000, ROUND_UP(mm+3000, 10000));
83 enum {CENTER,LEFT} halign;
84 enum {MIDDLE,TOP,BOTTOM} valign;
85 } text_render_options_t;
87 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
88 double x, double y, const char *fmt, ...)
91 cairo_text_extents_t extents;
97 vsnprintf(buffer, sizeof(buffer), fmt, args);
100 cairo_set_font_size(cr, tro->size);
101 cairo_text_extents(cr, buffer, &extents);
103 switch (tro->halign) {
105 dx = -(extents.width/2 + extents.x_bearing);
111 switch (tro->valign) {
113 dy = extents.height * 1.2;
116 dy = -extents.height * 0.8;
124 cairo_rel_move_to(cr, dx, dy);
126 cairo_text_path(cr, buffer);
127 set_source_rgb(gc, 0, 0, 0);
131 cairo_rel_move_to(cr, dx, dy);
133 set_source_rgb(gc, tro->r, tro->g, tro->b);
134 cairo_show_text(cr, buffer);
137 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
139 int sec = entry->sec;
140 depth_t depth = { entry->val };
144 switch (output_units.length) {
146 d = depth.mm / 1000.0;
154 plot_text(gc, tro, sec, depth.mm, fmt, d);
157 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
159 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
160 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
163 for (i = 0; i < pi->nr; i++) {
164 struct plot_data *entry = pi->entry + i;
166 if (entry->val < 2000)
169 if (entry == entry->max[2])
170 render_depth_sample(gc, entry, &deep);
172 if (entry == entry->min[2])
173 render_depth_sample(gc, entry, &shallow);
177 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
179 int maxtime, maxdepth;
181 /* Get plot scaling limits */
182 maxtime = round_seconds_up(dive->duration.seconds);
183 maxdepth = round_depth_up(dive->maxdepth);
185 gc->leftx = 0; gc->rightx = maxtime;
186 gc->topy = 0; gc->bottomy = maxdepth;
188 plot_text_samples(gc, pi);
191 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
194 struct plot_data *entry = pi->entry;
196 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
197 move_to(gc, entry->sec, entry->smoothed);
198 for (i = 1; i < pi->nr; i++) {
200 line_to(gc, entry->sec, entry->smoothed);
202 cairo_stroke(gc->cr);
205 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
209 struct plot_data *entry = pi->entry;
211 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
212 move_to(gc, entry->sec, entry->min[index]->val);
213 for (i = 1; i < pi->nr; i++) {
215 line_to(gc, entry->sec, entry->min[index]->val);
217 for (i = 1; i < pi->nr; i++) {
218 line_to(gc, entry->sec, entry->max[index]->val);
221 cairo_close_path(gc->cr);
225 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
229 plot_minmax_profile_minute(gc, pi, 2, 0.1);
230 plot_minmax_profile_minute(gc, pi, 1, 0.1);
231 plot_minmax_profile_minute(gc, pi, 0, 0.1);
234 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
237 cairo_t *cr = gc->cr;
238 int begins, sec, depth;
239 struct plot_data *entry;
240 int maxtime, maxdepth, marker;
242 /* Get plot scaling limits */
243 maxtime = round_seconds_up(dive->duration.seconds);
244 maxdepth = round_depth_up(dive->maxdepth);
246 /* Time markers: every 5 min */
247 gc->leftx = 0; gc->rightx = maxtime;
248 gc->topy = 0; gc->bottomy = 1.0;
249 for (i = 5*60; i < maxtime; i += 5*60) {
254 /* Depth markers: every 30 ft or 10 m*/
255 gc->leftx = 0; gc->rightx = 1.0;
256 gc->topy = 0; gc->bottomy = maxdepth;
257 switch (output_units.length) {
258 case METERS: marker = 10000; break;
259 case FEET: marker = 9144; break; /* 30 ft */
262 set_source_rgba(gc, 1, 1, 1, 0.5);
263 for (i = marker; i < maxdepth; i += marker) {
269 /* Show mean depth */
270 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
271 move_to(gc, 0, dive->meandepth.mm);
272 line_to(gc, 1, dive->meandepth.mm);
275 gc->leftx = 0; gc->rightx = maxtime;
277 plot_smoothed_profile(gc, pi);
278 plot_minmax_profile(gc, pi);
281 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
283 move_to(gc, entry->sec, entry->val);
284 for (i = 1; i < pi->nr; i++) {
287 if (sec <= maxtime) {
289 line_to(gc, sec, depth);
292 gc->topy = 0; gc->bottomy = 1.0;
293 line_to(gc, MIN(sec,maxtime), 0);
294 line_to(gc, begins, 0);
295 cairo_close_path(cr);
297 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
298 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)
307 int maxtime, mintemp, maxtemp;
309 /* Get plot scaling limits */
310 maxtime = round_seconds_up(dive->duration.seconds);
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_temperature_text(struct dive *dive, struct graphics_context *gc)
335 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
339 if (!setup_temperature_limits(dive, gc))
342 for (i = 0; i < dive->samples; i++) {
344 struct sample *sample = dive->sample+i;
345 int mkelvin = sample->temperature.mkelvin;
349 sec = sample->time.seconds;
353 if (output_units.temperature == FAHRENHEIT) {
354 deg = to_F(sample->temperature);
357 deg = to_C(sample->temperature);
360 plot_text(gc, &tro, sec, mkelvin, "%d %s", deg, unit);
364 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
367 cairo_t *cr = gc->cr;
370 if (!setup_temperature_limits(dive, gc))
373 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
374 for (i = 0; i < dive->samples; i++) {
375 struct sample *sample = dive->sample+i;
376 int mkelvin = sample->temperature.mkelvin;
383 line_to(gc, sample->time.seconds, mkelvin);
385 move_to(gc, sample->time.seconds, mkelvin);
391 /* gets both the actual start and end pressure as well as the scaling factors */
392 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
393 pressure_t *startp, pressure_t *endp)
398 gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
403 startp->mbar = endp->mbar = 0;
405 for (i = 0; i < dive->samples; i++) {
407 struct sample *sample = dive->sample + i;
409 /* FIXME! We only track cylinder 0 right now */
410 if (sample->cylinderindex)
412 mbar = sample->cylinderpressure.mbar;
426 gc->topy = 0; gc->bottomy = max * 1.5;
430 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
434 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
437 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
439 move_to(gc, 0, dive->cylinder[0].start.mbar);
440 for (i = 1; i < dive->samples; i++) {
442 struct sample *sample = dive->sample + i;
444 mbar = sample->cylinderpressure.mbar;
447 sec = sample->time.seconds;
448 if (sec <= dive->duration.seconds)
449 line_to(gc, sec, mbar);
452 * We may have "surface time" events, in which case we don't go
453 * back to dive duration
455 if (sec < dive->duration.seconds)
456 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
457 cairo_stroke(gc->cr);
461 * Return air usage (in liters).
463 static double calculate_airuse(struct dive *dive)
468 for (i = 0; i < MAX_CYLINDERS; i++) {
469 cylinder_t *cyl = dive->cylinder + i;
470 int size = cyl->type.size.mliter;
476 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
478 /* Liters of air at 1 atm == milliliters at 1k atm*/
479 airuse += kilo_atm * size;
484 static void plot_info(struct dive *dive, struct graphics_context *gc)
486 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
487 const double liters_per_cuft = 28.317;
488 const char *unit, *desc;
491 airuse = calculate_airuse(dive);
495 /* I really need to start addign some unit setting thing */
496 switch (output_units.volume) {
502 airuse /= liters_per_cuft;
505 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
506 if (dive->duration.seconds) {
507 double pressure = 1 + (dive->meandepth.mm / 10000.0);
508 double sac = airuse / pressure * 60 / dive->duration.seconds;
509 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
511 desc = dive->cylinder[0].type.description;
512 if (desc || dive->cylinder[0].gasmix.o2.permille) {
513 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
518 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
522 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
524 pressure_t startp, endp;
526 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
528 const char *unit = "bar";
530 switch (output_units.pressure) {
532 start = startp.mbar * 100;
533 end = startp.mbar * 100;
537 start = (startp.mbar + 500) / 1000;
538 end = (endp.mbar + 500) / 1000;
542 start = to_PSI(startp);
548 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
549 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
550 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
555 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
557 struct plot_data *p = entry;
558 int time = entry->sec;
559 int seconds = 90*(index+1);
560 struct plot_data *min, *max;
563 /* Go back 'seconds' in time */
565 if (p[-1].sec < time - seconds)
570 /* Then go forward until we hit an entry past the time */
576 if (p->sec > time + seconds)
585 entry->min[index] = min;
586 entry->max[index] = max;
587 entry->avg[index] = (avg + nr/2) / nr;
590 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
592 analyze_plot_info_minmax_minute(entry, first, last, 0);
593 analyze_plot_info_minmax_minute(entry, first, last, 1);
594 analyze_plot_info_minmax_minute(entry, first, last, 2);
597 static struct plot_info *analyze_plot_info(struct plot_info *pi)
602 /* Smoothing function: 5-point triangular smooth */
603 for (i = 2; i < nr-2; i++) {
604 struct plot_data *entry = pi->entry+i;
607 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
608 entry->smoothed = (val+4) / 9;
611 /* One-, two- and three-minute minmax data */
612 for (i = 0; i < nr; i++) {
613 struct plot_data *entry = pi->entry +i;
614 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
621 * Create a plot-info with smoothing and ranged min/max
623 * This also makes sure that we have extra empty events on both
624 * sides, so that you can do end-points without having to worry
627 static struct plot_info *depth_plot_info(struct dive *dive)
629 int i, nr = dive->samples + 4, sec;
630 size_t alloc_size = plot_info_size(nr);
631 struct plot_info *pi;
633 pi = malloc(alloc_size);
636 memset(pi, 0, alloc_size);
639 for (i = 0; i < dive->samples; i++) {
640 struct sample *sample = dive->sample+i;
641 struct plot_data *entry = pi->entry + i + 2;
643 sec = entry->sec = sample->time.seconds;
644 entry->val = sample->depth.mm;
646 /* Fill in the last two entries with empty values but valid times */
647 i = dive->samples + 2;
648 pi->entry[i].sec = sec + 20;
649 pi->entry[i+1].sec = sec + 40;
651 return analyze_plot_info(pi);
654 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
657 struct plot_info *pi = depth_plot_info(dive);
661 cairo_translate(gc->cr, topx, topy);
662 cairo_set_line_width(gc->cr, 2);
663 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
664 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
667 * We can use "cairo_translate()" because that doesn't
668 * scale line width etc. But the actual scaling we need
669 * do set up ourselves..
673 gc->maxx = (w - 2*topx);
674 gc->maxy = (h - 2*topy);
676 /* Temperature profile */
677 plot_temperature_profile(dive, gc);
679 /* Cylinder pressure plot */
680 plot_cylinder_pressure(dive, gc);
683 plot_depth_profile(dive, gc, pi);
685 /* Text on top of all graphs.. */
686 plot_temperature_text(dive, gc);
687 plot_depth_text(dive, gc, pi);
688 plot_cylinder_pressure_text(dive, gc);
690 /* And info box in the lower right corner.. */
691 gc->leftx = 0; gc->rightx = 1.0;
692 gc->topy = 0; gc->bottomy = 1.0;
695 /* Bounding box last */
696 set_source_rgb(gc, 1, 1, 1);
701 cairo_close_path(gc->cr);
702 cairo_stroke(gc->cr);
706 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
708 struct dive *dive = current_dive;
709 struct graphics_context gc = { .printer = 0 };
712 w = widget->allocation.width;
713 h = widget->allocation.height;
715 gc.cr = gdk_cairo_create(widget->window);
716 set_source_rgb(&gc, 0, 0, 0);
720 plot(&gc, w, h, dive);
722 cairo_destroy(gc.cr);
727 GtkWidget *dive_profile_widget(void)
731 da = gtk_drawing_area_new();
732 gtk_widget_set_size_request(da, 350, 250);
733 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);