]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Merge branch 'sacplot' of git://git.hohndel.org/subsurface
[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_rgb(gc, 0, 0, 0);
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, 1.0, 0.2, 0.2, CENTER, TOP};
300         static const text_render_options_t shallow = {14, 1.0, 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, 1, 0.2, 1, 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         for (i = incr; i < maxtime; i += incr) {
412                 move_to(gc, i, 0);
413                 line_to(gc, i, 1);
414         }
415         cairo_stroke(cr);
416
417         /* now the text on every second time marker */
418         text_render_options_t tro = {10, 0.2, 1.0, 0.2, CENTER, TOP};
419         for (i = incr; i < maxtime; i += 2 * incr)
420                 plot_text(gc, &tro, i, 1, "%d", i/60);
421
422         /* Depth markers: every 30 ft or 10 m*/
423         gc->leftx = 0; gc->rightx = 1.0;
424         gc->topy = 0; gc->bottomy = maxdepth;
425         switch (output_units.length) {
426         case METERS: marker = 10000; break;
427         case FEET: marker = 9144; break;        /* 30 ft */
428         }
429
430         set_source_rgba(gc, 1, 1, 1, 0.5);
431         for (i = marker; i < maxdepth; i += marker) {
432                 move_to(gc, 0, i);
433                 line_to(gc, 1, i);
434         }
435         cairo_stroke(cr);
436
437         /* Show mean depth */
438         if (! gc->printer) {
439                 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
440                 move_to(gc, 0, pi->meandepth);
441                 line_to(gc, 1, pi->meandepth);
442                 cairo_stroke(cr);
443         }
444
445         gc->leftx = 0; gc->rightx = maxtime;
446
447         /*
448          * These are good for debugging text placement etc,
449          * but not for actual display..
450          */
451         if (0) {
452                 plot_smoothed_profile(gc, pi);
453                 plot_minmax_profile(gc, pi);
454         }
455
456         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
457
458         /* Do the depth profile for the neat fill */
459         gc->topy = 0; gc->bottomy = maxdepth;
460         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
461
462         entry = pi->entry;
463         move_to(gc, 0, 0);
464         for (i = 0; i < pi->nr; i++, entry++)
465                 line_to(gc, entry->sec, entry->depth);
466         cairo_close_path(gc->cr);
467         if (gc->printer) {
468                 set_source_rgba(gc, 1, 1, 1, 0.2);
469                 cairo_fill_preserve(cr);
470                 set_source_rgb(gc, 1, 1, 1);
471                 cairo_stroke(cr);
472                 return;
473         }
474         cairo_fill(gc->cr);
475
476         /* Now do it again for the velocity colors */
477         entry = pi->entry;
478         for (i = 1; i < pi->nr; i++) {
479                 entry++;
480                 sec = entry->sec;
481                 /* we want to draw the segments in different colors
482                  * representing the vertical velocity, so we need to
483                  * chop this into short segments */
484                 depth = entry->depth;
485                 set_source_rgb_struct(gc, &velocity_color[entry->velocity]);
486                 move_to(gc, entry[-1].sec, entry[-1].depth);
487                 line_to(gc, sec, depth);
488                 cairo_stroke(cr);
489         }
490 }
491
492 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
493 {
494         int maxtime, mintemp, maxtemp, delta;
495
496         /* Get plot scaling limits */
497         maxtime = get_maxtime(pi);
498         mintemp = pi->mintemp;
499         maxtemp = pi->maxtemp;
500
501         gc->leftx = 0; gc->rightx = maxtime;
502         /* Show temperatures in roughly the lower third, but make sure the scale
503            is at least somewhat reasonable */
504         delta = maxtemp - mintemp;
505         if (delta > 3000) { /* more than 3K in fluctuation */
506                 gc->topy = maxtemp + delta*2;
507                 gc->bottomy = mintemp - delta/2;
508         } else {
509                 gc->topy = maxtemp + 1500 + delta*2;
510                 gc->bottomy = mintemp - delta/2;
511         }
512
513         return maxtemp > mintemp;
514 }
515
516 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
517 {
518         double deg;
519         const char *unit;
520         static const text_render_options_t tro = {12, 0.6, 0.6, 1.0, LEFT, TOP};
521
522         deg = get_temp_units(mkelvin, &unit);
523
524         plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
525 }
526
527 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
528 {
529         int i;
530         int last = -300, sec = 0;
531         int last_temperature = 0, last_printed_temp = 0;
532
533         if (!setup_temperature_limits(gc, pi))
534                 return;
535
536         for (i = 0; i < pi->nr; i++) {
537                 struct plot_data *entry = pi->entry+i;
538                 int mkelvin = entry->temperature;
539
540                 if (!mkelvin)
541                         continue;
542                 last_temperature = mkelvin;
543                 sec = entry->sec;
544                 /* don't print a temperature
545                  * if it's been less than 5min and less than a 2K change OR
546                  * if it's been less than 2min OR if the change from the
547                  * last print is less than .4K (and therefore less than 1F */
548                 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
549                         (sec < last + 120) ||
550                         (abs(mkelvin - last_printed_temp) < 400))
551                         continue;
552                 last = sec;
553                 plot_single_temp_text(gc,sec,mkelvin);
554                 last_printed_temp = mkelvin;
555         }
556         /* it would be nice to print the end temperature, if it's
557          * different or if the last temperature print has been more
558          * than a quarter of the dive back */
559         if ((abs(last_temperature - last_printed_temp) > 500) ||
560                 ((double)last / (double)sec < 0.75))
561                 plot_single_temp_text(gc, sec, last_temperature);
562 }
563
564 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
565 {
566         int i;
567         cairo_t *cr = gc->cr;
568         int last = 0;
569
570         if (!setup_temperature_limits(gc, pi))
571                 return;
572
573         set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
574         for (i = 0; i < pi->nr; i++) {
575                 struct plot_data *entry = pi->entry + i;
576                 int mkelvin = entry->temperature;
577                 int sec = entry->sec;
578                 if (!mkelvin) {
579                         if (!last)
580                                 continue;
581                         mkelvin = last;
582                 }
583                 if (last)
584                         line_to(gc, sec, mkelvin);
585                 else
586                         move_to(gc, sec, mkelvin);
587                 last = mkelvin;
588         }
589         cairo_stroke(cr);
590 }
591
592 /* gets both the actual start and end pressure as well as the scaling factors */
593 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
594 {
595         gc->leftx = 0;
596         gc->rightx = get_maxtime(pi);
597
598         gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
599         return pi->maxpressure != 0;
600 }
601
602 #define SAC_COLORS 9
603 static const rgb_t sac_color[SAC_COLORS] = {
604         { 0.0, 0.4, 0.2},
605         { 0.2, 0.6, 0.2},
606         { 0.4, 0.8, 0.2},
607         { 0.6, 0.8, 0.2},
608         { 0.8, 0.8, 0.2},
609         { 0.8, 0.6, 0.2},
610         { 0.8, 0.4, 0.2},
611         { 0.9, 0.3, 0.2},
612         { 1.0, 0.2, 0.2},
613 };
614
615 /* set the color for the pressure plot according to temporary sac rate
616  * as compared to avg_sac; the calculation simply maps the delta between
617  * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
618  * more than 6000 ml/min below avg_sac mapped to 0 */
619
620 static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
621 {
622         int sac_index = 0;
623         int delta = sac - avg_sac + 7000;
624
625         sac_index = delta / 2000;
626         if (sac_index < 0)
627                 sac_index = 0;
628         if (sac_index > SAC_COLORS - 1)
629                 sac_index = SAC_COLORS - 1;
630         set_source_rgb_struct(gc, &sac_color[sac_index]);
631 }
632
633 /* calculate the current SAC in ml/min and convert to int */
634 #define GET_LOCAL_SAC(_entry1, _entry2, _dive)  (int)                           \
635         ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) *                  \
636                 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter /  \
637                 (((_entry2)->sec - (_entry1)->sec) / 60.0) /                    \
638                 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) /         \
639                 1000.0)
640
641 #define SAC_WINDOW 45   /* sliding window in seconds for current SAC calculation */
642
643 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
644                                 struct dive *dive)
645 {
646         int i;
647         int last = -1;
648         int lift_pen = FALSE;
649         int first_plot = TRUE;
650         int sac = 0;
651         struct plot_data *last_entry = NULL;
652
653         if (!get_cylinder_pressure_range(gc, pi))
654                 return;
655
656         for (i = 0; i < pi->nr; i++) {
657                 int mbar;
658                 struct plot_data *entry = pi->entry + i;
659
660                 mbar = GET_PRESSURE(entry);
661                 if (!entry->same_cylinder) {
662                         lift_pen = TRUE;
663                         last_entry = NULL;
664                 }
665                 if (!mbar) {
666                         lift_pen = TRUE;
667                         continue;
668                 }
669                 if (!last_entry) {
670                         last = i;
671                         last_entry = entry;
672                         if (first_plot) {
673                                 /* don't start with a sac of 0, so just calculate the first one */
674                                 sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
675                         }
676                 } else {
677                         int j;
678                         sac = 0;
679                         for (j = last; j < i; j++)
680                                 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
681                         sac /= (i - last);
682                         if (entry->sec - last_entry->sec >= SAC_WINDOW) {
683                                 last++;
684                                 last_entry = pi->entry + last;
685                         }
686                 }
687                 set_sac_color(gc, sac, dive->sac);
688                 if (lift_pen) {
689                         if (!first_plot && entry->same_cylinder) {
690                                 /* if we have a previous event from the same tank,
691                                  * draw at least a short line */
692                                 int prev_pr;
693                                 prev_pr = GET_PRESSURE(entry - 1);
694                                 move_to(gc, (entry-1)->sec, prev_pr);
695                                 line_to(gc, entry->sec, mbar);
696                         } else {
697                                 first_plot = FALSE;
698                                 move_to(gc, entry->sec, mbar);
699                         }
700                         lift_pen = FALSE;
701                 } else {
702                         line_to(gc, entry->sec, mbar);
703                 }
704                 cairo_stroke(gc->cr);
705                 move_to(gc, entry->sec, mbar);
706         }
707 }
708
709 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
710                                 int xalign, int yalign)
711 {
712         int pressure;
713         const char *unit;
714
715         pressure = get_pressure_units(mbar, &unit);
716         text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
717         plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
718 }
719
720 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
721 {
722         int i;
723         int mbar, cyl;
724         int seen_cyl[MAX_CYLINDERS] = { FALSE, };
725         int last_pressure[MAX_CYLINDERS] = { 0, };
726         int last_time[MAX_CYLINDERS] = { 0, };
727         struct plot_data *entry;
728
729         if (!get_cylinder_pressure_range(gc, pi))
730                 return;
731
732         /* only loop over the actual events from the dive computer
733          * plus the second synthetic event at the start (to make sure
734          * we get "time=0" right)
735          * sadly with a recent change that first entry may no longer
736          * have any pressure reading - in that case just grab the
737          * pressure from the second entry */
738         if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
739                 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
740         for (i = 1; i < pi->nr; i++) {
741                 entry = pi->entry + i;
742
743                 if (!entry->same_cylinder) {
744                         cyl = entry->cylinderindex;
745                         if (!seen_cyl[cyl]) {
746                                 mbar = GET_PRESSURE(entry);
747                                 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
748                                 seen_cyl[cyl] = TRUE;
749                         }
750                         if (i > 2) {
751                                 /* remember the last pressure and time of
752                                  * the previous cylinder */
753                                 cyl = (entry - 1)->cylinderindex;
754                                 last_pressure[cyl] = GET_PRESSURE(entry - 1);
755                                 last_time[cyl] = (entry - 1)->sec;
756                         }
757                 }
758         }
759         cyl = entry->cylinderindex;
760         if (GET_PRESSURE(entry))
761                 last_pressure[cyl] = GET_PRESSURE(entry);
762         last_time[cyl] = entry->sec;
763
764         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
765                 if (last_time[cyl]) {
766                         plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
767                 }
768         }
769 }
770
771 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
772 {
773         struct plot_data *p = entry;
774         int time = entry->sec;
775         int seconds = 90*(index+1);
776         struct plot_data *min, *max;
777         int avg, nr;
778
779         /* Go back 'seconds' in time */
780         while (p > first) {
781                 if (p[-1].sec < time - seconds)
782                         break;
783                 p--;
784         }
785
786         /* Then go forward until we hit an entry past the time */
787         min = max = p;
788         avg = p->depth;
789         nr = 1;
790         while (++p < last) {
791                 int depth = p->depth;
792                 if (p->sec > time + seconds)
793                         break;
794                 avg += depth;
795                 nr ++;
796                 if (depth < min->depth)
797                         min = p;
798                 if (depth > max->depth)
799                         max = p;
800         }
801         entry->min[index] = min;
802         entry->max[index] = max;
803         entry->avg[index] = (avg + nr/2) / nr;
804 }
805
806 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
807 {
808         analyze_plot_info_minmax_minute(entry, first, last, 0);
809         analyze_plot_info_minmax_minute(entry, first, last, 1);
810         analyze_plot_info_minmax_minute(entry, first, last, 2);
811 }
812
813 static velocity_t velocity(int speed)
814 {
815         velocity_t v;
816
817         if (speed < -304) /* ascent faster than -60ft/min */
818                 v = CRAZY;
819         else if (speed < -152) /* above -30ft/min */
820                 v = FAST;
821         else if (speed < -76) /* -15ft/min */
822                 v = MODERATE;
823         else if (speed < -25) /* -5ft/min */
824                 v = SLOW;
825         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
826                                 for descent are usually about 2x ascent rate; still, we want 
827                                 stable to mean stable */
828                 v = STABLE;
829         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
830                 v = SLOW;
831         else if (speed < 304) /* up to 60ft/min is moderate */
832                 v = MODERATE;
833         else if (speed < 507) /* up to 100ft/min is fast */
834                 v = FAST;
835         else /* more than that is just crazy - you'll blow your ears out */
836                 v = CRAZY;
837
838         return v;
839 }
840 static struct plot_info *analyze_plot_info(struct plot_info *pi)
841 {
842         int i;
843         int nr = pi->nr;
844
845         /* Do pressure min/max based on the non-surface data */
846         for (i = 0; i < nr; i++) {
847                 struct plot_data *entry = pi->entry+i;
848                 int pressure = GET_PRESSURE(entry);
849                 int temperature = entry->temperature;
850
851                 if (pressure) {
852                         if (pressure > pi->maxpressure)
853                                 pi->maxpressure = pressure;
854                 }
855
856                 if (temperature) {
857                         if (!pi->mintemp || temperature < pi->mintemp)
858                                 pi->mintemp = temperature;
859                         if (temperature > pi->maxtemp)
860                                 pi->maxtemp = temperature;
861                 }
862         }
863
864         /* Smoothing function: 5-point triangular smooth */
865         for (i = 2; i < nr; i++) {
866                 struct plot_data *entry = pi->entry+i;
867                 int depth;
868
869                 if (i < nr-2) {
870                         depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
871                         entry->smoothed = (depth+4) / 9;
872                 }
873                 /* vertical velocity in mm/sec */
874                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
875                 if (entry[0].sec - entry[-1].sec) {
876                         entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
877                         /* if our samples are short and we aren't too FAST*/
878                         if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
879                                 int past = -2;
880                                 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
881                                         past--;
882                                 entry->velocity = velocity((entry[0].depth - entry[past].depth) / 
883                                                         (entry[0].sec - entry[past].sec));
884                         }
885                 } else
886                         entry->velocity = STABLE;
887         }
888
889         /* One-, two- and three-minute minmax data */
890         for (i = 0; i < nr; i++) {
891                 struct plot_data *entry = pi->entry +i;
892                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
893         }
894         
895         return pi;
896 }
897
898 /*
899  * simple structure to track the beginning and end tank pressure as
900  * well as the integral of depth over time spent while we have no
901  * pressure reading from the tank */
902 typedef struct pr_track_struct pr_track_t;
903 struct pr_track_struct {
904         int start;
905         int end;
906         int t_start;
907         int t_end;
908         double pressure_time;
909         pr_track_t *next;
910 };
911
912 static pr_track_t *pr_track_alloc(int start, int t_start) {
913         pr_track_t *pt = malloc(sizeof(pr_track_t));
914         pt->start = start;
915         pt->t_start = t_start;
916         pt->end = 0;
917         pt->t_end = 0;
918         pt->pressure_time = 0.0;
919         pt->next = NULL;
920         return pt;
921 }
922
923 /* poor man's linked list */
924 static pr_track_t *list_last(pr_track_t *list)
925 {
926         pr_track_t *tail = list;
927         if (!tail)
928                 return NULL;
929         while (tail->next) {
930                 tail = tail->next;
931         }
932         return tail;
933 }
934
935 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
936 {
937         pr_track_t *tail = list_last(list);
938         if (!tail)
939                 return element;
940         tail->next = element;
941         return list;
942 }
943
944 static void list_free(pr_track_t *list)
945 {
946         if (!list)
947                 return;
948         list_free(list->next);
949         free(list);
950 }
951
952 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
953                                         pr_track_t **track_pr)
954 {
955         pr_track_t *list = NULL;
956         pr_track_t *nlist = NULL;
957         double pt, magic;
958         int cyl, i;
959         struct plot_data *entry;
960         int cur_pr[MAX_CYLINDERS];
961
962         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
963                 cur_pr[cyl] = track_pr[cyl]->start;
964         }
965
966         /* The first two are "fillers", but in case we don't have a sample
967          * at time 0 we need to process the second of them here */
968         for (i = 1; i < pi->nr; i++) {
969                 entry = pi->entry + i;
970                 if (SENSOR_PRESSURE(entry)) {
971                         cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
972                 } else {
973                         if(!list || list->t_end < entry->sec) {
974                                 nlist = track_pr[entry->cylinderindex];
975                                 list = NULL;
976                                 while (nlist && nlist->t_start <= entry->sec) {
977                                         list = nlist;
978                                         nlist = list->next;
979                                 }
980                                 /* there may be multiple segments - so
981                                  * let's assemble the length */
982                                 nlist = list;
983                                 pt = list->pressure_time;
984                                 while (!nlist->end) {
985                                         nlist = nlist->next;
986                                         if (!nlist) {
987                                                 /* oops - we have no end pressure,
988                                                  * so this means this is a tank without
989                                                  * gas consumption information */
990                                                 break;
991                                         }
992                                         pt += nlist->pressure_time;
993                                 }
994                                 if (!nlist) {
995                                         /* just continue without calculating
996                                          * interpolated values */
997                                         INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
998                                         list = NULL;
999                                         continue;
1000                                 }
1001                                 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
1002                         }
1003                         if (pt != 0.0) {
1004                                 double cur_pt = (entry->sec - (entry-1)->sec) *
1005                                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1006                                 INTERPOLATED_PRESSURE(entry) =
1007                                         cur_pr[entry->cylinderindex] + cur_pt * magic;
1008                                 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1009                         } else
1010                                 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1011                 }
1012         }
1013 }
1014
1015 static int get_cylinder_index(struct dive *dive, struct event *ev)
1016 {
1017         int i;
1018
1019         /*
1020          * Try to find a cylinder that matches the O2 percentage
1021          * in the gas change event 'value' field.
1022          *
1023          * Crazy suunto gas change events. We really should do
1024          * this in libdivecomputer or something.
1025          */
1026         for (i = 0; i < MAX_CYLINDERS; i++) {
1027                 cylinder_t *cyl = dive->cylinder+i;
1028                 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1029                 if (o2 == ev->value)
1030                         return i;
1031         }
1032
1033         return 0;
1034 }
1035
1036 static struct event *get_next_gaschange(struct event *event)
1037 {
1038         while (event) {
1039                 if (!strcmp(event->name, "gaschange"))
1040                         return event;
1041                 event = event->next;
1042         }
1043         return event;
1044 }
1045
1046 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1047 {
1048         while (i < pi->nr) {
1049                 struct plot_data *entry = pi->entry+i;
1050                 if (entry->sec > end)
1051                         break;
1052                 if (entry->cylinderindex != cylinderindex) {
1053                         entry->cylinderindex = cylinderindex;
1054                         entry->pressure[0] = 0;
1055                 }
1056                 i++;
1057         }
1058         return i;
1059 }
1060
1061 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1062 {
1063         int i = 0, cylinderindex = 0;
1064         struct event *ev = get_next_gaschange(dive->events);
1065
1066         if (!ev)
1067                 return;
1068
1069         do {
1070                 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1071                 cylinderindex = get_cylinder_index(dive, ev);
1072                 ev = get_next_gaschange(ev->next);
1073         } while (ev);
1074         set_cylinder_index(pi, i, cylinderindex, ~0u);
1075 }
1076
1077 /* for computers that track gas changes through events */
1078 static int count_gas_change_events(struct dive *dive)
1079 {
1080         int count = 0;
1081         struct event *ev = get_next_gaschange(dive->events);
1082
1083         while (ev) {
1084                 count++;
1085                 ev = get_next_gaschange(ev->next);
1086         }
1087         return count;
1088 }
1089
1090 /*
1091  * Create a plot-info with smoothing and ranged min/max
1092  *
1093  * This also makes sure that we have extra empty events on both
1094  * sides, so that you can do end-points without having to worry
1095  * about it.
1096  */
1097 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1098 {
1099         int cylinderindex = -1;
1100         int lastdepth, lastindex;
1101         int i, pi_idx, nr, sec, cyl;
1102         size_t alloc_size;
1103         struct plot_info *pi;
1104         pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1105         pr_track_t *pr_track, *current;
1106         gboolean missing_pr = FALSE;
1107         struct plot_data *entry = NULL;
1108         struct event *ev;
1109
1110         /* we want to potentially add synthetic plot_info elements for the gas changes */
1111         nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1112         alloc_size = plot_info_size(nr);
1113         pi = malloc(alloc_size);
1114         if (!pi)
1115                 return pi;
1116         memset(pi, 0, alloc_size);
1117         pi->nr = nr;
1118         pi_idx = 2; /* the two extra events at the start */
1119         /* check for gas changes before the samples start */
1120         ev = get_next_gaschange(dive->events);
1121         while (ev && ev->time.seconds < dive_sample->time.seconds) {
1122                 entry = pi->entry + pi_idx;
1123                 entry->sec = ev->time.seconds;
1124                 entry->depth = 0; /* is that always correct ? */
1125                 pi_idx++;
1126                 ev = get_next_gaschange(ev->next);
1127         }
1128         if (ev && ev->time.seconds == dive_sample->time.seconds) {
1129                 /* we already have a sample at the time of the event */
1130                 ev = get_next_gaschange(ev->next);
1131         }
1132         sec = 0;
1133         lastindex = 0;
1134         lastdepth = -1;
1135         for (i = 0; i < nr_samples; i++) {
1136                 int depth;
1137                 int delay = 0;
1138                 struct sample *sample = dive_sample+i;
1139
1140                 entry = pi->entry + i + pi_idx;
1141                 while (ev && ev->time.seconds < sample->time.seconds) {
1142                         /* insert two fake plot info structures for the end of
1143                          * the old tank and the start of the new tank */
1144                         entry->sec = ev->time.seconds;
1145                         (entry+1)->sec = ev->time.seconds + 1;
1146                         /* we need a fake depth - let's interpolate */
1147                         if (i) {
1148                                 entry->depth = sample->depth.mm -
1149                                         (sample->depth.mm - (sample-1)->depth.mm) / 2;
1150                         } else
1151                                 entry->depth = sample->depth.mm;
1152                         (entry+1)->depth = entry->depth;
1153                         pi_idx += 2;
1154                         entry = pi->entry + i + pi_idx;
1155                         ev = get_next_gaschange(ev->next);
1156                 }
1157                 if (ev && ev->time.seconds == sample->time.seconds) {
1158                         /* we already have a sample at the time of the event
1159                          * just add a new one for the old tank and delay the
1160                          * real even by one second (to keep time monotonous) */
1161                         entry->sec = ev->time.seconds;
1162                         entry->depth = sample->depth.mm;
1163                         pi_idx++;
1164                         entry = pi->entry + i + pi_idx;
1165                         ev = get_next_gaschange(ev->next);
1166                         delay = 1;
1167                 }
1168                 sec = entry->sec = sample->time.seconds + delay;
1169                 depth = entry->depth = sample->depth.mm;
1170                 entry->cylinderindex = sample->cylinderindex;
1171                 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1172                 entry->temperature = sample->temperature.mkelvin;
1173
1174                 if (depth || lastdepth)
1175                         lastindex = i + pi_idx;
1176
1177                 lastdepth = depth;
1178                 if (depth > pi->maxdepth)
1179                         pi->maxdepth = depth;
1180         }
1181         entry = pi->entry + i + pi_idx;
1182         /* are there still unprocessed gas changes? that would be very strange */
1183         while (ev) {
1184                 entry->sec = ev->time.seconds;
1185                 entry->depth = 0; /* why are there gas changes after the dive is over? */
1186                 pi_idx++;
1187                 entry = pi->entry + i + pi_idx;
1188                 ev = get_next_gaschange(ev->next);
1189         }
1190         nr = nr_samples + pi_idx - 2;
1191         check_gas_change_events(dive, pi);
1192
1193         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1194                 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1195         current = track_pr[pi->entry[2].cylinderindex];
1196         for (i = 0; i < nr + 1; i++) {
1197                 entry = pi->entry + i + 1;
1198
1199                 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1200                 cylinderindex = entry->cylinderindex;
1201
1202                 /* track the segments per cylinder and their pressure/time integral */
1203                 if (!entry->same_cylinder) {
1204                         current->end = SENSOR_PRESSURE(entry-1);
1205                         current->t_end = (entry-1)->sec;
1206                         current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1207                         track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1208                 } else { /* same cylinder */
1209                         if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1210                                 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1211                                 /* transmitter changed its working status */
1212                                 current->end = SENSOR_PRESSURE(entry-1);
1213                                 current->t_end = (entry-1)->sec;
1214                                 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1215                                 track_pr[cylinderindex] =
1216                                         list_add(track_pr[cylinderindex], current);
1217                         }
1218                 }
1219                 /* finally, do the discrete integration to get the SAC rate equivalent */
1220                 current->pressure_time += (entry->sec - (entry-1)->sec) *
1221                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1222                 missing_pr |= !SENSOR_PRESSURE(entry);
1223         }
1224
1225         if (entry)
1226                 current->t_end = entry->sec;
1227
1228         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1229                 int pr = dive->cylinder[cyl].end.mbar;
1230                 if (pr && track_pr[cyl]) {
1231                         pr_track = list_last(track_pr[cyl]);
1232                         pr_track->end = pr;
1233                 }
1234         }
1235         /* Fill in the last two entries with empty values but valid times
1236          * without creating a false cylinder change event */
1237         i = nr + 2;
1238         pi->entry[i].sec = sec + 20;
1239         pi->entry[i].same_cylinder = 1;
1240         pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1241         INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1242         pi->entry[i+1].sec = sec + 40;
1243         pi->entry[i+1].same_cylinder = 1;
1244         pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1245         INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1246         /* the number of actual entries - some computers have lots of
1247          * depth 0 samples at the end of a dive, we want to make sure
1248          * we have exactly one of them at the end */
1249         pi->nr = lastindex+1;
1250         while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1251                 pi->nr++;
1252         pi->maxtime = pi->entry[lastindex].sec;
1253
1254         /* Analyze_plot_info() will do the sample max pressures,
1255          * this handles the manual pressures
1256          */
1257         pi->maxpressure = 0;
1258         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1259                 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1260                 if (mbar > pi->maxpressure)
1261                         pi->maxpressure = mbar;
1262         }
1263
1264         pi->meandepth = dive->meandepth.mm;
1265
1266         if (missing_pr) {
1267                 fill_missing_tank_pressures(dive, pi, track_pr);
1268         }
1269         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1270                 list_free(track_pr[cyl]);
1271         if (0) /* awesome for debugging - not useful otherwise */
1272                 dump_pi(pi);
1273         return analyze_plot_info(pi);
1274 }
1275
1276 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1277 {
1278         struct plot_info *pi;
1279         static struct sample fake[4];
1280         struct sample *sample = dive->sample;
1281         int nr = dive->samples;
1282
1283         if (!nr) {
1284                 int duration = dive->duration.seconds;
1285                 int maxdepth = dive->maxdepth.mm;
1286                 sample = fake;
1287                 fake[1].time.seconds = duration * 0.05;
1288                 fake[1].depth.mm = maxdepth;
1289                 fake[2].time.seconds = duration * 0.95;
1290                 fake[2].depth.mm = maxdepth;
1291                 fake[3].time.seconds = duration * 1.00;
1292                 nr = 4;
1293         }
1294
1295         pi = create_plot_info(dive, nr, sample);
1296
1297         cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1298         cairo_set_line_width(gc->cr, 2);
1299         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1300         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1301
1302         /*
1303          * We can use "cairo_translate()" because that doesn't
1304          * scale line width etc. But the actual scaling we need
1305          * do set up ourselves..
1306          *
1307          * Snif. What a pity.
1308          */
1309         gc->maxx = (drawing_area->width - 2*drawing_area->x);
1310         gc->maxy = (drawing_area->height - 2*drawing_area->y);
1311
1312         /* Temperature profile */
1313         plot_temperature_profile(gc, pi);
1314
1315         /* Depth profile */
1316         plot_depth_profile(gc, pi);
1317         plot_events(gc, pi, dive);
1318
1319         /* Cylinder pressure plot */
1320         plot_cylinder_pressure(gc, pi, dive);
1321
1322         /* Text on top of all graphs.. */
1323         plot_temperature_text(gc, pi);
1324         plot_depth_text(gc, pi);
1325         plot_cylinder_pressure_text(gc, pi);
1326
1327         /* Bounding box last */
1328         gc->leftx = 0; gc->rightx = 1.0;
1329         gc->topy = 0; gc->bottomy = 1.0;
1330
1331         set_source_rgb(gc, 1, 1, 1);
1332         move_to(gc, 0, 0);
1333         line_to(gc, 0, 1);
1334         line_to(gc, 1, 1);
1335         line_to(gc, 1, 0);
1336         cairo_close_path(gc->cr);
1337         cairo_stroke(gc->cr);
1338
1339         free(pi);
1340 }