]> git.street.me.uk Git - andy/viking.git/blobdiff - src/viktrack.c
fixed a v-t diagram crash bug
[andy/viking.git] / src / viktrack.c
index 8758b536a6e9929e5d89424f9d219e1c7e28b144..006cd695a22d502a7a62b481d039ac8e7b2ea896 100644 (file)
@@ -23,6 +23,7 @@
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <math.h>
 #include "coords.h"
 #include "vikcoord.h"
@@ -451,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 
@@ -494,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];
@@ -654,3 +679,65 @@ gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdoubl
   }
   return FALSE;
 }
+
+void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen)
+{
+  GList *tps;
+  GByteArray *b = g_byte_array_new();
+  guint len;
+  guint intp, ntp;
+
+  g_byte_array_append(b, (guint8 *)tr, sizeof(*tr));
+
+  /* we'll fill out number of trackpoints later */
+  intp = b->len;
+  g_byte_array_append(b, (guint8 *)&len, sizeof(len));
+
+  tps = tr->trackpoints;
+  ntp = 0;
+  while (tps) {
+    g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint));
+    tps = tps->next;
+    ntp++;
+  }
+  *(guint *)(b->data + intp) = ntp;
+
+  len = (tr->comment) ? strlen(tr->comment)+1 : 0; 
+  g_byte_array_append(b, (guint8 *)&len, sizeof(len)); 
+  if (tr->comment) g_byte_array_append(b, (guint8 *)tr->comment, len);
+
+  *data = b->data;
+  *datalen = b->len;
+  g_byte_array_free(b, FALSE);
+}
+
+VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
+{
+  guint len;
+  VikTrack *new_tr = vik_track_new();
+  VikTrackpoint *new_tp;
+  guint ntp;
+  gint i;
+
+  /* only the visibility is needed */
+  new_tr->visible = ((VikTrack *)data)->visible;
+  data += sizeof(*new_tr);
+
+  ntp = *(guint *)data;
+  data += sizeof(ntp);
+
+  for (i=0; i<ntp; i++) {
+    new_tp = vik_trackpoint_new();
+    memcpy(new_tp, data, sizeof(*new_tp));
+    data += sizeof(*new_tp);
+    new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp);
+  }
+
+  len = *(guint *)data;
+  data += sizeof(len);
+  if (len) {
+    new_tr->comment = g_strdup((gchar *)data);
+  }
+  return new_tr;
+}
+