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 cairo_set_line_width(gc->cr, 2);
240 /* Get plot scaling limits */
241 maxtime = round_seconds_up(dive->duration.seconds);
242 maxdepth = round_depth_up(dive->maxdepth);
244 /* Time markers: every 5 min */
245 gc->leftx = 0; gc->rightx = maxtime;
246 gc->topy = 0; gc->bottomy = 1.0;
247 for (i = 5*60; i < maxtime; i += 5*60) {
252 /* Depth markers: every 30 ft or 10 m*/
253 gc->leftx = 0; gc->rightx = 1.0;
254 gc->topy = 0; gc->bottomy = maxdepth;
255 switch (output_units.length) {
256 case METERS: marker = 10000; break;
257 case FEET: marker = 9144; break; /* 30 ft */
260 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
261 for (i = marker; i < maxdepth; i += marker) {
267 /* Show mean depth */
268 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
269 move_to(gc, 0, dive->meandepth.mm);
270 line_to(gc, 1, dive->meandepth.mm);
273 gc->leftx = 0; gc->rightx = maxtime;
275 plot_smoothed_profile(gc, pi);
276 plot_minmax_profile(gc, pi);
279 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
281 move_to(gc, entry->sec, entry->val);
282 for (i = 1; i < pi->nr; i++) {
285 if (sec <= maxtime) {
287 line_to(gc, sec, depth);
290 gc->topy = 0; gc->bottomy = 1.0;
291 line_to(gc, MIN(sec,maxtime), 0);
292 line_to(gc, begins, 0);
293 cairo_close_path(cr);
294 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
295 cairo_fill_preserve(cr);
296 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
300 static int setup_temperature_limits(struct dive *dive, struct graphics_context *gc)
303 int maxtime, mintemp, maxtemp;
305 /* Get plot scaling limits */
306 maxtime = round_seconds_up(dive->duration.seconds);
309 for (i = 0; i < dive->samples; i++) {
310 struct sample *sample = dive->sample+i;
311 int mkelvin = sample->temperature.mkelvin;
314 if (mkelvin > maxtemp)
316 if (mkelvin < mintemp)
320 gc->leftx = 0; gc->rightx = maxtime;
321 /* Show temperatures in roughly the lower third */
322 gc->topy = maxtemp + (maxtemp - mintemp)*2;
323 gc->bottomy = mintemp - (maxtemp - mintemp)/2;
325 return maxtemp > mintemp;
328 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc)
331 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
335 if (!setup_temperature_limits(dive, gc))
338 for (i = 0; i < dive->samples; i++) {
340 struct sample *sample = dive->sample+i;
341 int mkelvin = sample->temperature.mkelvin;
345 sec = sample->time.seconds;
349 if (output_units.temperature == FAHRENHEIT) {
350 deg = to_F(sample->temperature);
353 deg = to_C(sample->temperature);
356 plot_text(gc, &tro, sec, mkelvin, "%d %s", deg, unit);
360 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
363 cairo_t *cr = gc->cr;
366 if (!setup_temperature_limits(dive, gc))
369 cairo_set_source_rgba(cr, 0.2, 0.2, 1.0, 0.8);
370 for (i = 0; i < dive->samples; i++) {
371 struct sample *sample = dive->sample+i;
372 int mkelvin = sample->temperature.mkelvin;
379 line_to(gc, sample->time.seconds, mkelvin);
381 move_to(gc, sample->time.seconds, mkelvin);
387 /* gets both the actual start and end pressure as well as the scaling factors */
388 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
389 pressure_t *startp, pressure_t *endp)
394 gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
399 startp->mbar = endp->mbar = 0;
401 for (i = 0; i < dive->samples; i++) {
403 struct sample *sample = dive->sample + i;
405 /* FIXME! We only track cylinder 0 right now */
406 if (sample->cylinderindex)
408 mbar = sample->cylinderpressure.mbar;
422 gc->topy = 0; gc->bottomy = max * 1.5;
426 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
430 if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
433 cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
435 move_to(gc, 0, dive->cylinder[0].start.mbar);
436 for (i = 1; i < dive->samples; i++) {
438 struct sample *sample = dive->sample + i;
440 mbar = sample->cylinderpressure.mbar;
443 sec = sample->time.seconds;
444 if (sec <= dive->duration.seconds)
445 line_to(gc, sec, mbar);
448 * We may have "surface time" events, in which case we don't go
449 * back to dive duration
451 if (sec < dive->duration.seconds)
452 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
453 cairo_stroke(gc->cr);
457 * Return air usage (in liters).
459 static double calculate_airuse(struct dive *dive)
464 for (i = 0; i < MAX_CYLINDERS; i++) {
465 cylinder_t *cyl = dive->cylinder + i;
466 int size = cyl->type.size.mliter;
472 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
474 /* Liters of air at 1 atm == milliliters at 1k atm*/
475 airuse += kilo_atm * size;
480 static void plot_info(struct dive *dive, struct graphics_context *gc)
482 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
483 const double liters_per_cuft = 28.317;
484 const char *unit, *desc;
487 airuse = calculate_airuse(dive);
491 /* I really need to start addign some unit setting thing */
492 switch (output_units.volume) {
498 airuse /= liters_per_cuft;
501 plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
502 if (dive->duration.seconds) {
503 double pressure = 1 + (dive->meandepth.mm / 10000.0);
504 double sac = airuse / pressure * 60 / dive->duration.seconds;
505 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
507 desc = dive->cylinder[0].type.description;
508 if (desc || dive->cylinder[0].gasmix.o2.permille) {
509 int o2 = dive->cylinder[0].gasmix.o2.permille / 10;
514 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
518 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
520 pressure_t startp, endp;
522 if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
524 const char *unit = "bar";
526 switch (output_units.pressure) {
528 start = startp.mbar * 100;
529 end = startp.mbar * 100;
533 start = (startp.mbar + 500) / 1000;
534 end = (endp.mbar + 500) / 1000;
538 start = to_PSI(startp);
544 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
545 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
546 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
551 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
553 struct plot_data *p = entry;
554 int time = entry->sec;
555 int seconds = 90*(index+1);
556 struct plot_data *min, *max;
559 /* Go back 'seconds' in time */
561 if (p[-1].sec < time - seconds)
566 /* Then go forward until we hit an entry past the time */
572 if (p->sec > time + seconds)
581 entry->min[index] = min;
582 entry->max[index] = max;
583 entry->avg[index] = (avg + nr/2) / nr;
586 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
588 analyze_plot_info_minmax_minute(entry, first, last, 0);
589 analyze_plot_info_minmax_minute(entry, first, last, 1);
590 analyze_plot_info_minmax_minute(entry, first, last, 2);
593 static struct plot_info *analyze_plot_info(struct plot_info *pi)
598 /* Smoothing function: 5-point triangular smooth */
599 for (i = 2; i < nr-2; i++) {
600 struct plot_data *entry = pi->entry+i;
603 val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
604 entry->smoothed = (val+4) / 9;
607 /* One-, two- and three-minute minmax data */
608 for (i = 0; i < nr; i++) {
609 struct plot_data *entry = pi->entry +i;
610 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
617 * Create a plot-info with smoothing and ranged min/max
619 * This also makes sure that we have extra empty events on both
620 * sides, so that you can do end-points without having to worry
623 static struct plot_info *depth_plot_info(struct dive *dive)
625 int i, nr = dive->samples + 4, sec;
626 size_t alloc_size = plot_info_size(nr);
627 struct plot_info *pi;
629 pi = malloc(alloc_size);
632 memset(pi, 0, alloc_size);
635 for (i = 0; i < dive->samples; i++) {
636 struct sample *sample = dive->sample+i;
637 struct plot_data *entry = pi->entry + i + 2;
639 sec = entry->sec = sample->time.seconds;
640 entry->val = sample->depth.mm;
642 /* Fill in the last two entries with empty values but valid times */
643 i = dive->samples + 2;
644 pi->entry[i].sec = sec + 20;
645 pi->entry[i+1].sec = sec + 40;
647 return analyze_plot_info(pi);
650 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
653 struct plot_info *pi = depth_plot_info(dive);
657 cairo_translate(gc->cr, topx, topy);
660 * We can use "cairo_translate()" because that doesn't
661 * scale line width etc. But the actual scaling we need
662 * do set up ourselves..
666 gc->maxx = (w - 2*topx);
667 gc->maxy = (h - 2*topy);
669 /* Temperature profile */
670 plot_temperature_profile(dive, gc);
672 /* Cylinder pressure plot */
673 plot_cylinder_pressure(dive, gc);
676 plot_depth_profile(dive, gc, pi);
678 /* Text on top of all graphs.. */
679 plot_temperature_text(dive, gc);
680 plot_depth_text(dive, gc, pi);
681 plot_cylinder_pressure_text(dive, gc);
683 /* And info box in the lower right corner.. */
684 gc->leftx = 0; gc->rightx = 1.0;
685 gc->topy = 0; gc->bottomy = 1.0;
688 /* Bounding box last */
689 cairo_set_source_rgb(gc->cr, 1, 1, 1);
694 cairo_close_path(gc->cr);
695 cairo_stroke(gc->cr);
699 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
701 struct dive *dive = current_dive;
702 struct graphics_context gc;
705 w = widget->allocation.width;
706 h = widget->allocation.height;
708 gc.cr = gdk_cairo_create(widget->window);
709 cairo_set_source_rgb(gc.cr, 0, 0, 0);
713 plot(&gc, w, h, dive);
715 cairo_destroy(gc.cr);
720 GtkWidget *dive_profile_widget(void)
724 da = gtk_drawing_area_new();
725 gtk_widget_set_size_request(da, 350, 250);
726 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);