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)
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 )
{
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;
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;
}
else
iter = iter->next;
}
+
+ vik_track_calculate_bounds ( tr );
+
return num;
}
iter->prev = NULL;
rv[i] = vik_track_copy ( tr, FALSE );
rv[i]->trackpoints = iter;
+
+ vik_track_calculate_bounds ( rv[i] );
+
i++;
}
}
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. */
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);
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;
/**
* 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 )
} else
t1->trackpoints = t2->trackpoints;
t2->trackpoints = NULL;
+
+ // Trackpoints updated - so update the bounds
+ vik_track_calculate_bounds ( t1 );
}
/**