+
+ 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;
+ s[0] = 0;
+ t[0] = VIK_TRACKPOINT(iter->prev->data)->timestamp;
+ numpts++;
+ while (iter) {
+ s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) );
+ t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp;
+ numpts++;
+ iter = iter->next;
+ }
+
+ compute_spline(numpts, t, s, p);
+
+ /* the spline gives us distances at chunk_dur intervals. from these,
+ * we obtain average speed in each interval.
+ */
+ spline = 0;
+ T = t[spline];
+ s_prev =
+ 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;
+ for (i = 0; i < num_chunks; i++, T+=chunk_dur) {
+ while (T > t[spline+1]) {
+ spline++;
+ }
+ 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 */
+VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist )
+{
+ gdouble dist = vik_track_get_length_including_gaps(tr) * reldist;
+ gdouble current_dist = 0.0;
+ gdouble current_inc = 0.0;
+ if ( tr->trackpoints )
+ {
+ GList *iter = tr->trackpoints->next;
+ GList *last_iter = NULL;
+ while (iter)
+ {
+ current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
+ &(VIK_TRACKPOINT(iter->prev->data)->coord) );
+ current_dist += current_inc;
+ if ( current_dist >= dist )
+ break;
+ last_iter = iter;
+ iter = iter->next;
+ }
+ if (!iter) /* passing the end the track */
+ return (last_iter ? last_iter->data : 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) )
+ iter = iter->prev;
+
+ return VIK_TRACKPOINT(iter->data);
+
+ }
+ return NULL;
+}
+
+gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt )
+{
+ *min_alt = 25000;
+ *max_alt = -5000;
+ if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) {
+ GList *iter = tr->trackpoints->next;
+ gdouble tmp_alt;
+ while (iter)
+ {
+ tmp_alt = VIK_TRACKPOINT(iter->data)->altitude;
+ if ( tmp_alt > *max_alt )
+ *max_alt = tmp_alt;
+ if ( tmp_alt < *min_alt )
+ *min_alt = tmp_alt;