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 rgb[] = {
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 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
94 set_source_rgba(gc, r, g, b, 1);
97 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
99 /* debugging tool - not normally used */
100 static void dump_pi (struct plot_info *pi)
104 printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
105 " maxpressure:%d mintemp:%d maxtemp:%d\n",
106 pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
107 pi->maxpressure, pi->mintemp, pi->maxtemp);
108 for (i = 0; i < pi->nr; i++)
109 printf(" entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
110 " temperature:%d depth:%d smoothed:%d}\n",
111 i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
112 pi->entry[i].pressure[0], pi->entry[i].pressure[1],
113 pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
118 * When showing dive profiles, we scale things to the
119 * current dive. However, we don't scale past less than
120 * 30 minutes or 90 ft, just so that small dives show
122 * we also need to add 180 seconds at the end so the min/max
125 static int get_maxtime(struct plot_info *pi)
127 int seconds = pi->maxtime;
128 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
129 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
132 static int get_maxdepth(struct plot_info *pi)
134 unsigned mm = pi->maxdepth;
135 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
136 return MAX(30000, ROUND_UP(mm+3000, 10000));
143 } text_render_options_t;
146 #define CENTER (-0.5)
153 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
154 double x, double y, const char *fmt, ...)
156 cairo_t *cr = gc->cr;
157 cairo_font_extents_t fe;
158 cairo_text_extents_t extents;
164 vsnprintf(buffer, sizeof(buffer), fmt, args);
167 cairo_set_font_size(cr, tro->size);
168 cairo_font_extents(cr, &fe);
169 cairo_text_extents(cr, buffer, &extents);
170 dx = tro->hpos * extents.width + extents.x_bearing;
171 dy = tro->vpos * extents.height + fe.descent;
174 cairo_rel_move_to(cr, dx, dy);
176 cairo_text_path(cr, buffer);
177 set_source_rgb(gc, 0, 0, 0);
181 cairo_rel_move_to(cr, dx, dy);
183 set_source_rgb(gc, tro->r, tro->g, tro->b);
184 cairo_show_text(cr, buffer);
191 static struct ev_select *ev_namelist;
192 static int evn_allocated;
195 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
199 for (i = 0; i < evn_used; i++) {
200 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
204 void remember_event(const char *eventname)
208 if (!eventname || (len = strlen(eventname)) == 0)
210 while (i < evn_used) {
211 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
215 if (evn_used == evn_allocated) {
217 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
219 /* we are screwed, but let's just bail out */
222 ev_namelist[evn_used].ev_name = strdup(eventname);
223 ev_namelist[evn_used].plot_ev = TRUE;
227 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
232 /* is plotting this event disabled? */
234 for (i = 0; i < evn_used; i++) {
235 if (! strcmp(event->name, ev_namelist[i].ev_name)) {
236 if (ev_namelist[i].plot_ev)
243 for (i = 0; i < pi->nr; i++) {
244 struct plot_data *data = pi->entry + i;
245 if (event->time.seconds < data->sec)
249 /* draw a little tirangular marker and attach tooltip */
250 x = SCALEX(gc, event->time.seconds);
251 y = SCALEY(gc, depth);
252 set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
253 cairo_move_to(gc->cr, x-15, y+6);
254 cairo_line_to(gc->cr, x-3 , y+6);
255 cairo_line_to(gc->cr, x-9, y-6);
256 cairo_line_to(gc->cr, x-15, y+6);
257 cairo_stroke_preserve(gc->cr);
259 set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
260 cairo_move_to(gc->cr, x-9, y-3);
261 cairo_line_to(gc->cr, x-9, y+1);
262 cairo_move_to(gc->cr, x-9, y+4);
263 cairo_line_to(gc->cr, x-9, y+4);
264 cairo_stroke(gc->cr);
265 attach_tooltip(x-15, y-6, 12, 12, event->name);
268 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
270 static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
271 struct event *event = dive->events;
277 plot_one_event(gc, pi, event, &tro);
282 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
284 int sec = entry->sec, decimals;
287 d = get_depth_units(entry->depth, &decimals, NULL);
289 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
292 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
294 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
295 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
299 for (i = 0; i < pi->nr; i++) {
300 struct plot_data *entry = pi->entry + i;
302 if (entry->depth < 2000)
305 if ((entry == entry->max[2]) && entry->depth != last) {
306 render_depth_sample(gc, entry, &deep);
310 if ((entry == entry->min[2]) && entry->depth != last) {
311 render_depth_sample(gc, entry, &shallow);
315 if (entry->depth != last)
320 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
322 int maxtime, maxdepth;
324 /* Get plot scaling limits */
325 maxtime = get_maxtime(pi);
326 maxdepth = get_maxdepth(pi);
328 gc->leftx = 0; gc->rightx = maxtime;
329 gc->topy = 0; gc->bottomy = maxdepth;
331 plot_text_samples(gc, pi);
334 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
337 struct plot_data *entry = pi->entry;
339 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
340 move_to(gc, entry->sec, entry->smoothed);
341 for (i = 1; i < pi->nr; i++) {
343 line_to(gc, entry->sec, entry->smoothed);
345 cairo_stroke(gc->cr);
348 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
352 struct plot_data *entry = pi->entry;
354 set_source_rgba(gc, 1, 0.2, 1, a);
355 move_to(gc, entry->sec, entry->min[index]->depth);
356 for (i = 1; i < pi->nr; i++) {
358 line_to(gc, entry->sec, entry->min[index]->depth);
360 for (i = 1; i < pi->nr; i++) {
361 line_to(gc, entry->sec, entry->max[index]->depth);
364 cairo_close_path(gc->cr);
368 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
372 plot_minmax_profile_minute(gc, pi, 2, 0.1);
373 plot_minmax_profile_minute(gc, pi, 1, 0.1);
374 plot_minmax_profile_minute(gc, pi, 0, 0.1);
377 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
380 cairo_t *cr = gc->cr;
382 struct plot_data *entry;
383 int maxtime, maxdepth, marker;
384 int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
386 /* Get plot scaling limits */
387 maxtime = get_maxtime(pi);
388 maxdepth = get_maxdepth(pi);
390 /* Time markers: at most every 5 min, but no more than 12 markers
391 * and for convenience we do 5, 10, 15 or 30 min intervals.
392 * This allows for 6h dives - enough (I hope) for even the craziest
393 * divers - but just in case, for those 8h depth-record-breaking dives,
394 * we double the interval if this still doesn't get us to 12 or fewer
397 while (maxtime / increments[i] > 12 && i < 4)
399 incr = increments[i];
400 while (maxtime / incr > 12)
403 gc->leftx = 0; gc->rightx = maxtime;
404 gc->topy = 0; gc->bottomy = 1.0;
405 set_source_rgba(gc, 1, 1, 1, 0.5);
406 for (i = incr; i < maxtime; i += incr) {
412 /* now the text on every second time marker */
413 text_render_options_t tro = {10, 0.2, 1.0, 0.2, CENTER, TOP};
414 for (i = incr; i < maxtime; i += 2 * incr)
415 plot_text(gc, &tro, i, 1, "%d", i/60);
417 /* Depth markers: every 30 ft or 10 m*/
418 gc->leftx = 0; gc->rightx = 1.0;
419 gc->topy = 0; gc->bottomy = maxdepth;
420 switch (output_units.length) {
421 case METERS: marker = 10000; break;
422 case FEET: marker = 9144; break; /* 30 ft */
425 set_source_rgba(gc, 1, 1, 1, 0.5);
426 for (i = marker; i < maxdepth; i += marker) {
432 /* Show mean depth */
434 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
435 move_to(gc, 0, pi->meandepth);
436 line_to(gc, 1, pi->meandepth);
440 gc->leftx = 0; gc->rightx = maxtime;
443 * These are good for debugging text placement etc,
444 * but not for actual display..
447 plot_smoothed_profile(gc, pi);
448 plot_minmax_profile(gc, pi);
451 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
453 /* Do the depth profile for the neat fill */
454 gc->topy = 0; gc->bottomy = maxdepth;
455 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
459 for (i = 0; i < pi->nr; i++, entry++)
460 line_to(gc, entry->sec, entry->depth);
461 cairo_close_path(gc->cr);
463 set_source_rgba(gc, 1, 1, 1, 0.2);
464 cairo_fill_preserve(cr);
465 set_source_rgb(gc, 1, 1, 1);
471 /* Now do it again for the velocity colors */
473 for (i = 1; i < pi->nr; i++) {
476 /* we want to draw the segments in different colors
477 * representing the vertical velocity, so we need to
478 * chop this into short segments */
479 rgb_t color = rgb[entry->velocity];
480 depth = entry->depth;
481 set_source_rgb(gc, color.r, color.g, color.b);
482 move_to(gc, entry[-1].sec, entry[-1].depth);
483 line_to(gc, sec, depth);
488 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
490 int maxtime, mintemp, maxtemp, delta;
492 /* Get plot scaling limits */
493 maxtime = get_maxtime(pi);
494 mintemp = pi->mintemp;
495 maxtemp = pi->maxtemp;
497 gc->leftx = 0; gc->rightx = maxtime;
498 /* Show temperatures in roughly the lower third, but make sure the scale
499 is at least somewhat reasonable */
500 delta = maxtemp - mintemp;
501 if (delta > 3000) { /* more than 3K in fluctuation */
502 gc->topy = maxtemp + delta*2;
503 gc->bottomy = mintemp - delta/2;
505 gc->topy = maxtemp + 1500 + delta*2;
506 gc->bottomy = mintemp - delta/2;
509 return maxtemp > mintemp;
512 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
516 static const text_render_options_t tro = {12, 0.6, 0.6, 1.0, LEFT, TOP};
518 deg = get_temp_units(mkelvin, &unit);
520 plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
523 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
526 int last = -300, sec = 0;
527 int last_temperature = 0, last_printed_temp = 0;
529 if (!setup_temperature_limits(gc, pi))
532 for (i = 0; i < pi->nr; i++) {
533 struct plot_data *entry = pi->entry+i;
534 int mkelvin = entry->temperature;
538 last_temperature = mkelvin;
540 /* don't print a temperature
541 * if it's been less than 5min and less than a 2K change OR
542 * if it's been less than 2min OR if the change from the
543 * last print is less than .4K (and therefore less than 1F */
544 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
545 (sec < last + 120) ||
546 (abs(mkelvin - last_printed_temp) < 400))
549 plot_single_temp_text(gc,sec,mkelvin);
550 last_printed_temp = mkelvin;
552 /* it would be nice to print the end temperature, if it's
553 * different or if the last temperature print has been more
554 * than a quarter of the dive back */
555 if ((abs(last_temperature - last_printed_temp) > 500) ||
556 ((double)last / (double)sec < 0.75))
557 plot_single_temp_text(gc, sec, last_temperature);
560 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
563 cairo_t *cr = gc->cr;
566 if (!setup_temperature_limits(gc, pi))
569 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
570 for (i = 0; i < pi->nr; i++) {
571 struct plot_data *entry = pi->entry + i;
572 int mkelvin = entry->temperature;
573 int sec = entry->sec;
580 line_to(gc, sec, mkelvin);
582 move_to(gc, sec, mkelvin);
588 /* gets both the actual start and end pressure as well as the scaling factors */
589 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
592 gc->rightx = get_maxtime(pi);
594 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
595 return pi->maxpressure != 0;
598 /* set the color for the pressure plot according to temporary sac rate */
599 static void set_sac_color(struct graphics_context *gc, double sac)
602 set_source_rgba(gc, 0.0, 0.4, 0.2, 0.80);
604 set_source_rgba(gc, 0.2, 0.6, 0.2, 0.80);
606 set_source_rgba(gc, 0.4, 0.8, 0.2, 0.80);
608 set_source_rgba(gc, 0.6, 0.8, 0.2, 0.80);
610 set_source_rgba(gc, 0.8, 0.8, 0.2, 0.80);
612 set_source_rgba(gc, 0.8, 0.6, 0.2, 0.80);
614 set_source_rgba(gc, 0.8, 0.4, 0.2, 0.80);
616 set_source_rgba(gc, 0.9, 0.3, 0.2, 0.80);
618 set_source_rgba(gc, 1.0, 0.2, 0.2, 0.80);
621 /* calculate the current SAC in l/min */
622 #define GET_LOCAL_SAC(_entry1, _entry2, _dive) \
623 ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) * \
624 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter / \
625 (((_entry2)->sec - (_entry1)->sec) / 60.0) / \
626 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) / \
629 #define SAC_WINDOW 45 /* sliding window in seconds for current SAC calculation */
631 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
636 int lift_pen = FALSE;
637 int first_plot = TRUE;
639 struct plot_data *last_entry = NULL;
641 if (!get_cylinder_pressure_range(gc, pi))
644 for (i = 0; i < pi->nr; i++) {
646 struct plot_data *entry = pi->entry + i;
648 mbar = GET_PRESSURE(entry);
649 if (!entry->same_cylinder) {
661 /* don't start with a sac of 0 */
663 struct plot_data *future_entry = pi->entry + fe;
664 while (fe < pi->nr && future_entry->sec - entry->sec < SAC_WINDOW) {
666 future_entry = pi->entry + fe;
668 sac = GET_LOCAL_SAC(entry, future_entry, dive);
670 } else if (entry->sec - last_entry->sec >= SAC_WINDOW) {
671 sac = GET_LOCAL_SAC(last_entry, entry, dive);
673 last_entry = pi->entry + last;
675 set_sac_color(gc, sac);
677 if (!first_plot && entry->same_cylinder) {
678 /* if we have a previous event from the same tank,
679 * draw at least a short line */
681 prev_pr = GET_PRESSURE(entry - 1);
682 move_to(gc, (entry-1)->sec, prev_pr);
683 line_to(gc, entry->sec, mbar);
686 move_to(gc, entry->sec, mbar);
690 line_to(gc, entry->sec, mbar);
692 cairo_stroke(gc->cr);
693 move_to(gc, entry->sec, mbar);
697 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
698 int xalign, int yalign)
703 pressure = get_pressure_units(mbar, &unit);
704 text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
705 plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
708 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
712 int seen_cyl[MAX_CYLINDERS] = { FALSE, };
713 int last_pressure[MAX_CYLINDERS] = { 0, };
714 int last_time[MAX_CYLINDERS] = { 0, };
715 struct plot_data *entry;
717 if (!get_cylinder_pressure_range(gc, pi))
720 /* only loop over the actual events from the dive computer
721 * plus the second synthetic event at the start (to make sure
722 * we get "time=0" right)
723 * sadly with a recent change that first entry may no longer
724 * have any pressure reading - in that case just grab the
725 * pressure from the second entry */
726 if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
727 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
728 for (i = 1; i < pi->nr; i++) {
729 entry = pi->entry + i;
731 if (!entry->same_cylinder) {
732 cyl = entry->cylinderindex;
733 if (!seen_cyl[cyl]) {
734 mbar = GET_PRESSURE(entry);
735 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
736 seen_cyl[cyl] = TRUE;
739 /* remember the last pressure and time of
740 * the previous cylinder */
741 cyl = (entry - 1)->cylinderindex;
742 last_pressure[cyl] = GET_PRESSURE(entry - 1);
743 last_time[cyl] = (entry - 1)->sec;
747 cyl = entry->cylinderindex;
748 if (GET_PRESSURE(entry))
749 last_pressure[cyl] = GET_PRESSURE(entry);
750 last_time[cyl] = entry->sec;
752 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
753 if (last_time[cyl]) {
754 plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
759 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
761 struct plot_data *p = entry;
762 int time = entry->sec;
763 int seconds = 90*(index+1);
764 struct plot_data *min, *max;
767 /* Go back 'seconds' in time */
769 if (p[-1].sec < time - seconds)
774 /* Then go forward until we hit an entry past the time */
779 int depth = p->depth;
780 if (p->sec > time + seconds)
784 if (depth < min->depth)
786 if (depth > max->depth)
789 entry->min[index] = min;
790 entry->max[index] = max;
791 entry->avg[index] = (avg + nr/2) / nr;
794 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
796 analyze_plot_info_minmax_minute(entry, first, last, 0);
797 analyze_plot_info_minmax_minute(entry, first, last, 1);
798 analyze_plot_info_minmax_minute(entry, first, last, 2);
801 static velocity_t velocity(int speed)
805 if (speed < -304) /* ascent faster than -60ft/min */
807 else if (speed < -152) /* above -30ft/min */
809 else if (speed < -76) /* -15ft/min */
811 else if (speed < -25) /* -5ft/min */
813 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
814 for descent are usually about 2x ascent rate; still, we want
815 stable to mean stable */
817 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
819 else if (speed < 304) /* up to 60ft/min is moderate */
821 else if (speed < 507) /* up to 100ft/min is fast */
823 else /* more than that is just crazy - you'll blow your ears out */
828 static struct plot_info *analyze_plot_info(struct plot_info *pi)
833 /* Do pressure min/max based on the non-surface data */
834 for (i = 0; i < nr; i++) {
835 struct plot_data *entry = pi->entry+i;
836 int pressure = GET_PRESSURE(entry);
837 int temperature = entry->temperature;
840 if (pressure > pi->maxpressure)
841 pi->maxpressure = pressure;
845 if (!pi->mintemp || temperature < pi->mintemp)
846 pi->mintemp = temperature;
847 if (temperature > pi->maxtemp)
848 pi->maxtemp = temperature;
852 /* Smoothing function: 5-point triangular smooth */
853 for (i = 2; i < nr; i++) {
854 struct plot_data *entry = pi->entry+i;
858 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
859 entry->smoothed = (depth+4) / 9;
861 /* vertical velocity in mm/sec */
862 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
863 if (entry[0].sec - entry[-1].sec) {
864 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
865 /* if our samples are short and we aren't too FAST*/
866 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
868 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
870 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
871 (entry[0].sec - entry[past].sec));
874 entry->velocity = STABLE;
877 /* One-, two- and three-minute minmax data */
878 for (i = 0; i < nr; i++) {
879 struct plot_data *entry = pi->entry +i;
880 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
887 * simple structure to track the beginning and end tank pressure as
888 * well as the integral of depth over time spent while we have no
889 * pressure reading from the tank */
890 typedef struct pr_track_struct pr_track_t;
891 struct pr_track_struct {
896 double pressure_time;
900 static pr_track_t *pr_track_alloc(int start, int t_start) {
901 pr_track_t *pt = malloc(sizeof(pr_track_t));
903 pt->t_start = t_start;
906 pt->pressure_time = 0.0;
911 /* poor man's linked list */
912 static pr_track_t *list_last(pr_track_t *list)
914 pr_track_t *tail = list;
923 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
925 pr_track_t *tail = list_last(list);
928 tail->next = element;
932 static void list_free(pr_track_t *list)
936 list_free(list->next);
940 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
941 pr_track_t **track_pr)
943 pr_track_t *list = NULL;
944 pr_track_t *nlist = NULL;
947 struct plot_data *entry;
948 int cur_pr[MAX_CYLINDERS];
950 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
951 cur_pr[cyl] = track_pr[cyl]->start;
954 /* The first two are "fillers", but in case we don't have a sample
955 * at time 0 we need to process the second of them here */
956 for (i = 1; i < pi->nr; i++) {
957 entry = pi->entry + i;
958 if (SENSOR_PRESSURE(entry)) {
959 cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
961 if(!list || list->t_end < entry->sec) {
962 nlist = track_pr[entry->cylinderindex];
964 while (nlist && nlist->t_start <= entry->sec) {
968 /* there may be multiple segments - so
969 * let's assemble the length */
971 pt = list->pressure_time;
972 while (!nlist->end) {
975 /* oops - we have no end pressure,
976 * so this means this is a tank without
977 * gas consumption information */
980 pt += nlist->pressure_time;
983 /* just continue without calculating
984 * interpolated values */
985 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
989 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt; }
991 double cur_pt = (entry->sec - (entry-1)->sec) *
992 (1 + entry->depth / 10000.0);
993 INTERPOLATED_PRESSURE(entry) =
994 cur_pr[entry->cylinderindex] + cur_pt * magic;
995 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
997 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1002 static int get_cylinder_index(struct dive *dive, struct event *ev)
1007 * Try to find a cylinder that matches the O2 percentage
1008 * in the gas change event 'value' field.
1010 * Crazy suunto gas change events. We really should do
1011 * this in libdivecomputer or something.
1013 for (i = 0; i < MAX_CYLINDERS; i++) {
1014 cylinder_t *cyl = dive->cylinder+i;
1015 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1016 if (o2 == ev->value)
1023 static struct event *get_next_gaschange(struct event *event)
1026 if (!strcmp(event->name, "gaschange"))
1028 event = event->next;
1033 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1035 while (i < pi->nr) {
1036 struct plot_data *entry = pi->entry+i;
1037 if (entry->sec > end)
1039 if (entry->cylinderindex != cylinderindex) {
1040 entry->cylinderindex = cylinderindex;
1041 entry->pressure[0] = 0;
1048 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1050 int i = 0, cylinderindex = 0;
1051 struct event *ev = get_next_gaschange(dive->events);
1057 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1058 cylinderindex = get_cylinder_index(dive, ev);
1059 ev = get_next_gaschange(ev->next);
1061 set_cylinder_index(pi, i, cylinderindex, ~0u);
1064 /* for computers that track gas changes through events */
1065 static int count_gas_change_events(struct dive *dive)
1068 struct event *ev = get_next_gaschange(dive->events);
1072 ev = get_next_gaschange(ev->next);
1078 * Create a plot-info with smoothing and ranged min/max
1080 * This also makes sure that we have extra empty events on both
1081 * sides, so that you can do end-points without having to worry
1084 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1086 int cylinderindex = -1;
1087 int lastdepth, lastindex;
1088 int i, pi_idx, nr, sec, cyl;
1090 struct plot_info *pi;
1091 pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1092 pr_track_t *pr_track, *current;
1093 gboolean missing_pr = FALSE;
1094 struct plot_data *entry = NULL;
1097 /* we want to potentially add synthetic plot_info elements for the gas changes */
1098 nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1099 alloc_size = plot_info_size(nr);
1100 pi = malloc(alloc_size);
1103 memset(pi, 0, alloc_size);
1105 pi_idx = 2; /* the two extra events at the start */
1106 /* check for gas changes before the samples start */
1107 ev = get_next_gaschange(dive->events);
1108 while (ev && ev->time.seconds < dive_sample->time.seconds) {
1109 entry = pi->entry + pi_idx;
1110 entry->sec = ev->time.seconds;
1111 entry->depth = 0; /* is that always correct ? */
1113 ev = get_next_gaschange(ev->next);
1115 if (ev && ev->time.seconds == dive_sample->time.seconds) {
1116 /* we already have a sample at the time of the event */
1117 ev = get_next_gaschange(ev->next);
1122 for (i = 0; i < nr_samples; i++) {
1125 struct sample *sample = dive_sample+i;
1127 entry = pi->entry + i + pi_idx;
1128 while (ev && ev->time.seconds < sample->time.seconds) {
1129 /* insert two fake plot info structures for the end of
1130 * the old tank and the start of the new tank */
1131 entry->sec = ev->time.seconds;
1132 (entry+1)->sec = ev->time.seconds + 1;
1133 /* we need a fake depth - let's interpolate */
1135 entry->depth = sample->depth.mm -
1136 (sample->depth.mm - (sample-1)->depth.mm) / 2;
1138 entry->depth = sample->depth.mm;
1139 (entry+1)->depth = entry->depth;
1141 entry = pi->entry + i + pi_idx;
1142 ev = get_next_gaschange(ev->next);
1144 if (ev && ev->time.seconds == sample->time.seconds) {
1145 /* we already have a sample at the time of the event
1146 * just add a new one for the old tank and delay the
1147 * real even by one second (to keep time monotonous) */
1148 entry->sec = ev->time.seconds;
1149 entry->depth = sample->depth.mm;
1151 entry = pi->entry + i + pi_idx;
1152 ev = get_next_gaschange(ev->next);
1155 sec = entry->sec = sample->time.seconds + delay;
1156 depth = entry->depth = sample->depth.mm;
1157 entry->cylinderindex = sample->cylinderindex;
1158 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1159 entry->temperature = sample->temperature.mkelvin;
1161 if (depth || lastdepth)
1162 lastindex = i + pi_idx;
1165 if (depth > pi->maxdepth)
1166 pi->maxdepth = depth;
1168 entry = pi->entry + i + pi_idx;
1169 /* are there still unprocessed gas changes? that would be very strange */
1171 entry->sec = ev->time.seconds;
1172 entry->depth = 0; /* why are there gas changes after the dive is over? */
1174 entry = pi->entry + i + pi_idx;
1175 ev = get_next_gaschange(ev->next);
1177 nr = nr_samples + pi_idx - 2;
1178 check_gas_change_events(dive, pi);
1180 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1181 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1182 current = track_pr[pi->entry[2].cylinderindex];
1183 for (i = 0; i < nr + 1; i++) {
1184 entry = pi->entry + i + 1;
1186 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1187 cylinderindex = entry->cylinderindex;
1189 /* track the segments per cylinder and their pressure/time integral */
1190 if (!entry->same_cylinder) {
1191 current->end = SENSOR_PRESSURE(entry-1);
1192 current->t_end = (entry-1)->sec;
1193 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1194 track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1195 } else { /* same cylinder */
1196 if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1197 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1198 /* transmitter changed its working status */
1199 current->end = SENSOR_PRESSURE(entry-1);
1200 current->t_end = (entry-1)->sec;
1201 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1202 track_pr[cylinderindex] =
1203 list_add(track_pr[cylinderindex], current);
1206 /* finally, do the discrete integration to get the SAC rate equivalent */
1207 current->pressure_time += (entry->sec - (entry-1)->sec) *
1208 (1 + entry->depth / 10000.0);
1209 missing_pr |= !SENSOR_PRESSURE(entry);
1213 current->t_end = entry->sec;
1215 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1216 int pr = dive->cylinder[cyl].end.mbar;
1217 if (pr && track_pr[cyl]) {
1218 pr_track = list_last(track_pr[cyl]);
1222 /* Fill in the last two entries with empty values but valid times
1223 * without creating a false cylinder change event */
1225 pi->entry[i].sec = sec + 20;
1226 pi->entry[i].same_cylinder = 1;
1227 pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1228 INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1229 pi->entry[i+1].sec = sec + 40;
1230 pi->entry[i+1].same_cylinder = 1;
1231 pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1232 INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1233 /* the number of actual entries - some computers have lots of
1234 * depth 0 samples at the end of a dive, we want to make sure
1235 * we have exactly one of them at the end */
1236 pi->nr = lastindex+1;
1237 while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1239 pi->maxtime = pi->entry[lastindex].sec;
1241 /* Analyze_plot_info() will do the sample max pressures,
1242 * this handles the manual pressures
1244 pi->maxpressure = 0;
1245 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1246 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1247 if (mbar > pi->maxpressure)
1248 pi->maxpressure = mbar;
1251 pi->meandepth = dive->meandepth.mm;
1254 fill_missing_tank_pressures(dive, pi, track_pr);
1256 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1257 list_free(track_pr[cyl]);
1258 if (0) /* awesome for debugging - not useful otherwise */
1260 return analyze_plot_info(pi);
1263 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1265 struct plot_info *pi;
1266 static struct sample fake[4];
1267 struct sample *sample = dive->sample;
1268 int nr = dive->samples;
1271 int duration = dive->duration.seconds;
1272 int maxdepth = dive->maxdepth.mm;
1274 fake[1].time.seconds = duration * 0.05;
1275 fake[1].depth.mm = maxdepth;
1276 fake[2].time.seconds = duration * 0.95;
1277 fake[2].depth.mm = maxdepth;
1278 fake[3].time.seconds = duration * 1.00;
1282 pi = create_plot_info(dive, nr, sample);
1284 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1285 cairo_set_line_width(gc->cr, 2);
1286 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1287 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1290 * We can use "cairo_translate()" because that doesn't
1291 * scale line width etc. But the actual scaling we need
1292 * do set up ourselves..
1294 * Snif. What a pity.
1296 gc->maxx = (drawing_area->width - 2*drawing_area->x);
1297 gc->maxy = (drawing_area->height - 2*drawing_area->y);
1299 /* Temperature profile */
1300 plot_temperature_profile(gc, pi);
1302 /* Cylinder pressure plot */
1303 plot_cylinder_pressure(gc, pi, dive);
1306 plot_depth_profile(gc, pi);
1307 plot_events(gc, pi, dive);
1309 /* Text on top of all graphs.. */
1310 plot_temperature_text(gc, pi);
1311 plot_depth_text(gc, pi);
1312 plot_cylinder_pressure_text(gc, pi);
1314 /* Bounding box last */
1315 gc->leftx = 0; gc->rightx = 1.0;
1316 gc->topy = 0; gc->bottomy = 1.0;
1318 set_source_rgb(gc, 1, 1, 1);
1323 cairo_close_path(gc->cr);
1324 cairo_stroke(gc->cr);