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