+VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start )
+{
+ time_t t_pos, t_start, t_end, t_total;
+ t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
+ t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp;
+ t_total = t_end - t_start;
+
+ t_pos = t_start + t_total * reltime;
+
+ if ( !tr->trackpoints )
+ return NULL;
+
+ GList *iter = tr->trackpoints;
+
+ while (iter) {
+ if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos)
+ break;
+ if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) {
+ if (iter->prev == NULL) /* first trackpoint */
+ break;
+ time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp;
+ time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos;
+ if (t_before <= t_after)
+ iter = iter->prev;
+ break;
+ }
+ else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */
+ break;
+ iter = iter->next;
+ }
+
+ if (!iter)
+ return NULL;
+ if (seconds_from_start)
+ *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp;
+ return VIK_TRACKPOINT(iter->data);
+}
+
+VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr )
+{
+ gdouble maxspeed = 0.0, speed = 0.0;
+
+ if ( !tr->trackpoints )
+ return NULL;
+
+ GList *iter = tr->trackpoints;
+ VikTrackpoint *max_speed_tp = NULL;
+
+ while (iter) {
+ if (iter->prev) {
+ if ( VIK_TRACKPOINT(iter->data)->has_timestamp &&
+ VIK_TRACKPOINT(iter->prev->data)->has_timestamp &&
+ (! VIK_TRACKPOINT(iter->data)->newsegment) ) {
+ speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) )
+ / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp);
+ if ( speed > maxspeed ) {
+ maxspeed = speed;
+ max_speed_tp = VIK_TRACKPOINT(iter->data);
+ }
+ }
+ }
+ iter = iter->next;
+ }
+
+ if (!max_speed_tp)
+ return NULL;
+
+ return max_speed_tp;
+}
+
+VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr )
+{
+ gdouble maxalt = -5000.0;
+ if ( !tr->trackpoints )
+ return NULL;
+
+ GList *iter = tr->trackpoints;
+ VikTrackpoint *max_alt_tp = NULL;
+
+ while (iter) {
+ if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) {
+ maxalt = VIK_TRACKPOINT(iter->data)->altitude;
+ max_alt_tp = VIK_TRACKPOINT(iter->data);
+ }
+ iter = iter->next;
+ }
+
+ if (!max_alt_tp)
+ return NULL;
+
+ return max_alt_tp;
+}
+
+VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr )
+{
+ gdouble minalt = 25000.0;
+ if ( !tr->trackpoints )
+ return NULL;
+
+ GList *iter = tr->trackpoints;
+ VikTrackpoint *min_alt_tp = NULL;
+
+ while (iter) {
+ if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) {
+ minalt = VIK_TRACKPOINT(iter->data)->altitude;
+ min_alt_tp = VIK_TRACKPOINT(iter->data);
+ }
+ iter = iter->next;
+ }
+
+ if (!min_alt_tp)
+ return NULL;
+
+ return min_alt_tp;
+}
+