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;
24 int minpressure, maxpressure;
25 int endpressure; /* start pressure better be max pressure */
28 unsigned int same_cylinder:1;
29 unsigned int cylinderindex;
31 /* pressure[0] is sensor pressure
32 * pressure[1] is interpolated pressure */
39 struct plot_data *min[3];
40 struct plot_data *max[3];
45 #define INTERPOLATED_PR 1
46 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
47 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
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))
100 * When showing dive profiles, we scale things to the
101 * current dive. However, we don't scale past less than
102 * 30 minutes or 90 ft, just so that small dives show
104 * we also need to add 180 seconds at the end so the min/max
107 static int get_maxtime(struct plot_info *pi)
109 int seconds = pi->maxtime;
110 /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
111 return MAX(30*60, ROUND_UP(seconds+150, 60*5));
114 static int get_maxdepth(struct plot_info *pi)
116 unsigned mm = pi->maxdepth;
117 /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
118 return MAX(30000, ROUND_UP(mm+3000, 10000));
125 } text_render_options_t;
128 #define CENTER (-0.5)
135 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
136 double x, double y, const char *fmt, ...)
138 cairo_t *cr = gc->cr;
139 cairo_font_extents_t fe;
140 cairo_text_extents_t extents;
146 vsnprintf(buffer, sizeof(buffer), fmt, args);
149 cairo_set_font_size(cr, tro->size);
150 cairo_font_extents(cr, &fe);
151 cairo_text_extents(cr, buffer, &extents);
152 dx = tro->hpos * extents.width + extents.x_bearing;
153 dy = tro->vpos * extents.height + fe.descent;
156 cairo_rel_move_to(cr, dx, dy);
158 cairo_text_path(cr, buffer);
159 set_source_rgb(gc, 0, 0, 0);
163 cairo_rel_move_to(cr, dx, dy);
165 set_source_rgb(gc, tro->r, tro->g, tro->b);
166 cairo_show_text(cr, buffer);
169 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
174 for (i = 0; i < pi->nr; i++) {
175 struct plot_data *data = pi->entry + i;
176 if (event->time.seconds < data->sec)
180 /* draw a little tirangular marker and attach tooltip */
181 x = SCALEX(gc, event->time.seconds);
182 y = SCALEY(gc, depth);
183 set_source_rgba(gc, 1.0, 1.0, 0.1, 0.8);
184 cairo_move_to(gc->cr, x-15, y+6);
185 cairo_line_to(gc->cr, x-3 , y+6);
186 cairo_line_to(gc->cr, x-9, y-6);
187 cairo_line_to(gc->cr, x-15, y+6);
188 cairo_stroke_preserve(gc->cr);
190 set_source_rgba(gc, 0.0, 0.0, 0.0, 0.8);
191 cairo_move_to(gc->cr, x-9, y-3);
192 cairo_line_to(gc->cr, x-9, y+1);
193 cairo_move_to(gc->cr, x-9, y+4);
194 cairo_line_to(gc->cr, x-9, y+4);
195 cairo_stroke(gc->cr);
196 attach_tooltip(x-15, y-6, 12, 12, event->name);
199 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
201 static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
202 struct event *event = dive->events;
208 plot_one_event(gc, pi, event, &tro);
213 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
215 int sec = entry->sec, decimals;
218 d = get_depth_units(entry->depth, &decimals, NULL);
220 plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
223 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
225 static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
226 static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
229 for (i = 0; i < pi->nr; i++) {
230 struct plot_data *entry = pi->entry + i;
232 if (entry->depth < 2000)
235 if (entry == entry->max[2])
236 render_depth_sample(gc, entry, &deep);
238 if (entry == entry->min[2])
239 render_depth_sample(gc, entry, &shallow);
243 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
245 int maxtime, maxdepth;
247 /* Get plot scaling limits */
248 maxtime = get_maxtime(pi);
249 maxdepth = get_maxdepth(pi);
251 gc->leftx = 0; gc->rightx = maxtime;
252 gc->topy = 0; gc->bottomy = maxdepth;
254 plot_text_samples(gc, pi);
257 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
260 struct plot_data *entry = pi->entry;
262 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
263 move_to(gc, entry->sec, entry->smoothed);
264 for (i = 1; i < pi->nr; i++) {
266 line_to(gc, entry->sec, entry->smoothed);
268 cairo_stroke(gc->cr);
271 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
275 struct plot_data *entry = pi->entry;
277 set_source_rgba(gc, 1, 0.2, 1, a);
278 move_to(gc, entry->sec, entry->min[index]->depth);
279 for (i = 1; i < pi->nr; i++) {
281 line_to(gc, entry->sec, entry->min[index]->depth);
283 for (i = 1; i < pi->nr; i++) {
284 line_to(gc, entry->sec, entry->max[index]->depth);
287 cairo_close_path(gc->cr);
291 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
295 plot_minmax_profile_minute(gc, pi, 2, 0.1);
296 plot_minmax_profile_minute(gc, pi, 1, 0.1);
297 plot_minmax_profile_minute(gc, pi, 0, 0.1);
300 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
303 cairo_t *cr = gc->cr;
305 struct plot_data *entry;
306 int maxtime, maxdepth, marker;
308 /* Get plot scaling limits */
309 maxtime = get_maxtime(pi);
310 maxdepth = get_maxdepth(pi);
312 /* Time markers: every 5 min */
313 gc->leftx = 0; gc->rightx = maxtime;
314 gc->topy = 0; gc->bottomy = 1.0;
315 for (i = 5*60; i < maxtime; i += 5*60) {
320 /* Depth markers: every 30 ft or 10 m*/
321 gc->leftx = 0; gc->rightx = 1.0;
322 gc->topy = 0; gc->bottomy = maxdepth;
323 switch (output_units.length) {
324 case METERS: marker = 10000; break;
325 case FEET: marker = 9144; break; /* 30 ft */
328 set_source_rgba(gc, 1, 1, 1, 0.5);
329 for (i = marker; i < maxdepth; i += marker) {
335 /* Show mean depth */
337 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
338 move_to(gc, 0, pi->meandepth);
339 line_to(gc, 1, pi->meandepth);
343 gc->leftx = 0; gc->rightx = maxtime;
346 * These are good for debugging text placement etc,
347 * but not for actual display..
350 plot_smoothed_profile(gc, pi);
351 plot_minmax_profile(gc, pi);
354 set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
356 /* Do the depth profile for the neat fill */
357 gc->topy = 0; gc->bottomy = maxdepth;
358 set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
362 for (i = 0; i < pi->nr; i++, entry++)
363 line_to(gc, entry->sec, entry->depth);
364 cairo_close_path(gc->cr);
366 set_source_rgba(gc, 1, 1, 1, 0.2);
367 cairo_fill_preserve(cr);
368 set_source_rgb(gc, 1, 1, 1);
374 /* Now do it again for the velocity colors */
376 for (i = 1; i < pi->nr; i++) {
379 /* we want to draw the segments in different colors
380 * representing the vertical velocity, so we need to
381 * chop this into short segments */
382 rgb_t color = rgb[entry->velocity];
383 depth = entry->depth;
384 set_source_rgb(gc, color.r, color.g, color.b);
385 move_to(gc, entry[-1].sec, entry[-1].depth);
386 line_to(gc, sec, depth);
391 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
393 int maxtime, mintemp, maxtemp, delta;
395 /* Get plot scaling limits */
396 maxtime = get_maxtime(pi);
397 mintemp = pi->mintemp;
398 maxtemp = pi->maxtemp;
400 gc->leftx = 0; gc->rightx = maxtime;
401 /* Show temperatures in roughly the lower third, but make sure the scale
402 is at least somewhat reasonable */
403 delta = maxtemp - mintemp;
404 if (delta > 3000) { /* more than 3K in fluctuation */
405 gc->topy = maxtemp + delta*2;
406 gc->bottomy = mintemp - delta/2;
408 gc->topy = maxtemp + 1500 + delta*2;
409 gc->bottomy = mintemp - delta/2;
412 return maxtemp > mintemp;
415 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
419 static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
420 temperature_t temperature = { mkelvin };
422 if (output_units.temperature == FAHRENHEIT) {
423 deg = to_F(temperature);
424 unit = UTF8_DEGREE "F";
426 deg = to_C(temperature);
427 unit = UTF8_DEGREE "C";
429 plot_text(gc, &tro, sec, temperature.mkelvin, "%d%s", deg, unit);
432 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
435 int last = 0, sec = 0;
436 int last_temperature = 0, last_printed_temp = 0;
438 if (!setup_temperature_limits(gc, pi))
441 for (i = 0; i < pi->nr; i++) {
442 struct plot_data *entry = pi->entry+i;
443 int mkelvin = entry->temperature;
447 last_temperature = mkelvin;
449 if (sec < last + 300)
452 plot_single_temp_text(gc,sec,mkelvin);
453 last_printed_temp = mkelvin;
455 /* it would be nice to print the end temperature, if it's different */
456 if (abs(last_temperature - last_printed_temp) > 500)
457 plot_single_temp_text(gc, sec, last_temperature);
460 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
463 cairo_t *cr = gc->cr;
466 if (!setup_temperature_limits(gc, pi))
469 set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
470 for (i = 0; i < pi->nr; i++) {
471 struct plot_data *entry = pi->entry + i;
472 int mkelvin = entry->temperature;
473 int sec = entry->sec;
480 line_to(gc, sec, mkelvin);
482 move_to(gc, sec, mkelvin);
488 /* gets both the actual start and end pressure as well as the scaling factors */
489 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
492 gc->rightx = get_maxtime(pi);
494 gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
495 return pi->maxpressure != 0;
498 static void plot_pressure_helper(struct graphics_context *gc, struct plot_info *pi, int type)
501 int lift_pen = FALSE;
503 for (i = 0; i < pi->nr; i++) {
505 struct plot_data *entry = pi->entry + i;
507 mbar = entry->pressure[type];
508 if (!entry->same_cylinder)
515 if (i > 0 && entry->same_cylinder) {
516 /* if we have a previous event from the same tank,
517 * draw at least a short line .
518 * This uses the implementation detail that the
519 * type is either 0 or 1 */
521 prev_pr = (entry-1)->pressure[type] ? : (entry-1)->pressure[1 - type];
522 move_to(gc, (entry-1)->sec, prev_pr);
523 line_to(gc, entry->sec, mbar);
525 move_to(gc, entry->sec, mbar);
529 line_to(gc, entry->sec, mbar);
531 cairo_stroke(gc->cr);
535 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
537 if (!get_cylinder_pressure_range(gc, pi))
540 /* first plot the pressure readings we have from the dive computer */
541 set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
542 plot_pressure_helper(gc, pi, SENSOR_PR);
544 /* then, in a different color, the interpolated values */
545 set_source_rgba(gc, 1.0, 1.0, 0.2, 0.80);
546 plot_pressure_helper(gc, pi, INTERPOLATED_PR);
549 static int mbar_to_PSI(int mbar)
551 pressure_t p = {mbar};
555 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
556 int xalign, int yalign)
561 switch (output_units.pressure) {
563 pressure = mbar * 100;
567 pressure = (mbar + 500) / 1000;
571 pressure = mbar_to_PSI(mbar);
575 text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign};
576 plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
579 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
583 int seen_cyl[MAX_CYLINDERS] = { FALSE, };
584 int last_pressure[MAX_CYLINDERS] = { 0, };
585 int last_time[MAX_CYLINDERS] = { 0, };
586 struct plot_data *entry;
588 if (!get_cylinder_pressure_range(gc, pi))
591 /* only loop over the actual events from the dive computer */
592 for (i = 2; i < pi->nr - 2; i++) {
593 entry = pi->entry + i;
595 if (!entry->same_cylinder) {
596 cyl = entry->cylinderindex;
597 if (!seen_cyl[cyl]) {
598 mbar = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
599 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
600 seen_cyl[cyl] = TRUE;
603 /* remember the last pressure and time of
604 * the previous cylinder */
605 cyl = (entry - 1)->cylinderindex;
607 SENSOR_PRESSURE(entry - 1) ? : INTERPOLATED_PRESSURE(entry - 1);
608 last_time[cyl] = (entry - 1)->sec;
612 cyl = entry->cylinderindex;
613 last_pressure[cyl] = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
614 last_time[cyl] = entry->sec;
616 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
617 if (last_time[cyl]) {
618 plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
623 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
625 struct plot_data *p = entry;
626 int time = entry->sec;
627 int seconds = 90*(index+1);
628 struct plot_data *min, *max;
631 /* Go back 'seconds' in time */
633 if (p[-1].sec < time - seconds)
638 /* Then go forward until we hit an entry past the time */
643 int depth = p->depth;
644 if (p->sec > time + seconds)
648 if (depth < min->depth)
650 if (depth > max->depth)
653 entry->min[index] = min;
654 entry->max[index] = max;
655 entry->avg[index] = (avg + nr/2) / nr;
658 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
660 analyze_plot_info_minmax_minute(entry, first, last, 0);
661 analyze_plot_info_minmax_minute(entry, first, last, 1);
662 analyze_plot_info_minmax_minute(entry, first, last, 2);
665 static velocity_t velocity(int speed)
669 if (speed < -304) /* ascent faster than -60ft/min */
671 else if (speed < -152) /* above -30ft/min */
673 else if (speed < -76) /* -15ft/min */
675 else if (speed < -25) /* -5ft/min */
677 else if (speed < 25) /* very hard to find data, but it appears that the recommendations
678 for descent are usually about 2x ascent rate; still, we want
679 stable to mean stable */
681 else if (speed < 152) /* between 5 and 30ft/min is considered slow */
683 else if (speed < 304) /* up to 60ft/min is moderate */
685 else if (speed < 507) /* up to 100ft/min is fast */
687 else /* more than that is just crazy - you'll blow your ears out */
692 static struct plot_info *analyze_plot_info(struct plot_info *pi)
697 /* Do pressure min/max based on the non-surface data */
698 for (i = 0; i < nr; i++) {
699 struct plot_data *entry = pi->entry+i;
700 int pressure = SENSOR_PRESSURE(entry) ? : INTERPOLATED_PRESSURE(entry);
701 int temperature = entry->temperature;
704 if (!pi->minpressure || pressure < pi->minpressure)
705 pi->minpressure = pressure;
706 if (pressure > pi->maxpressure)
707 pi->maxpressure = pressure;
711 if (!pi->mintemp || temperature < pi->mintemp)
712 pi->mintemp = temperature;
713 if (temperature > pi->maxtemp)
714 pi->maxtemp = temperature;
718 /* Smoothing function: 5-point triangular smooth */
719 for (i = 2; i < nr; i++) {
720 struct plot_data *entry = pi->entry+i;
724 depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
725 entry->smoothed = (depth+4) / 9;
727 /* vertical velocity in mm/sec */
728 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
729 if (entry[0].sec - entry[-1].sec) {
730 entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
731 /* if our samples are short and we aren't too FAST*/
732 if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
734 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
736 entry->velocity = velocity((entry[0].depth - entry[past].depth) /
737 (entry[0].sec - entry[past].sec));
740 entry->velocity = STABLE;
743 /* One-, two- and three-minute minmax data */
744 for (i = 0; i < nr; i++) {
745 struct plot_data *entry = pi->entry +i;
746 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
753 * simple structure to track the beginning and end tank pressure as
754 * well as the integral of depth over time spent while we have no
755 * pressure reading from the tank */
756 typedef struct pr_track_struct pr_track_t;
757 struct pr_track_struct {
762 double pressure_time;
766 static pr_track_t *pr_track_alloc(int start, int t_start) {
767 pr_track_t *pt = malloc(sizeof(pr_track_t));
769 pt->t_start = t_start;
772 pt->pressure_time = 0.0;
777 /* poor man's linked list */
778 static pr_track_t *list_last(pr_track_t *list)
780 pr_track_t *tail = list;
789 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
791 pr_track_t *tail = list_last(list);
794 tail->next = element;
798 static void list_free(pr_track_t *list)
802 list_free(list->next);
806 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
807 pr_track_t **track_pr)
809 pr_track_t *list = NULL;
810 pr_track_t *nlist = NULL;
813 struct plot_data *entry;
814 int cur_pr[MAX_CYLINDERS];
816 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
817 cur_pr[cyl] = track_pr[cyl]->start;
819 for (i = 0; i < dive->samples; i++) {
820 entry = pi->entry + i + 2;
821 if (SENSOR_PRESSURE(entry)) {
822 cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
824 if(!list || list->t_end < entry->sec) {
825 nlist = track_pr[entry->cylinderindex];
827 while (nlist && nlist->t_start <= entry->sec) {
831 /* there may be multiple segments - so
832 * let's assemble the length */
834 pt = list->pressure_time;
835 while (!nlist->end) {
838 /* oops - we have no end pressure,
839 * so this means this is a tank without
840 * gas consumption information */
843 pt += nlist->pressure_time;
846 /* just continue without calculating
847 * interpolated values */
851 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt; }
853 double cur_pt = (entry->sec - (entry-1)->sec) *
854 (1 + entry->depth / 10000.0);
855 INTERPOLATED_PRESSURE(entry) =
856 cur_pr[entry->cylinderindex] + cur_pt * magic;
857 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
864 * Create a plot-info with smoothing and ranged min/max
866 * This also makes sure that we have extra empty events on both
867 * sides, so that you can do end-points without having to worry
870 static struct plot_info *create_plot_info(struct dive *dive)
872 int cylinderindex = -1;
873 int lastdepth, lastindex;
874 int i, nr = dive->samples + 4, sec, cyl;
875 size_t alloc_size = plot_info_size(nr);
876 struct plot_info *pi;
877 pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
878 pr_track_t *pr_track, *current;
879 gboolean missing_pr = FALSE;
880 struct plot_data *entry;
882 pi = malloc(alloc_size);
885 memset(pi, 0, alloc_size);
890 for (i = 0; i < dive->samples; i++) {
892 struct sample *sample = dive->sample+i;
894 entry = pi->entry + i + 2;
895 sec = entry->sec = sample->time.seconds;
896 depth = entry->depth = sample->depth.mm;
897 entry->cylinderindex = sample->cylinderindex;
898 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
899 entry->temperature = sample->temperature.mkelvin;
901 if (depth || lastdepth)
905 if (depth > pi->maxdepth)
906 pi->maxdepth = depth;
909 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
910 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
911 current = track_pr[dive->sample[0].cylinderindex];
912 for (i = 0; i < dive->samples; i++) {
913 entry = pi->entry + i + 2;
915 entry->same_cylinder = entry->cylinderindex == cylinderindex;
916 cylinderindex = entry->cylinderindex;
918 /* track the segments per cylinder and their pressure/time integral */
919 if (!entry->same_cylinder) {
920 current->end = SENSOR_PRESSURE(entry-1);
921 current->t_end = (entry-1)->sec;
922 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
923 track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
924 } else { /* same cylinder */
925 if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
926 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
927 /* transmitter changed its working status */
928 current->end = SENSOR_PRESSURE(entry-1);
929 current->t_end = (entry-1)->sec;
930 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
931 track_pr[cylinderindex] =
932 list_add(track_pr[cylinderindex], current);
935 /* finally, do the discrete integration to get the SAC rate equivalent */
936 current->pressure_time += (entry->sec - (entry-1)->sec) *
937 (1 + entry->depth / 10000.0);
938 missing_pr |= !SENSOR_PRESSURE(entry);
941 current->t_end = entry->sec;
942 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
943 int pr = dive->cylinder[cyl].end.mbar;
944 if (pr && track_pr[cyl]) {
945 pr_track = list_last(track_pr[cyl]);
951 /* Fill in the last two entries with empty values but valid times */
952 i = dive->samples + 2;
953 pi->entry[i].sec = sec + 20;
954 pi->entry[i+1].sec = sec + 40;
955 pi->nr = lastindex+1;
956 pi->maxtime = pi->entry[lastindex].sec;
958 pi->endpressure = pi->minpressure = dive->cylinder[0].end.mbar;
959 pi->maxpressure = dive->cylinder[0].start.mbar;
961 pi->meandepth = dive->meandepth.mm;
964 fill_missing_tank_pressures(dive, pi, track_pr);
966 for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
967 list_free(track_pr[cyl]);
968 return analyze_plot_info(pi);
971 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
973 struct plot_info *pi = create_plot_info(dive);
975 cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
976 cairo_set_line_width(gc->cr, 2);
977 cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
978 cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
981 * We can use "cairo_translate()" because that doesn't
982 * scale line width etc. But the actual scaling we need
983 * do set up ourselves..
987 gc->maxx = (drawing_area->width - 2*drawing_area->x);
988 gc->maxy = (drawing_area->height - 2*drawing_area->y);
990 /* Temperature profile */
991 plot_temperature_profile(gc, pi);
993 /* Cylinder pressure plot */
994 plot_cylinder_pressure(gc, pi);
997 plot_depth_profile(gc, pi);
998 plot_events(gc, pi, dive);
1000 /* Text on top of all graphs.. */
1001 plot_temperature_text(gc, pi);
1002 plot_depth_text(gc, pi);
1003 plot_cylinder_pressure_text(gc, pi);
1005 /* Bounding box last */
1006 gc->leftx = 0; gc->rightx = 1.0;
1007 gc->topy = 0; gc->bottomy = 1.0;
1009 set_source_rgb(gc, 1, 1, 1);
1014 cairo_close_path(gc->cr);
1015 cairo_stroke(gc->cr);