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