]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Merge branch 'otu-tracking-v2' of git://github.com/dirkhh/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 minpressure, maxpressure;
25         int mintemp, maxtemp;
26         struct plot_data {
27                 int sec;
28                 int pressure, temperature;
29                 /* Depth info */
30                 int val;
31                 int smoothed;
32                 velocity_t velocity;
33                 struct plot_data *min[3];
34                 struct plot_data *max[3];
35                 int avg[3];
36         } entry[];
37 };
38
39 /* convert velocity to colors */
40 typedef struct { double r, g, b; } rgb_t;
41 static const rgb_t rgb[] = {
42         [STABLE]   = {0.0, 0.4, 0.0},
43         [SLOW]     = {0.4, 0.8, 0.0},
44         [MODERATE] = {0.8, 0.8, 0.0},
45         [FAST]     = {0.8, 0.5, 0.0},
46         [CRAZY]    = {1.0, 0.0, 0.0},
47 };
48
49 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
50
51 /* Scale to 0,0 -> maxx,maxy */
52 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
53 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
54 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
55
56 static void move_to(struct graphics_context *gc, double x, double y)
57 {
58         cairo_move_to(gc->cr, SCALE(gc, x, y));
59 }
60
61 static void line_to(struct graphics_context *gc, double x, double y)
62 {
63         cairo_line_to(gc->cr, SCALE(gc, x, y));
64 }
65
66 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
67 {
68         /*
69          * For printers, we still honor 'a', but ignore colors
70          * for now. Black is white and white is black
71          */
72         if (gc->printer) {
73                 double sum = r+g+b;
74                 if (sum > 0.8)
75                         r = g = b = 0;
76                 else
77                         r = g = b = 1;
78         }
79         cairo_set_source_rgba(gc->cr, r, g, b, a);
80 }
81
82 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
83 {
84         set_source_rgba(gc, r, g, b, 1);
85 }
86
87 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
88
89 /*
90  * When showing dive profiles, we scale things to the
91  * current dive. However, we don't scale past less than
92  * 30 minutes or 90 ft, just so that small dives show
93  * up as such.
94  * we also need to add 180 seconds at the end so the min/max
95  * plots correctly
96  */
97 static int get_maxtime(struct plot_info *pi)
98 {
99         int seconds = pi->maxtime;
100         /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
101         return MAX(30*60, ROUND_UP(seconds+150, 60*5));
102 }
103
104 static int get_maxdepth(struct plot_info *pi)
105 {
106         unsigned mm = pi->maxdepth;
107         /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
108         return MAX(30000, ROUND_UP(mm+3000, 10000));
109 }
110
111 typedef struct {
112         int size;
113         double r,g,b;
114         double hpos, vpos;
115 } text_render_options_t;
116
117 #define RIGHT (-1.0)
118 #define CENTER (-0.5)
119 #define LEFT (0.0)
120
121 #define TOP (1)
122 #define MIDDLE (0)
123 #define BOTTOM (-1)
124
125 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
126                       double x, double y, const char *fmt, ...)
127 {
128         cairo_t *cr = gc->cr;
129         cairo_font_extents_t fe;
130         cairo_text_extents_t extents;
131         double dx, dy;
132         char buffer[80];
133         va_list args;
134
135         va_start(args, fmt);
136         vsnprintf(buffer, sizeof(buffer), fmt, args);
137         va_end(args);
138
139         cairo_set_font_size(cr, tro->size);
140         cairo_font_extents(cr, &fe);
141         cairo_text_extents(cr, buffer, &extents);
142         dx = tro->hpos * extents.width + extents.x_bearing;
143         dy = tro->vpos * extents.height + fe.descent;
144
145         move_to(gc, x, y);
146         cairo_rel_move_to(cr, dx, dy);
147
148         cairo_text_path(cr, buffer);
149         set_source_rgb(gc, 0, 0, 0);
150         cairo_stroke(cr);
151
152         move_to(gc, x, y);
153         cairo_rel_move_to(cr, dx, dy);
154
155         set_source_rgb(gc, tro->r, tro->g, tro->b);
156         cairo_show_text(cr, buffer);
157 }
158
159 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
160 {
161         int i, depth = 0;
162
163         for (i = 0; i < pi->nr; i++) {
164                 struct plot_data *data = pi->entry + i;
165                 if (event->time.seconds < data->sec)
166                         break;
167                 depth = data->val;
168         }
169         plot_text(gc, tro, event->time.seconds, depth, "%s", event->name);
170 }
171
172 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
173 {
174         static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
175         struct event *event = dive->events;
176
177         if (gc->printer)
178                 return;
179
180         while (event) {
181                 plot_one_event(gc, pi, event, &tro);
182                 event = event->next;
183         }
184 }
185
186 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
187 {
188         int sec = entry->sec, decimals;
189         double d;
190
191         d = get_depth_units(entry->val, &decimals, NULL);
192
193         plot_text(gc, tro, sec, entry->val, "%.*f", decimals, d);
194 }
195
196 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
197 {
198         static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
199         static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
200         int i;
201
202         for (i = 0; i < pi->nr; i++) {
203                 struct plot_data *entry = pi->entry + i;
204
205                 if (entry->val < 2000)
206                         continue;
207
208                 if (entry == entry->max[2])
209                         render_depth_sample(gc, entry, &deep);
210
211                 if (entry == entry->min[2])
212                         render_depth_sample(gc, entry, &shallow);
213         }
214 }
215
216 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
217 {
218         int maxtime, maxdepth;
219
220         /* Get plot scaling limits */
221         maxtime = get_maxtime(pi);
222         maxdepth = get_maxdepth(pi);
223
224         gc->leftx = 0; gc->rightx = maxtime;
225         gc->topy = 0; gc->bottomy = maxdepth;
226
227         plot_text_samples(gc, pi);
228 }
229
230 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
231 {
232         int i;
233         struct plot_data *entry = pi->entry;
234
235         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
236         move_to(gc, entry->sec, entry->smoothed);
237         for (i = 1; i < pi->nr; i++) {
238                 entry++;
239                 line_to(gc, entry->sec, entry->smoothed);
240         }
241         cairo_stroke(gc->cr);
242 }
243
244 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
245                                 int index, double a)
246 {
247         int i;
248         struct plot_data *entry = pi->entry;
249
250         set_source_rgba(gc, 1, 0.2, 1, a);
251         move_to(gc, entry->sec, entry->min[index]->val);
252         for (i = 1; i < pi->nr; i++) {
253                 entry++;
254                 line_to(gc, entry->sec, entry->min[index]->val);
255         }
256         for (i = 1; i < pi->nr; i++) {
257                 line_to(gc, entry->sec, entry->max[index]->val);
258                 entry--;
259         }
260         cairo_close_path(gc->cr);
261         cairo_fill(gc->cr);
262 }
263
264 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
265 {
266         if (gc->printer)
267                 return;
268         plot_minmax_profile_minute(gc, pi, 2, 0.1);
269         plot_minmax_profile_minute(gc, pi, 1, 0.1);
270         plot_minmax_profile_minute(gc, pi, 0, 0.1);
271 }
272
273 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
274 {
275         int i;
276         cairo_t *cr = gc->cr;
277         int sec, depth;
278         struct plot_data *entry;
279         int maxtime, maxdepth, marker;
280
281         /* Get plot scaling limits */
282         maxtime = get_maxtime(pi);
283         maxdepth = get_maxdepth(pi);
284
285         /* Time markers: every 5 min */
286         gc->leftx = 0; gc->rightx = maxtime;
287         gc->topy = 0; gc->bottomy = 1.0;
288         for (i = 5*60; i < maxtime; i += 5*60) {
289                 move_to(gc, i, 0);
290                 line_to(gc, i, 1);
291         }
292
293         /* Depth markers: every 30 ft or 10 m*/
294         gc->leftx = 0; gc->rightx = 1.0;
295         gc->topy = 0; gc->bottomy = maxdepth;
296         switch (output_units.length) {
297         case METERS: marker = 10000; break;
298         case FEET: marker = 9144; break;        /* 30 ft */
299         }
300
301         set_source_rgba(gc, 1, 1, 1, 0.5);
302         for (i = marker; i < maxdepth; i += marker) {
303                 move_to(gc, 0, i);
304                 line_to(gc, 1, i);
305         }
306         cairo_stroke(cr);
307
308         /* Show mean depth */
309         set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
310         move_to(gc, 0, pi->meandepth);
311         line_to(gc, 1, pi->meandepth);
312         cairo_stroke(cr);
313
314         gc->leftx = 0; gc->rightx = maxtime;
315
316         /*
317          * These are good for debugging text placement etc,
318          * but not for actual display..
319          */
320         if (0) {
321                 plot_smoothed_profile(gc, pi);
322                 plot_minmax_profile(gc, pi);
323         }
324
325         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
326
327         /* Do the depth profile for the neat fill */
328         gc->topy = 0; gc->bottomy = maxdepth;
329         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
330
331         entry = pi->entry;
332         move_to(gc, 0, 0);
333         for (i = 0; i < pi->nr; i++, entry++)
334                 line_to(gc, entry->sec, entry->val);
335         cairo_close_path(gc->cr);
336         if (gc->printer) {
337                 set_source_rgba(gc, 1, 1, 1, 0.2);
338                 cairo_fill_preserve(cr);
339                 set_source_rgb(gc, 1, 1, 1);
340                 cairo_stroke(cr);
341                 return;
342         }
343         cairo_fill(gc->cr);
344
345         /* Now do it again for the velocity colors */
346         entry = pi->entry;
347         for (i = 1; i < pi->nr; i++) {
348                 entry++;
349                 sec = entry->sec;
350                 /* we want to draw the segments in different colors
351                  * representing the vertical velocity, so we need to
352                  * chop this into short segments */
353                 rgb_t color = rgb[entry->velocity];
354                 depth = entry->val;
355                 set_source_rgb(gc, color.r, color.g, color.b);
356                 move_to(gc, entry[-1].sec, entry[-1].val);
357                 line_to(gc, sec, depth);
358                 cairo_stroke(cr);
359         }
360 }
361
362 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
363 {
364         int maxtime, mintemp, maxtemp, delta;
365
366         /* Get plot scaling limits */
367         maxtime = get_maxtime(pi);
368         mintemp = pi->mintemp;
369         maxtemp = pi->maxtemp;
370
371         gc->leftx = 0; gc->rightx = maxtime;
372         /* Show temperatures in roughly the lower third, but make sure the scale
373            is at least somewhat reasonable */
374         delta = maxtemp - mintemp;
375         if (delta > 3000) { /* more than 3K in fluctuation */
376                 gc->topy = maxtemp + delta*2;
377                 gc->bottomy = mintemp - delta/2;
378         } else {
379                 gc->topy = maxtemp + 1500 + delta*2;
380                 gc->bottomy = mintemp - delta/2;
381         }
382
383         return maxtemp > mintemp;
384 }
385
386 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
387 {
388         int deg;
389         const char *unit;
390         static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
391         temperature_t temperature = { mkelvin };
392
393         if (output_units.temperature == FAHRENHEIT) {
394                 deg = to_F(temperature);
395                 unit = UTF8_DEGREE "F";
396         } else {
397                 deg = to_C(temperature);
398                 unit = UTF8_DEGREE "C";
399         }
400         plot_text(gc, &tro, sec, temperature.mkelvin, "%d%s", deg, unit);
401 }
402
403 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
404 {
405         int i;
406         int last = 0, sec = 0;
407         int last_temperature = 0, last_printed_temp = 0;
408
409         if (!setup_temperature_limits(gc, pi))
410                 return;
411
412         for (i = 0; i < pi->nr; i++) {
413                 struct plot_data *entry = pi->entry+i;
414                 int mkelvin = entry->temperature;
415
416                 if (!mkelvin)
417                         continue;
418                 last_temperature = mkelvin;
419                 sec = entry->sec;
420                 if (sec < last + 300)
421                         continue;
422                 last = sec;
423                 plot_single_temp_text(gc,sec,mkelvin);
424                 last_printed_temp = mkelvin;
425         }
426         /* it would be nice to print the end temperature, if it's different */
427         if (abs(last_temperature - last_printed_temp) > 500)
428                 plot_single_temp_text(gc, sec, last_temperature);
429 }
430
431 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
432 {
433         int i;
434         cairo_t *cr = gc->cr;
435         int last = 0;
436
437         if (!setup_temperature_limits(gc, pi))
438                 return;
439
440         set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
441         for (i = 0; i < pi->nr; i++) {
442                 struct plot_data *entry = pi->entry + i;
443                 int mkelvin = entry->temperature;
444                 int sec = entry->sec;
445                 if (!mkelvin) {
446                         if (!last)
447                                 continue;
448                         mkelvin = last;
449                 }
450                 if (last)
451                         line_to(gc, sec, mkelvin);
452                 else
453                         move_to(gc, sec, mkelvin);
454                 last = mkelvin;
455         }
456         cairo_stroke(cr);
457 }
458
459 /* gets both the actual start and end pressure as well as the scaling factors */
460 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
461 {
462         gc->leftx = 0;
463         gc->rightx = get_maxtime(pi);
464
465         gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
466         return pi->maxpressure != 0;
467 }
468
469 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
470 {
471         int i;
472
473         if (!get_cylinder_pressure_range(gc, pi))
474                 return;
475
476         set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
477
478         move_to(gc, 0, pi->maxpressure);
479         for (i = 1; i < pi->nr; i++) {
480                 int mbar;
481                 struct plot_data *entry = pi->entry + i;
482
483                 mbar = entry->pressure;
484                 if (!mbar)
485                         continue;
486                 line_to(gc, entry->sec, mbar);
487         }
488         line_to(gc, pi->maxtime, pi->minpressure);
489         cairo_stroke(gc->cr);
490 }
491
492 static int mbar_to_PSI(int mbar)
493 {
494         pressure_t p = {mbar};
495         return to_PSI(p);
496 }
497
498 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
499 {
500         if (get_cylinder_pressure_range(gc, pi)) {
501                 int start, end;
502                 const char *unit = "bar";
503
504                 switch (output_units.pressure) {
505                 case PASCAL:
506                         start = pi->maxpressure * 100;
507                         end = pi->minpressure * 100;
508                         unit = "pascal";
509                         break;
510                 case BAR:
511                         start = (pi->maxpressure + 500) / 1000;
512                         end = (pi->minpressure + 500) / 1000;
513                         unit = "bar";
514                         break;
515                 case PSI:
516                         start = mbar_to_PSI(pi->maxpressure);
517                         end = mbar_to_PSI(pi->minpressure);
518                         unit = "psi";
519                         break;
520                 }
521
522                 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
523                 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
524                 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
525                           "%d %s", end, unit);
526         }
527 }
528
529 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
530 {
531         struct plot_data *p = entry;
532         int time = entry->sec;
533         int seconds = 90*(index+1);
534         struct plot_data *min, *max;
535         int avg, nr;
536
537         /* Go back 'seconds' in time */
538         while (p > first) {
539                 if (p[-1].sec < time - seconds)
540                         break;
541                 p--;
542         }
543
544         /* Then go forward until we hit an entry past the time */
545         min = max = p;
546         avg = p->val;
547         nr = 1;
548         while (++p < last) {
549                 int val = p->val;
550                 if (p->sec > time + seconds)
551                         break;
552                 avg += val;
553                 nr ++;
554                 if (val < min->val)
555                         min = p;
556                 if (val > max->val)
557                         max = p;
558         }
559         entry->min[index] = min;
560         entry->max[index] = max;
561         entry->avg[index] = (avg + nr/2) / nr;
562 }
563
564 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
565 {
566         analyze_plot_info_minmax_minute(entry, first, last, 0);
567         analyze_plot_info_minmax_minute(entry, first, last, 1);
568         analyze_plot_info_minmax_minute(entry, first, last, 2);
569 }
570
571 static velocity_t velocity(int speed)
572 {
573         velocity_t v;
574
575         if (speed < -304) /* ascent faster than -60ft/min */
576                 v = CRAZY;
577         else if (speed < -152) /* above -30ft/min */
578                 v = FAST;
579         else if (speed < -76) /* -15ft/min */
580                 v = MODERATE;
581         else if (speed < -25) /* -5ft/min */
582                 v = SLOW;
583         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
584                                 for descent are usually about 2x ascent rate; still, we want 
585                                 stable to mean stable */
586                 v = STABLE;
587         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
588                 v = SLOW;
589         else if (speed < 304) /* up to 60ft/min is moderate */
590                 v = MODERATE;
591         else if (speed < 507) /* up to 100ft/min is fast */
592                 v = FAST;
593         else /* more than that is just crazy - you'll blow your ears out */
594                 v = CRAZY;
595
596         return v;
597 }
598 static struct plot_info *analyze_plot_info(struct plot_info *pi)
599 {
600         int i;
601         int nr = pi->nr;
602
603         /* Do pressure min/max based on the non-surface data */
604         for (i = 0; i < nr; i++) {
605                 struct plot_data *entry = pi->entry+i;
606                 int pressure = entry->pressure;
607                 int temperature = entry->temperature;
608
609                 if (pressure) {
610                         if (!pi->minpressure || pressure < pi->minpressure)
611                                 pi->minpressure = pressure;
612                         if (pressure > pi->maxpressure)
613                                 pi->maxpressure = pressure;
614                 }
615
616                 if (temperature) {
617                         if (!pi->mintemp || temperature < pi->mintemp)
618                                 pi->mintemp = temperature;
619                         if (temperature > pi->maxtemp)
620                                 pi->maxtemp = temperature;
621                 }
622         }
623
624         /* Smoothing function: 5-point triangular smooth */
625         for (i = 2; i < nr-1; i++) {
626                 struct plot_data *entry = pi->entry+i;
627                 int val;
628
629                 if (i < nr-2) {
630                         val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
631                         entry->smoothed = (val+4) / 9;
632                 }
633                 /* vertical velocity in mm/sec */
634                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
635                 if (entry[0].sec - entry[-1].sec) {
636                         entry->velocity = velocity((entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec));
637                         /* if our samples are short and we aren't too FAST*/
638                         if (entry[0].sec - entry[-1].sec < 30 && entry->velocity < FAST) { 
639                                 int past = -2;
640                                 while (i+past > 0 && entry[0].sec - entry[past].sec < 30)
641                                         past--;
642                                 entry->velocity = velocity((entry[0].val - entry[past].val) / 
643                                                         (entry[0].sec - entry[past].sec));
644                         }
645                 } else
646                         entry->velocity = STABLE;
647         }
648
649         /* One-, two- and three-minute minmax data */
650         for (i = 0; i < nr; i++) {
651                 struct plot_data *entry = pi->entry +i;
652                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
653         }
654         
655         return pi;
656 }
657
658 /*
659  * Create a plot-info with smoothing and ranged min/max
660  *
661  * This also makes sure that we have extra empty events on both
662  * sides, so that you can do end-points without having to worry
663  * about it.
664  */
665 static struct plot_info *create_plot_info(struct dive *dive)
666 {
667         int lastdepth, lastindex;
668         int i, nr = dive->samples + 4, sec;
669         size_t alloc_size = plot_info_size(nr);
670         struct plot_info *pi;
671
672         pi = malloc(alloc_size);
673         if (!pi)
674                 return pi;
675         memset(pi, 0, alloc_size);
676         pi->nr = nr;
677         sec = 0;
678         lastindex = 0;
679         lastdepth = -1;
680         for (i = 0; i < dive->samples; i++) {
681                 int depth;
682                 struct sample *sample = dive->sample+i;
683                 struct plot_data *entry = pi->entry + i + 2;
684
685                 sec = entry->sec = sample->time.seconds;
686                 depth = entry->val = sample->depth.mm;
687                 entry->pressure = sample->cylinderpressure.mbar;
688                 entry->temperature = sample->temperature.mkelvin;
689
690                 if (depth || lastdepth)
691                         lastindex = i+2;
692
693                 lastdepth = depth;
694                 if (depth > pi->maxdepth)
695                         pi->maxdepth = depth;
696         }
697         if (lastdepth)
698                 lastindex = i + 2;
699         /* Fill in the last two entries with empty values but valid times */
700         i = dive->samples + 2;
701         pi->entry[i].sec = sec + 20;
702         pi->entry[i+1].sec = sec + 40;
703
704         pi->nr = lastindex+1;
705         pi->maxtime = pi->entry[lastindex].sec;
706
707         pi->minpressure = dive->cylinder[0].end.mbar;
708         pi->maxpressure = dive->cylinder[0].start.mbar;
709
710         pi->meandepth = dive->meandepth.mm;
711
712         return analyze_plot_info(pi);
713 }
714
715 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
716 {
717         double topx, topy;
718         struct plot_info *pi = create_plot_info(dive);
719
720         topx = w / 20.0;
721         topy = h / 20.0;
722         cairo_translate(gc->cr, topx, topy);
723         cairo_set_line_width(gc->cr, 2);
724         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
725         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
726
727         /*
728          * We can use "cairo_translate()" because that doesn't
729          * scale line width etc. But the actual scaling we need
730          * do set up ourselves..
731          *
732          * Snif. What a pity.
733          */
734         gc->maxx = (w - 2*topx);
735         gc->maxy = (h - 2*topy);
736
737         /* Temperature profile */
738         plot_temperature_profile(gc, pi);
739
740         /* Cylinder pressure plot */
741         plot_cylinder_pressure(gc, pi);
742
743         /* Depth profile */
744         plot_depth_profile(gc, pi);
745         plot_events(gc, pi, dive);
746
747         /* Text on top of all graphs.. */
748         plot_temperature_text(gc, pi);
749         plot_depth_text(gc, pi);
750         plot_cylinder_pressure_text(gc, pi);
751
752         /* Bounding box last */
753         gc->leftx = 0; gc->rightx = 1.0;
754         gc->topy = 0; gc->bottomy = 1.0;
755
756         set_source_rgb(gc, 1, 1, 1);
757         move_to(gc, 0, 0);
758         line_to(gc, 0, 1);
759         line_to(gc, 1, 1);
760         line_to(gc, 1, 0);
761         cairo_close_path(gc->cr);
762         cairo_stroke(gc->cr);
763
764         free(pi);
765 }