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