]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Prettier profile colors
[ext/subsurface.git] / profile.c
1 /* profile.c */
2 /* creates all the necessary data for drawing the dive profile 
3  * uses cairo to draw it
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include <time.h>
10
11 #include "dive.h"
12 #include "display.h"
13 #include "divelist.h"
14
15 int selected_dive = 0;
16
17 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
18 /* Plot info with smoothing, velocity indication
19  * and one-, two- and three-minute minimums and maximums */
20 struct plot_info {
21         int nr;
22         int maxtime;
23         int meandepth, maxdepth;
24         int maxpressure;
25         int mintemp, maxtemp;
26         struct plot_data {
27                 unsigned int same_cylinder:1;
28                 unsigned int cylinderindex;
29                 int sec;
30                 /* pressure[0] is sensor pressure
31                  * pressure[1] is interpolated pressure */
32                 int pressure[2];
33                 int temperature;
34                 /* Depth info */
35                 int depth;
36                 int smoothed;
37                 velocity_t velocity;
38                 struct plot_data *min[3];
39                 struct plot_data *max[3];
40                 int avg[3];
41         } entry[];
42 };
43 #define SENSOR_PR 0
44 #define INTERPOLATED_PR 1
45 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
46 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
47 #define GET_PRESSURE(_entry) (SENSOR_PRESSURE(_entry) ? : INTERPOLATED_PRESSURE(_entry))
48
49 /* convert velocity to colors */
50 typedef struct { double r, g, b; } rgb_t;
51 static const rgb_t velocity_color[] = {
52         [STABLE]   = {0.0, 0.4, 0.0},
53         [SLOW]     = {0.4, 0.8, 0.0},
54         [MODERATE] = {0.8, 0.8, 0.0},
55         [FAST]     = {0.8, 0.5, 0.0},
56         [CRAZY]    = {1.0, 0.0, 0.0},
57 };
58
59 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
60
61 /* Scale to 0,0 -> maxx,maxy */
62 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
63 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
64 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
65
66 static void move_to(struct graphics_context *gc, double x, double y)
67 {
68         cairo_move_to(gc->cr, SCALE(gc, x, y));
69 }
70
71 static void line_to(struct graphics_context *gc, double x, double y)
72 {
73         cairo_line_to(gc->cr, SCALE(gc, x, y));
74 }
75
76 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
77 {
78         /*
79          * For printers, we still honor 'a', but ignore colors
80          * for now. Black is white and white is black
81          */
82         if (gc->printer) {
83                 double sum = r+g+b;
84                 if (sum > 0.8)
85                         r = g = b = 0;
86                 else
87                         r = g = b = 1;
88         }
89         cairo_set_source_rgba(gc->cr, r, g, b, a);
90 }
91
92 static void set_source_rgb_struct(struct graphics_context *gc, const rgb_t *rgb)
93 {
94         set_source_rgba(gc, rgb->r, rgb->g, rgb->b, 1);
95 }
96
97 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
98 {
99         set_source_rgba(gc, r, g, b, 1);
100 }
101
102 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
103
104 /* debugging tool - not normally used */
105 static void dump_pi (struct plot_info *pi)
106 {
107         int i;
108
109         printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
110                 "    maxpressure:%d mintemp:%d maxtemp:%d\n",
111                 pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
112                 pi->maxpressure, pi->mintemp, pi->maxtemp);
113         for (i = 0; i < pi->nr; i++)
114                 printf("    entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
115                         "                temperature:%d depth:%d smoothed:%d}\n",
116                         i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
117                         pi->entry[i].pressure[0], pi->entry[i].pressure[1],
118                         pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
119         printf("   }\n");
120 }
121
122 /*
123  * When showing dive profiles, we scale things to the
124  * current dive. However, we don't scale past less than
125  * 30 minutes or 90 ft, just so that small dives show
126  * up as such.
127  * we also need to add 180 seconds at the end so the min/max
128  * plots correctly
129  */
130 static int get_maxtime(struct plot_info *pi)
131 {
132         int seconds = pi->maxtime;
133         /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
134         return MAX(30*60, ROUND_UP(seconds+150, 60*5));
135 }
136
137 static int get_maxdepth(struct plot_info *pi)
138 {
139         unsigned mm = pi->maxdepth;
140         /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
141         return MAX(30000, ROUND_UP(mm+3000, 10000));
142 }
143
144 typedef struct {
145         int size;
146         double r,g,b;
147         double hpos, vpos;
148 } text_render_options_t;
149
150 #define RIGHT (-1.0)
151 #define CENTER (-0.5)
152 #define LEFT (0.0)
153
154 #define TOP (1)
155 #define MIDDLE (0)
156 #define BOTTOM (-1)
157
158 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
159                       double x, double y, const char *fmt, ...)
160 {
161         cairo_t *cr = gc->cr;
162         cairo_font_extents_t fe;
163         cairo_text_extents_t extents;
164         double dx, dy;
165         char buffer[80];
166         va_list args;
167
168         va_start(args, fmt);
169         vsnprintf(buffer, sizeof(buffer), fmt, args);
170         va_end(args);
171
172         cairo_set_font_size(cr, tro->size);
173         cairo_font_extents(cr, &fe);
174         cairo_text_extents(cr, buffer, &extents);
175         dx = tro->hpos * extents.width + extents.x_bearing;
176         dy = tro->vpos * extents.height + fe.descent;
177
178         move_to(gc, x, y);
179         cairo_rel_move_to(cr, dx, dy);
180
181         cairo_text_path(cr, buffer);
182         set_source_rgba(gc, 0.95, 0.95, 0.95, 0.95);
183         cairo_stroke(cr);
184
185         move_to(gc, x, y);
186         cairo_rel_move_to(cr, dx, dy);
187
188         set_source_rgb(gc, tro->r, tro->g, tro->b);
189         cairo_show_text(cr, buffer);
190 }
191
192 struct ev_select {
193         char *ev_name;
194         gboolean plot_ev;
195 };
196 static struct ev_select *ev_namelist;
197 static int evn_allocated;
198 static int evn_used;
199
200 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
201 {
202         int i;
203
204         for (i = 0; i < evn_used; i++) {
205                 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
206         }
207 }
208
209 void remember_event(const char *eventname)
210 {
211         int i=0, len;
212
213         if (!eventname || (len = strlen(eventname)) == 0)
214                 return;
215         while (i < evn_used) {
216                 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
217                         return;
218                 i++;
219         }
220         if (evn_used == evn_allocated) {
221                 evn_allocated += 10;
222                 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
223                 if (! ev_namelist)
224                         /* we are screwed, but let's just bail out */
225                         return;
226         }
227         ev_namelist[evn_used].ev_name = strdup(eventname);
228         ev_namelist[evn_used].plot_ev = TRUE;
229         evn_used++;
230 }
231
232 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
233 {
234         int i, depth = 0;
235         int x,y;
236
237         /* is plotting this event disabled? */
238         if (event->name) {
239                 for (i = 0; i < evn_used; i++) {
240                         if (! strcmp(event->name, ev_namelist[i].ev_name)) {
241                                 if (ev_namelist[i].plot_ev)
242                                         break;
243                                 else
244                                         return;
245                         }
246                 }
247         }
248         for (i = 0; i < pi->nr; i++) {
249                 struct plot_data *data = pi->entry + i;
250                 if (event->time.seconds < data->sec)
251                         break;
252                 depth = data->depth;
253         }
254         /* draw a little tirangular marker and attach tooltip */
255         x = SCALEX(gc, event->time.seconds);
256         y = SCALEY(gc, depth);
257         set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
258         cairo_move_to(gc->cr, x-15, y+6);
259         cairo_line_to(gc->cr, x-3  , y+6);
260         cairo_line_to(gc->cr, x-9, y-6);
261         cairo_line_to(gc->cr, x-15, y+6);
262         cairo_stroke_preserve(gc->cr);
263         cairo_fill(gc->cr);
264         set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
265         cairo_move_to(gc->cr, x-9, y-3);
266         cairo_line_to(gc->cr, x-9, y+1);
267         cairo_move_to(gc->cr, x-9, y+4);
268         cairo_line_to(gc->cr, x-9, y+4);
269         cairo_stroke(gc->cr);
270         attach_tooltip(x-15, y-6, 12, 12, event->name);
271 }
272
273 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
274 {
275         static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
276         struct event *event = dive->events;
277
278         if (gc->printer)
279                 return;
280
281         while (event) {
282                 plot_one_event(gc, pi, event, &tro);
283                 event = event->next;
284         }
285 }
286
287 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
288 {
289         int sec = entry->sec, decimals;
290         double d;
291
292         d = get_depth_units(entry->depth, &decimals, NULL);
293
294         plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
295 }
296
297 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
298 {
299         static const text_render_options_t deep = {14, 0.8, 0.2, 0.2, CENTER, TOP};
300         static const text_render_options_t shallow = {14, 0.8, 0.2, 0.2, CENTER, BOTTOM};
301         int i;
302         int last = -1;
303
304         for (i = 0; i < pi->nr; i++) {
305                 struct plot_data *entry = pi->entry + i;
306
307                 if (entry->depth < 2000)
308                         continue;
309
310                 if ((entry == entry->max[2]) && entry->depth != last) {
311                         render_depth_sample(gc, entry, &deep);
312                         last = entry->depth;
313                 }
314
315                 if ((entry == entry->min[2]) && entry->depth != last) {
316                         render_depth_sample(gc, entry, &shallow);
317                         last = entry->depth;
318                 }
319
320                 if (entry->depth != last)
321                         last = -1;
322         }
323 }
324
325 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
326 {
327         int maxtime, maxdepth;
328
329         /* Get plot scaling limits */
330         maxtime = get_maxtime(pi);
331         maxdepth = get_maxdepth(pi);
332
333         gc->leftx = 0; gc->rightx = maxtime;
334         gc->topy = 0; gc->bottomy = maxdepth;
335
336         plot_text_samples(gc, pi);
337 }
338
339 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
340 {
341         int i;
342         struct plot_data *entry = pi->entry;
343
344         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
345         move_to(gc, entry->sec, entry->smoothed);
346         for (i = 1; i < pi->nr; i++) {
347                 entry++;
348                 line_to(gc, entry->sec, entry->smoothed);
349         }
350         cairo_stroke(gc->cr);
351 }
352
353 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
354                                 int index, double a)
355 {
356         int i;
357         struct plot_data *entry = pi->entry;
358
359         set_source_rgba(gc, 0.7, 0.2, 0.7, a);
360         move_to(gc, entry->sec, entry->min[index]->depth);
361         for (i = 1; i < pi->nr; i++) {
362                 entry++;
363                 line_to(gc, entry->sec, entry->min[index]->depth);
364         }
365         for (i = 1; i < pi->nr; i++) {
366                 line_to(gc, entry->sec, entry->max[index]->depth);
367                 entry--;
368         }
369         cairo_close_path(gc->cr);
370         cairo_fill(gc->cr);
371 }
372
373 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
374 {
375         if (gc->printer)
376                 return;
377         plot_minmax_profile_minute(gc, pi, 2, 0.1);
378         plot_minmax_profile_minute(gc, pi, 1, 0.1);
379         plot_minmax_profile_minute(gc, pi, 0, 0.1);
380 }
381
382 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
383 {
384         int i, incr;
385         cairo_t *cr = gc->cr;
386         int sec, depth;
387         struct plot_data *entry;
388         int maxtime, maxdepth, marker;
389         int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
390
391         /* Get plot scaling limits */
392         maxtime = get_maxtime(pi);
393         maxdepth = get_maxdepth(pi);
394
395         /* Time markers: at most every 5 min, but no more than 12 markers
396          * and for convenience we do 5, 10, 15 or 30 min intervals.
397          * This allows for 6h dives - enough (I hope) for even the craziest
398          * divers - but just in case, for those 8h depth-record-breaking dives,
399          * we double the interval if this still doesn't get us to 12 or fewer
400          * time markers */
401         i = 0;
402         while (maxtime / increments[i] > 12 && i < 4)
403                 i++;
404         incr = increments[i];
405         while (maxtime / incr > 12)
406                 incr *= 2;
407
408         gc->leftx = 0; gc->rightx = maxtime;
409         gc->topy = 0; gc->bottomy = 1.0;
410         set_source_rgba(gc, 1, 1, 1, 0.5);
411         cairo_set_line_width(gc->cr, 1);
412
413         for (i = incr; i < maxtime; i += incr) {
414                 move_to(gc, i, 0);
415                 line_to(gc, i, 1);
416         }
417         cairo_stroke(cr);
418
419         /* now the text on every second time marker */
420         text_render_options_t tro = {10, 0.1, 0.5, 0.1, CENTER, TOP};
421         for (i = incr; i < maxtime; i += 2 * incr)
422                 plot_text(gc, &tro, i, 1, "%d", i/60);
423
424         /* Depth markers: every 30 ft or 10 m*/
425         gc->leftx = 0; gc->rightx = 1.0;
426         gc->topy = 0; gc->bottomy = maxdepth;
427         switch (output_units.length) {
428         case METERS: marker = 10000; break;
429         case FEET: marker = 9144; break;        /* 30 ft */
430         }
431
432         set_source_rgba(gc, 1, 1, 1, 0.5);
433         for (i = marker; i < maxdepth; i += marker) {
434                 move_to(gc, 0, i);
435                 line_to(gc, 1, i);
436         }
437         cairo_stroke(cr);
438
439         /* Show mean depth */
440         if (! gc->printer) {
441                 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
442                 move_to(gc, 0, pi->meandepth);
443                 line_to(gc, 1, pi->meandepth);
444                 cairo_stroke(cr);
445         }
446
447         gc->leftx = 0; gc->rightx = maxtime;
448
449         /*
450          * These are good for debugging text placement etc,
451          * but not for actual display..
452          */
453         if (0) {
454                 plot_smoothed_profile(gc, pi);
455                 plot_minmax_profile(gc, pi);
456         }
457
458         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
459
460         /* Do the depth profile for the neat fill */
461         gc->topy = 0; gc->bottomy = maxdepth;
462
463         cairo_pattern_t *pat;
464         pat = cairo_pattern_create_linear (0.0, 0.0,  0.0, 256.0);
465         cairo_pattern_add_color_stop_rgba (pat, 1, 0.2, 0.2, 0.8, 0.6);
466         cairo_pattern_add_color_stop_rgba (pat, 0, 0.9, 0.9, 0.9, 0.6);
467
468         cairo_set_source(gc->cr, pat);
469         cairo_pattern_destroy(pat);
470         cairo_set_line_width(gc->cr, 2);
471
472         entry = pi->entry;
473         move_to(gc, 0, 0);
474         for (i = 0; i < pi->nr; i++, entry++)
475                 line_to(gc, entry->sec, entry->depth);
476         cairo_close_path(gc->cr);
477         if (gc->printer) {
478                 set_source_rgba(gc, 1, 1, 1, 0.2);
479                 cairo_fill_preserve(cr);
480                 set_source_rgb(gc, 1, 1, 1);
481                 cairo_stroke(cr);
482                 return;
483         }
484         cairo_fill(gc->cr);
485
486         /* Now do it again for the velocity colors */
487         entry = pi->entry;
488         for (i = 1; i < pi->nr; i++) {
489                 entry++;
490                 sec = entry->sec;
491                 /* we want to draw the segments in different colors
492                  * representing the vertical velocity, so we need to
493                  * chop this into short segments */
494                 depth = entry->depth;
495                 set_source_rgb_struct(gc, &velocity_color[entry->velocity]);
496                 move_to(gc, entry[-1].sec, entry[-1].depth);
497                 line_to(gc, sec, depth);
498                 cairo_stroke(cr);
499         }
500 }
501
502 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
503 {
504         int maxtime, mintemp, maxtemp, delta;
505
506         /* Get plot scaling limits */
507         maxtime = get_maxtime(pi);
508         mintemp = pi->mintemp;
509         maxtemp = pi->maxtemp;
510
511         gc->leftx = 0; gc->rightx = maxtime;
512         /* Show temperatures in roughly the lower third, but make sure the scale
513            is at least somewhat reasonable */
514         delta = maxtemp - mintemp;
515         if (delta > 3000) { /* more than 3K in fluctuation */
516                 gc->topy = maxtemp + delta*2;
517                 gc->bottomy = mintemp - delta/2;
518         } else {
519                 gc->topy = maxtemp + 1500 + delta*2;
520                 gc->bottomy = mintemp - delta/2;
521         }
522
523         return maxtemp > mintemp;
524 }
525
526 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
527 {
528         double deg;
529         const char *unit;
530         static const text_render_options_t tro = {12, 0.2, 0.2, 0.7, LEFT, TOP};
531
532         deg = get_temp_units(mkelvin, &unit);
533
534         plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
535 }
536
537 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
538 {
539         int i;
540         int last = -300, sec = 0;
541         int last_temperature = 0, last_printed_temp = 0;
542
543         if (!setup_temperature_limits(gc, pi))
544                 return;
545
546         for (i = 0; i < pi->nr; i++) {
547                 struct plot_data *entry = pi->entry+i;
548                 int mkelvin = entry->temperature;
549
550                 if (!mkelvin)
551                         continue;
552                 last_temperature = mkelvin;
553                 sec = entry->sec;
554                 /* don't print a temperature
555                  * if it's been less than 5min and less than a 2K change OR
556                  * if it's been less than 2min OR if the change from the
557                  * last print is less than .4K (and therefore less than 1F */
558                 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
559                         (sec < last + 120) ||
560                         (abs(mkelvin - last_printed_temp) < 400))
561                         continue;
562                 last = sec;
563                 plot_single_temp_text(gc,sec,mkelvin);
564                 last_printed_temp = mkelvin;
565         }
566         /* it would be nice to print the end temperature, if it's
567          * different or if the last temperature print has been more
568          * than a quarter of the dive back */
569         if ((abs(last_temperature - last_printed_temp) > 500) ||
570                 ((double)last / (double)sec < 0.75))
571                 plot_single_temp_text(gc, sec, last_temperature);
572 }
573
574 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
575 {
576         int i;
577         cairo_t *cr = gc->cr;
578         int last = 0;
579
580         if (!setup_temperature_limits(gc, pi))
581                 return;
582
583         cairo_set_line_width(gc->cr, 2);
584         set_source_rgba(gc, 0.2, 0.2, 0.9, 0.8);
585         for (i = 0; i < pi->nr; i++) {
586                 struct plot_data *entry = pi->entry + i;
587                 int mkelvin = entry->temperature;
588                 int sec = entry->sec;
589                 if (!mkelvin) {
590                         if (!last)
591                                 continue;
592                         mkelvin = last;
593                 }
594                 if (last)
595                         line_to(gc, sec, mkelvin);
596                 else
597                         move_to(gc, sec, mkelvin);
598                 last = mkelvin;
599         }
600         cairo_stroke(cr);
601 }
602
603 /* gets both the actual start and end pressure as well as the scaling factors */
604 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
605 {
606         gc->leftx = 0;
607         gc->rightx = get_maxtime(pi);
608
609         gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
610         return pi->maxpressure != 0;
611 }
612
613 #define SAC_COLORS 9
614 static const rgb_t sac_color[SAC_COLORS] = {
615         { 0.0, 0.4, 0.2},
616         { 0.2, 0.6, 0.2},
617         { 0.4, 0.8, 0.2},
618         { 0.6, 0.8, 0.2},
619         { 0.8, 0.8, 0.2},
620         { 0.8, 0.6, 0.2},
621         { 0.8, 0.4, 0.2},
622         { 0.9, 0.3, 0.2},
623         { 1.0, 0.2, 0.2},
624 };
625
626 /* set the color for the pressure plot according to temporary sac rate
627  * as compared to avg_sac; the calculation simply maps the delta between
628  * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
629  * more than 6000 ml/min below avg_sac mapped to 0 */
630
631 static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
632 {
633         int sac_index = 0;
634         int delta = sac - avg_sac + 7000;
635
636         if (!gc->printer) {
637                 sac_index = delta / 2000;
638                 if (sac_index < 0)
639                         sac_index = 0;
640                 if (sac_index > SAC_COLORS - 1)
641                         sac_index = SAC_COLORS - 1;
642                 set_source_rgb_struct(gc, &sac_color[sac_index]);
643         } else {
644                 set_source_rgb(gc, 1.0, 1.0, 1.0);
645         }
646 }
647
648 /* calculate the current SAC in ml/min and convert to int */
649 #define GET_LOCAL_SAC(_entry1, _entry2, _dive)  (int)                           \
650         ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) *                  \
651                 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter /  \
652                 (((_entry2)->sec - (_entry1)->sec) / 60.0) /                    \
653                 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) /         \
654                 1000.0)
655
656 #define SAC_WINDOW 45   /* sliding window in seconds for current SAC calculation */
657
658 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
659                                 struct dive *dive)
660 {
661         int i;
662         int last = -1;
663         int lift_pen = FALSE;
664         int first_plot = TRUE;
665         int sac = 0;
666         struct plot_data *last_entry = NULL;
667
668         if (!get_cylinder_pressure_range(gc, pi))
669                 return;
670
671         cairo_set_line_width(gc->cr, 2);
672
673         for (i = 0; i < pi->nr; i++) {
674                 int mbar;
675                 struct plot_data *entry = pi->entry + i;
676
677                 mbar = GET_PRESSURE(entry);
678                 if (!entry->same_cylinder) {
679                         lift_pen = TRUE;
680                         last_entry = NULL;
681                 }
682                 if (!mbar) {
683                         lift_pen = TRUE;
684                         continue;
685                 }
686                 if (!last_entry) {
687                         last = i;
688                         last_entry = entry;
689                         sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
690                 } else {
691                         int j;
692                         sac = 0;
693                         for (j = last; j < i; j++)
694                                 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
695                         sac /= (i - last);
696                         if (entry->sec - last_entry->sec >= SAC_WINDOW) {
697                                 last++;
698                                 last_entry = pi->entry + last;
699                         }
700                 }
701                 set_sac_color(gc, sac, dive->sac);
702                 if (lift_pen) {
703                         if (!first_plot && entry->same_cylinder) {
704                                 /* if we have a previous event from the same tank,
705                                  * draw at least a short line */
706                                 int prev_pr;
707                                 prev_pr = GET_PRESSURE(entry - 1);
708                                 move_to(gc, (entry-1)->sec, prev_pr);
709                                 line_to(gc, entry->sec, mbar);
710                         } else {
711                                 first_plot = FALSE;
712                                 move_to(gc, entry->sec, mbar);
713                         }
714                         lift_pen = FALSE;
715                 } else {
716                         line_to(gc, entry->sec, mbar);
717                 }
718                 cairo_stroke(gc->cr);
719                 move_to(gc, entry->sec, mbar);
720         }
721 }
722
723 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
724                                 int xalign, int yalign)
725 {
726         int pressure;
727         const char *unit;
728
729         pressure = get_pressure_units(mbar, &unit);
730         text_render_options_t tro = {10, 0.2, 0.6, 0.2, xalign, yalign};
731         plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
732 }
733
734 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
735 {
736         int i;
737         int mbar, cyl;
738         int seen_cyl[MAX_CYLINDERS] = { FALSE, };
739         int last_pressure[MAX_CYLINDERS] = { 0, };
740         int last_time[MAX_CYLINDERS] = { 0, };
741         struct plot_data *entry;
742
743         if (!get_cylinder_pressure_range(gc, pi))
744                 return;
745
746         /* only loop over the actual events from the dive computer
747          * plus the second synthetic event at the start (to make sure
748          * we get "time=0" right)
749          * sadly with a recent change that first entry may no longer
750          * have any pressure reading - in that case just grab the
751          * pressure from the second entry */
752         if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
753                 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
754         for (i = 1; i < pi->nr; i++) {
755                 entry = pi->entry + i;
756
757                 if (!entry->same_cylinder) {
758                         cyl = entry->cylinderindex;
759                         if (!seen_cyl[cyl]) {
760                                 mbar = GET_PRESSURE(entry);
761                                 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
762                                 seen_cyl[cyl] = TRUE;
763                         }
764                         if (i > 2) {
765                                 /* remember the last pressure and time of
766                                  * the previous cylinder */
767                                 cyl = (entry - 1)->cylinderindex;
768                                 last_pressure[cyl] = GET_PRESSURE(entry - 1);
769                                 last_time[cyl] = (entry - 1)->sec;
770                         }
771                 }
772         }
773         cyl = entry->cylinderindex;
774         if (GET_PRESSURE(entry))
775                 last_pressure[cyl] = GET_PRESSURE(entry);
776         last_time[cyl] = entry->sec;
777
778         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
779                 if (last_time[cyl]) {
780                         plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
781                 }
782         }
783 }
784
785 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
786 {
787         struct plot_data *p = entry;
788         int time = entry->sec;
789         int seconds = 90*(index+1);
790         struct plot_data *min, *max;
791         int avg, nr;
792
793         /* Go back 'seconds' in time */
794         while (p > first) {
795                 if (p[-1].sec < time - seconds)
796                         break;
797                 p--;
798         }
799
800         /* Then go forward until we hit an entry past the time */
801         min = max = p;
802         avg = p->depth;
803         nr = 1;
804         while (++p < last) {
805                 int depth = p->depth;
806                 if (p->sec > time + seconds)
807                         break;
808                 avg += depth;
809                 nr ++;
810                 if (depth < min->depth)
811                         min = p;
812                 if (depth > max->depth)
813                         max = p;
814         }
815         entry->min[index] = min;
816         entry->max[index] = max;
817         entry->avg[index] = (avg + nr/2) / nr;
818 }
819
820 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
821 {
822         analyze_plot_info_minmax_minute(entry, first, last, 0);
823         analyze_plot_info_minmax_minute(entry, first, last, 1);
824         analyze_plot_info_minmax_minute(entry, first, last, 2);
825 }
826
827 static velocity_t velocity(int speed)
828 {
829         velocity_t v;
830
831         if (speed < -304) /* ascent faster than -60ft/min */
832                 v = CRAZY;
833         else if (speed < -152) /* above -30ft/min */
834                 v = FAST;
835         else if (speed < -76) /* -15ft/min */
836                 v = MODERATE;
837         else if (speed < -25) /* -5ft/min */
838                 v = SLOW;
839         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
840                                 for descent are usually about 2x ascent rate; still, we want 
841                                 stable to mean stable */
842                 v = STABLE;
843         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
844                 v = SLOW;
845         else if (speed < 304) /* up to 60ft/min is moderate */
846                 v = MODERATE;
847         else if (speed < 507) /* up to 100ft/min is fast */
848                 v = FAST;
849         else /* more than that is just crazy - you'll blow your ears out */
850                 v = CRAZY;
851
852         return v;
853 }
854 static struct plot_info *analyze_plot_info(struct plot_info *pi)
855 {
856         int i;
857         int nr = pi->nr;
858
859         /* Do pressure min/max based on the non-surface data */
860         for (i = 0; i < nr; i++) {
861                 struct plot_data *entry = pi->entry+i;
862                 int pressure = GET_PRESSURE(entry);
863                 int temperature = entry->temperature;
864
865                 if (pressure) {
866                         if (pressure > pi->maxpressure)
867                                 pi->maxpressure = pressure;
868                 }
869
870                 if (temperature) {
871                         if (!pi->mintemp || temperature < pi->mintemp)
872                                 pi->mintemp = temperature;
873                         if (temperature > pi->maxtemp)
874                                 pi->maxtemp = temperature;
875                 }
876         }
877
878         /* Smoothing function: 5-point triangular smooth */
879         for (i = 2; i < nr; i++) {
880                 struct plot_data *entry = pi->entry+i;
881                 int depth;
882
883                 if (i < nr-2) {
884                         depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
885                         entry->smoothed = (depth+4) / 9;
886                 }
887                 /* vertical velocity in mm/sec */
888                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
889                 if (entry[0].sec - entry[-1].sec) {
890                         entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
891                         /* if our samples are short and we aren't too FAST*/
892                         if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
893                                 int past = -2;
894                                 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
895                                         past--;
896                                 entry->velocity = velocity((entry[0].depth - entry[past].depth) / 
897                                                         (entry[0].sec - entry[past].sec));
898                         }
899                 } else
900                         entry->velocity = STABLE;
901         }
902
903         /* One-, two- and three-minute minmax data */
904         for (i = 0; i < nr; i++) {
905                 struct plot_data *entry = pi->entry +i;
906                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
907         }
908         
909         return pi;
910 }
911
912 /*
913  * simple structure to track the beginning and end tank pressure as
914  * well as the integral of depth over time spent while we have no
915  * pressure reading from the tank */
916 typedef struct pr_track_struct pr_track_t;
917 struct pr_track_struct {
918         int start;
919         int end;
920         int t_start;
921         int t_end;
922         double pressure_time;
923         pr_track_t *next;
924 };
925
926 static pr_track_t *pr_track_alloc(int start, int t_start) {
927         pr_track_t *pt = malloc(sizeof(pr_track_t));
928         pt->start = start;
929         pt->t_start = t_start;
930         pt->end = 0;
931         pt->t_end = 0;
932         pt->pressure_time = 0.0;
933         pt->next = NULL;
934         return pt;
935 }
936
937 /* poor man's linked list */
938 static pr_track_t *list_last(pr_track_t *list)
939 {
940         pr_track_t *tail = list;
941         if (!tail)
942                 return NULL;
943         while (tail->next) {
944                 tail = tail->next;
945         }
946         return tail;
947 }
948
949 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
950 {
951         pr_track_t *tail = list_last(list);
952         if (!tail)
953                 return element;
954         tail->next = element;
955         return list;
956 }
957
958 static void list_free(pr_track_t *list)
959 {
960         if (!list)
961                 return;
962         list_free(list->next);
963         free(list);
964 }
965
966 static void dump_pr_track(pr_track_t **track_pr)
967 {
968         int cyl;
969         pr_track_t *list;
970
971         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
972                 list = track_pr[cyl];
973                 while (list) {
974                         printf("cyl%d: start %d end %d t_start %d t_end %d pt %6.3f\n", cyl,
975                                 list->start, list->end, list->t_start, list->t_end, list->pressure_time);
976                         list = list->next;
977                 }
978         }
979 }
980
981 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
982                                         pr_track_t **track_pr)
983 {
984         pr_track_t *list = NULL;
985         pr_track_t *nlist = NULL;
986         double pt, magic;
987         int cyl, i;
988         struct plot_data *entry;
989         int cur_pr[MAX_CYLINDERS];
990
991         if (0) {
992                 /* another great debugging tool */
993                 dump_pr_track(track_pr);
994         }
995         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
996                 cur_pr[cyl] = track_pr[cyl]->start;
997         }
998
999         /* The first two are "fillers", but in case we don't have a sample
1000          * at time 0 we need to process the second of them here */
1001         for (i = 1; i < pi->nr; i++) {
1002                 entry = pi->entry + i;
1003                 if (SENSOR_PRESSURE(entry)) {
1004                         cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
1005                 } else {
1006                         if(!list || list->t_end < entry->sec) {
1007                                 nlist = track_pr[entry->cylinderindex];
1008                                 list = NULL;
1009                                 while (nlist && nlist->t_start <= entry->sec) {
1010                                         list = nlist;
1011                                         nlist = list->next;
1012                                 }
1013                                 /* there may be multiple segments - so
1014                                  * let's assemble the length */
1015                                 nlist = list;
1016                                 pt = list->pressure_time;
1017                                 while (!nlist->end) {
1018                                         nlist = nlist->next;
1019                                         if (!nlist) {
1020                                                 /* oops - we have no end pressure,
1021                                                  * so this means this is a tank without
1022                                                  * gas consumption information */
1023                                                 break;
1024                                         }
1025                                         pt += nlist->pressure_time;
1026                                 }
1027                                 if (!nlist) {
1028                                         /* just continue without calculating
1029                                          * interpolated values */
1030                                         INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1031                                         list = NULL;
1032                                         continue;
1033                                 }
1034                                 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
1035                         }
1036                         if (pt != 0.0) {
1037                                 double cur_pt = (entry->sec - (entry-1)->sec) *
1038                                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1039                                 INTERPOLATED_PRESSURE(entry) =
1040                                         cur_pr[entry->cylinderindex] + cur_pt * magic + 0.5;
1041                                 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1042                         } else
1043                                 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1044                 }
1045         }
1046 }
1047
1048 static int get_cylinder_index(struct dive *dive, struct event *ev)
1049 {
1050         int i;
1051
1052         /*
1053          * Try to find a cylinder that matches the O2 percentage
1054          * in the gas change event 'value' field.
1055          *
1056          * Crazy suunto gas change events. We really should do
1057          * this in libdivecomputer or something.
1058          */
1059         for (i = 0; i < MAX_CYLINDERS; i++) {
1060                 cylinder_t *cyl = dive->cylinder+i;
1061                 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1062                 if (o2 == ev->value)
1063                         return i;
1064         }
1065
1066         return 0;
1067 }
1068
1069 static struct event *get_next_gaschange(struct event *event)
1070 {
1071         while (event) {
1072                 if (!strcmp(event->name, "gaschange"))
1073                         return event;
1074                 event = event->next;
1075         }
1076         return event;
1077 }
1078
1079 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1080 {
1081         while (i < pi->nr) {
1082                 struct plot_data *entry = pi->entry+i;
1083                 if (entry->sec > end)
1084                         break;
1085                 if (entry->cylinderindex != cylinderindex) {
1086                         entry->cylinderindex = cylinderindex;
1087                         entry->pressure[0] = 0;
1088                 }
1089                 i++;
1090         }
1091         return i;
1092 }
1093
1094 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1095 {
1096         int i = 0, cylinderindex = 0;
1097         struct event *ev = get_next_gaschange(dive->events);
1098
1099         if (!ev)
1100                 return;
1101
1102         do {
1103                 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1104                 cylinderindex = get_cylinder_index(dive, ev);
1105                 ev = get_next_gaschange(ev->next);
1106         } while (ev);
1107         set_cylinder_index(pi, i, cylinderindex, ~0u);
1108 }
1109
1110 /* for computers that track gas changes through events */
1111 static int count_gas_change_events(struct dive *dive)
1112 {
1113         int count = 0;
1114         struct event *ev = get_next_gaschange(dive->events);
1115
1116         while (ev) {
1117                 count++;
1118                 ev = get_next_gaschange(ev->next);
1119         }
1120         return count;
1121 }
1122
1123 /*
1124  * Create a plot-info with smoothing and ranged min/max
1125  *
1126  * This also makes sure that we have extra empty events on both
1127  * sides, so that you can do end-points without having to worry
1128  * about it.
1129  */
1130 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1131 {
1132         int cylinderindex = -1;
1133         int lastdepth, lastindex;
1134         int i, pi_idx, nr, sec, cyl;
1135         size_t alloc_size;
1136         struct plot_info *pi;
1137         pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1138         pr_track_t *pr_track, *current;
1139         gboolean missing_pr = FALSE;
1140         struct plot_data *entry = NULL;
1141         struct event *ev;
1142
1143         /* we want to potentially add synthetic plot_info elements for the gas changes */
1144         nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1145         alloc_size = plot_info_size(nr);
1146         pi = malloc(alloc_size);
1147         if (!pi)
1148                 return pi;
1149         memset(pi, 0, alloc_size);
1150         pi->nr = nr;
1151         pi_idx = 2; /* the two extra events at the start */
1152         /* check for gas changes before the samples start */
1153         ev = get_next_gaschange(dive->events);
1154         while (ev && ev->time.seconds < dive_sample->time.seconds) {
1155                 entry = pi->entry + pi_idx;
1156                 entry->sec = ev->time.seconds;
1157                 entry->depth = 0; /* is that always correct ? */
1158                 pi_idx++;
1159                 ev = get_next_gaschange(ev->next);
1160         }
1161         if (ev && ev->time.seconds == dive_sample->time.seconds) {
1162                 /* we already have a sample at the time of the event */
1163                 ev = get_next_gaschange(ev->next);
1164         }
1165         sec = 0;
1166         lastindex = 0;
1167         lastdepth = -1;
1168         for (i = 0; i < nr_samples; i++) {
1169                 int depth;
1170                 int delay = 0;
1171                 struct sample *sample = dive_sample+i;
1172
1173                 entry = pi->entry + i + pi_idx;
1174                 while (ev && ev->time.seconds < sample->time.seconds) {
1175                         /* insert two fake plot info structures for the end of
1176                          * the old tank and the start of the new tank */
1177                         if (ev->time.seconds == sample->time.seconds - 1) {
1178                                 entry->sec = ev->time.seconds - 1;
1179                                 (entry+1)->sec = ev->time.seconds;
1180                         } else {
1181                                 entry->sec = ev->time.seconds;
1182                                 (entry+1)->sec = ev->time.seconds + 1;
1183                         }
1184                         /* we need a fake depth - let's interpolate */
1185                         if (i) {
1186                                 entry->depth = sample->depth.mm -
1187                                         (sample->depth.mm - (sample-1)->depth.mm) / 2;
1188                         } else
1189                                 entry->depth = sample->depth.mm;
1190                         (entry+1)->depth = entry->depth;
1191                         pi_idx += 2;
1192                         entry = pi->entry + i + pi_idx;
1193                         ev = get_next_gaschange(ev->next);
1194                 }
1195                 if (ev && ev->time.seconds == sample->time.seconds) {
1196                         /* we already have a sample at the time of the event
1197                          * just add a new one for the old tank and delay the
1198                          * real even by one second (to keep time monotonous) */
1199                         entry->sec = ev->time.seconds;
1200                         entry->depth = sample->depth.mm;
1201                         pi_idx++;
1202                         entry = pi->entry + i + pi_idx;
1203                         ev = get_next_gaschange(ev->next);
1204                         delay = 1;
1205                 }
1206                 sec = entry->sec = sample->time.seconds + delay;
1207                 depth = entry->depth = sample->depth.mm;
1208                 entry->cylinderindex = sample->cylinderindex;
1209                 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1210                 entry->temperature = sample->temperature.mkelvin;
1211
1212                 if (depth || lastdepth)
1213                         lastindex = i + pi_idx;
1214
1215                 lastdepth = depth;
1216                 if (depth > pi->maxdepth)
1217                         pi->maxdepth = depth;
1218         }
1219         entry = pi->entry + i + pi_idx;
1220         /* are there still unprocessed gas changes? that would be very strange */
1221         while (ev) {
1222                 entry->sec = ev->time.seconds;
1223                 entry->depth = 0; /* why are there gas changes after the dive is over? */
1224                 pi_idx++;
1225                 entry = pi->entry + i + pi_idx;
1226                 ev = get_next_gaschange(ev->next);
1227         }
1228         nr = nr_samples + pi_idx - 2;
1229         check_gas_change_events(dive, pi);
1230
1231         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1232                 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1233         current = track_pr[pi->entry[2].cylinderindex];
1234         for (i = 0; i < nr + 1; i++) {
1235                 entry = pi->entry + i + 1;
1236
1237                 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1238                 cylinderindex = entry->cylinderindex;
1239
1240                 /* track the segments per cylinder and their pressure/time integral */
1241                 if (!entry->same_cylinder) {
1242                         current->end = SENSOR_PRESSURE(entry-1);
1243                         current->t_end = (entry-1)->sec;
1244                         current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1245                         track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1246                 } else { /* same cylinder */
1247                         if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1248                                 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1249                                 /* transmitter changed its working status */
1250                                 current->end = SENSOR_PRESSURE(entry-1);
1251                                 current->t_end = (entry-1)->sec;
1252                                 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1253                                 track_pr[cylinderindex] =
1254                                         list_add(track_pr[cylinderindex], current);
1255                         }
1256                 }
1257                 /* finally, do the discrete integration to get the SAC rate equivalent */
1258                 current->pressure_time += (entry->sec - (entry-1)->sec) *
1259                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1260                 missing_pr |= !SENSOR_PRESSURE(entry);
1261         }
1262
1263         if (entry)
1264                 current->t_end = entry->sec;
1265
1266         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1267                 int pr = dive->cylinder[cyl].end.mbar;
1268                 if (pr && track_pr[cyl]) {
1269                         pr_track = list_last(track_pr[cyl]);
1270                         pr_track->end = pr;
1271                 }
1272         }
1273         /* Fill in the last two entries with empty values but valid times
1274          * without creating a false cylinder change event */
1275         i = nr + 2;
1276         pi->entry[i].sec = sec + 20;
1277         pi->entry[i].same_cylinder = 1;
1278         pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1279         INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1280         pi->entry[i+1].sec = sec + 40;
1281         pi->entry[i+1].same_cylinder = 1;
1282         pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1283         INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1284         /* the number of actual entries - some computers have lots of
1285          * depth 0 samples at the end of a dive, we want to make sure
1286          * we have exactly one of them at the end */
1287         pi->nr = lastindex+1;
1288         while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1289                 pi->nr++;
1290         pi->maxtime = pi->entry[lastindex].sec;
1291
1292         /* Analyze_plot_info() will do the sample max pressures,
1293          * this handles the manual pressures
1294          */
1295         pi->maxpressure = 0;
1296         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1297                 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1298                 if (mbar > pi->maxpressure)
1299                         pi->maxpressure = mbar;
1300         }
1301
1302         pi->meandepth = dive->meandepth.mm;
1303
1304         if (missing_pr) {
1305                 fill_missing_tank_pressures(dive, pi, track_pr);
1306         }
1307         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1308                 list_free(track_pr[cyl]);
1309         if (0) /* awesome for debugging - not useful otherwise */
1310                 dump_pi(pi);
1311         return analyze_plot_info(pi);
1312 }
1313
1314 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1315 {
1316         struct plot_info *pi;
1317         static struct sample fake[4];
1318         struct sample *sample = dive->sample;
1319         int nr = dive->samples;
1320
1321         if (!nr) {
1322                 int duration = dive->duration.seconds;
1323                 int maxdepth = dive->maxdepth.mm;
1324                 sample = fake;
1325                 fake[1].time.seconds = duration * 0.05;
1326                 fake[1].depth.mm = maxdepth;
1327                 fake[2].time.seconds = duration * 0.95;
1328                 fake[2].depth.mm = maxdepth;
1329                 fake[3].time.seconds = duration * 1.00;
1330                 nr = 4;
1331         }
1332
1333         pi = create_plot_info(dive, nr, sample);
1334
1335         cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1336         cairo_set_line_width(gc->cr, 1);
1337         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1338         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1339
1340         /*
1341          * We can use "cairo_translate()" because that doesn't
1342          * scale line width etc. But the actual scaling we need
1343          * do set up ourselves..
1344          *
1345          * Snif. What a pity.
1346          */
1347         gc->maxx = (drawing_area->width - 2*drawing_area->x);
1348         gc->maxy = (drawing_area->height - 2*drawing_area->y);
1349
1350         cairo_set_source_rgba(gc->cr, 0.95, 0.95, 0.90, 0.95);
1351         cairo_paint(gc->cr);
1352
1353         /* Temperature profile */
1354         plot_temperature_profile(gc, pi);
1355
1356         /* Depth profile */
1357         plot_depth_profile(gc, pi);
1358         plot_events(gc, pi, dive);
1359
1360         /* Cylinder pressure plot */
1361         plot_cylinder_pressure(gc, pi, dive);
1362
1363         /* Text on top of all graphs.. */
1364         plot_temperature_text(gc, pi);
1365         plot_depth_text(gc, pi);
1366         plot_cylinder_pressure_text(gc, pi);
1367
1368         /* Bounding box last */
1369         gc->leftx = 0; gc->rightx = 1.0;
1370         gc->topy = 0; gc->bottomy = 1.0;
1371
1372         set_source_rgb(gc, 1, 1, 1);
1373         cairo_set_line_width(gc->cr, 1);
1374         move_to(gc, 0, 0);
1375         line_to(gc, 0, 1);
1376         line_to(gc, 1, 1);
1377         line_to(gc, 1, 0);
1378         cairo_close_path(gc->cr);
1379         cairo_stroke(gc->cr);
1380
1381         free(pi);
1382 }