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_single_temp_text(struct graphics_context *gc, int sec, temperature_t temperature)
335 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
337 if (output_units.temperature == FAHRENHEIT) {
338 deg = to_F(temperature);
341 deg = to_C(temperature);
344 plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
347 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc)
351 temperature_t last_temperature, last_printed_temp;
353 if (!setup_temperature_limits(dive, gc))
356 for (i = 0; i < dive->samples; i++) {
357 struct sample *sample = dive->sample+i;
358 if (sample->time.seconds > dive->duration.seconds)
359 break; /* let's not plot surface temp events */
360 int mkelvin = sample->temperature.mkelvin;
364 last_temperature = sample->temperature;
365 sec = sample->time.seconds;
369 plot_single_temp_text(gc,sec,sample->temperature);
370 last_printed_temp = last_temperature ;
372 /* it would be nice to print the end temperature, if it's different */
373 if (last_temperature.mkelvin != last_printed_temp.mkelvin) {
374 plot_single_temp_text(gc,dive->duration.seconds,last_temperature);
378 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
381 cairo_t *cr = gc->cr;
384 if (!setup_temperature_limits(dive, gc))
387 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
388 for (i = 0; i < dive->samples; i++) {
389 struct sample *sample = dive->sample+i;
390 if (sample->time.seconds > dive->duration.seconds)
391 break; /* let's not plot surface temp events */
392 int mkelvin = sample->temperature.mkelvin;
399 line_to(gc, sample->time.seconds, mkelvin);
401 move_to(gc, sample->time.seconds, mkelvin);
407 /* gets both the actual start and end pressure as well as the scaling factors */
408 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
409 pressure_t *startp, pressure_t *endp)
414 gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
419 startp->mbar = endp->mbar = 0;
421 for (i = 0; i < dive->samples; i++) {
423 struct sample *sample = dive->sample + i;
425 /* FIXME! We only track cylinder 0 right now */
426 if (sample->cylinderindex)
428 mbar = sample->cylinderpressure.mbar;
442 gc->topy = 0; gc->bottomy = max * 1.5;
446 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
450 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
453 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
455 move_to(gc, 0, dive->cylinder[0].start.mbar);
456 for (i = 1; i < dive->samples; i++) {
458 struct sample *sample = dive->sample + i;
460 mbar = sample->cylinderpressure.mbar;
463 sec = sample->time.seconds;
464 if (sec <= dive->duration.seconds)
465 line_to(gc, sec, mbar);
468 * We may have "surface time" events, in which case we don't go
469 * back to dive duration
471 if (sec < dive->duration.seconds)
472 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
473 cairo_stroke(gc->cr);
477 * Return air usage (in liters).
479 static double calculate_airuse(struct dive *dive)
484 for (i = 0; i < MAX_CYLINDERS; i++) {
485 cylinder_t *cyl = dive->cylinder + i;
486 int size = cyl->type.size.mliter;
492 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
494 /* Liters of air at 1 atm == milliliters at 1k atm*/
495 airuse += kilo_atm * size;
500 static void plot_info(struct dive *dive, struct graphics_context *gc)
502 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
503 const double liters_per_cuft = 28.317;
504 const char *unit, *desc;
507 airuse = calculate_airuse(dive);
511 /* I really need to start addign some unit setting thing */
512 switch (output_units.volume) {
518 airuse /= liters_per_cuft;
521 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
522 if (dive->duration.seconds) {
523 double pressure = 1 + (dive->meandepth.mm / 10000.0);
524 double sac = airuse / pressure * 60 / dive->duration.seconds;
525 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
527 desc = dive->cylinder[0].type.description;
528 if (desc || dive->cylinder[0].gasmix.o2.permille) {
529 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
534 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
538 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
540 pressure_t startp, endp;
542 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
544 const char *unit = "bar";
546 switch (output_units.pressure) {
548 start = startp.mbar * 100;
549 end = startp.mbar * 100;
553 start = (startp.mbar + 500) / 1000;
554 end = (endp.mbar + 500) / 1000;
558 start = to_PSI(startp);
564 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
565 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
566 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
571 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
573 struct plot_data *p = entry;
574 int time = entry->sec;
575 int seconds = 90*(index+1);
576 struct plot_data *min, *max;
579 /* Go back 'seconds' in time */
581 if (p[-1].sec < time - seconds)
586 /* Then go forward until we hit an entry past the time */
592 if (p->sec > time + seconds)
601 entry->min[index] = min;
602 entry->max[index] = max;
603 entry->avg[index] = (avg + nr/2) / nr;
606 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
608 analyze_plot_info_minmax_minute(entry, first, last, 0);
609 analyze_plot_info_minmax_minute(entry, first, last, 1);
610 analyze_plot_info_minmax_minute(entry, first, last, 2);
613 static struct plot_info *analyze_plot_info(struct plot_info *pi)
618 /* Smoothing function: 5-point triangular smooth */
619 for (i = 2; i < nr-2; i++) {
620 struct plot_data *entry = pi->entry+i;
623 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
624 entry->smoothed = (val+4) / 9;
627 /* One-, two- and three-minute minmax data */
628 for (i = 0; i < nr; i++) {
629 struct plot_data *entry = pi->entry +i;
630 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
637 * Create a plot-info with smoothing and ranged min/max
639 * This also makes sure that we have extra empty events on both
640 * sides, so that you can do end-points without having to worry
643 static struct plot_info *depth_plot_info(struct dive *dive)
645 int i, nr = dive->samples + 4, sec;
646 size_t alloc_size = plot_info_size(nr);
647 struct plot_info *pi;
649 pi = malloc(alloc_size);
652 memset(pi, 0, alloc_size);
655 for (i = 0; i < dive->samples; i++) {
656 struct sample *sample = dive->sample+i;
657 struct plot_data *entry = pi->entry + i + 2;
659 sec = entry->sec = sample->time.seconds;
660 entry->val = sample->depth.mm;
662 /* Fill in the last two entries with empty values but valid times */
663 i = dive->samples + 2;
664 pi->entry[i].sec = sec + 20;
665 pi->entry[i+1].sec = sec + 40;
667 return analyze_plot_info(pi);
670 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
673 struct plot_info *pi = depth_plot_info(dive);
677 cairo_translate(gc->cr, topx, topy);
678 cairo_set_line_width(gc->cr, 2);
679 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
680 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
683 * We can use "cairo_translate()" because that doesn't
684 * scale line width etc. But the actual scaling we need
685 * do set up ourselves..
689 gc->maxx = (w - 2*topx);
690 gc->maxy = (h - 2*topy);
692 /* Temperature profile */
693 plot_temperature_profile(dive, gc);
695 /* Cylinder pressure plot */
696 plot_cylinder_pressure(dive, gc);
699 plot_depth_profile(dive, gc, pi);
701 /* Text on top of all graphs.. */
702 plot_temperature_text(dive, gc);
703 plot_depth_text(dive, gc, pi);
704 plot_cylinder_pressure_text(dive, gc);
706 /* And info box in the lower right corner.. */
707 gc->leftx = 0; gc->rightx = 1.0;
708 gc->topy = 0; gc->bottomy = 1.0;
711 /* Bounding box last */
712 set_source_rgb(gc, 1, 1, 1);
717 cairo_close_path(gc->cr);
718 cairo_stroke(gc->cr);
722 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
724 struct dive *dive = current_dive;
725 struct graphics_context gc = { .printer = 0 };
728 w = widget->allocation.width;
729 h = widget->allocation.height;
731 gc.cr = gdk_cairo_create(widget->window);
732 set_source_rgb(&gc, 0, 0, 0);
736 plot(&gc, w, h, dive);
738 cairo_destroy(gc.cr);
743 GtkWidget *dive_profile_widget(void)
747 da = gtk_drawing_area_new();
748 gtk_widget_set_size_request(da, 350, 250);
749 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);