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_feet_up(int feet)
27 return MAX(90, ROUND_UP(feet+5, 15));
32 enum {CENTER,LEFT} allign;
33 } text_render_options_t;
35 static void plot_text(cairo_t *cr, text_render_options_t *tro,
36 double x, double y, const char *fmt, ...)
38 cairo_text_extents_t extents;
43 vsnprintf(buffer, sizeof(buffer), fmt, args);
46 cairo_text_extents(cr, buffer, &extents);
48 if (tro->allign == CENTER)
49 x -= extents.width/2 + extents.x_bearing;
50 y += extents.height * 1.2;
52 cairo_move_to(cr, x, y);
53 cairo_text_path(cr, buffer);
54 cairo_set_source_rgb(cr, 0, 0, 0);
57 cairo_move_to(cr, x, y);
58 cairo_set_source_rgb(cr, tro->r, tro->g, tro->b);
59 cairo_show_text(cr, buffer);
63 * Find the next maximum point in a 10-minute window.
65 * We exit early if we hit "enough" of a depth reversal,
66 * which is roughly 10 feet.
68 static int next_minmax(struct dive *dive, int index, int minmax)
70 const int enough = 3000;
71 int timelimit, depthlimit, result;
72 struct sample *sample = dive->sample + index;
74 if (index >= dive->samples)
78 depthlimit = sample->depth.mm;
86 if (index >= dive->samples)
88 time = sample->time.seconds;
89 depth = sample->depth.mm;
94 if (depth <= depthlimit) {
95 if (depthlimit - depth > enough)
100 if (depth >= depthlimit) {
101 if (depth - depthlimit > enough)
109 /* Look up to ten minutes into the future */
110 timelimit = time + 600;
115 /* Scale to 0,0 -> maxx,maxy */
116 #define SCALE(x,y) (x)*maxx/scalex,(y)*maxy/scaley
118 static void plot_depth_text(struct dive *dive, cairo_t *cr,
119 double maxx, double maxy)
121 double scalex, scaley;
122 int maxtime, maxdepth;
125 /* Get plot scaling limits */
126 maxtime = round_seconds_up(dive->duration.seconds);
127 maxdepth = round_feet_up(to_feet(dive->maxdepth));
132 cairo_set_font_size(cr, 14);
133 cairo_set_source_rgb(cr, 1, 0.2, 0.2);
135 while ((i = next_minmax(dive, i, 1)) != 0) {
136 text_render_options_t tro = {1.0, 0.2, 0.2, CENTER};
137 struct sample *sample = dive->sample+i;
138 int sec = sample->time.seconds;
139 int depth = to_feet(sample->depth);
141 plot_text(cr, &tro, SCALE(sec, depth), "%d ft", depth);
142 i = next_minmax(dive, i, 0);
148 static void plot_depth_profile(struct dive *dive, cairo_t *cr,
149 double maxx, double maxy)
151 double scalex, scaley;
152 int begins, sec, depth;
154 struct sample *sample;
155 int maxtime, maxdepth;
157 samples = dive->samples;
161 cairo_set_line_width(cr, 2);
163 /* Get plot scaling limits */
164 maxtime = round_seconds_up(dive->duration.seconds);
165 maxdepth = round_feet_up(to_feet(dive->maxdepth));
167 /* Time markers: every 5 min */
170 for (i = 5*60; i < maxtime; i += 5*60) {
171 cairo_move_to(cr, SCALE(i, 0));
172 cairo_line_to(cr, SCALE(i, 1));
175 /* Depth markers: every 15 ft */
178 cairo_set_source_rgba(cr, 1, 1, 1, 0.5);
179 for (i = 15; i < maxdepth; i += 15) {
180 cairo_move_to(cr, SCALE(0, i));
181 cairo_line_to(cr, SCALE(1, i));
185 /* Show mean depth */
186 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.40);
187 cairo_move_to(cr, SCALE(0, to_feet(dive->meandepth)));
188 cairo_line_to(cr, SCALE(1, to_feet(dive->meandepth)));
193 sample = dive->sample;
194 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
195 begins = sample->time.seconds;
196 cairo_move_to(cr, SCALE(sample->time.seconds, to_feet(sample->depth)));
197 for (i = 1; i < dive->samples; i++) {
199 sec = sample->time.seconds;
200 depth = to_feet(sample->depth);
201 cairo_line_to(cr, SCALE(sec, depth));
204 cairo_line_to(cr, SCALE(sec, 0));
205 cairo_line_to(cr, SCALE(begins, 0));
206 cairo_close_path(cr);
207 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.20);
208 cairo_fill_preserve(cr);
209 cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.80);
213 /* gets both the actual start and end pressure as well as the scaling factors */
214 static int get_cylinder_pressure_range(struct dive *dive, double *scalex, double *scaley,
215 double *startp, double *endp)
221 *scalex = round_seconds_up(dive->duration.seconds);
226 *startp = *endp = 0.0;
228 for (i = 0; i < dive->samples; i++) {
229 struct sample *sample = dive->sample + i;
231 /* FIXME! We only track cylinder 0 right now */
232 if (sample->cylinderindex)
234 if (!sample->cylinderpressure.mbar)
236 bar = sample->cylinderpressure.mbar;
237 if (bar != 0.0 && startp && *startp == 0.0)
252 static void plot_cylinder_pressure(struct dive *dive, cairo_t *cr,
253 double maxx, double maxy)
256 double scalex, scaley;
258 if (!get_cylinder_pressure_range(dive, &scalex, &scaley, NULL, NULL))
261 cairo_set_source_rgba(cr, 0.2, 1.0, 0.2, 0.80);
263 cairo_move_to(cr, SCALE(0, dive->cylinder[0].start.mbar));
264 for (i = 1; i < dive->samples; i++) {
266 struct sample *sample = dive->sample + i;
268 mbar = sample->cylinderpressure.mbar;
271 sec = sample->time.seconds;
272 cairo_line_to(cr, SCALE(sec, mbar));
275 * We may have "surface time" events, in which case we don't go
276 * back to dive duration
278 if (sec < dive->duration.seconds)
279 cairo_line_to(cr, SCALE(dive->duration.seconds, dive->cylinder[0].end.mbar));
284 * Return air usage (in liters).
286 static double calculate_airuse(struct dive *dive)
291 for (i = 0; i < MAX_CYLINDERS; i++) {
292 cylinder_t *cyl = dive->cylinder + i;
293 int size = cyl->type.size.mliter;
299 kilo_atm = (cyl->start.mbar - cyl->end.mbar) / 1013250.0;
301 /* Liters of air at 1 atm == milliliters at 1k atm*/
302 airuse += kilo_atm * size;
307 static void plot_info(struct dive *dive, cairo_t *cr,
308 double maxx, double maxy)
310 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
311 const double liters_per_cuft = 28.317;
314 airuse = calculate_airuse(dive);
318 /* I really need to start addign some unit setting thing */
319 airuse /= liters_per_cuft;
320 plot_text(cr, &tro, maxx*0.8, maxy*0.8, "cuft: %4.2f", airuse);
321 if (dive->duration.seconds) {
322 double pressure = 1 + (dive->meandepth.mm / 10000.0);
323 double sac = airuse / pressure * 60 / dive->duration.seconds;
324 plot_text(cr, &tro, maxx*0.8, maxy*0.85, "SAC: %4.2f", sac);
328 static void plot_cylinder_pressure_text(struct dive *dive, cairo_t *cr,
329 double maxx, double maxy)
331 double scalex, scaley;
334 cairo_set_font_size(cr, 10);
336 if (get_cylinder_pressure_range(dive, &scalex, &scaley,
338 text_render_options_t tro = {0.2, 1.0, 0.2, LEFT};
339 plot_text(cr, &tro, SCALE(0, startp), "%3.0f bar", startp/1000.0);
340 plot_text(cr, &tro, SCALE(dive->duration.seconds, endp),
341 "%3.0f bar", endp/1000.0);
345 static void plot(cairo_t *cr, int w, int h, struct dive *dive)
347 double topx, topy, maxx, maxy;
348 double scalex, scaley;
354 cairo_translate(cr, topx, topy);
356 /* Cylinder pressure plot */
357 plot_cylinder_pressure(dive, cr, maxx, maxy);
360 plot_depth_profile(dive, cr, maxx, maxy);
362 /* Text on top of all graphs.. */
363 plot_depth_text(dive, cr, maxx, maxy);
364 plot_cylinder_pressure_text(dive, cr, maxx, maxy);
366 /* And info box in the lower right corner.. */
367 plot_info(dive, cr, maxx, maxy);
369 /* Bounding box last */
370 scalex = scaley = 1.0;
371 cairo_set_source_rgb(cr, 1, 1, 1);
372 cairo_move_to(cr, SCALE(0,0));
373 cairo_line_to(cr, SCALE(0,1));
374 cairo_line_to(cr, SCALE(1,1));
375 cairo_line_to(cr, SCALE(1,0));
376 cairo_close_path(cr);
381 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
383 struct dive *dive = current_dive;
387 w = widget->allocation.width;
388 h = widget->allocation.height;
390 cr = gdk_cairo_create(widget->window);
391 cairo_set_source_rgb(cr, 0, 0, 0);
395 plot(cr, w, h, dive);
402 GtkWidget *dive_profile_widget(void)
406 da = gtk_drawing_area_new();
407 gtk_widget_set_size_request(da, 450, 350);
408 g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);