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