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 {
28 /* Plot info with smoothing and one-, two- and three-minute minimums and maximums */
36 struct plot_data *min[3];
37 struct plot_data *max[3];
41 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
43 /* Scale to 0,0 -> maxx,maxy */
44 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
45 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
46 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
48 static void move_to(struct graphics_context *gc, double x, double y)
50 cairo_move_to(gc->cr, SCALE(gc, x, y));
53 static void line_to(struct graphics_context *gc, double x, double y)
55 cairo_line_to(gc->cr, SCALE(gc, x, y));
58 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
61 * When showing dive profiles, we scale things to the
62 * current dive. However, we don't scale past less than
63 * 30 minutes or 90 ft, just so that small dives show
66 static int round_seconds_up(int seconds)
68 return MAX(30*60, ROUND_UP(seconds, 60*10));
71 static int round_depth_up(depth_t depth)
73 unsigned mm = depth.mm;
75 return MAX(30000, ROUND_UP(mm+3000, 10000));
81 enum {CENTER,LEFT} halign;
82 enum {MIDDLE,TOP,BOTTOM} valign;
83 } text_render_options_t;
85 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
86 double x, double y, const char *fmt, ...)
89 cairo_text_extents_t extents;
95 vsnprintf(buffer, sizeof(buffer), fmt, args);
98 cairo_set_font_size(cr, tro->size);
99 cairo_text_extents(cr, buffer, &extents);
101 switch (tro->halign) {
103 dx = -(extents.width/2 + extents.x_bearing);
109 switch (tro->valign) {
111 dy = extents.height * 1.2;
114 dy = -extents.height * 0.8;
122 cairo_rel_move_to(cr, dx, dy);
124 cairo_text_path(cr, buffer);
125 cairo_set_source_rgb(cr, 0, 0, 0);
129 cairo_rel_move_to(cr, dx, dy);
131 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
132 cairo_show_text(cr, buffer);
135 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
137 int sec = entry->sec;
138 depth_t depth = { entry->val };
142 switch (output_units.length) {
144 d = depth.mm / 1000.0;
152 plot_text(gc, tro, sec, depth.mm, fmt, d);
155 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
157 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
158 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
161 for (i = 0; i < pi->nr; i++) {
162 struct plot_data *entry = pi->entry + i;
164 if (entry->val < 2000)
167 if (entry == entry->max[2])
168 render_depth_sample(gc, entry, &deep);
170 if (entry == entry->min[2])
171 render_depth_sample(gc, entry, &shallow);
175 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
177 int maxtime, maxdepth;
179 /* Get plot scaling limits */
180 maxtime = round_seconds_up(dive->duration.seconds);
181 maxdepth = round_depth_up(dive->maxdepth);
183 gc->leftx = 0; gc->rightx = maxtime;
184 gc->topy = 0; gc->bottomy = maxdepth;
186 plot_text_samples(gc, pi);
189 static void plot_smoothed_profile(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, 0.2, 0.20);
195 move_to(gc, entry->sec, entry->smoothed);
196 for (i = 1; i < pi->nr; i++) {
198 line_to(gc, entry->sec, entry->smoothed);
200 cairo_stroke(gc->cr);
203 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
207 struct plot_data *entry = pi->entry;
209 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
210 move_to(gc, entry->sec, entry->min[index]->val);
211 for (i = 1; i < pi->nr; i++) {
213 line_to(gc, entry->sec, entry->min[index]->val);
215 for (i = 1; i < pi->nr; i++) {
216 line_to(gc, entry->sec, entry->max[index]->val);
219 cairo_close_path(gc->cr);
223 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
225 plot_minmax_profile_minute(gc, pi, 2, 0.1);
226 plot_minmax_profile_minute(gc, pi, 1, 0.1);
227 plot_minmax_profile_minute(gc, pi, 0, 0.1);
230 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
233 cairo_t *cr = gc->cr;
234 int begins, sec, depth;
235 struct plot_data *entry;
236 int maxtime, maxdepth, marker;
238 /* Get plot scaling limits */
239 maxtime = round_seconds_up(dive->duration.seconds);
240 maxdepth = round_depth_up(dive->maxdepth);
242 /* Time markers: every 5 min */
243 gc->leftx = 0; gc->rightx = maxtime;
244 gc->topy = 0; gc->bottomy = 1.0;
245 for (i = 5*60; i < maxtime; i += 5*60) {
250 /* Depth markers: every 30 ft or 10 m*/
251 gc->leftx = 0; gc->rightx = 1.0;
252 gc->topy = 0; gc->bottomy = maxdepth;
253 switch (output_units.length) {
254 case METERS: marker = 10000; break;
255 case FEET: marker = 9144; break; /* 30 ft */
258 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
259 for (i = marker; i < maxdepth; i += marker) {
265 /* Show mean depth */
266 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
267 move_to(gc, 0, dive->meandepth.mm);
268 line_to(gc, 1, dive->meandepth.mm);
271 gc->leftx = 0; gc->rightx = maxtime;
273 plot_smoothed_profile(gc, pi);
274 plot_minmax_profile(gc, pi);
277 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
279 move_to(gc, entry->sec, entry->val);
280 for (i = 1; i < pi->nr; i++) {
283 if (sec <= maxtime) {
285 line_to(gc, sec, depth);
288 gc->topy = 0; gc->bottomy = 1.0;
289 line_to(gc, MIN(sec,maxtime), 0);
290 line_to(gc, begins, 0);
291 cairo_close_path(cr);
292 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
293 cairo_fill_preserve(cr);
294 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
298 static int setup_temperature_limits(struct dive *dive, struct graphics_context *gc)
301 int maxtime, mintemp, maxtemp;
303 /* Get plot scaling limits */
304 maxtime = round_seconds_up(dive->duration.seconds);
307 for (i = 0; i < dive->samples; i++) {
308 struct sample *sample = dive->sample+i;
309 int mkelvin = sample->temperature.mkelvin;
312 if (mkelvin > maxtemp)
314 if (mkelvin < mintemp)
318 gc->leftx = 0; gc->rightx = maxtime;
319 /* Show temperatures in roughly the lower third */
320 gc->topy = maxtemp + (maxtemp - mintemp)*2;
321 gc->bottomy = mintemp - (maxtemp - mintemp)/2;
323 return maxtemp > mintemp;
326 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc)
329 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
333 if (!setup_temperature_limits(dive, gc))
336 for (i = 0; i < dive->samples; i++) {
338 struct sample *sample = dive->sample+i;
339 int mkelvin = sample->temperature.mkelvin;
343 sec = sample->time.seconds;
347 if (output_units.temperature == FAHRENHEIT) {
348 deg = to_F(sample->temperature);
351 deg = to_C(sample->temperature);
354 plot_text(gc, &tro, sec, mkelvin, "%d %s", deg, unit);
358 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
361 cairo_t *cr = gc->cr;
364 if (!setup_temperature_limits(dive, gc))
367 cairo_set_source_rgba(cr, 0.2, 0.2, 1.0, 0.8);
368 for (i = 0; i < dive->samples; i++) {
369 struct sample *sample = dive->sample+i;
370 int mkelvin = sample->temperature.mkelvin;
377 line_to(gc, sample->time.seconds, mkelvin);
379 move_to(gc, sample->time.seconds, mkelvin);
385 /* gets both the actual start and end pressure as well as the scaling factors */
386 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
387 pressure_t *startp, pressure_t *endp)
392 gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
397 startp->mbar = endp->mbar = 0;
399 for (i = 0; i < dive->samples; i++) {
401 struct sample *sample = dive->sample + i;
403 /* FIXME! We only track cylinder 0 right now */
404 if (sample->cylinderindex)
406 mbar = sample->cylinderpressure.mbar;
420 gc->topy = 0; gc->bottomy = max * 1.5;
424 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
428 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
431 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
433 move_to(gc, 0, dive->cylinder[0].start.mbar);
434 for (i = 1; i < dive->samples; i++) {
436 struct sample *sample = dive->sample + i;
438 mbar = sample->cylinderpressure.mbar;
441 sec = sample->time.seconds;
442 if (sec <= dive->duration.seconds)
443 line_to(gc, sec, mbar);
446 * We may have "surface time" events, in which case we don't go
447 * back to dive duration
449 if (sec < dive->duration.seconds)
450 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
451 cairo_stroke(gc->cr);
455 * Return air usage (in liters).
457 static double calculate_airuse(struct dive *dive)
462 for (i = 0; i < MAX_CYLINDERS; i++) {
463 cylinder_t *cyl = dive->cylinder + i;
464 int size = cyl->type.size.mliter;
470 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
472 /* Liters of air at 1 atm == milliliters at 1k atm*/
473 airuse += kilo_atm * size;
478 static void plot_info(struct dive *dive, struct graphics_context *gc)
480 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
481 const double liters_per_cuft = 28.317;
482 const char *unit, *desc;
485 airuse = calculate_airuse(dive);
489 /* I really need to start addign some unit setting thing */
490 switch (output_units.volume) {
496 airuse /= liters_per_cuft;
499 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
500 if (dive->duration.seconds) {
501 double pressure = 1 + (dive->meandepth.mm / 10000.0);
502 double sac = airuse / pressure * 60 / dive->duration.seconds;
503 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
505 desc = dive->cylinder[0].type.description;
506 if (desc || dive->cylinder[0].gasmix.o2.permille) {
507 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
512 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
516 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
518 pressure_t startp, endp;
520 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
522 const char *unit = "bar";
524 switch (output_units.pressure) {
526 start = startp.mbar * 100;
527 end = startp.mbar * 100;
531 start = (startp.mbar + 500) / 1000;
532 end = (endp.mbar + 500) / 1000;
536 start = to_PSI(startp);
542 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
543 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
544 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
549 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
551 struct plot_data *p = entry;
552 int time = entry->sec;
553 int seconds = 90*(index+1);
554 struct plot_data *min, *max;
557 /* Go back 'seconds' in time */
559 if (p[-1].sec < time - seconds)
564 /* Then go forward until we hit an entry past the time */
570 if (p->sec > time + seconds)
579 entry->min[index] = min;
580 entry->max[index] = max;
581 entry->avg[index] = (avg + nr/2) / nr;
584 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
586 analyze_plot_info_minmax_minute(entry, first, last, 0);
587 analyze_plot_info_minmax_minute(entry, first, last, 1);
588 analyze_plot_info_minmax_minute(entry, first, last, 2);
591 static struct plot_info *analyze_plot_info(struct plot_info *pi)
596 /* Smoothing function: 5-point triangular smooth */
597 for (i = 2; i < nr-2; i++) {
598 struct plot_data *entry = pi->entry+i;
601 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
602 entry->smoothed = (val+4) / 9;
605 /* One-, two- and three-minute minmax data */
606 for (i = 0; i < nr; i++) {
607 struct plot_data *entry = pi->entry +i;
608 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
615 * Create a plot-info with smoothing and ranged min/max
617 * This also makes sure that we have extra empty events on both
618 * sides, so that you can do end-points without having to worry
621 static struct plot_info *depth_plot_info(struct dive *dive)
623 int i, nr = dive->samples + 4, sec;
624 size_t alloc_size = plot_info_size(nr);
625 struct plot_info *pi;
627 pi = malloc(alloc_size);
630 memset(pi, 0, alloc_size);
633 for (i = 0; i < dive->samples; i++) {
634 struct sample *sample = dive->sample+i;
635 struct plot_data *entry = pi->entry + i + 2;
637 sec = entry->sec = sample->time.seconds;
638 entry->val = sample->depth.mm;
640 /* Fill in the last two entries with empty values but valid times */
641 i = dive->samples + 2;
642 pi->entry[i].sec = sec + 20;
643 pi->entry[i+1].sec = sec + 40;
645 return analyze_plot_info(pi);
648 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
651 struct plot_info *pi = depth_plot_info(dive);
655 cairo_translate(gc->cr, topx, topy);
656 cairo_set_line_width(gc->cr, 2);
657 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
658 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
661 * We can use "cairo_translate()" because that doesn't
662 * scale line width etc. But the actual scaling we need
663 * do set up ourselves..
667 gc->maxx = (w - 2*topx);
668 gc->maxy = (h - 2*topy);
670 /* Temperature profile */
671 plot_temperature_profile(dive, gc);
673 /* Cylinder pressure plot */
674 plot_cylinder_pressure(dive, gc);
677 plot_depth_profile(dive, gc, pi);
679 /* Text on top of all graphs.. */
680 plot_temperature_text(dive, gc);
681 plot_depth_text(dive, gc, pi);
682 plot_cylinder_pressure_text(dive, gc);
684 /* And info box in the lower right corner.. */
685 gc->leftx = 0; gc->rightx = 1.0;
686 gc->topy = 0; gc->bottomy = 1.0;
689 /* Bounding box last */
690 cairo_set_source_rgb(gc->cr, 1, 1, 1);
695 cairo_close_path(gc->cr);
696 cairo_stroke(gc->cr);
700 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
702 struct dive *dive = current_dive;
703 struct graphics_context gc;
706 w = widget->allocation.width;
707 h = widget->allocation.height;
709 gc.cr = gdk_cairo_create(widget->window);
710 cairo_set_source_rgb(gc.cr, 0, 0, 0);
714 plot(&gc, w, h, dive);
716 cairo_destroy(gc.cr);
721 GtkWidget *dive_profile_widget(void)
725 da = gtk_drawing_area_new();
726 gtk_widget_set_size_request(da, 350, 250);
727 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);