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 round_seconds_up(int seconds)
71 return MAX(30*60, ROUND_UP(seconds, 60*10));
74 static int round_depth_up(depth_t depth)
76 unsigned mm = depth.mm;
78 return MAX(30000, ROUND_UP(mm+3000, 10000));
84 enum {CENTER,LEFT} halign;
85 enum {MIDDLE,TOP,BOTTOM} valign;
86 } text_render_options_t;
88 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
89 double x, double y, const char *fmt, ...)
92 cairo_text_extents_t extents;
98 vsnprintf(buffer, sizeof(buffer), fmt, args);
101 cairo_set_font_size(cr, tro->size);
102 cairo_text_extents(cr, buffer, &extents);
104 switch (tro->halign) {
106 dx = -(extents.width/2 + extents.x_bearing);
112 switch (tro->valign) {
114 dy = extents.height * 1.2;
117 dy = -extents.height * 0.8;
125 cairo_rel_move_to(cr, dx, dy);
127 cairo_text_path(cr, buffer);
128 set_source_rgb(gc, 0, 0, 0);
132 cairo_rel_move_to(cr, dx, dy);
134 set_source_rgb(gc, tro->r, tro->g, tro->b);
135 cairo_show_text(cr, buffer);
138 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
140 int sec = entry->sec;
141 depth_t depth = { entry->val };
145 switch (output_units.length) {
147 d = depth.mm / 1000.0;
155 plot_text(gc, tro, sec, depth.mm, fmt, d);
158 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
160 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
161 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
164 for (i = 0; i < pi->nr; i++) {
165 struct plot_data *entry = pi->entry + i;
167 if (entry->val < 2000)
170 if (entry == entry->max[2])
171 render_depth_sample(gc, entry, &deep);
173 if (entry == entry->min[2])
174 render_depth_sample(gc, entry, &shallow);
178 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
180 int maxtime, maxdepth;
182 /* Get plot scaling limits */
183 maxtime = round_seconds_up(dive->duration.seconds);
184 maxdepth = round_depth_up(dive->maxdepth);
186 gc->leftx = 0; gc->rightx = maxtime;
187 gc->topy = 0; gc->bottomy = maxdepth;
189 plot_text_samples(gc, pi);
192 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
195 struct plot_data *entry = pi->entry;
197 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
198 move_to(gc, entry->sec, entry->smoothed);
199 for (i = 1; i < pi->nr; i++) {
201 line_to(gc, entry->sec, entry->smoothed);
203 cairo_stroke(gc->cr);
206 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
210 struct plot_data *entry = pi->entry;
212 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
213 move_to(gc, entry->sec, entry->min[index]->val);
214 for (i = 1; i < pi->nr; i++) {
216 line_to(gc, entry->sec, entry->min[index]->val);
218 for (i = 1; i < pi->nr; i++) {
219 line_to(gc, entry->sec, entry->max[index]->val);
222 cairo_close_path(gc->cr);
226 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
230 plot_minmax_profile_minute(gc, pi, 2, 0.1);
231 plot_minmax_profile_minute(gc, pi, 1, 0.1);
232 plot_minmax_profile_minute(gc, pi, 0, 0.1);
235 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
238 cairo_t *cr = gc->cr;
239 int begins, sec, depth;
240 struct plot_data *entry;
241 int maxtime, maxdepth, marker;
243 /* Get plot scaling limits */
244 maxtime = round_seconds_up(dive->duration.seconds);
245 maxdepth = round_depth_up(dive->maxdepth);
247 /* Time markers: every 5 min */
248 gc->leftx = 0; gc->rightx = maxtime;
249 gc->topy = 0; gc->bottomy = 1.0;
250 for (i = 5*60; i < maxtime; i += 5*60) {
255 /* Depth markers: every 30 ft or 10 m*/
256 gc->leftx = 0; gc->rightx = 1.0;
257 gc->topy = 0; gc->bottomy = maxdepth;
258 switch (output_units.length) {
259 case METERS: marker = 10000; break;
260 case FEET: marker = 9144; break; /* 30 ft */
263 set_source_rgba(gc, 1, 1, 1, 0.5);
264 for (i = marker; i < maxdepth; i += marker) {
270 /* Show mean depth */
271 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
272 move_to(gc, 0, dive->meandepth.mm);
273 line_to(gc, 1, dive->meandepth.mm);
276 gc->leftx = 0; gc->rightx = maxtime;
278 plot_smoothed_profile(gc, pi);
279 plot_minmax_profile(gc, pi);
282 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
284 move_to(gc, entry->sec, entry->val);
285 for (i = 1; i < pi->nr; i++) {
288 if (sec <= maxtime) {
290 line_to(gc, sec, depth);
293 gc->topy = 0; gc->bottomy = 1.0;
294 line_to(gc, MIN(sec,maxtime), 0);
295 line_to(gc, begins, 0);
296 cairo_close_path(cr);
297 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
298 cairo_fill_preserve(cr);
299 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
303 static int setup_temperature_limits(struct dive *dive, struct graphics_context *gc)
306 int maxtime, mintemp, maxtemp;
308 /* Get plot scaling limits */
309 maxtime = round_seconds_up(dive->duration.seconds);
312 for (i = 0; i < dive->samples; i++) {
313 struct sample *sample = dive->sample+i;
314 int mkelvin = sample->temperature.mkelvin;
317 if (mkelvin > maxtemp)
319 if (mkelvin < mintemp)
323 gc->leftx = 0; gc->rightx = maxtime;
324 /* Show temperatures in roughly the lower third */
325 gc->topy = maxtemp + (maxtemp - mintemp)*2;
326 gc->bottomy = mintemp - (maxtemp - mintemp)/2;
328 return maxtemp > mintemp;
331 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc)
334 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
338 if (!setup_temperature_limits(dive, gc))
341 for (i = 0; i < dive->samples; i++) {
343 struct sample *sample = dive->sample+i;
344 int mkelvin = sample->temperature.mkelvin;
348 sec = sample->time.seconds;
352 if (output_units.temperature == FAHRENHEIT) {
353 deg = to_F(sample->temperature);
356 deg = to_C(sample->temperature);
359 plot_text(gc, &tro, sec, mkelvin, "%d %s", deg, unit);
363 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
366 cairo_t *cr = gc->cr;
369 if (!setup_temperature_limits(dive, gc))
372 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
373 for (i = 0; i < dive->samples; i++) {
374 struct sample *sample = dive->sample+i;
375 if (sample->time.seconds > dive->duration.seconds)
376 break; /* let's not plot surface temp events */
377 int mkelvin = sample->temperature.mkelvin;
384 line_to(gc, sample->time.seconds, mkelvin);
386 move_to(gc, sample->time.seconds, mkelvin);
392 /* gets both the actual start and end pressure as well as the scaling factors */
393 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
394 pressure_t *startp, pressure_t *endp)
399 gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
404 startp->mbar = endp->mbar = 0;
406 for (i = 0; i < dive->samples; i++) {
408 struct sample *sample = dive->sample + i;
410 /* FIXME! We only track cylinder 0 right now */
411 if (sample->cylinderindex)
413 mbar = sample->cylinderpressure.mbar;
427 gc->topy = 0; gc->bottomy = max * 1.5;
431 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
435 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
438 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
440 move_to(gc, 0, dive->cylinder[0].start.mbar);
441 for (i = 1; i < dive->samples; i++) {
443 struct sample *sample = dive->sample + i;
445 mbar = sample->cylinderpressure.mbar;
448 sec = sample->time.seconds;
449 if (sec <= dive->duration.seconds)
450 line_to(gc, sec, mbar);
453 * We may have "surface time" events, in which case we don't go
454 * back to dive duration
456 if (sec < dive->duration.seconds)
457 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
458 cairo_stroke(gc->cr);
462 * Return air usage (in liters).
464 static double calculate_airuse(struct dive *dive)
469 for (i = 0; i < MAX_CYLINDERS; i++) {
470 cylinder_t *cyl = dive->cylinder + i;
471 int size = cyl->type.size.mliter;
477 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
479 /* Liters of air at 1 atm == milliliters at 1k atm*/
480 airuse += kilo_atm * size;
485 static void plot_info(struct dive *dive, struct graphics_context *gc)
487 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
488 const double liters_per_cuft = 28.317;
489 const char *unit, *desc;
492 airuse = calculate_airuse(dive);
496 /* I really need to start addign some unit setting thing */
497 switch (output_units.volume) {
503 airuse /= liters_per_cuft;
506 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
507 if (dive->duration.seconds) {
508 double pressure = 1 + (dive->meandepth.mm / 10000.0);
509 double sac = airuse / pressure * 60 / dive->duration.seconds;
510 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
512 desc = dive->cylinder[0].type.description;
513 if (desc || dive->cylinder[0].gasmix.o2.permille) {
514 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
519 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
523 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
525 pressure_t startp, endp;
527 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
529 const char *unit = "bar";
531 switch (output_units.pressure) {
533 start = startp.mbar * 100;
534 end = startp.mbar * 100;
538 start = (startp.mbar + 500) / 1000;
539 end = (endp.mbar + 500) / 1000;
543 start = to_PSI(startp);
549 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
550 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
551 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
556 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
558 struct plot_data *p = entry;
559 int time = entry->sec;
560 int seconds = 90*(index+1);
561 struct plot_data *min, *max;
564 /* Go back 'seconds' in time */
566 if (p[-1].sec < time - seconds)
571 /* Then go forward until we hit an entry past the time */
577 if (p->sec > time + seconds)
586 entry->min[index] = min;
587 entry->max[index] = max;
588 entry->avg[index] = (avg + nr/2) / nr;
591 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
593 analyze_plot_info_minmax_minute(entry, first, last, 0);
594 analyze_plot_info_minmax_minute(entry, first, last, 1);
595 analyze_plot_info_minmax_minute(entry, first, last, 2);
598 static struct plot_info *analyze_plot_info(struct plot_info *pi)
603 /* Smoothing function: 5-point triangular smooth */
604 for (i = 2; i < nr-2; i++) {
605 struct plot_data *entry = pi->entry+i;
608 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
609 entry->smoothed = (val+4) / 9;
612 /* One-, two- and three-minute minmax data */
613 for (i = 0; i < nr; i++) {
614 struct plot_data *entry = pi->entry +i;
615 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
622 * Create a plot-info with smoothing and ranged min/max
624 * This also makes sure that we have extra empty events on both
625 * sides, so that you can do end-points without having to worry
628 static struct plot_info *depth_plot_info(struct dive *dive)
630 int i, nr = dive->samples + 4, sec;
631 size_t alloc_size = plot_info_size(nr);
632 struct plot_info *pi;
634 pi = malloc(alloc_size);
637 memset(pi, 0, alloc_size);
640 for (i = 0; i < dive->samples; i++) {
641 struct sample *sample = dive->sample+i;
642 struct plot_data *entry = pi->entry + i + 2;
644 sec = entry->sec = sample->time.seconds;
645 entry->val = sample->depth.mm;
647 /* Fill in the last two entries with empty values but valid times */
648 i = dive->samples + 2;
649 pi->entry[i].sec = sec + 20;
650 pi->entry[i+1].sec = sec + 40;
652 return analyze_plot_info(pi);
655 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
658 struct plot_info *pi = depth_plot_info(dive);
662 cairo_translate(gc->cr, topx, topy);
663 cairo_set_line_width(gc->cr, 2);
664 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
665 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
668 * We can use "cairo_translate()" because that doesn't
669 * scale line width etc. But the actual scaling we need
670 * do set up ourselves..
674 gc->maxx = (w - 2*topx);
675 gc->maxy = (h - 2*topy);
677 /* Temperature profile */
678 plot_temperature_profile(dive, gc);
680 /* Cylinder pressure plot */
681 plot_cylinder_pressure(dive, gc);
684 plot_depth_profile(dive, gc, pi);
686 /* Text on top of all graphs.. */
687 plot_temperature_text(dive, gc);
688 plot_depth_text(dive, gc, pi);
689 plot_cylinder_pressure_text(dive, gc);
691 /* And info box in the lower right corner.. */
692 gc->leftx = 0; gc->rightx = 1.0;
693 gc->topy = 0; gc->bottomy = 1.0;
696 /* Bounding box last */
697 set_source_rgb(gc, 1, 1, 1);
702 cairo_close_path(gc->cr);
703 cairo_stroke(gc->cr);
707 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
709 struct dive *dive = current_dive;
710 struct graphics_context gc = { .printer = 0 };
713 w = widget->allocation.width;
714 h = widget->allocation.height;
716 gc.cr = gdk_cairo_create(widget->window);
717 set_source_rgb(&gc, 0, 0, 0);
721 plot(&gc, w, h, dive);
723 cairo_destroy(gc.cr);
728 GtkWidget *dive_profile_widget(void)
732 da = gtk_drawing_area_new();
733 gtk_widget_set_size_request(da, 350, 250);
734 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);