2 /* creates all the necessary data for drawing the dive profile
3 * uses cairo to draw it
15 int selected_dive = 0;
17 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
18 /* Plot info with smoothing, velocity indication
19 * and one-, two- and three-minute minimums and maximums */
23 int meandepth, maxdepth;
27 unsigned int same_cylinder:1;
28 unsigned int cylinderindex;
30 /* pressure[0] is sensor pressure
31 * pressure[1] is interpolated pressure */
38 struct plot_data *min[3];
39 struct plot_data *max[3];
44 #define INTERPOLATED_PR 1
45 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
46 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
47 #define GET_PRESSURE(_entry) (SENSOR_PRESSURE(_entry) ? : INTERPOLATED_PRESSURE(_entry))
49 /* convert velocity to colors */
50 typedef struct { double r, g, b; } rgb_t;
51 static const rgb_t velocity_color[] = {
52 [STABLE] = {0.0, 0.4, 0.0},
53 [SLOW] = {0.4, 0.8, 0.0},
54 [MODERATE] = {0.8, 0.8, 0.0},
55 [FAST] = {0.8, 0.5, 0.0},
56 [CRAZY] = {1.0, 0.0, 0.0},
59 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
61 /* Scale to 0,0 -> maxx,maxy */
62 #define SCALEX(gc,x) (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
63 #define SCALEY(gc,y) (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
64 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
66 static void move_to(struct graphics_context *gc, double x, double y)
68 cairo_move_to(gc->cr, SCALE(gc, x, y));
71 static void line_to(struct graphics_context *gc, double x, double y)
73 cairo_line_to(gc->cr, SCALE(gc, x, y));
76 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
79 * For printers, we still honor 'a', but ignore colors
80 * for now. Black is white and white is black
89 cairo_set_source_rgba(gc->cr, r, g, b, a);
92 static void set_source_rgb_struct(struct graphics_context *gc, const rgb_t *rgb)
94 set_source_rgba(gc, rgb->r, rgb->g, rgb->b, 1);
97 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
99 set_source_rgba(gc, r, g, b, 1);
102 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
104 /* debugging tool - not normally used */
105 static void dump_pi (struct plot_info *pi)
109 printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
110 " maxpressure:%d mintemp:%d maxtemp:%d\n",
111 pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
112 pi->maxpressure, pi->mintemp, pi->maxtemp);
113 for (i = 0; i < pi->nr; i++)
114 printf(" entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
115 " temperature:%d depth:%d smoothed:%d}\n",
116 i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
117 pi->entry[i].pressure[0], pi->entry[i].pressure[1],
118 pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
123 * When showing dive profiles, we scale things to the
124 * current dive. However, we don't scale past less than
125 * 30 minutes or 90 ft, just so that small dives show
127 * we also need to add 180 seconds at the end so the min/max
130 static int get_maxtime(struct plot_info *pi)
132 int seconds = pi->maxtime;
133 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
134 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
137 static int get_maxdepth(struct plot_info *pi)
139 unsigned mm = pi->maxdepth;
140 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
141 return MAX(30000, ROUND_UP(mm+3000, 10000));
148 } text_render_options_t;
151 #define CENTER (-0.5)
158 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
159 double x, double y, const char *fmt, ...)
161 cairo_t *cr = gc->cr;
162 cairo_font_extents_t fe;
163 cairo_text_extents_t extents;
169 vsnprintf(buffer, sizeof(buffer), fmt, args);
172 cairo_set_font_size(cr, tro->size);
173 cairo_font_extents(cr, &fe);
174 cairo_text_extents(cr, buffer, &extents);
175 dx = tro->hpos * extents.width + extents.x_bearing;
176 dy = tro->vpos * extents.height + fe.descent;
179 cairo_rel_move_to(cr, dx, dy);
181 cairo_text_path(cr, buffer);
182 set_source_rgb(gc, 0, 0, 0);
186 cairo_rel_move_to(cr, dx, dy);
188 set_source_rgb(gc, tro->r, tro->g, tro->b);
189 cairo_show_text(cr, buffer);
196 static struct ev_select *ev_namelist;
197 static int evn_allocated;
200 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
204 for (i = 0; i < evn_used; i++) {
205 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
209 void remember_event(const char *eventname)
213 if (!eventname || (len = strlen(eventname)) == 0)
215 while (i < evn_used) {
216 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
220 if (evn_used == evn_allocated) {
222 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
224 /* we are screwed, but let's just bail out */
227 ev_namelist[evn_used].ev_name = strdup(eventname);
228 ev_namelist[evn_used].plot_ev = TRUE;
232 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
237 /* is plotting this event disabled? */
239 for (i = 0; i < evn_used; i++) {
240 if (! strcmp(event->name, ev_namelist[i].ev_name)) {
241 if (ev_namelist[i].plot_ev)
248 for (i = 0; i < pi->nr; i++) {
249 struct plot_data *data = pi->entry + i;
250 if (event->time.seconds < data->sec)
254 /* draw a little tirangular marker and attach tooltip */
255 x = SCALEX(gc, event->time.seconds);
256 y = SCALEY(gc, depth);
257 set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
258 cairo_move_to(gc->cr, x-15, y+6);
259 cairo_line_to(gc->cr, x-3 , y+6);
260 cairo_line_to(gc->cr, x-9, y-6);
261 cairo_line_to(gc->cr, x-15, y+6);
262 cairo_stroke_preserve(gc->cr);
264 set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
265 cairo_move_to(gc->cr, x-9, y-3);
266 cairo_line_to(gc->cr, x-9, y+1);
267 cairo_move_to(gc->cr, x-9, y+4);
268 cairo_line_to(gc->cr, x-9, y+4);
269 cairo_stroke(gc->cr);
270 attach_tooltip(x-15, y-6, 12, 12, event->name);
273 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
275 static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
276 struct event *event = dive->events;
282 plot_one_event(gc, pi, event, &tro);
287 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
289 int sec = entry->sec, decimals;
292 d = get_depth_units(entry->depth, &decimals, NULL);
294 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
297 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
299 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
300 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
304 for (i = 0; i < pi->nr; i++) {
305 struct plot_data *entry = pi->entry + i;
307 if (entry->depth < 2000)
310 if ((entry == entry->max[2]) && entry->depth != last) {
311 render_depth_sample(gc, entry, &deep);
315 if ((entry == entry->min[2]) && entry->depth != last) {
316 render_depth_sample(gc, entry, &shallow);
320 if (entry->depth != last)
325 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
327 int maxtime, maxdepth;
329 /* Get plot scaling limits */
330 maxtime = get_maxtime(pi);
331 maxdepth = get_maxdepth(pi);
333 gc->leftx = 0; gc->rightx = maxtime;
334 gc->topy = 0; gc->bottomy = maxdepth;
336 plot_text_samples(gc, pi);
339 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
342 struct plot_data *entry = pi->entry;
344 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
345 move_to(gc, entry->sec, entry->smoothed);
346 for (i = 1; i < pi->nr; i++) {
348 line_to(gc, entry->sec, entry->smoothed);
350 cairo_stroke(gc->cr);
353 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
357 struct plot_data *entry = pi->entry;
359 set_source_rgba(gc, 1, 0.2, 1, a);
360 move_to(gc, entry->sec, entry->min[index]->depth);
361 for (i = 1; i < pi->nr; i++) {
363 line_to(gc, entry->sec, entry->min[index]->depth);
365 for (i = 1; i < pi->nr; i++) {
366 line_to(gc, entry->sec, entry->max[index]->depth);
369 cairo_close_path(gc->cr);
373 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
377 plot_minmax_profile_minute(gc, pi, 2, 0.1);
378 plot_minmax_profile_minute(gc, pi, 1, 0.1);
379 plot_minmax_profile_minute(gc, pi, 0, 0.1);
382 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
385 cairo_t *cr = gc->cr;
387 struct plot_data *entry;
388 int maxtime, maxdepth, marker;
389 int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
391 /* Get plot scaling limits */
392 maxtime = get_maxtime(pi);
393 maxdepth = get_maxdepth(pi);
395 /* Time markers: at most every 5 min, but no more than 12 markers
396 * and for convenience we do 5, 10, 15 or 30 min intervals.
397 * This allows for 6h dives - enough (I hope) for even the craziest
398 * divers - but just in case, for those 8h depth-record-breaking dives,
399 * we double the interval if this still doesn't get us to 12 or fewer
402 while (maxtime / increments[i] > 12 && i < 4)
404 incr = increments[i];
405 while (maxtime / incr > 12)
408 gc->leftx = 0; gc->rightx = maxtime;
409 gc->topy = 0; gc->bottomy = 1.0;
410 set_source_rgba(gc, 1, 1, 1, 0.5);
411 for (i = incr; i < maxtime; i += incr) {
417 /* now the text on every second time marker */
418 text_render_options_t tro = {10, 0.2, 1.0, 0.2, CENTER, TOP};
419 for (i = incr; i < maxtime; i += 2 * incr)
420 plot_text(gc, &tro, i, 1, "%d", i/60);
422 /* Depth markers: every 30 ft or 10 m*/
423 gc->leftx = 0; gc->rightx = 1.0;
424 gc->topy = 0; gc->bottomy = maxdepth;
425 switch (output_units.length) {
426 case METERS: marker = 10000; break;
427 case FEET: marker = 9144; break; /* 30 ft */
430 set_source_rgba(gc, 1, 1, 1, 0.5);
431 for (i = marker; i < maxdepth; i += marker) {
437 /* Show mean depth */
439 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
440 move_to(gc, 0, pi->meandepth);
441 line_to(gc, 1, pi->meandepth);
445 gc->leftx = 0; gc->rightx = maxtime;
448 * These are good for debugging text placement etc,
449 * but not for actual display..
452 plot_smoothed_profile(gc, pi);
453 plot_minmax_profile(gc, pi);
456 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
458 /* Do the depth profile for the neat fill */
459 gc->topy = 0; gc->bottomy = maxdepth;
460 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
464 for (i = 0; i < pi->nr; i++, entry++)
465 line_to(gc, entry->sec, entry->depth);
466 cairo_close_path(gc->cr);
468 set_source_rgba(gc, 1, 1, 1, 0.2);
469 cairo_fill_preserve(cr);
470 set_source_rgb(gc, 1, 1, 1);
476 /* Now do it again for the velocity colors */
478 for (i = 1; i < pi->nr; i++) {
481 /* we want to draw the segments in different colors
482 * representing the vertical velocity, so we need to
483 * chop this into short segments */
484 depth = entry->depth;
485 set_source_rgb_struct(gc, &velocity_color[entry->velocity]);
486 move_to(gc, entry[-1].sec, entry[-1].depth);
487 line_to(gc, sec, depth);
492 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
494 int maxtime, mintemp, maxtemp, delta;
496 /* Get plot scaling limits */
497 maxtime = get_maxtime(pi);
498 mintemp = pi->mintemp;
499 maxtemp = pi->maxtemp;
501 gc->leftx = 0; gc->rightx = maxtime;
502 /* Show temperatures in roughly the lower third, but make sure the scale
503 is at least somewhat reasonable */
504 delta = maxtemp - mintemp;
505 if (delta > 3000) { /* more than 3K in fluctuation */
506 gc->topy = maxtemp + delta*2;
507 gc->bottomy = mintemp - delta/2;
509 gc->topy = maxtemp + 1500 + delta*2;
510 gc->bottomy = mintemp - delta/2;
513 return maxtemp > mintemp;
516 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
520 static const text_render_options_t tro = {12, 0.6, 0.6, 1.0, LEFT, TOP};
522 deg = get_temp_units(mkelvin, &unit);
524 plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
527 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
530 int last = -300, sec = 0;
531 int last_temperature = 0, last_printed_temp = 0;
533 if (!setup_temperature_limits(gc, pi))
536 for (i = 0; i < pi->nr; i++) {
537 struct plot_data *entry = pi->entry+i;
538 int mkelvin = entry->temperature;
542 last_temperature = mkelvin;
544 /* don't print a temperature
545 * if it's been less than 5min and less than a 2K change OR
546 * if it's been less than 2min OR if the change from the
547 * last print is less than .4K (and therefore less than 1F */
548 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
549 (sec < last + 120) ||
550 (abs(mkelvin - last_printed_temp) < 400))
553 plot_single_temp_text(gc,sec,mkelvin);
554 last_printed_temp = mkelvin;
556 /* it would be nice to print the end temperature, if it's
557 * different or if the last temperature print has been more
558 * than a quarter of the dive back */
559 if ((abs(last_temperature - last_printed_temp) > 500) ||
560 ((double)last / (double)sec < 0.75))
561 plot_single_temp_text(gc, sec, last_temperature);
564 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
567 cairo_t *cr = gc->cr;
570 if (!setup_temperature_limits(gc, pi))
573 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
574 for (i = 0; i < pi->nr; i++) {
575 struct plot_data *entry = pi->entry + i;
576 int mkelvin = entry->temperature;
577 int sec = entry->sec;
584 line_to(gc, sec, mkelvin);
586 move_to(gc, sec, mkelvin);
592 /* gets both the actual start and end pressure as well as the scaling factors */
593 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
596 gc->rightx = get_maxtime(pi);
598 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
599 return pi->maxpressure != 0;
603 static const rgb_t sac_color[SAC_COLORS] = {
615 /* set the color for the pressure plot according to temporary sac rate
616 * as compared to avg_sac; the calculation simply maps the delta between
617 * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
618 * more than 6000 ml/min below avg_sac mapped to 0 */
620 static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
623 int delta = sac - avg_sac + 7000;
626 sac_index = delta / 2000;
629 if (sac_index > SAC_COLORS - 1)
630 sac_index = SAC_COLORS - 1;
631 set_source_rgb_struct(gc, &sac_color[sac_index]);
633 set_source_rgb(gc, 1.0, 1.0, 1.0);
637 /* calculate the current SAC in ml/min and convert to int */
638 #define GET_LOCAL_SAC(_entry1, _entry2, _dive) (int) \
639 ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) * \
640 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter / \
641 (((_entry2)->sec - (_entry1)->sec) / 60.0) / \
642 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) / \
645 #define SAC_WINDOW 45 /* sliding window in seconds for current SAC calculation */
647 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
652 int lift_pen = FALSE;
653 int first_plot = TRUE;
655 struct plot_data *last_entry = NULL;
657 if (!get_cylinder_pressure_range(gc, pi))
660 for (i = 0; i < pi->nr; i++) {
662 struct plot_data *entry = pi->entry + i;
664 mbar = GET_PRESSURE(entry);
665 if (!entry->same_cylinder) {
676 sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
680 for (j = last; j < i; j++)
681 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
683 if (entry->sec - last_entry->sec >= SAC_WINDOW) {
685 last_entry = pi->entry + last;
688 set_sac_color(gc, sac, dive->sac);
690 if (!first_plot && entry->same_cylinder) {
691 /* if we have a previous event from the same tank,
692 * draw at least a short line */
694 prev_pr = GET_PRESSURE(entry - 1);
695 move_to(gc, (entry-1)->sec, prev_pr);
696 line_to(gc, entry->sec, mbar);
699 move_to(gc, entry->sec, mbar);
703 line_to(gc, entry->sec, mbar);
705 cairo_stroke(gc->cr);
706 move_to(gc, entry->sec, mbar);
710 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
711 int xalign, int yalign)
716 pressure = get_pressure_units(mbar, &unit);
717 text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
718 plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
721 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
725 int seen_cyl[MAX_CYLINDERS] = { FALSE, };
726 int last_pressure[MAX_CYLINDERS] = { 0, };
727 int last_time[MAX_CYLINDERS] = { 0, };
728 struct plot_data *entry;
730 if (!get_cylinder_pressure_range(gc, pi))
733 /* only loop over the actual events from the dive computer
734 * plus the second synthetic event at the start (to make sure
735 * we get "time=0" right)
736 * sadly with a recent change that first entry may no longer
737 * have any pressure reading - in that case just grab the
738 * pressure from the second entry */
739 if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
740 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
741 for (i = 1; i < pi->nr; i++) {
742 entry = pi->entry + i;
744 if (!entry->same_cylinder) {
745 cyl = entry->cylinderindex;
746 if (!seen_cyl[cyl]) {
747 mbar = GET_PRESSURE(entry);
748 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
749 seen_cyl[cyl] = TRUE;
752 /* remember the last pressure and time of
753 * the previous cylinder */
754 cyl = (entry - 1)->cylinderindex;
755 last_pressure[cyl] = GET_PRESSURE(entry - 1);
756 last_time[cyl] = (entry - 1)->sec;
760 cyl = entry->cylinderindex;
761 if (GET_PRESSURE(entry))
762 last_pressure[cyl] = GET_PRESSURE(entry);
763 last_time[cyl] = entry->sec;
765 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
766 if (last_time[cyl]) {
767 plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
772 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
774 struct plot_data *p = entry;
775 int time = entry->sec;
776 int seconds = 90*(index+1);
777 struct plot_data *min, *max;
780 /* Go back 'seconds' in time */
782 if (p[-1].sec < time - seconds)
787 /* Then go forward until we hit an entry past the time */
792 int depth = p->depth;
793 if (p->sec > time + seconds)
797 if (depth < min->depth)
799 if (depth > max->depth)
802 entry->min[index] = min;
803 entry->max[index] = max;
804 entry->avg[index] = (avg + nr/2) / nr;
807 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
809 analyze_plot_info_minmax_minute(entry, first, last, 0);
810 analyze_plot_info_minmax_minute(entry, first, last, 1);
811 analyze_plot_info_minmax_minute(entry, first, last, 2);
814 static velocity_t velocity(int speed)
818 if (speed < -304) /* ascent faster than -60ft/min */
820 else if (speed < -152) /* above -30ft/min */
822 else if (speed < -76) /* -15ft/min */
824 else if (speed < -25) /* -5ft/min */
826 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
827 for descent are usually about 2x ascent rate; still, we want
828 stable to mean stable */
830 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
832 else if (speed < 304) /* up to 60ft/min is moderate */
834 else if (speed < 507) /* up to 100ft/min is fast */
836 else /* more than that is just crazy - you'll blow your ears out */
841 static struct plot_info *analyze_plot_info(struct plot_info *pi)
846 /* Do pressure min/max based on the non-surface data */
847 for (i = 0; i < nr; i++) {
848 struct plot_data *entry = pi->entry+i;
849 int pressure = GET_PRESSURE(entry);
850 int temperature = entry->temperature;
853 if (pressure > pi->maxpressure)
854 pi->maxpressure = pressure;
858 if (!pi->mintemp || temperature < pi->mintemp)
859 pi->mintemp = temperature;
860 if (temperature > pi->maxtemp)
861 pi->maxtemp = temperature;
865 /* Smoothing function: 5-point triangular smooth */
866 for (i = 2; i < nr; i++) {
867 struct plot_data *entry = pi->entry+i;
871 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
872 entry->smoothed = (depth+4) / 9;
874 /* vertical velocity in mm/sec */
875 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
876 if (entry[0].sec - entry[-1].sec) {
877 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
878 /* if our samples are short and we aren't too FAST*/
879 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
881 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
883 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
884 (entry[0].sec - entry[past].sec));
887 entry->velocity = STABLE;
890 /* One-, two- and three-minute minmax data */
891 for (i = 0; i < nr; i++) {
892 struct plot_data *entry = pi->entry +i;
893 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
900 * simple structure to track the beginning and end tank pressure as
901 * well as the integral of depth over time spent while we have no
902 * pressure reading from the tank */
903 typedef struct pr_track_struct pr_track_t;
904 struct pr_track_struct {
909 double pressure_time;
913 static pr_track_t *pr_track_alloc(int start, int t_start) {
914 pr_track_t *pt = malloc(sizeof(pr_track_t));
916 pt->t_start = t_start;
919 pt->pressure_time = 0.0;
924 /* poor man's linked list */
925 static pr_track_t *list_last(pr_track_t *list)
927 pr_track_t *tail = list;
936 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
938 pr_track_t *tail = list_last(list);
941 tail->next = element;
945 static void list_free(pr_track_t *list)
949 list_free(list->next);
953 static void dump_pr_track(pr_track_t **track_pr)
958 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
959 list = track_pr[cyl];
961 printf("cyl%d: start %d end %d t_start %d t_end %d pt %6.3f\n", cyl,
962 list->start, list->end, list->t_start, list->t_end, list->pressure_time);
968 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
969 pr_track_t **track_pr)
971 pr_track_t *list = NULL;
972 pr_track_t *nlist = NULL;
975 struct plot_data *entry;
976 int cur_pr[MAX_CYLINDERS];
979 /* another great debugging tool */
980 dump_pr_track(track_pr);
982 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
983 cur_pr[cyl] = track_pr[cyl]->start;
986 /* The first two are "fillers", but in case we don't have a sample
987 * at time 0 we need to process the second of them here */
988 for (i = 1; i < pi->nr; i++) {
989 entry = pi->entry + i;
990 if (SENSOR_PRESSURE(entry)) {
991 cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
993 if(!list || list->t_end < entry->sec) {
994 nlist = track_pr[entry->cylinderindex];
996 while (nlist && nlist->t_start <= entry->sec) {
1000 /* there may be multiple segments - so
1001 * let's assemble the length */
1003 pt = list->pressure_time;
1004 while (!nlist->end) {
1005 nlist = nlist->next;
1007 /* oops - we have no end pressure,
1008 * so this means this is a tank without
1009 * gas consumption information */
1012 pt += nlist->pressure_time;
1015 /* just continue without calculating
1016 * interpolated values */
1017 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1021 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
1024 double cur_pt = (entry->sec - (entry-1)->sec) *
1025 (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1026 INTERPOLATED_PRESSURE(entry) =
1027 cur_pr[entry->cylinderindex] + cur_pt * magic + 0.5;
1028 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1030 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1035 static int get_cylinder_index(struct dive *dive, struct event *ev)
1040 * Try to find a cylinder that matches the O2 percentage
1041 * in the gas change event 'value' field.
1043 * Crazy suunto gas change events. We really should do
1044 * this in libdivecomputer or something.
1046 for (i = 0; i < MAX_CYLINDERS; i++) {
1047 cylinder_t *cyl = dive->cylinder+i;
1048 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1049 if (o2 == ev->value)
1056 static struct event *get_next_gaschange(struct event *event)
1059 if (!strcmp(event->name, "gaschange"))
1061 event = event->next;
1066 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1068 while (i < pi->nr) {
1069 struct plot_data *entry = pi->entry+i;
1070 if (entry->sec > end)
1072 if (entry->cylinderindex != cylinderindex) {
1073 entry->cylinderindex = cylinderindex;
1074 entry->pressure[0] = 0;
1081 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1083 int i = 0, cylinderindex = 0;
1084 struct event *ev = get_next_gaschange(dive->events);
1090 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1091 cylinderindex = get_cylinder_index(dive, ev);
1092 ev = get_next_gaschange(ev->next);
1094 set_cylinder_index(pi, i, cylinderindex, ~0u);
1097 /* for computers that track gas changes through events */
1098 static int count_gas_change_events(struct dive *dive)
1101 struct event *ev = get_next_gaschange(dive->events);
1105 ev = get_next_gaschange(ev->next);
1111 * Create a plot-info with smoothing and ranged min/max
1113 * This also makes sure that we have extra empty events on both
1114 * sides, so that you can do end-points without having to worry
1117 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1119 int cylinderindex = -1;
1120 int lastdepth, lastindex;
1121 int i, pi_idx, nr, sec, cyl;
1123 struct plot_info *pi;
1124 pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1125 pr_track_t *pr_track, *current;
1126 gboolean missing_pr = FALSE;
1127 struct plot_data *entry = NULL;
1130 /* we want to potentially add synthetic plot_info elements for the gas changes */
1131 nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1132 alloc_size = plot_info_size(nr);
1133 pi = malloc(alloc_size);
1136 memset(pi, 0, alloc_size);
1138 pi_idx = 2; /* the two extra events at the start */
1139 /* check for gas changes before the samples start */
1140 ev = get_next_gaschange(dive->events);
1141 while (ev && ev->time.seconds < dive_sample->time.seconds) {
1142 entry = pi->entry + pi_idx;
1143 entry->sec = ev->time.seconds;
1144 entry->depth = 0; /* is that always correct ? */
1146 ev = get_next_gaschange(ev->next);
1148 if (ev && ev->time.seconds == dive_sample->time.seconds) {
1149 /* we already have a sample at the time of the event */
1150 ev = get_next_gaschange(ev->next);
1155 for (i = 0; i < nr_samples; i++) {
1158 struct sample *sample = dive_sample+i;
1160 entry = pi->entry + i + pi_idx;
1161 while (ev && ev->time.seconds < sample->time.seconds) {
1162 /* insert two fake plot info structures for the end of
1163 * the old tank and the start of the new tank */
1164 if (ev->time.seconds == sample->time.seconds - 1) {
1165 entry->sec = ev->time.seconds - 1;
1166 (entry+1)->sec = ev->time.seconds;
1168 entry->sec = ev->time.seconds;
1169 (entry+1)->sec = ev->time.seconds + 1;
1171 /* we need a fake depth - let's interpolate */
1173 entry->depth = sample->depth.mm -
1174 (sample->depth.mm - (sample-1)->depth.mm) / 2;
1176 entry->depth = sample->depth.mm;
1177 (entry+1)->depth = entry->depth;
1179 entry = pi->entry + i + pi_idx;
1180 ev = get_next_gaschange(ev->next);
1182 if (ev && ev->time.seconds == sample->time.seconds) {
1183 /* we already have a sample at the time of the event
1184 * just add a new one for the old tank and delay the
1185 * real even by one second (to keep time monotonous) */
1186 entry->sec = ev->time.seconds;
1187 entry->depth = sample->depth.mm;
1189 entry = pi->entry + i + pi_idx;
1190 ev = get_next_gaschange(ev->next);
1193 sec = entry->sec = sample->time.seconds + delay;
1194 depth = entry->depth = sample->depth.mm;
1195 entry->cylinderindex = sample->cylinderindex;
1196 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1197 entry->temperature = sample->temperature.mkelvin;
1199 if (depth || lastdepth)
1200 lastindex = i + pi_idx;
1203 if (depth > pi->maxdepth)
1204 pi->maxdepth = depth;
1206 entry = pi->entry + i + pi_idx;
1207 /* are there still unprocessed gas changes? that would be very strange */
1209 entry->sec = ev->time.seconds;
1210 entry->depth = 0; /* why are there gas changes after the dive is over? */
1212 entry = pi->entry + i + pi_idx;
1213 ev = get_next_gaschange(ev->next);
1215 nr = nr_samples + pi_idx - 2;
1216 check_gas_change_events(dive, pi);
1218 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1219 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1220 current = track_pr[pi->entry[2].cylinderindex];
1221 for (i = 0; i < nr + 1; i++) {
1222 entry = pi->entry + i + 1;
1224 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1225 cylinderindex = entry->cylinderindex;
1227 /* track the segments per cylinder and their pressure/time integral */
1228 if (!entry->same_cylinder) {
1229 current->end = SENSOR_PRESSURE(entry-1);
1230 current->t_end = (entry-1)->sec;
1231 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1232 track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1233 } else { /* same cylinder */
1234 if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1235 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1236 /* transmitter changed its working status */
1237 current->end = SENSOR_PRESSURE(entry-1);
1238 current->t_end = (entry-1)->sec;
1239 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1240 track_pr[cylinderindex] =
1241 list_add(track_pr[cylinderindex], current);
1244 /* finally, do the discrete integration to get the SAC rate equivalent */
1245 current->pressure_time += (entry->sec - (entry-1)->sec) *
1246 (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1247 missing_pr |= !SENSOR_PRESSURE(entry);
1251 current->t_end = entry->sec;
1253 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1254 int pr = dive->cylinder[cyl].end.mbar;
1255 if (pr && track_pr[cyl]) {
1256 pr_track = list_last(track_pr[cyl]);
1260 /* Fill in the last two entries with empty values but valid times
1261 * without creating a false cylinder change event */
1263 pi->entry[i].sec = sec + 20;
1264 pi->entry[i].same_cylinder = 1;
1265 pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1266 INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1267 pi->entry[i+1].sec = sec + 40;
1268 pi->entry[i+1].same_cylinder = 1;
1269 pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1270 INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1271 /* the number of actual entries - some computers have lots of
1272 * depth 0 samples at the end of a dive, we want to make sure
1273 * we have exactly one of them at the end */
1274 pi->nr = lastindex+1;
1275 while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1277 pi->maxtime = pi->entry[lastindex].sec;
1279 /* Analyze_plot_info() will do the sample max pressures,
1280 * this handles the manual pressures
1282 pi->maxpressure = 0;
1283 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1284 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1285 if (mbar > pi->maxpressure)
1286 pi->maxpressure = mbar;
1289 pi->meandepth = dive->meandepth.mm;
1292 fill_missing_tank_pressures(dive, pi, track_pr);
1294 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1295 list_free(track_pr[cyl]);
1296 if (0) /* awesome for debugging - not useful otherwise */
1298 return analyze_plot_info(pi);
1301 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1303 struct plot_info *pi;
1304 static struct sample fake[4];
1305 struct sample *sample = dive->sample;
1306 int nr = dive->samples;
1309 int duration = dive->duration.seconds;
1310 int maxdepth = dive->maxdepth.mm;
1312 fake[1].time.seconds = duration * 0.05;
1313 fake[1].depth.mm = maxdepth;
1314 fake[2].time.seconds = duration * 0.95;
1315 fake[2].depth.mm = maxdepth;
1316 fake[3].time.seconds = duration * 1.00;
1320 pi = create_plot_info(dive, nr, sample);
1322 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1323 cairo_set_line_width(gc->cr, 2);
1324 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1325 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1328 * We can use "cairo_translate()" because that doesn't
1329 * scale line width etc. But the actual scaling we need
1330 * do set up ourselves..
1332 * Snif. What a pity.
1334 gc->maxx = (drawing_area->width - 2*drawing_area->x);
1335 gc->maxy = (drawing_area->height - 2*drawing_area->y);
1337 /* Temperature profile */
1338 plot_temperature_profile(gc, pi);
1341 plot_depth_profile(gc, pi);
1342 plot_events(gc, pi, dive);
1344 /* Cylinder pressure plot */
1345 plot_cylinder_pressure(gc, pi, dive);
1347 /* Text on top of all graphs.. */
1348 plot_temperature_text(gc, pi);
1349 plot_depth_text(gc, pi);
1350 plot_cylinder_pressure_text(gc, pi);
1352 /* Bounding box last */
1353 gc->leftx = 0; gc->rightx = 1.0;
1354 gc->topy = 0; gc->bottomy = 1.0;
1356 set_source_rgb(gc, 1, 1, 1);
1361 cairo_close_path(gc->cr);
1362 cairo_stroke(gc->cr);