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 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 void plot_text_samples(struct dive *dive, cairo_t *cr,
120 double maxx, double maxy,
121 double scalex, double scaley,
122 struct sample *a, struct sample *b)
124 struct sample *max, *min;
128 if (b->time.seconds - a->time.seconds < 3*60)
131 max = next_minmax(a, b, 1);
133 text_render_options_t tro = {1.0, 0.2, 0.2, CENTER};
134 int sec = max->time.seconds;
135 depth_t depth = max->depth;
139 min = next_minmax(max, b, 0);
140 plot_text_samples(dive, cr, maxx, maxy, scalex, scaley, a, max);
142 plot_text_samples(dive, cr, maxx, maxy, scalex, scaley, max, min);
143 plot_text_samples(dive, cr, maxx, maxy, scalex, scaley, min, b);
145 plot_text_samples(dive, cr, maxx, maxy, scalex, scaley, max, b);
147 switch (output_units.length) {
149 d = depth.mm / 1000.0;
158 plot_text(cr, &tro, SCALE(sec, depth.mm), fmt, d);
163 static void plot_depth_text(struct dive *dive, cairo_t *cr,
164 double maxx, double maxy)
166 struct sample *sample, *end;
167 double scalex, scaley;
168 int maxtime, maxdepth;
170 /* Get plot scaling limits */
171 maxtime = round_seconds_up(dive->duration.seconds);
172 maxdepth = round_depth_up(dive->maxdepth);
177 cairo_set_font_size(cr, 14);
178 cairo_set_source_rgb(cr, 1, 0.2, 0.2);
181 * We never take the last sample into account.
182 * It should be a surface event anyway, although
183 * there are buggy cases where it isn't..
185 sample = dive->sample;
186 end = dive->sample + dive->samples - 1;
188 plot_text_samples(dive, cr, maxx, maxy, scalex, scaley, sample, end);
191 static void plot_depth_profile(struct dive *dive, cairo_t *cr,
192 double maxx, double maxy)
194 double scalex, scaley;
195 int begins, sec, depth;
197 struct sample *sample;
198 int maxtime, maxdepth, marker;
200 samples = dive->samples;
204 cairo_set_line_width(cr, 2);
206 /* Get plot scaling limits */
207 maxtime = round_seconds_up(dive->duration.seconds);
208 maxdepth = round_depth_up(dive->maxdepth);
210 /* Time markers: every 5 min */
213 for (i = 5*60; i < maxtime; i += 5*60) {
214 cairo_move_to(cr, SCALE(i, 0));
215 cairo_line_to(cr, SCALE(i, 1));
218 /* Depth markers: every 30 ft or 10 m*/
221 switch (output_units.length) {
222 case METERS: marker = 10000; break;
223 case FEET: marker = 9144; break; /* 30 ft */
226 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
227 for (i = marker; i < maxdepth; i += marker) {
228 cairo_move_to(cr, SCALE(0, i));
229 cairo_line_to(cr, SCALE(1, i));
233 /* Show mean depth */
234 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
235 cairo_move_to(cr, SCALE(0, dive->meandepth.mm));
236 cairo_line_to(cr, SCALE(1, dive->meandepth.mm));
241 sample = dive->sample;
242 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
243 begins = sample->time.seconds;
244 cairo_move_to(cr, SCALE(sample->time.seconds, sample->depth.mm));
245 for (i = 1; i < dive->samples; i++) {
247 sec = sample->time.seconds;
248 if (sec <= maxtime) {
249 depth = sample->depth.mm;
250 cairo_line_to(cr, SCALE(sec, depth));
254 cairo_line_to(cr, SCALE(MIN(sec,maxtime), 0));
255 cairo_line_to(cr, SCALE(begins, 0));
256 cairo_close_path(cr);
257 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
258 cairo_fill_preserve(cr);
259 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
263 /* gets both the actual start and end pressure as well as the scaling factors */
264 static int get_cylinder_pressure_range(struct dive *dive, double *scalex, double *scaley,
265 pressure_t *startp, pressure_t *endp)
270 *scalex = round_seconds_up(dive->duration.seconds);
275 startp->mbar = endp->mbar = 0;
277 for (i = 0; i < dive->samples; i++) {
279 struct sample *sample = dive->sample + i;
281 /* FIXME! We only track cylinder 0 right now */
282 if (sample->cylinderindex)
284 mbar = sample->cylinderpressure.mbar;
302 static void plot_cylinder_pressure(struct dive *dive, cairo_t *cr,
303 double maxx, double maxy)
306 double scalex, scaley;
308 if (!get_cylinder_pressure_range(dive, &scalex, &scaley, NULL, NULL))
311 cairo_set_source_rgba(cr, 0.2, 1.0, 0.2, 0.80);
313 cairo_move_to(cr, SCALE(0, dive->cylinder[0].start.mbar));
314 for (i = 1; i < dive->samples; i++) {
316 struct sample *sample = dive->sample + i;
318 mbar = sample->cylinderpressure.mbar;
321 sec = sample->time.seconds;
322 if (sec <= dive->duration.seconds)
323 cairo_line_to(cr, SCALE(sec, mbar));
326 * We may have "surface time" events, in which case we don't go
327 * back to dive duration
329 if (sec < dive->duration.seconds)
330 cairo_line_to(cr, SCALE(dive->duration.seconds, dive->cylinder[0].end.mbar));
335 * Return air usage (in liters).
337 static double calculate_airuse(struct dive *dive)
342 for (i = 0; i < MAX_CYLINDERS; i++) {
343 cylinder_t *cyl = dive->cylinder + i;
344 int size = cyl->type.size.mliter;
350 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
352 /* Liters of air at 1 atm == milliliters at 1k atm*/
353 airuse += kilo_atm * size;
358 static void plot_info(struct dive *dive, cairo_t *cr,
359 double maxx, double maxy)
361 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
362 const double liters_per_cuft = 28.317;
366 airuse = calculate_airuse(dive);
370 /* I really need to start addign some unit setting thing */
371 switch (output_units.volume) {
377 airuse /= liters_per_cuft;
380 plot_text(cr, &tro, maxx*0.8, maxy*0.8, "vol: %4.2f %s", airuse, unit);
381 if (dive->duration.seconds) {
382 double pressure = 1 + (dive->meandepth.mm / 10000.0);
383 double sac = airuse / pressure * 60 / dive->duration.seconds;
384 plot_text(cr, &tro, maxx*0.8, maxy*0.85, "SAC: %4.2f %s/min", sac, unit);
388 static void plot_cylinder_pressure_text(struct dive *dive, cairo_t *cr,
389 double maxx, double maxy)
391 double scalex, scaley;
392 pressure_t startp, endp;
394 cairo_set_font_size(cr, 10);
396 if (get_cylinder_pressure_range(dive, &scalex, &scaley,
399 const char *unit = "bar";
401 switch (output_units.pressure) {
403 start = startp.mbar * 100;
404 end = startp.mbar * 100;
408 start = (startp.mbar + 500) / 1000;
409 end = (endp.mbar + 500) / 1000;
413 start = to_PSI(startp);
419 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
420 plot_text(cr, &tro, SCALE(0, startp.mbar), "%d %s", start, unit);
421 plot_text(cr, &tro, SCALE(dive->duration.seconds, endp.mbar),
426 static void plot(cairo_t *cr, int w, int h, struct dive *dive)
428 double topx, topy, maxx, maxy;
429 double scalex, scaley;
435 cairo_translate(cr, topx, topy);
437 /* Cylinder pressure plot */
438 plot_cylinder_pressure(dive, cr, maxx, maxy);
441 plot_depth_profile(dive, cr, maxx, maxy);
443 /* Text on top of all graphs.. */
444 plot_depth_text(dive, cr, maxx, maxy);
445 plot_cylinder_pressure_text(dive, cr, maxx, maxy);
447 /* And info box in the lower right corner.. */
448 plot_info(dive, cr, maxx, maxy);
450 /* Bounding box last */
451 scalex = scaley = 1.0;
452 cairo_set_source_rgb(cr, 1, 1, 1);
453 cairo_move_to(cr, SCALE(0,0));
454 cairo_line_to(cr, SCALE(0,1));
455 cairo_line_to(cr, SCALE(1,1));
456 cairo_line_to(cr, SCALE(1,0));
457 cairo_close_path(cr);
462 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
464 struct dive *dive = current_dive;
468 w = widget->allocation.width;
469 h = widget->allocation.height;
471 cr = gdk_cairo_create(widget->window);
472 cairo_set_source_rgb(cr, 0, 0, 0);
476 plot(cr, w, h, dive);
483 GtkWidget *dive_profile_widget(void)
487 da = gtk_drawing_area_new();
488 gtk_widget_set_size_request(da, 450, 350);
489 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);