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