X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/ef4c26714c6cb2f82b5f681020213e4a68524f92..80471a6a905e00bf80ad04fa2061f88ea81f15cb:/src/viktrack.c diff --git a/src/viktrack.c b/src/viktrack.c index 006cd695..2607be14 100644 --- a/src/viktrack.c +++ b/src/viktrack.c @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -29,6 +28,7 @@ #include "vikcoord.h" #include "viktrack.h" #include "globals.h" +#include "dems.h" VikTrack *vik_track_new() { @@ -61,6 +61,17 @@ void vik_track_ref(VikTrack *tr) tr->ref_count++; } +void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog) +{ + /* Warning: does not check for existing dialog */ + tr->property_dialog = dialog; +} + +void vik_track_clear_property_dialog(VikTrack *tr) +{ + tr->property_dialog = NULL; +} + void vik_track_free(VikTrack *tr) { if ( tr->ref_count-- > 1 ) @@ -70,6 +81,8 @@ void vik_track_free(VikTrack *tr) g_free ( tr->comment ); g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); g_list_free( tr->trackpoints ); + if (tr->property_dialog) + gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) ); g_free ( tr ); } @@ -93,7 +106,11 @@ VikTrack *vik_track_copy ( const VikTrack *tr ) VikTrackpoint *vik_trackpoint_new() { - return g_malloc0(sizeof(VikTrackpoint)); + VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint)); + tp->extended = FALSE; + tp->speed = NAN; + tp->course = NAN; + return tp; } void vik_trackpoint_free(VikTrackpoint *tp) @@ -325,6 +342,9 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) GList *iter = tr->trackpoints; + if (!iter || !iter->next) /* zero- or one-point track */ + return NULL; + { /* test if there's anything worth calculating */ gboolean okay = FALSE; while ( iter ) @@ -338,7 +358,7 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) return NULL; } - + iter = tr->trackpoints; g_assert ( num_chunks < 16000 ); @@ -347,6 +367,10 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) total_length = vik_track_get_length_including_gaps ( tr ); chunk_length = total_length / num_chunks; + /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */ + if (chunk_length <= 0) + return NULL; + current_dist = 0.0; current_area_under_curve = 0; current_chunk = 0; @@ -409,6 +433,12 @@ gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) dist_along_seg = chunk_length - current_dist; if ( ignore_it || !iter->next ) { pts[current_chunk] = current_area_under_curve / current_dist; + if (!iter->next) { + int i; + for (i = current_chunk + 1; i < num_chunks; i++) + pts[i] = pts[current_chunk]; + break; + } } else { current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length); @@ -444,107 +474,15 @@ void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *up = *down = VIK_DEFAULT_ALTITUDE; } -typedef struct { - double a, b, c, d; -} spline_coeff_t; - -void compute_spline(int n, double *x, double *f, spline_coeff_t *p) -{ - double *h, *alpha, *B, *m; - int i; - int orig_n = n; - double new_x[3], new_f[3]; - - if (n==0) return; - if (n==1) { - new_x[0] = x[0]; - new_f[0] = f[0]; - new_x[1] = x[0]+0.00001; - new_f[1] = f[0]; - x = new_x; - f = new_f; - n = 3; - } - if (n==2) { - new_x[0] = x[0]; - new_f[0] = f[0]; - new_x[1] = x[1]; - new_f[1] = f[1]; - new_x[2] = x[1] + x[1]-x[0]; - new_f[2] = f[1] + f[1]-f[0]; - x = new_x; - f = new_f; - n = 3; - } - - /* we're solving a linear system of equations of the form Ax = B. - * The matrix a is tridiagonal and consists of coefficients in - * the h[] and alpha[] arrays. - */ - - h = (double *)malloc(sizeof(double) * (n-1)); - for (i=0; i=0; i--) { - m[i] = (B[i]-h[i+1]*m[i+1])/alpha[i]; - } - - for (i=0; itrackpoints ) return NULL; @@ -559,7 +497,7 @@ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks ) return NULL; if (duration < 0) { - fprintf(stderr, "negative duration: unsorted trackpoint timestamps?\n"); + g_warning("negative duration: unsorted trackpoint timestamps?"); return NULL; } pt_count = vik_track_get_tp_count(tr); @@ -569,7 +507,6 @@ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks ) s = g_malloc(sizeof(double) * pt_count); t = g_malloc(sizeof(double) * pt_count); - p = g_malloc(sizeof(spline_coeff_t) * (pt_count-1)); iter = tr->trackpoints->next; numpts = 0; @@ -583,80 +520,121 @@ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks ) iter = iter->next; } - compute_spline(numpts, t, s, p); - - /* - printf("Got spline\n"); - for (i=0; i t[spline+1]) { - spline++; + index = 0; /* index of the current trackpoint. */ + for (i = 0; i < num_chunks; i++) { + /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur. + * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints. + */ + if (t[0] + i*chunk_dur >= t[index]) { + gdouble acc_t = 0, acc_s = 0; + numpts = 0; + while (t[0] + i*chunk_dur >= t[index]) { + acc_s += (s[index+1]-s[index]); + acc_t += (t[index+1]-t[index]); + index++; + numpts++; + } + v[i] = acc_s/acc_t; + } + else if (i) { + v[i] = v[i-1]; + } + else { + v[i] = 0; } - s_now = - p[spline].d * pow(T - t[spline+1], 3) + - p[spline].c * pow(T - t[spline+1], 2) + - p[spline].b * (T - t[spline+1]) + - p[spline].a; - v[i] = (s_now - s_prev) / chunk_dur; - s_prev = s_now; - /* - * old method of averages - v[i] = (s[spline+1]-s[spline])/(t[spline+1]-t[spline]); - */ } g_free(s); g_free(t); - g_free(p); return v; } /* by Alex Foobarian */ -VikCoord *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist ) +VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start ) { gdouble dist = vik_track_get_length_including_gaps(tr) * reldist; gdouble current_dist = 0.0; gdouble current_inc = 0.0; - VikCoord *rv; if ( tr->trackpoints ) { GList *iter = tr->trackpoints->next; + GList *last_iter = NULL; + gdouble last_dist = 0.0; while (iter) { current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) ); + last_dist = current_dist; current_dist += current_inc; if ( current_dist >= dist ) break; + last_iter = iter; iter = iter->next; } + if (!iter) { /* passing the end the track */ + if (last_iter) { + if (meters_from_start) + *meters_from_start = last_dist; + return(VIK_TRACKPOINT(last_iter->data)); + } + else + return NULL; + } /* we've gone past the dist already, was prev trackpoint closer? */ /* should do a vik_coord_average_weighted() thingy. */ - if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) + if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) { + if (meters_from_start) + *meters_from_start = last_dist; iter = iter->prev; + } + else + if (meters_from_start) + *meters_from_start = current_dist; + + return VIK_TRACKPOINT(iter->data); + + } + return NULL; +} +VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start ) +{ + time_t t_pos, t_start, t_end, t_total; + t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; + t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; + t_total = t_end - t_start; + t_pos = t_start + t_total * reltime; - rv = g_malloc(sizeof(VikCoord)); - *rv = VIK_TRACKPOINT(iter->data)->coord; + if ( !tr->trackpoints ) + return NULL; - return rv; + GList *iter = tr->trackpoints; + while (iter) { + if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos) + break; + if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) { + if (iter->prev == NULL) /* first trackpoint */ + break; + time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp; + time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos; + if (t_before <= t_after) + iter = iter->prev; + break; + } + else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */ + break; + iter = iter->next; } - return NULL; + + if (!iter) + return NULL; + if (seconds_from_start) + *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; + return VIK_TRACKPOINT(iter->data); } gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt ) @@ -741,3 +719,77 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) return new_tr; } +void vik_track_apply_dem_data ( VikTrack *tr ) +{ + GList *tp_iter; + gint16 elev; + tp_iter = tr->trackpoints; + while ( tp_iter ) { + /* TODO: of the 4 possible choices we have for choosing an elevation + * (trackpoint in between samples), choose the one with the least elevation change + * as the last */ + elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST ); + if ( elev != VIK_DEM_INVALID_ELEVATION ) + VIK_TRACKPOINT(tp_iter->data)->altitude = elev; + tp_iter = tp_iter->next; + } +} + +/* appends t2 to t1, leaving t2 with no trackpoints */ +void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 ) +{ + if ( t1->trackpoints ) { + GList *tpiter = t1->trackpoints; + while ( tpiter->next ) + tpiter = tpiter->next; + tpiter->next = t2->trackpoints; + t2->trackpoints->prev = tpiter; + } else + t1->trackpoints = t2->trackpoints; + t2->trackpoints = NULL; +} + +/* starting at the end, looks backwards for the last "double point", a duplicate trackpoint. + * this is indicative of magic scissors continued use. If there is no double point, + * deletes all the trackpoints. Returns the new end of the track (or the start if + * there are no double points + */ +VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr ) +{ + GList *iter = tr->trackpoints; + VikCoord *rv; + + if ( !iter ) + return NULL; + while ( iter->next ) + iter = iter->next; + + + while ( iter->prev ) { + if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) { + GList *prev = iter->prev; + + rv = g_malloc(sizeof(VikCoord)); + *rv = *((VikCoord *) iter->data); + + /* truncate trackpoint list */ + iter->prev = NULL; /* pretend it's the end */ + g_list_foreach ( iter, (GFunc) g_free, NULL ); + g_list_free( iter ); + + prev->next = NULL; + + return rv; + } + iter = iter->prev; + } + + /* no double point found! */ + rv = g_malloc(sizeof(VikCoord)); + *rv = *((VikCoord *) tr->trackpoints->data); + g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); + g_list_free( tr->trackpoints ); + tr->trackpoints = NULL; + return rv; +} +