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