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