]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
6475008c93d44dd2cad224d39f01626aa4b228fb
[ext/subsurface.git] / profile.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <time.h>
5
6 #include "dive.h"
7 #include "display.h"
8 #include "divelist.h"
9
10 int selected_dive = 0;
11
12 /*
13  * Cairo scaling really is horribly horribly mis-designed.
14  *
15  * Which is sad, because I really like Cairo otherwise. But
16  * the fact that the line width is scaled with the same scale
17  * as the coordinate system is a f*&%ing disaster. So we
18  * can't use it, and instead have this butt-ugly wrapper thing..
19  */
20 struct graphics_context {
21         cairo_t *cr;
22         double maxx, maxy;
23         double scalex, scaley;
24 };
25
26 /* Scale to 0,0 -> maxx,maxy */
27 #define SCALE(gc,x,y) (x)*gc->maxx/gc->scalex,(y)*gc->maxy/gc->scaley
28
29 static void move_to(struct graphics_context *gc, double x, double y)
30 {
31         cairo_move_to(gc->cr, SCALE(gc, x, y));
32 }
33
34 static void line_to(struct graphics_context *gc, double x, double y)
35 {
36         cairo_line_to(gc->cr, SCALE(gc, x, y));
37 }
38
39 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
40
41 /*
42  * When showing dive profiles, we scale things to the
43  * current dive. However, we don't scale past less than
44  * 30 minutes or 90 ft, just so that small dives show
45  * up as such.
46  */
47 static int round_seconds_up(int seconds)
48 {
49         return MAX(30*60, ROUND_UP(seconds, 60*10));
50 }
51
52 static int round_depth_up(depth_t depth)
53 {
54         unsigned mm = depth.mm;
55         /* Minimum 30m */
56         return MAX(30000, ROUND_UP(mm+3000, 10000));
57 }
58
59 typedef struct {
60         double r,g,b;
61         enum {CENTER,LEFT} allign;
62 } text_render_options_t;
63
64 static void plot_text(struct graphics_context *gc, text_render_options_t *tro,
65                       double x, double y, const char *fmt, ...)
66 {
67         cairo_t *cr = gc->cr;
68         cairo_text_extents_t extents;
69         double dx, dy;
70         char buffer[80];
71         va_list args;
72
73         va_start(args, fmt);
74         vsnprintf(buffer, sizeof(buffer), fmt, args);
75         va_end(args);
76
77         cairo_text_extents(cr, buffer, &extents);
78         dx = 0;
79         if (tro->allign == CENTER)
80                 dx = -(extents.width/2 + extents.x_bearing);
81         dy = extents.height * 1.2;
82
83         move_to(gc, x, y);
84         cairo_rel_move_to(cr, dx, dy);
85
86         cairo_text_path(cr, buffer);
87         cairo_set_source_rgb(cr, 0, 0, 0);
88         cairo_stroke(cr);
89
90         move_to(gc, x, y);
91         cairo_rel_move_to(cr, dx, dy);
92
93         cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
94         cairo_show_text(cr, buffer);
95 }
96
97 /*
98  * Find the next maximum point in a 10-minute window.
99  *
100  * We exit early if we hit "enough" of a depth reversal,
101  * which is roughly 10 feet.
102  */
103 static struct sample *next_minmax(struct sample *sample, struct sample *end, int minmax)
104 {
105         const int enough = 3000;
106         struct sample *result;
107         int timelimit, depthlimit;
108
109         if (sample >= end)
110                 return 0;
111
112         timelimit = 24*60*60;
113         depthlimit = sample->depth.mm;
114         result = NULL;
115
116         for (;;) {
117                 int time, depth;
118
119                 sample++;
120                 if (sample >= end)
121                         return NULL;
122                 time = sample->time.seconds;
123                 depth = sample->depth.mm;
124                 if (time > timelimit)
125                         break;
126
127                 if (minmax) {
128                         if (depth <= depthlimit) {
129                                 if (depthlimit - depth > enough)
130                                         break;
131                                 continue;
132                         }
133                 } else {
134                         if (depth >= depthlimit) {
135                                 if (depth - depthlimit > enough)
136                                         break;
137                                 continue;
138                         }
139                 }
140
141                 result = sample;
142                 depthlimit = depth;
143                 /* Look up to ten minutes into the future */
144                 timelimit = time + 600;
145         }
146         return result;
147 }
148
149 static void render_depth_sample(struct graphics_context *gc, struct sample *sample)
150 {
151         text_render_options_t tro = {1.0, 0.2, 0.2, CENTER};
152         int sec = sample->time.seconds;
153         depth_t depth = sample->depth;
154         const char *fmt;
155         double d;
156
157         switch (output_units.length) {
158         case METERS:
159                 d = depth.mm / 1000.0;
160                 fmt = "%.1f";
161                 break;
162         case FEET:
163                 d = to_feet(depth);
164                 fmt = "%.0f";
165                 break;
166         }
167         plot_text(gc, &tro, sec, depth.mm, fmt, d);
168 }
169
170
171 void plot_text_samples(struct dive *dive, struct graphics_context *gc,
172                         struct sample *a, struct sample *b)
173 {
174         struct sample *max, *min;
175
176         if (b < a)
177                 return;
178         if (b->time.seconds - a->time.seconds < 3*60)
179                 return;
180
181         max = next_minmax(a, b, 1);
182         if (max) {
183                 render_depth_sample(gc, max);
184                 min = next_minmax(max, b, 0);
185                 if (min) {
186                         plot_text_samples(dive, gc, min, b);
187                         return;
188                 }
189         }
190 }
191
192 static void plot_depth_text(struct dive *dive, struct graphics_context *gc)
193 {
194         struct sample *sample, *end;
195         int maxtime, maxdepth;
196
197         /* Get plot scaling limits */
198         maxtime = round_seconds_up(dive->duration.seconds);
199         maxdepth = round_depth_up(dive->maxdepth);
200
201         gc->scalex = maxtime;
202         gc->scaley = maxdepth;
203
204         cairo_set_font_size(gc->cr, 14);
205
206         /*
207          * We never take the last sample into account.
208          * It should be a surface event anyway, although
209          * there are buggy cases where it isn't..
210          */
211         sample = dive->sample;
212         end = dive->sample + dive->samples - 1;
213
214         plot_text_samples(dive, gc, sample, end);
215 }
216
217 static void plot_depth_profile(struct dive *dive, struct graphics_context *gc)
218 {
219         cairo_t *cr = gc->cr;
220         int begins, sec, depth;
221         int i, samples;
222         struct sample *sample;
223         int maxtime, maxdepth, marker;
224
225         samples = dive->samples;
226         if (!samples)
227                 return;
228
229         cairo_set_line_width(gc->cr, 2);
230
231         /* Get plot scaling limits */
232         maxtime = round_seconds_up(dive->duration.seconds);
233         maxdepth = round_depth_up(dive->maxdepth);
234
235         /* Time markers: every 5 min */
236         gc->scalex = maxtime;
237         gc->scaley = 1.0;
238         for (i = 5*60; i < maxtime; i += 5*60) {
239                 move_to(gc, i, 0);
240                 line_to(gc, i, 1);
241         }
242
243         /* Depth markers: every 30 ft or 10 m*/
244         gc->scalex = 1.0;
245         gc->scaley = maxdepth;
246         switch (output_units.length) {
247         case METERS: marker = 10000; break;
248         case FEET: marker = 9144; break;        /* 30 ft */
249         }
250
251         cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
252         for (i = marker; i < maxdepth; i += marker) {
253                 move_to(gc, 0, i);
254                 line_to(gc, 1, i);
255         }
256         cairo_stroke(cr);
257
258         /* Show mean depth */
259         cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
260         move_to(gc, 0, dive->meandepth.mm);
261         line_to(gc, 1, dive->meandepth.mm);
262         cairo_stroke(cr);
263
264         gc->scalex = maxtime;
265
266         sample = dive->sample;
267         cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
268         begins = sample->time.seconds;
269         move_to(gc, sample->time.seconds, sample->depth.mm);
270         for (i = 1; i < dive->samples; i++) {
271                 sample++;
272                 sec = sample->time.seconds;
273                 if (sec <= maxtime) {
274                         depth = sample->depth.mm;
275                         line_to(gc, sec, depth);
276                 }
277         }
278         gc->scaley = 1.0;
279         line_to(gc, MIN(sec,maxtime), 0);
280         line_to(gc, begins, 0);
281         cairo_close_path(cr);
282         cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
283         cairo_fill_preserve(cr);
284         cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
285         cairo_stroke(cr);
286 }
287
288 /* gets both the actual start and end pressure as well as the scaling factors */
289 static int get_cylinder_pressure_range(struct dive *dive, struct graphics_context *gc,
290         pressure_t *startp, pressure_t *endp)
291 {
292         int i;
293         int min, max;
294
295         gc->scalex = round_seconds_up(dive->duration.seconds);
296
297         max = 0;
298         min = 5000000;
299         if (startp)
300                 startp->mbar = endp->mbar = 0;
301
302         for (i = 0; i < dive->samples; i++) {
303                 int mbar;
304                 struct sample *sample = dive->sample + i;
305
306                 /* FIXME! We only track cylinder 0 right now */
307                 if (sample->cylinderindex)
308                         continue;
309                 mbar = sample->cylinderpressure.mbar;
310                 if (!mbar)
311                         continue;
312                 if (mbar < min)
313                         min = mbar;
314                 if (mbar > max)
315                         max = mbar;
316         }
317         if (startp)
318                 startp->mbar = max;
319         if (endp)
320                 endp->mbar = min;
321         if (!max)
322                 return 0;
323         gc->scaley = max * 1.5;
324         return 1;
325 }
326
327 static void plot_cylinder_pressure(struct dive *dive, struct graphics_context *gc)
328 {
329         int i, sec = -1;
330
331         if (!get_cylinder_pressure_range(dive, gc, NULL, NULL))
332                 return;
333
334         cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
335
336         move_to(gc, 0, dive->cylinder[0].start.mbar);
337         for (i = 1; i < dive->samples; i++) {
338                 int mbar;
339                 struct sample *sample = dive->sample + i;
340
341                 mbar = sample->cylinderpressure.mbar;
342                 if (!mbar)
343                         continue;
344                 sec = sample->time.seconds;
345                 if (sec <= dive->duration.seconds)
346                         line_to(gc, sec, mbar);
347         }
348         /*
349          * We may have "surface time" events, in which case we don't go
350          * back to dive duration
351          */
352         if (sec < dive->duration.seconds)
353                 line_to(gc, dive->duration.seconds, dive->cylinder[0].end.mbar);
354         cairo_stroke(gc->cr);
355 }
356
357 /*
358  * Return air usage (in liters).
359  */
360 static double calculate_airuse(struct dive *dive)
361 {
362         double airuse = 0;
363         int i;
364
365         for (i = 0; i < MAX_CYLINDERS; i++) {
366                 cylinder_t *cyl = dive->cylinder + i;
367                 int size = cyl->type.size.mliter;
368                 double kilo_atm;
369
370                 if (!size)
371                         continue;
372
373                 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
374
375                 /* Liters of air at 1 atm == milliliters at 1k atm*/
376                 airuse += kilo_atm * size;
377         }
378         return airuse;
379 }
380
381 static void plot_info(struct dive *dive, struct graphics_context *gc)
382 {
383         text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
384         const double liters_per_cuft = 28.317;
385         const char *unit;
386         double airuse;
387
388         airuse = calculate_airuse(dive);
389         if (!airuse)
390                 return;
391
392         /* I really need to start addign some unit setting thing */
393         switch (output_units.volume) {
394         case LITER:
395                 unit = "l";
396                 break;
397         case CUFT:
398                 unit = "cuft";
399                 airuse /= liters_per_cuft;
400                 break;
401         }
402         plot_text(gc, &tro, 0.8, 0.8, "vol: %4.2f %s", airuse, unit);
403         if (dive->duration.seconds) {
404                 double pressure = 1 + (dive->meandepth.mm / 10000.0);
405                 double sac = airuse / pressure * 60 / dive->duration.seconds;
406                 plot_text(gc, &tro, 0.8, 0.85, "SAC: %4.2f %s/min", sac, unit);
407         }
408 }
409
410 static void plot_cylinder_pressure_text(struct dive *dive, struct graphics_context *gc)
411 {
412         pressure_t startp, endp;
413
414         cairo_set_font_size(gc->cr, 10);
415
416         if (get_cylinder_pressure_range(dive, gc, &startp, &endp)) {
417                 int start, end;
418                 const char *unit = "bar";
419
420                 switch (output_units.pressure) {
421                 case PASCAL:
422                         start = startp.mbar * 100;
423                         end = startp.mbar * 100;
424                         unit = "pascal";
425                         break;
426                 case BAR:
427                         start = (startp.mbar + 500) / 1000;
428                         end = (endp.mbar + 500) / 1000;
429                         unit = "bar";
430                         break;
431                 case PSI:
432                         start = to_PSI(startp);
433                         end = to_PSI(endp);
434                         unit = "psi";
435                         break;
436                 }
437
438                 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
439                 plot_text(gc, &tro, 0, startp.mbar, "%d %s", start, unit);
440                 plot_text(gc, &tro, dive->duration.seconds, endp.mbar,
441                           "%d %s", end, unit);
442         }
443 }
444
445 static void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
446 {
447         double topx, topy;
448
449         topx = w / 20.0;
450         topy = h / 20.0;
451         cairo_translate(gc->cr, topx, topy);
452
453         /*
454          * We can use "cairo_translate()" because that doesn't
455          * scale line width etc. But the actual scaling we need
456          * do set up ourselves..
457          *
458          * Snif. What a pity.
459          */
460         gc->maxx = (w - 2*topx);
461         gc->maxy = (h - 2*topy);
462
463         /* Cylinder pressure plot */
464         plot_cylinder_pressure(dive, gc);
465
466         /* Depth profile */
467         plot_depth_profile(dive, gc);
468
469         /* Text on top of all graphs.. */
470         plot_depth_text(dive, gc);
471         plot_cylinder_pressure_text(dive, gc);
472
473         /* And info box in the lower right corner.. */
474         gc->scalex = gc->scaley = 1.0;
475         plot_info(dive, gc);
476
477         /* Bounding box last */
478         cairo_set_source_rgb(gc->cr, 1, 1, 1);
479         move_to(gc, 0, 0);
480         line_to(gc, 0, 1);
481         line_to(gc, 1, 1);
482         line_to(gc, 1, 0);
483         cairo_close_path(gc->cr);
484         cairo_stroke(gc->cr);
485
486 }
487
488 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
489 {
490         struct dive *dive = current_dive;
491         struct graphics_context gc;
492         int w,h;
493
494         w = widget->allocation.width;
495         h = widget->allocation.height;
496
497         gc.cr = gdk_cairo_create(widget->window);
498         cairo_set_source_rgb(gc.cr, 0, 0, 0);
499         cairo_paint(gc.cr);
500
501         if (dive)
502                 plot(&gc, w, h, dive);
503
504         cairo_destroy(gc.cr);
505
506         return FALSE;
507 }
508
509 GtkWidget *dive_profile_widget(void)
510 {
511         GtkWidget *da;
512
513         da = gtk_drawing_area_new();
514         gtk_widget_set_size_request(da, 450, 350);
515         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
516
517         return da;
518 }