]> git.street.me.uk Git - andy/viking.git/blobdiff - src/viktrack.c
Remove now unused alphabetical sort functions as they have been superseded.
[andy/viking.git] / src / viktrack.c
index 6e0732a2e2dbc3426c34e70fb9bf1424ee552779..5422e7843aa619a85335c2734e7ce2780577e97e 100644 (file)
@@ -59,10 +59,7 @@ void vik_track_set_name(VikTrack *tr, const gchar *name)
   if ( tr->name )
     g_free ( tr->name );
 
-  if ( name && name[0] != '\0' )
-    tr->name = g_strdup(name);
-  else
-    tr->name = NULL;
+  tr->name = g_strdup(name);
 }
 
 void vik_track_set_comment(VikTrack *tr, const gchar *comment)
@@ -141,6 +138,7 @@ VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points )
   new_tr->is_route = tr->is_route;
   new_tr->has_color = tr->has_color;
   new_tr->color = tr->color;
+  new_tr->bbox = tr->bbox;
   new_tr->trackpoints = NULL;
   if ( copy_points )
   {
@@ -182,6 +180,48 @@ VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp)
   return rv;
 }
 
+/**
+ * track_recalculate_bounds_last_tp:
+ * @trk:   The track to consider the recalculation on
+ *
+ * A faster bounds check, since it only considers the last track point
+ */
+static void track_recalculate_bounds_last_tp ( VikTrack *trk )
+{
+  GList *tpl = g_list_last ( trk->trackpoints );
+
+  if ( tpl ) {
+    struct LatLon ll;
+    // See if this trackpoint increases the track bounds and update if so
+    vik_coord_to_latlon ( &(VIK_TRACKPOINT(tpl->data)->coord), &ll );
+    if ( ll.lat > trk->bbox.north )
+      trk->bbox.north = ll.lat;
+    if ( ll.lon < trk->bbox.west )
+      trk->bbox.west = ll.lon;
+    if ( ll.lat < trk->bbox.south )
+      trk->bbox.south = ll.lat;
+    if ( ll.lon > trk->bbox.east )
+      trk->bbox.east = ll.lon;
+  }
+}
+
+/**
+ * vik_track_add_trackpoint:
+ * @tr:          The track to which the trackpoint will be added
+ * @tp:          The trackpoint to add
+ * @recalculate: Whether to perform any associated properties recalculations
+ *               Generally one should avoid recalculation via this method if adding lots of points
+ *               (But ensure calculate_bounds() is called after adding all points!!)
+ *
+ * The trackpoint is added to the end of the existing trackpoint list
+ */
+void vik_track_add_trackpoint ( VikTrack *tr, VikTrackpoint *tp, gboolean recalculate )
+{
+  tr->trackpoints = g_list_append ( tr->trackpoints, tp );
+  if ( recalculate )
+    track_recalculate_bounds_last_tp ( tr );
+}
+
 gdouble vik_track_get_length(const VikTrack *tr)
 {
   gdouble len = 0.0;
@@ -265,6 +305,10 @@ gulong vik_track_remove_dup_points ( VikTrack *tr )
     else
       iter = iter->next;
   }
+
+  // NB isn't really be necessary as removing duplicate points shouldn't alter the bounds!
+  vik_track_calculate_bounds ( tr );
+
   return num;
 }
 
@@ -315,6 +359,9 @@ gulong vik_track_remove_same_time_points ( VikTrack *tr )
     else
       iter = iter->next;
   }
+
+  vik_track_calculate_bounds ( tr );
+
   return num;
 }
 
@@ -385,6 +432,9 @@ VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len)
       iter->prev = NULL;
       rv[i] = vik_track_copy ( tr, FALSE );
       rv[i]->trackpoints = iter;
+
+      vik_track_calculate_bounds ( rv[i] );
+
       i++;
     }
   }
@@ -418,11 +468,13 @@ guint vik_track_merge_segments(VikTrack *tr)
 
 void vik_track_reverse ( VikTrack *tr )
 {
-  GList *iter;
+  if ( ! tr->trackpoints )
+    return;
+
   tr->trackpoints = g_list_reverse(tr->trackpoints);
 
   /* fix 'newsegment' */
-  iter = g_list_last ( tr->trackpoints );
+  GList *iter = g_list_last ( tr->trackpoints );
   while ( iter )
   {
     if ( ! iter->next ) /* last segment, was first, cancel newsegment. */
@@ -1274,6 +1326,7 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
   new_tr->is_route = ((VikTrack *)data)->is_route;
   new_tr->has_color = ((VikTrack *)data)->has_color;
   new_tr->color = ((VikTrack *)data)->color;
+  new_tr->bbox = ((VikTrack *)data)->bbox;
 
   data += sizeof(*new_tr);
 
@@ -1304,6 +1357,48 @@ VikTrack *vik_track_unmarshall (guint8 *data, guint datalen)
   return new_tr;
 }
 
+/**
+ * (Re)Calculate the bounds of the given track,
+ *  updating the track's bounds data.
+ * This should be called whenever a track's trackpoints are changed
+ */
+void vik_track_calculate_bounds ( VikTrack *trk )
+{
+  GList *tp_iter;
+  tp_iter = trk->trackpoints;
+  
+  struct LatLon topleft, bottomright, ll;
+  
+  // Set bounds to first point
+  if ( tp_iter ) {
+    vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &topleft );
+    vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &bottomright );
+  }
+  while ( tp_iter ) {
+
+    // See if this trackpoint increases the track bounds.
+   
+    vik_coord_to_latlon ( &(VIK_TRACKPOINT(tp_iter->data)->coord), &ll );
+  
+    if ( ll.lat > topleft.lat) topleft.lat = ll.lat;
+    if ( ll.lon < topleft.lon) topleft.lon = ll.lon;
+    if ( ll.lat < bottomright.lat) bottomright.lat = ll.lat;
+    if ( ll.lon > bottomright.lon) bottomright.lon = ll.lon;
+    
+    tp_iter = tp_iter->next;
+  }
+  g_debug ( g_strdup_printf("Bounds of track: '%s' is: %f,%f to: %f,%f", trk->name, topleft.lat, topleft.lon, bottomright.lat, bottomright.lon ) );
+
+  trk->bbox.north = topleft.lat;
+  trk->bbox.east = bottomright.lon;
+  trk->bbox.south = bottomright.lat;
+  trk->bbox.west = topleft.lon;
+}
+
+/**
+ *
+ */
 void vik_track_apply_dem_data ( VikTrack *tr )
 {
   GList *tp_iter;
@@ -1322,7 +1417,6 @@ void vik_track_apply_dem_data ( VikTrack *tr )
 
 /**
  * vik_track_apply_dem_data_last_trackpoint:
- * 
  * Apply DEM data (if available) - to only the last trackpoint
  */
 void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr )
@@ -1348,6 +1442,9 @@ void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 )
   } else
     t1->trackpoints = t2->trackpoints;
   t2->trackpoints = NULL;
+
+  // Trackpoints updated - so update the bounds
+  vik_track_calculate_bounds ( t1 );
 }
 
 /**