+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;
+}
+