]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Label the temperature graph
[ext/subsurface.git] / profile.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <time.h>
6
7 #include "dive.h"
8 #include "display.h"
9 #include "divelist.h"
10
11 int selected_dive = 0;
12
13 /*
14  * Cairo scaling really is horribly horribly mis-designed.
15  *
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..
20  */
21 struct graphics_context {
22         cairo_t *cr;
23         double maxx, maxy;
24         double leftx, rightx;
25         double topy, bottomy;
26 };
27
28 /* Plot info with smoothing and one-, two- and three-minute minimums and maximums */
29 struct plot_info {
30         int nr;
31         int maxtime;
32         struct plot_data {
33                 int sec;
34                 int val;
35                 int smoothed;
36                 struct plot_data *min[3];
37                 struct plot_data *max[3];
38                 int avg[3];
39         } entry[];
40 };
41 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
42
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)
47
48 static void move_to(struct graphics_context *gc, double x, double y)
49 {
50         cairo_move_to(gc->cr, SCALE(gc, x, y));
51 }
52
53 static void line_to(struct graphics_context *gc, double x, double y)
54 {
55         cairo_line_to(gc->cr, SCALE(gc, x, y));
56 }
57
58 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
59
60 /*
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
64  * up as such.
65  */
66 static int round_seconds_up(int seconds)
67 {
68         return MAX(30*60, ROUND_UP(seconds, 60*10));
69 }
70
71 static int round_depth_up(depth_t depth)
72 {
73         unsigned mm = depth.mm;
74         /* Minimum 30m */
75         return MAX(30000, ROUND_UP(mm+3000, 10000));
76 }
77
78 typedef struct {
79         int size;
80         double r,g,b;
81         enum {CENTER,LEFT} halign;
82         enum {MIDDLE,TOP,BOTTOM} valign;
83 } text_render_options_t;
84
85 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
86                       double x, double y, const char *fmt, ...)
87 {
88         cairo_t *cr = gc->cr;
89         cairo_text_extents_t extents;
90         double dx, dy;
91         char buffer[80];
92         va_list args;
93
94         va_start(args, fmt);
95         vsnprintf(buffer, sizeof(buffer), fmt, args);
96         va_end(args);
97
98         cairo_set_font_size(cr, tro->size);
99         cairo_text_extents(cr, buffer, &extents);
100         dx = 0;
101         switch (tro->halign) {
102         case CENTER:
103                 dx = -(extents.width/2 + extents.x_bearing);
104                 break;
105         case LEFT:
106                 dx = 0;
107                 break;
108         }
109         switch (tro->valign) {
110         case TOP:
111                 dy = extents.height * 1.2;
112                 break;
113         case BOTTOM:
114                 dy = -extents.height * 0.8;
115                 break;
116         case MIDDLE:
117                 dy = 0;
118                 break;
119         }
120
121         move_to(gc, x, y);
122         cairo_rel_move_to(cr, dx, dy);
123
124         cairo_text_path(cr, buffer);
125         cairo_set_source_rgb(cr, 0, 0, 0);
126         cairo_stroke(cr);
127
128         move_to(gc, x, y);
129         cairo_rel_move_to(cr, dx, dy);
130
131         cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
132         cairo_show_text(cr, buffer);
133 }
134
135 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
136 {
137         int sec = entry->sec;
138         depth_t depth = { entry->val };
139         const char *fmt;
140         double d;
141
142         switch (output_units.length) {
143         case METERS:
144                 d = depth.mm / 1000.0;
145                 fmt = "%.1f";
146                 break;
147         case FEET:
148                 d = to_feet(depth);
149                 fmt = "%.0f";
150                 break;
151         }
152         plot_text(gc, tro, sec, depth.mm, fmt, d);
153 }
154
155 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
156 {
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};
159         int i;
160
161         for (i = 0; i < pi->nr; i++) {
162                 struct plot_data *entry = pi->entry + i;
163
164                 if (entry->val < 2000)
165                         continue;
166
167                 if (entry == entry->max[2])
168                         render_depth_sample(gc, entry, &deep);
169
170                 if (entry == entry->min[2])
171                         render_depth_sample(gc, entry, &shallow);
172         }
173 }
174
175 static void plot_depth_text(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
176 {
177         int maxtime, maxdepth;
178
179         /* Get plot scaling limits */
180         maxtime = round_seconds_up(dive->duration.seconds);
181         maxdepth = round_depth_up(dive->maxdepth);
182
183         gc->leftx = 0; gc->rightx = maxtime;
184         gc->topy = 0; gc->bottomy = maxdepth;
185
186         plot_text_samples(gc, pi);
187 }
188
189 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
190 {
191         int i;
192         struct plot_data *entry = pi->entry;
193
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++) {
197                 entry++;
198                 line_to(gc, entry->sec, entry->smoothed);
199         }
200         cairo_stroke(gc->cr);
201 }
202
203 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
204                                 int index, double a)
205 {
206         int i;
207         struct plot_data *entry = pi->entry;
208
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++) {
212                 entry++;
213                 line_to(gc, entry->sec, entry->min[index]->val);
214         }
215         for (i = 1; i < pi->nr; i++) {
216                 line_to(gc, entry->sec, entry->max[index]->val);
217                 entry--;
218         }
219         cairo_close_path(gc->cr);
220         cairo_fill(gc->cr);
221 }
222
223 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
224 {
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);
228 }
229
230 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc, struct plot_info *pi)
231 {
232         int i;
233         cairo_t *cr = gc->cr;
234         int begins, sec, depth;
235         struct plot_data *entry;
236         int maxtime, maxdepth, marker;
237
238         cairo_set_line_width(gc->cr, 2);
239
240         /* Get plot scaling limits */
241         maxtime = round_seconds_up(dive->duration.seconds);
242         maxdepth = round_depth_up(dive->maxdepth);
243
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) {
248                 move_to(gc, i, 0);
249                 line_to(gc, i, 1);
250         }
251
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 */
258         }
259
260         cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
261         for (i = marker; i < maxdepth; i += marker) {
262                 move_to(gc, 0, i);
263                 line_to(gc, 1, i);
264         }
265         cairo_stroke(cr);
266
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);
271         cairo_stroke(cr);
272
273         gc->leftx = 0; gc->rightx = maxtime;
274
275         plot_smoothed_profile(gc, pi);
276         plot_minmax_profile(gc, pi);
277
278         entry = pi->entry;
279         cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
280         begins = entry->sec;
281         move_to(gc, entry->sec, entry->val);
282         for (i = 1; i < pi->nr; i++) {
283                 entry++;
284                 sec = entry->sec;
285                 if (sec <= maxtime) {
286                         depth = entry->val;
287                         line_to(gc, sec, depth);
288                 }
289         }
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);
297         cairo_stroke(cr);
298 }
299
300 static int setup_temperature_limits(struct dive *dive, struct graphics_context *gc)
301 {
302         int i;
303         int maxtime, mintemp, maxtemp;
304
305         /* Get plot scaling limits */
306         maxtime = round_seconds_up(dive->duration.seconds);
307         mintemp = INT_MAX;
308         maxtemp = 0;
309         for (i = 0; i < dive->samples; i++) {
310                 struct sample *sample = dive->sample+i;
311                 int mkelvin = sample->temperature.mkelvin;
312                 if (!mkelvin)
313                         continue;
314                 if (mkelvin > maxtemp)
315                         maxtemp = mkelvin;
316                 if (mkelvin < mintemp)
317                         mintemp = mkelvin;
318         }
319
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;
324
325         return maxtemp > mintemp;
326 }
327
328 static void plot_temperature_text(struct dive *dive, struct graphics_context *gc)
329 {
330         int i;
331         static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
332
333         int last = 0;
334
335         if (!setup_temperature_limits(dive, gc))
336                 return;
337
338         for (i = 0; i < dive->samples; i++) {
339                 const char *unit;
340                 struct sample *sample = dive->sample+i;
341                 int mkelvin = sample->temperature.mkelvin;
342                 int sec, deg;
343                 if (!mkelvin)
344                         continue;
345                 sec = sample->time.seconds;
346                 if (sec < last)
347                         continue;
348                 last = sec + 300;
349                 if (output_units.temperature == FAHRENHEIT) {
350                         deg = to_F(sample->temperature);
351                         unit = "F";
352                 } else {
353                         deg = to_C(sample->temperature);
354                         unit = "C";
355                 }
356                 plot_text(gc, &tro, sec, mkelvin, "%d %s", deg, unit);
357         }
358 }
359
360 static void plot_temperature_profile(struct dive *dive, struct graphics_context *gc)
361 {
362         int i;
363         cairo_t *cr = gc->cr;
364         int last = 0;
365
366         if (!setup_temperature_limits(dive, gc))
367                 return;
368
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;
373                 if (!mkelvin) {
374                         if (!last)
375                                 continue;
376                         mkelvin = last;
377                 }
378                 if (last)
379                         line_to(gc, sample->time.seconds, mkelvin);
380                 else
381                         move_to(gc, sample->time.seconds, mkelvin);
382                 last = mkelvin;
383         }
384         cairo_stroke(cr);
385 }
386
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)
390 {
391         int i;
392         int min, max;
393
394         gc->leftx = 0; gc->rightx = round_seconds_up(dive->duration.seconds);
395
396         max = 0;
397         min = 5000000;
398         if (startp)
399                 startp->mbar = endp->mbar = 0;
400
401         for (i = 0; i < dive->samples; i++) {
402                 int mbar;
403                 struct sample *sample = dive->sample + i;
404
405                 /* FIXME! We only track cylinder 0 right now */
406                 if (sample->cylinderindex)
407                         continue;
408                 mbar = sample->cylinderpressure.mbar;
409                 if (!mbar)
410                         continue;
411                 if (mbar < min)
412                         min = mbar;
413                 if (mbar > max)
414                         max = mbar;
415         }
416         if (startp)
417                 startp->mbar = max;
418         if (endp)
419                 endp->mbar = min;
420         if (!max)
421                 return 0;
422         gc->topy = 0; gc->bottomy = max * 1.5;
423         return 1;
424 }
425
426 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
427 {
428         int i, sec = -1;
429
430         if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
431                 return;
432
433         cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
434
435         move_to(gc, 0, dive->cylinder[0].start.mbar);
436         for (i = 1; i < dive->samples; i++) {
437                 int mbar;
438                 struct sample *sample = dive->sample + i;
439
440                 mbar = sample->cylinderpressure.mbar;
441                 if (!mbar)
442                         continue;
443                 sec = sample->time.seconds;
444                 if (sec <= dive->duration.seconds)
445                         line_to(gc, sec, mbar);
446         }
447         /*
448          * We may have "surface time" events, in which case we don't go
449          * back to dive duration
450          */
451         if (sec < dive->duration.seconds)
452                 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
453         cairo_stroke(gc->cr);
454 }
455
456 /*
457  * Return air usage (in liters).
458  */
459 static double calculate_airuse(struct dive *dive)
460 {
461         double airuse = 0;
462         int i;
463
464         for (i = 0; i < MAX_CYLINDERS; i++) {
465                 cylinder_t *cyl = dive->cylinder + i;
466                 int size = cyl->type.size.mliter;
467                 double kilo_atm;
468
469                 if (!size)
470                         continue;
471
472                 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
473
474                 /* Liters of air at 1 atm == milliliters at 1k atm*/
475                 airuse += kilo_atm * size;
476         }
477         return airuse;
478 }
479
480 static void plot_info(struct dive *dive, struct graphics_context *gc)
481 {
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;
485         double airuse;
486
487         airuse = calculate_airuse(dive);
488         if (!airuse)
489                 return;
490
491         /* I really need to start addign some unit setting thing */
492         switch (output_units.volume) {
493         case LITER:
494                 unit = "l";
495                 break;
496         case CUFT:
497                 unit = "cuft";
498                 airuse /= liters_per_cuft;
499                 break;
500         }
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);
506         }
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;
510                 if (!desc)
511                         desc = "";
512                 if (!o2)
513                         o2 = 21;
514                 plot_text(gc, &tro, 0.8, 0.9, "%s (%d%%)", desc, o2);
515         }
516 }
517
518 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
519 {
520         pressure_t startp, endp;
521
522         if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
523                 int start, end;
524                 const char *unit = "bar";
525
526                 switch (output_units.pressure) {
527                 case PASCAL:
528                         start = startp.mbar * 100;
529                         end = startp.mbar * 100;
530                         unit = "pascal";
531                         break;
532                 case BAR:
533                         start = (startp.mbar + 500) / 1000;
534                         end = (endp.mbar + 500) / 1000;
535                         unit = "bar";
536                         break;
537                 case PSI:
538                         start = to_PSI(startp);
539                         end = to_PSI(endp);
540                         unit = "psi";
541                         break;
542                 }
543
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,
547                           "%d %s", end, unit);
548         }
549 }
550
551 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
552 {
553         struct plot_data *p = entry;
554         int time = entry->sec;
555         int seconds = 90*(index+1);
556         struct plot_data *min, *max;
557         int avg, nr;
558
559         /* Go back 'seconds' in time */
560         while (p > first) {
561                 if (p[-1].sec < time - seconds)
562                         break;
563                 p--;
564         }
565
566         /* Then go forward until we hit an entry past the time */
567         min = max = p;
568         avg = p->val;
569         nr = 1;
570         while (++p < last) {
571                 int val = p->val;
572                 if (p->sec > time + seconds)
573                         break;
574                 avg += val;
575                 nr ++;
576                 if (val < min->val)
577                         min = p;
578                 if (val > max->val)
579                         max = p;
580         }
581         entry->min[index] = min;
582         entry->max[index] = max;
583         entry->avg[index] = (avg + nr/2) / nr;
584 }
585
586 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
587 {
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);
591 }
592
593 static struct plot_info *analyze_plot_info(struct plot_info *pi)
594 {
595         int i;
596         int nr = pi->nr;
597
598         /* Smoothing function: 5-point triangular smooth */
599         for (i = 2; i < nr-2; i++) {
600                 struct plot_data *entry = pi->entry+i;
601                 int val;
602
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;
605         }
606
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);
611         }
612         
613         return pi;
614 }
615
616 /*
617  * Create a plot-info with smoothing and ranged min/max
618  *
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
621  * about it.
622  */
623 static struct plot_info *depth_plot_info(struct dive *dive)
624 {
625         int i, nr = dive->samples + 4, sec;
626         size_t alloc_size = plot_info_size(nr);
627         struct plot_info *pi;
628
629         pi = malloc(alloc_size);
630         if (!pi)
631                 return pi;
632         memset(pi, 0, alloc_size);
633         pi->nr = nr;
634         sec = 0;
635         for (i = 0; i < dive->samples; i++) {
636                 struct sample *sample = dive->sample+i;
637                 struct plot_data *entry = pi->entry + i + 2;
638
639                 sec = entry->sec = sample->time.seconds;
640                 entry->val = sample->depth.mm;
641         }
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;
646
647         return analyze_plot_info(pi);
648 }
649
650 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
651 {
652         double topx, topy;
653         struct plot_info *pi = depth_plot_info(dive);
654
655         topx = w / 20.0;
656         topy = h / 20.0;
657         cairo_translate(gc->cr, topx, topy);
658
659         /*
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..
663          *
664          * Snif. What a pity.
665          */
666         gc->maxx = (w - 2*topx);
667         gc->maxy = (h - 2*topy);
668
669         /* Temperature profile */
670         plot_temperature_profile(dive, gc);
671
672         /* Cylinder pressure plot */
673         plot_cylinder_pressure(dive, gc);
674
675         /* Depth profile */
676         plot_depth_profile(dive, gc, pi);
677
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);
682
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;
686         plot_info(dive, gc);
687
688         /* Bounding box last */
689         cairo_set_source_rgb(gc->cr, 1, 1, 1);
690         move_to(gc, 0, 0);
691         line_to(gc, 0, 1);
692         line_to(gc, 1, 1);
693         line_to(gc, 1, 0);
694         cairo_close_path(gc->cr);
695         cairo_stroke(gc->cr);
696
697 }
698
699 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
700 {
701         struct dive *dive = current_dive;
702         struct graphics_context gc;
703         int w,h;
704
705         w = widget->allocation.width;
706         h = widget->allocation.height;
707
708         gc.cr = gdk_cairo_create(widget->window);
709         cairo_set_source_rgb(gc.cr, 0, 0, 0);
710         cairo_paint(gc.cr);
711
712         if (dive)
713                 plot(&gc, w, h, dive);
714
715         cairo_destroy(gc.cr);
716
717         return FALSE;
718 }
719
720 GtkWidget *dive_profile_widget(void)
721 {
722         GtkWidget *da;
723
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);
727
728         return da;
729 }