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 int next_minmax(struct dive *dive, int index, int minmax)
72 const int enough = 3000;
73 int timelimit, depthlimit, result;
74 struct sample *sample = dive->sample + index;
76 if (index >= dive->samples)
80 depthlimit = sample->depth.mm;
88 if (index >= dive->samples)
90 time = sample->time.seconds;
91 depth = sample->depth.mm;
96 if (depth <= depthlimit) {
97 if (depthlimit - depth > enough)
102 if (depth >= depthlimit) {
103 if (depth - depthlimit > enough)
111 /* Look up to ten minutes into the future */
112 timelimit = time + 600;
117 /* Scale to 0,0 -> maxx,maxy */
118 #define SCALE(x,y) (x)*maxx/scalex,(y)*maxy/scaley
120 static void plot_depth_text(struct dive *dive, cairo_t *cr,
121 double maxx, double maxy)
123 double scalex, scaley;
124 int maxtime, maxdepth;
127 /* Get plot scaling limits */
128 maxtime = round_seconds_up(dive->duration.seconds);
129 maxdepth = round_depth_up(dive->maxdepth);
134 cairo_set_font_size(cr, 14);
135 cairo_set_source_rgb(cr, 1, 0.2, 0.2);
137 while ((i = next_minmax(dive, i, 1)) != 0) {
138 text_render_options_t tro = {1.0, 0.2, 0.2, CENTER};
139 struct sample *sample = dive->sample+i;
140 int sec = sample->time.seconds;
141 depth_t depth = sample->depth;
145 switch (output_units.length) {
147 d = depth.mm / 1000.0;
156 plot_text(cr, &tro, SCALE(sec, depth.mm), fmt, d);
157 i = next_minmax(dive, i, 0);
163 static void plot_depth_profile(struct dive *dive, cairo_t *cr,
164 double maxx, double maxy)
166 double scalex, scaley;
167 int begins, sec, depth;
169 struct sample *sample;
170 int maxtime, maxdepth, marker;
172 samples = dive->samples;
176 cairo_set_line_width(cr, 2);
178 /* Get plot scaling limits */
179 maxtime = round_seconds_up(dive->duration.seconds);
180 maxdepth = round_depth_up(dive->maxdepth);
182 /* Time markers: every 5 min */
185 for (i = 5*60; i < maxtime; i += 5*60) {
186 cairo_move_to(cr, SCALE(i, 0));
187 cairo_line_to(cr, SCALE(i, 1));
190 /* Depth markers: every 30 ft or 10 m*/
193 switch (output_units.length) {
194 case METERS: marker = 10000; break;
195 case FEET: marker = 9144; break; /* 30 ft */
198 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
199 for (i = marker; i < maxdepth; i += marker) {
200 cairo_move_to(cr, SCALE(0, i));
201 cairo_line_to(cr, SCALE(1, i));
205 /* Show mean depth */
206 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
207 cairo_move_to(cr, SCALE(0, dive->meandepth.mm));
208 cairo_line_to(cr, SCALE(1, dive->meandepth.mm));
213 sample = dive->sample;
214 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
215 begins = sample->time.seconds;
216 cairo_move_to(cr, SCALE(sample->time.seconds, sample->depth.mm));
217 for (i = 1; i < dive->samples; i++) {
219 sec = sample->time.seconds;
220 if (sec <= maxtime) {
221 depth = sample->depth.mm;
222 cairo_line_to(cr, SCALE(sec, depth));
226 cairo_line_to(cr, SCALE(MIN(sec,maxtime), 0));
227 cairo_line_to(cr, SCALE(begins, 0));
228 cairo_close_path(cr);
229 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
230 cairo_fill_preserve(cr);
231 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
235 /* gets both the actual start and end pressure as well as the scaling factors */
236 static int get_cylinder_pressure_range(struct dive *dive, double *scalex, double *scaley,
237 pressure_t *startp, pressure_t *endp)
242 *scalex = round_seconds_up(dive->duration.seconds);
247 startp->mbar = endp->mbar = 0;
249 for (i = 0; i < dive->samples; i++) {
251 struct sample *sample = dive->sample + i;
253 /* FIXME! We only track cylinder 0 right now */
254 if (sample->cylinderindex)
256 mbar = sample->cylinderpressure.mbar;
274 static void plot_cylinder_pressure(struct dive *dive, cairo_t *cr,
275 double maxx, double maxy)
278 double scalex, scaley;
280 if (!get_cylinder_pressure_range(dive, &scalex, &scaley, NULL, NULL))
283 cairo_set_source_rgba(cr, 0.2, 1.0, 0.2, 0.80);
285 cairo_move_to(cr, SCALE(0, dive->cylinder[0].start.mbar));
286 for (i = 1; i < dive->samples; i++) {
288 struct sample *sample = dive->sample + i;
290 mbar = sample->cylinderpressure.mbar;
293 sec = sample->time.seconds;
294 if (sec <= dive->duration.seconds)
295 cairo_line_to(cr, SCALE(sec, mbar));
298 * We may have "surface time" events, in which case we don't go
299 * back to dive duration
301 if (sec < dive->duration.seconds)
302 cairo_line_to(cr, SCALE(dive->duration.seconds, dive->cylinder[0].end.mbar));
307 * Return air usage (in liters).
309 static double calculate_airuse(struct dive *dive)
314 for (i = 0; i < MAX_CYLINDERS; i++) {
315 cylinder_t *cyl = dive->cylinder + i;
316 int size = cyl->type.size.mliter;
322 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
324 /* Liters of air at 1 atm == milliliters at 1k atm*/
325 airuse += kilo_atm * size;
330 static void plot_info(struct dive *dive, cairo_t *cr,
331 double maxx, double maxy)
333 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
334 const double liters_per_cuft = 28.317;
338 airuse = calculate_airuse(dive);
342 /* I really need to start addign some unit setting thing */
343 switch (output_units.volume) {
349 airuse /= liters_per_cuft;
352 plot_text(cr, &tro, maxx*0.8, maxy*0.8, "vol: %4.2f %s", airuse, unit);
353 if (dive->duration.seconds) {
354 double pressure = 1 + (dive->meandepth.mm / 10000.0);
355 double sac = airuse / pressure * 60 / dive->duration.seconds;
356 plot_text(cr, &tro, maxx*0.8, maxy*0.85, "SAC: %4.2f %s/min", sac, unit);
360 static void plot_cylinder_pressure_text(struct dive *dive, cairo_t *cr,
361 double maxx, double maxy)
363 double scalex, scaley;
364 pressure_t startp, endp;
366 cairo_set_font_size(cr, 10);
368 if (get_cylinder_pressure_range(dive, &scalex, &scaley,
371 const char *unit = "bar";
373 switch (output_units.pressure) {
375 start = startp.mbar * 100;
376 end = startp.mbar * 100;
380 start = (startp.mbar + 500) / 1000;
381 end = (endp.mbar + 500) / 1000;
385 start = to_PSI(startp);
391 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
392 plot_text(cr, &tro, SCALE(0, startp.mbar), "%d %s", start, unit);
393 plot_text(cr, &tro, SCALE(dive->duration.seconds, endp.mbar),
398 static void plot(cairo_t *cr, int w, int h, struct dive *dive)
400 double topx, topy, maxx, maxy;
401 double scalex, scaley;
407 cairo_translate(cr, topx, topy);
409 /* Cylinder pressure plot */
410 plot_cylinder_pressure(dive, cr, maxx, maxy);
413 plot_depth_profile(dive, cr, maxx, maxy);
415 /* Text on top of all graphs.. */
416 plot_depth_text(dive, cr, maxx, maxy);
417 plot_cylinder_pressure_text(dive, cr, maxx, maxy);
419 /* And info box in the lower right corner.. */
420 plot_info(dive, cr, maxx, maxy);
422 /* Bounding box last */
423 scalex = scaley = 1.0;
424 cairo_set_source_rgb(cr, 1, 1, 1);
425 cairo_move_to(cr, SCALE(0,0));
426 cairo_line_to(cr, SCALE(0,1));
427 cairo_line_to(cr, SCALE(1,1));
428 cairo_line_to(cr, SCALE(1,0));
429 cairo_close_path(cr);
434 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
436 struct dive *dive = current_dive;
440 w = widget->allocation.width;
441 h = widget->allocation.height;
443 cr = gdk_cairo_create(widget->window);
444 cairo_set_source_rgb(cr, 0, 0, 0);
448 plot(cr, w, h, dive);
455 GtkWidget *dive_profile_widget(void)
459 da = gtk_drawing_area_new();
460 gtk_widget_set_size_request(da, 450, 350);
461 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);