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