]> git.street.me.uk Git - andy/viking.git/blobdiff - src/viktrack.c
Gpslayer: New menu items to remove all tracks and waypoints in GPS folders.
[andy/viking.git] / src / viktrack.c
index 9bbea58fc59b5a278d134a71e09bebefc96b3696..dc87516f3d185983ac260bb725d078dfcfee2b21 100644 (file)
@@ -452,6 +452,30 @@ 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 
@@ -495,7 +519,7 @@ void compute_spline(int n, double *x, double *f, spline_coeff_t *p)
     m[i] = (B[i]-h[i+1]*m[i+1])/alpha[i];
   }
 
-  for (i=0; i<n-1; i++) {
+  for (i=0; i<orig_n-1; i++) {
     double mi, mi1;
     mi = (i==(n-2)) ? 0 : m[i];
     mi1 = (i==0) ? 0 : m[i-1];
@@ -521,12 +545,20 @@ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
   int i, pt_count, numpts, spline;
   GList *iter;
   spline_coeff_t *p;
+  GList *mytr;
 
   if ( ! tr->trackpoints )
     return NULL;
 
   g_assert ( num_chunks < 16000 );
 
+#ifdef XXXXXXXXXXXXXXXXXX
+  iter = tr->trackpoints;
+  while (iter) {
+    
+  }
+#endif /*XXXXXXXXXXXXXXXXXX*/
+
   t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
   t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
   duration = t2 - t1;
@@ -601,15 +633,15 @@ gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks )
 }
 
 /* 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 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;
     while (iter)
     {
       current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord),
@@ -617,19 +649,17 @@ VikCoord *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble re
       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;
 
-
-
-    rv = g_malloc(sizeof(VikCoord));
-    *rv = VIK_TRACKPOINT(iter->data)->coord;
-
-    return rv;
+    return VIK_TRACKPOINT(iter->data);
 
   }
   return NULL;