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 plot_data *entry, const text_render_options_t *tro)
134 int sec = entry->sec;
135 depth_t depth = { entry->val };
139 switch (output_units.length) {
141 d = depth.mm / 1000.0;
149 plot_text(gc, tro, sec, depth.mm, fmt, d);
152 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
154 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
155 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
158 for (i = 0; i < pi->nr; i++) {
159 struct plot_data *entry = pi->entry + i;
161 if (entry->val < 2000)
164 if (entry == entry->max[2])
165 render_depth_sample(gc, entry, &deep);
167 if (entry == entry->min[2])
168 render_depth_sample(gc, entry, &shallow);
172 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
174 int maxtime, maxdepth;
176 /* Get plot scaling limits */
177 maxtime = round_seconds_up(dive->duration.seconds);
178 maxdepth = round_depth_up(dive->maxdepth);
180 gc->scalex = maxtime;
181 gc->scaley = maxdepth;
183 plot_text_samples(gc, pi);
186 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
189 struct plot_data *entry = pi->entry;
191 cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
192 move_to(gc, entry->sec, entry->smoothed);
193 for (i = 1; i < pi->nr; i++) {
195 line_to(gc, entry->sec, entry->smoothed);
197 cairo_stroke(gc->cr);
200 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
204 struct plot_data *entry = pi->entry;
206 cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
207 move_to(gc, entry->sec, entry->min[index]->val);
208 for (i = 1; i < pi->nr; i++) {
210 line_to(gc, entry->sec, entry->min[index]->val);
212 for (i = 1; i < pi->nr; i++) {
213 line_to(gc, entry->sec, entry->max[index]->val);
216 cairo_close_path(gc->cr);
220 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
222 plot_minmax_profile_minute(gc, pi, 2, 0.1);
223 plot_minmax_profile_minute(gc, pi, 1, 0.1);
224 plot_minmax_profile_minute(gc, pi, 0, 0.1);
227 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
230 cairo_t *cr = gc->cr;
231 int begins, sec, depth;
232 struct plot_data *entry;
233 int maxtime, maxdepth, marker;
235 cairo_set_line_width(gc->cr, 2);
237 /* Get plot scaling limits */
238 maxtime = round_seconds_up(dive->duration.seconds);
239 maxdepth = round_depth_up(dive->maxdepth);
241 /* Time markers: every 5 min */
242 gc->scalex = maxtime;
244 for (i = 5*60; i < maxtime; i += 5*60) {
249 /* Depth markers: every 30 ft or 10 m*/
251 gc->scaley = maxdepth;
252 switch (output_units.length) {
253 case METERS: marker = 10000; break;
254 case FEET: marker = 9144; break; /* 30 ft */
257 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
258 for (i = marker; i < maxdepth; i += marker) {
264 /* Show mean depth */
265 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
266 move_to(gc, 0, dive->meandepth.mm);
267 line_to(gc, 1, dive->meandepth.mm);
270 gc->scalex = maxtime;
272 plot_smoothed_profile(gc, pi);
273 plot_minmax_profile(gc, pi);
276 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
278 move_to(gc, entry->sec, entry->val);
279 for (i = 1; i < pi->nr; i++) {
282 if (sec <= maxtime) {
284 line_to(gc, sec, depth);
288 line_to(gc, MIN(sec,maxtime), 0);
289 line_to(gc, begins, 0);
290 cairo_close_path(cr);
291 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
292 cairo_fill_preserve(cr);
293 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
297 /* gets both the actual start and end pressure as well as the scaling factors */
298 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
299 pressure_t *startp, pressure_t *endp)
304 gc->scalex = round_seconds_up(dive->duration.seconds);
309 startp->mbar = endp->mbar = 0;
311 for (i = 0; i < dive->samples; i++) {
313 struct sample *sample = dive->sample + i;
315 /* FIXME! We only track cylinder 0 right now */
316 if (sample->cylinderindex)
318 mbar = sample->cylinderpressure.mbar;
332 gc->scaley = max * 1.5;
336 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
340 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
343 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
345 move_to(gc, 0, dive->cylinder[0].start.mbar);
346 for (i = 1; i < dive->samples; i++) {
348 struct sample *sample = dive->sample + i;
350 mbar = sample->cylinderpressure.mbar;
353 sec = sample->time.seconds;
354 if (sec <= dive->duration.seconds)
355 line_to(gc, sec, mbar);
358 * We may have "surface time" events, in which case we don't go
359 * back to dive duration
361 if (sec < dive->duration.seconds)
362 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
363 cairo_stroke(gc->cr);
367 * Return air usage (in liters).
369 static double calculate_airuse(struct dive *dive)
374 for (i = 0; i < MAX_CYLINDERS; i++) {
375 cylinder_t *cyl = dive->cylinder + i;
376 int size = cyl->type.size.mliter;
382 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
384 /* Liters of air at 1 atm == milliliters at 1k atm*/
385 airuse += kilo_atm * size;
390 static void plot_info(struct dive *dive, struct graphics_context *gc)
392 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
393 const double liters_per_cuft = 28.317;
394 const char *unit, *desc;
397 airuse = calculate_airuse(dive);
401 /* I really need to start addign some unit setting thing */
402 switch (output_units.volume) {
408 airuse /= liters_per_cuft;
411 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
412 if (dive->duration.seconds) {
413 double pressure = 1 + (dive->meandepth.mm / 10000.0);
414 double sac = airuse / pressure * 60 / dive->duration.seconds;
415 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
417 desc = dive->cylinder[0].type.description;
418 if (desc || dive->cylinder[0].gasmix.o2.permille) {
419 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
424 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
428 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
430 pressure_t startp, endp;
432 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
434 const char *unit = "bar";
436 switch (output_units.pressure) {
438 start = startp.mbar * 100;
439 end = startp.mbar * 100;
443 start = (startp.mbar + 500) / 1000;
444 end = (endp.mbar + 500) / 1000;
448 start = to_PSI(startp);
454 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
455 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
456 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
461 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
463 struct plot_data *p = entry;
464 int time = entry->sec;
465 int seconds = 90*(index+1);
466 struct plot_data *min, *max;
469 /* Go back 'seconds' in time */
471 if (p[-1].sec < time - seconds)
476 /* Then go forward until we hit an entry past the time */
482 if (p->sec > time + seconds)
491 entry->min[index] = min;
492 entry->max[index] = max;
493 entry->avg[index] = (avg + nr/2) / nr;
496 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
498 analyze_plot_info_minmax_minute(entry, first, last, 0);
499 analyze_plot_info_minmax_minute(entry, first, last, 1);
500 analyze_plot_info_minmax_minute(entry, first, last, 2);
503 static struct plot_info *analyze_plot_info(struct plot_info *pi)
508 /* Smoothing function: 5-point triangular smooth */
509 for (i = 2; i < nr-2; i++) {
510 struct plot_data *entry = pi->entry+i;
513 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
514 entry->smoothed = (val+4) / 9;
517 /* One-, two- and three-minute minmax data */
518 for (i = 0; i < nr; i++) {
519 struct plot_data *entry = pi->entry +i;
520 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
527 * Create a plot-info with smoothing and ranged min/max
529 * This also makes sure that we have extra empty events on both
530 * sides, so that you can do end-points without having to worry
533 static struct plot_info *depth_plot_info(struct dive *dive)
535 int i, nr = dive->samples + 4, sec;
536 size_t alloc_size = plot_info_size(nr);
537 struct plot_info *pi;
539 pi = malloc(alloc_size);
542 memset(pi, 0, alloc_size);
545 for (i = 0; i < dive->samples; i++) {
546 struct sample *sample = dive->sample+i;
547 struct plot_data *entry = pi->entry + i + 2;
549 sec = entry->sec = sample->time.seconds;
550 entry->val = sample->depth.mm;
552 /* Fill in the last two entries with empty values but valid times */
553 i = dive->samples + 2;
554 pi->entry[i].sec = sec + 20;
555 pi->entry[i+1].sec = sec + 40;
557 return analyze_plot_info(pi);
560 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
563 struct plot_info *pi = depth_plot_info(dive);
567 cairo_translate(gc->cr, topx, topy);
570 * We can use "cairo_translate()" because that doesn't
571 * scale line width etc. But the actual scaling we need
572 * do set up ourselves..
576 gc->maxx = (w - 2*topx);
577 gc->maxy = (h - 2*topy);
579 /* Cylinder pressure plot */
580 plot_cylinder_pressure(dive, gc);
583 plot_depth_profile(dive, gc, pi);
585 /* Text on top of all graphs.. */
586 plot_depth_text(dive, gc, pi);
587 plot_cylinder_pressure_text(dive, gc);
589 /* And info box in the lower right corner.. */
590 gc->scalex = gc->scaley = 1.0;
593 /* Bounding box last */
594 cairo_set_source_rgb(gc->cr, 1, 1, 1);
599 cairo_close_path(gc->cr);
600 cairo_stroke(gc->cr);
604 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
606 struct dive *dive = current_dive;
607 struct graphics_context gc;
610 w = widget->allocation.width;
611 h = widget->allocation.height;
613 gc.cr = gdk_cairo_create(widget->window);
614 cairo_set_source_rgb(gc.cr, 0, 0, 0);
618 plot(&gc, w, h, dive);
620 cairo_destroy(gc.cr);
625 GtkWidget *dive_profile_widget(void)
629 da = gtk_drawing_area_new();
630 gtk_widget_set_size_request(da, 350, 250);
631 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);