10 int selected_dive = 0;
12 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
15 * When showing dive profiles, we scale things to the
16 * current dive. However, we don't scale past less than
17 * 30 minutes or 90 ft, just so that small dives show
20 static int round_seconds_up(int seconds)
22 return MAX(30*60, ROUND_UP(seconds, 60*10));
25 static int round_depth_up(depth_t depth)
27 unsigned mm = depth.mm;
29 return MAX(30000, ROUND_UP(mm+3000, 10000));
34 enum {CENTER,LEFT} allign;
35 } text_render_options_t;
37 static void plot_text(cairo_t *cr, text_render_options_t *tro,
38 double x, double y, const char *fmt, ...)
40 cairo_text_extents_t extents;
45 vsnprintf(buffer, sizeof(buffer), fmt, args);
48 cairo_text_extents(cr, buffer, &extents);
50 if (tro->allign == CENTER)
51 x -= extents.width/2 + extents.x_bearing;
52 y += extents.height * 1.2;
54 cairo_move_to(cr, x, y);
55 cairo_text_path(cr, buffer);
56 cairo_set_source_rgb(cr, 0, 0, 0);
59 cairo_move_to(cr, x, y);
60 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
61 cairo_show_text(cr, buffer);
65 * Find the next maximum point in a 10-minute window.
67 * We exit early if we hit "enough" of a depth reversal,
68 * which is roughly 10 feet.
70 static struct sample *next_minmax(struct dive *dive, struct sample *sample, struct sample *end, int minmax)
72 const int enough = 3000;
73 struct sample *result;
74 int timelimit, depthlimit;
80 depthlimit = sample->depth.mm;
89 time = sample->time.seconds;
90 depth = sample->depth.mm;
95 if (depth <= depthlimit) {
96 if (depthlimit - depth > enough)
101 if (depth >= depthlimit) {
102 if (depth - depthlimit > enough)
110 /* Look up to ten minutes into the future */
111 timelimit = time + 600;
116 /* Scale to 0,0 -> maxx,maxy */
117 #define SCALE(x,y) (x)*maxx/scalex,(y)*maxy/scaley
119 static void plot_depth_text(struct dive *dive, cairo_t *cr,
120 double maxx, double maxy)
122 struct sample *sample, *end;
123 double scalex, scaley;
124 int maxtime, maxdepth;
126 /* Get plot scaling limits */
127 maxtime = round_seconds_up(dive->duration.seconds);
128 maxdepth = round_depth_up(dive->maxdepth);
133 cairo_set_font_size(cr, 14);
134 cairo_set_source_rgb(cr, 1, 0.2, 0.2);
137 * We never take the last sample into account.
138 * It should be a surface event anyway, although
139 * there are buggy cases where it isn't..
141 sample = dive->sample;
142 end = dive->sample + dive->samples - 1;
144 while ((sample = next_minmax(dive, sample, end, 1)) != NULL) {
145 text_render_options_t tro = {1.0, 0.2, 0.2, CENTER};
146 int sec = sample->time.seconds;
147 depth_t depth = sample->depth;
151 switch (output_units.length) {
153 d = depth.mm / 1000.0;
162 plot_text(cr, &tro, SCALE(sec, depth.mm), fmt, d);
163 sample = next_minmax(dive, sample, end, 0);
169 static void plot_depth_profile(struct dive *dive, cairo_t *cr,
170 double maxx, double maxy)
172 double scalex, scaley;
173 int begins, sec, depth;
175 struct sample *sample;
176 int maxtime, maxdepth, marker;
178 samples = dive->samples;
182 cairo_set_line_width(cr, 2);
184 /* Get plot scaling limits */
185 maxtime = round_seconds_up(dive->duration.seconds);
186 maxdepth = round_depth_up(dive->maxdepth);
188 /* Time markers: every 5 min */
191 for (i = 5*60; i < maxtime; i += 5*60) {
192 cairo_move_to(cr, SCALE(i, 0));
193 cairo_line_to(cr, SCALE(i, 1));
196 /* Depth markers: every 30 ft or 10 m*/
199 switch (output_units.length) {
200 case METERS: marker = 10000; break;
201 case FEET: marker = 9144; break; /* 30 ft */
204 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
205 for (i = marker; i < maxdepth; i += marker) {
206 cairo_move_to(cr, SCALE(0, i));
207 cairo_line_to(cr, SCALE(1, i));
211 /* Show mean depth */
212 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
213 cairo_move_to(cr, SCALE(0, dive->meandepth.mm));
214 cairo_line_to(cr, SCALE(1, dive->meandepth.mm));
219 sample = dive->sample;
220 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
221 begins = sample->time.seconds;
222 cairo_move_to(cr, SCALE(sample->time.seconds, sample->depth.mm));
223 for (i = 1; i < dive->samples; i++) {
225 sec = sample->time.seconds;
226 if (sec <= maxtime) {
227 depth = sample->depth.mm;
228 cairo_line_to(cr, SCALE(sec, depth));
232 cairo_line_to(cr, SCALE(MIN(sec,maxtime), 0));
233 cairo_line_to(cr, SCALE(begins, 0));
234 cairo_close_path(cr);
235 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
236 cairo_fill_preserve(cr);
237 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
241 /* gets both the actual start and end pressure as well as the scaling factors */
242 static int get_cylinder_pressure_range(struct dive *dive, double *scalex, double *scaley,
243 pressure_t *startp, pressure_t *endp)
248 *scalex = round_seconds_up(dive->duration.seconds);
253 startp->mbar = endp->mbar = 0;
255 for (i = 0; i < dive->samples; i++) {
257 struct sample *sample = dive->sample + i;
259 /* FIXME! We only track cylinder 0 right now */
260 if (sample->cylinderindex)
262 mbar = sample->cylinderpressure.mbar;
280 static void plot_cylinder_pressure(struct dive *dive, cairo_t *cr,
281 double maxx, double maxy)
284 double scalex, scaley;
286 if (!get_cylinder_pressure_range(dive, &scalex, &scaley, NULL, NULL))
289 cairo_set_source_rgba(cr, 0.2, 1.0, 0.2, 0.80);
291 cairo_move_to(cr, SCALE(0, dive->cylinder[0].start.mbar));
292 for (i = 1; i < dive->samples; i++) {
294 struct sample *sample = dive->sample + i;
296 mbar = sample->cylinderpressure.mbar;
299 sec = sample->time.seconds;
300 if (sec <= dive->duration.seconds)
301 cairo_line_to(cr, SCALE(sec, mbar));
304 * We may have "surface time" events, in which case we don't go
305 * back to dive duration
307 if (sec < dive->duration.seconds)
308 cairo_line_to(cr, SCALE(dive->duration.seconds, dive->cylinder[0].end.mbar));
313 * Return air usage (in liters).
315 static double calculate_airuse(struct dive *dive)
320 for (i = 0; i < MAX_CYLINDERS; i++) {
321 cylinder_t *cyl = dive->cylinder + i;
322 int size = cyl->type.size.mliter;
328 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
330 /* Liters of air at 1 atm == milliliters at 1k atm*/
331 airuse += kilo_atm * size;
336 static void plot_info(struct dive *dive, cairo_t *cr,
337 double maxx, double maxy)
339 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
340 const double liters_per_cuft = 28.317;
344 airuse = calculate_airuse(dive);
348 /* I really need to start addign some unit setting thing */
349 switch (output_units.volume) {
355 airuse /= liters_per_cuft;
358 plot_text(cr, &tro, maxx*0.8, maxy*0.8, "vol: %4.2f %s", airuse, unit);
359 if (dive->duration.seconds) {
360 double pressure = 1 + (dive->meandepth.mm / 10000.0);
361 double sac = airuse / pressure * 60 / dive->duration.seconds;
362 plot_text(cr, &tro, maxx*0.8, maxy*0.85, "SAC: %4.2f %s/min", sac, unit);
366 static void plot_cylinder_pressure_text(struct dive *dive, cairo_t *cr,
367 double maxx, double maxy)
369 double scalex, scaley;
370 pressure_t startp, endp;
372 cairo_set_font_size(cr, 10);
374 if (get_cylinder_pressure_range(dive, &scalex, &scaley,
377 const char *unit = "bar";
379 switch (output_units.pressure) {
381 start = startp.mbar * 100;
382 end = startp.mbar * 100;
386 start = (startp.mbar + 500) / 1000;
387 end = (endp.mbar + 500) / 1000;
391 start = to_PSI(startp);
397 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
398 plot_text(cr, &tro, SCALE(0, startp.mbar), "%d %s", start, unit);
399 plot_text(cr, &tro, SCALE(dive->duration.seconds, endp.mbar),
404 static void plot(cairo_t *cr, int w, int h, struct dive *dive)
406 double topx, topy, maxx, maxy;
407 double scalex, scaley;
413 cairo_translate(cr, topx, topy);
415 /* Cylinder pressure plot */
416 plot_cylinder_pressure(dive, cr, maxx, maxy);
419 plot_depth_profile(dive, cr, maxx, maxy);
421 /* Text on top of all graphs.. */
422 plot_depth_text(dive, cr, maxx, maxy);
423 plot_cylinder_pressure_text(dive, cr, maxx, maxy);
425 /* And info box in the lower right corner.. */
426 plot_info(dive, cr, maxx, maxy);
428 /* Bounding box last */
429 scalex = scaley = 1.0;
430 cairo_set_source_rgb(cr, 1, 1, 1);
431 cairo_move_to(cr, SCALE(0,0));
432 cairo_line_to(cr, SCALE(0,1));
433 cairo_line_to(cr, SCALE(1,1));
434 cairo_line_to(cr, SCALE(1,0));
435 cairo_close_path(cr);
440 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
442 struct dive *dive = current_dive;
446 w = widget->allocation.width;
447 h = widget->allocation.height;
449 cr = gdk_cairo_create(widget->window);
450 cairo_set_source_rgb(cr, 0, 0, 0);
454 plot(cr, w, h, dive);
461 GtkWidget *dive_profile_widget(void)
465 da = gtk_drawing_area_new();
466 gtk_widget_set_size_request(da, 450, 350);
467 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);