]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
0d2b891f | 5 | * Copyright (c) 2012, Rob Norris <rw_norris@hotmail.com> |
50a14534 EB |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
8c00358d GB |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
50a14534 EB |
25 | |
26 | #include <glib.h> | |
27 | #include <time.h> | |
bf35388d | 28 | #include <stdlib.h> |
8c00358d | 29 | #ifdef HAVE_STRING_H |
ddc47a46 | 30 | #include <string.h> |
8c00358d GB |
31 | #endif |
32 | #ifdef HAVE_MATH_H | |
bf35388d | 33 | #include <math.h> |
8c00358d GB |
34 | #endif |
35 | ||
50a14534 EB |
36 | #include "coords.h" |
37 | #include "vikcoord.h" | |
38 | #include "viktrack.h" | |
39 | #include "globals.h" | |
ad0a8c2d | 40 | #include "dems.h" |
50a14534 EB |
41 | |
42 | VikTrack *vik_track_new() | |
43 | { | |
8c4f1350 | 44 | VikTrack *tr = g_malloc0 ( sizeof ( VikTrack ) ); |
50a14534 EB |
45 | tr->ref_count = 1; |
46 | return tr; | |
47 | } | |
48 | ||
49 | void vik_track_set_comment_no_copy(VikTrack *tr, gchar *comment) | |
50 | { | |
51 | if ( tr->comment ) | |
52 | g_free ( tr->comment ); | |
53 | tr->comment = comment; | |
54 | } | |
55 | ||
56 | ||
ce4bd1cf RN |
57 | void vik_track_set_name(VikTrack *tr, const gchar *name) |
58 | { | |
59 | if ( tr->name ) | |
60 | g_free ( tr->name ); | |
61 | ||
62 | if ( name && name[0] != '\0' ) | |
63 | tr->name = g_strdup(name); | |
64 | else | |
65 | tr->name = NULL; | |
66 | } | |
67 | ||
50a14534 EB |
68 | void vik_track_set_comment(VikTrack *tr, const gchar *comment) |
69 | { | |
70 | if ( tr->comment ) | |
71 | g_free ( tr->comment ); | |
72 | ||
73 | if ( comment && comment[0] != '\0' ) | |
74 | tr->comment = g_strdup(comment); | |
75 | else | |
76 | tr->comment = NULL; | |
77 | } | |
78 | ||
6b2f262e RN |
79 | void vik_track_set_description(VikTrack *tr, const gchar *description) |
80 | { | |
81 | if ( tr->description ) | |
82 | g_free ( tr->description ); | |
83 | ||
84 | if ( description && description[0] != '\0' ) | |
85 | tr->description = g_strdup(description); | |
86 | else | |
87 | tr->description = NULL; | |
88 | } | |
89 | ||
50a14534 EB |
90 | void vik_track_ref(VikTrack *tr) |
91 | { | |
92 | tr->ref_count++; | |
93 | } | |
94 | ||
21700912 QT |
95 | void vik_track_set_property_dialog(VikTrack *tr, GtkWidget *dialog) |
96 | { | |
97 | /* Warning: does not check for existing dialog */ | |
98 | tr->property_dialog = dialog; | |
99 | } | |
100 | ||
101 | void vik_track_clear_property_dialog(VikTrack *tr) | |
102 | { | |
103 | tr->property_dialog = NULL; | |
104 | } | |
105 | ||
50a14534 EB |
106 | void vik_track_free(VikTrack *tr) |
107 | { | |
108 | if ( tr->ref_count-- > 1 ) | |
109 | return; | |
110 | ||
ce4bd1cf RN |
111 | if ( tr->name ) |
112 | g_free ( tr->name ); | |
50a14534 EB |
113 | if ( tr->comment ) |
114 | g_free ( tr->comment ); | |
6b2f262e RN |
115 | if ( tr->description ) |
116 | g_free ( tr->description ); | |
50a14534 EB |
117 | g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); |
118 | g_list_free( tr->trackpoints ); | |
21700912 | 119 | if (tr->property_dialog) |
63b31477 RN |
120 | if ( GTK_IS_WIDGET(tr->property_dialog) ) |
121 | gtk_widget_destroy ( GTK_WIDGET(tr->property_dialog) ); | |
50a14534 EB |
122 | g_free ( tr ); |
123 | } | |
124 | ||
03817fbf RN |
125 | /** |
126 | * vik_track_copy: | |
127 | * @tr: The Track to copy | |
128 | * @copy_points: Whether to copy the track points or not | |
129 | * | |
130 | * Normally for copying the track it's best to copy all the trackpoints | |
131 | * However for some operations such as splitting tracks the trackpoints will be managed separately, so no need to copy them. | |
132 | * | |
133 | * Returns: the copied VikTrack | |
134 | */ | |
135 | VikTrack *vik_track_copy ( const VikTrack *tr, gboolean copy_points ) | |
50a14534 EB |
136 | { |
137 | VikTrack *new_tr = vik_track_new(); | |
138 | VikTrackpoint *new_tp; | |
139 | GList *tp_iter = tr->trackpoints; | |
140 | new_tr->visible = tr->visible; | |
0d2b891f | 141 | new_tr->is_route = tr->is_route; |
b1453c16 RN |
142 | new_tr->has_color = tr->has_color; |
143 | new_tr->color = tr->color; | |
50a14534 | 144 | new_tr->trackpoints = NULL; |
03817fbf | 145 | if ( copy_points ) |
50a14534 | 146 | { |
03817fbf RN |
147 | while ( tp_iter ) |
148 | { | |
149 | new_tp = g_malloc ( sizeof ( VikTrackpoint ) ); | |
150 | *new_tp = *((VikTrackpoint *)(tp_iter->data)); | |
151 | new_tr->trackpoints = g_list_append ( new_tr->trackpoints, new_tp ); | |
152 | tp_iter = tp_iter->next; | |
153 | } | |
50a14534 | 154 | } |
ce4bd1cf | 155 | vik_track_set_name(new_tr,tr->name); |
50a14534 | 156 | vik_track_set_comment(new_tr,tr->comment); |
6b2f262e | 157 | vik_track_set_description(new_tr,tr->description); |
50a14534 EB |
158 | return new_tr; |
159 | } | |
160 | ||
161 | VikTrackpoint *vik_trackpoint_new() | |
162 | { | |
a2817d3c | 163 | VikTrackpoint *tp = g_malloc0(sizeof(VikTrackpoint)); |
a2817d3c QT |
164 | tp->speed = NAN; |
165 | tp->course = NAN; | |
8541f2cf T |
166 | tp->altitude = VIK_DEFAULT_ALTITUDE; |
167 | tp->hdop = VIK_DEFAULT_DOP; | |
168 | tp->vdop = VIK_DEFAULT_DOP; | |
169 | tp->pdop = VIK_DEFAULT_DOP; | |
a2817d3c | 170 | return tp; |
50a14534 EB |
171 | } |
172 | ||
173 | void vik_trackpoint_free(VikTrackpoint *tp) | |
174 | { | |
175 | g_free(tp); | |
176 | } | |
177 | ||
178 | VikTrackpoint *vik_trackpoint_copy(VikTrackpoint *tp) | |
179 | { | |
180 | VikTrackpoint *rv = vik_trackpoint_new(); | |
181 | *rv = *tp; | |
182 | return rv; | |
183 | } | |
184 | ||
185 | gdouble vik_track_get_length(const VikTrack *tr) | |
186 | { | |
187 | gdouble len = 0.0; | |
188 | if ( tr->trackpoints ) | |
189 | { | |
190 | GList *iter = tr->trackpoints->next; | |
191 | while (iter) | |
192 | { | |
193 | if ( ! VIK_TRACKPOINT(iter->data)->newsegment ) | |
194 | len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), | |
195 | &(VIK_TRACKPOINT(iter->prev->data)->coord) ); | |
196 | iter = iter->next; | |
197 | } | |
198 | } | |
199 | return len; | |
200 | } | |
201 | ||
202 | gdouble vik_track_get_length_including_gaps(const VikTrack *tr) | |
203 | { | |
204 | gdouble len = 0.0; | |
205 | if ( tr->trackpoints ) | |
206 | { | |
207 | GList *iter = tr->trackpoints->next; | |
208 | while (iter) | |
209 | { | |
210 | len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), | |
211 | &(VIK_TRACKPOINT(iter->prev->data)->coord) ); | |
212 | iter = iter->next; | |
213 | } | |
214 | } | |
215 | return len; | |
216 | } | |
217 | ||
218 | gulong vik_track_get_tp_count(const VikTrack *tr) | |
219 | { | |
220 | gulong num = 0; | |
221 | GList *iter = tr->trackpoints; | |
222 | while ( iter ) | |
223 | { | |
224 | num++; | |
225 | iter = iter->next; | |
226 | } | |
227 | return num; | |
228 | } | |
229 | ||
230 | gulong vik_track_get_dup_point_count ( const VikTrack *tr ) | |
231 | { | |
232 | gulong num = 0; | |
233 | GList *iter = tr->trackpoints; | |
234 | while ( iter ) | |
235 | { | |
236 | if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord), | |
237 | &(VIK_TRACKPOINT(iter->next->data)->coord) ) ) | |
238 | num++; | |
239 | iter = iter->next; | |
240 | } | |
241 | return num; | |
242 | } | |
243 | ||
4f48c541 RN |
244 | /* |
245 | * Deletes adjacent points that have the same position | |
246 | * Returns the number of points that were deleted | |
247 | */ | |
248 | gulong vik_track_remove_dup_points ( VikTrack *tr ) | |
50a14534 | 249 | { |
4f48c541 | 250 | gulong num = 0; |
50a14534 EB |
251 | GList *iter = tr->trackpoints; |
252 | while ( iter ) | |
253 | { | |
254 | if ( iter->next && vik_coord_equals ( &(VIK_TRACKPOINT(iter->data)->coord), | |
255 | &(VIK_TRACKPOINT(iter->next->data)->coord) ) ) | |
256 | { | |
4f48c541 | 257 | num++; |
d6caf4c0 RN |
258 | // Maintain track segments |
259 | if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next ) | |
260 | VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE; | |
261 | ||
262 | vik_trackpoint_free ( iter->next->data ); | |
2a0cbd46 RN |
263 | tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next ); |
264 | } | |
265 | else | |
266 | iter = iter->next; | |
267 | } | |
268 | return num; | |
269 | } | |
270 | ||
271 | /* | |
272 | * Get a count of trackpoints with the same defined timestamp | |
273 | * Note is using timestamps with a resolution with 1 second | |
274 | */ | |
275 | gulong vik_track_get_same_time_point_count ( const VikTrack *tr ) | |
276 | { | |
277 | gulong num = 0; | |
278 | GList *iter = tr->trackpoints; | |
279 | while ( iter ) { | |
280 | if ( iter->next && | |
281 | ( VIK_TRACKPOINT(iter->data)->has_timestamp && | |
282 | VIK_TRACKPOINT(iter->next->data)->has_timestamp ) && | |
283 | ( VIK_TRACKPOINT(iter->data)->timestamp == | |
284 | VIK_TRACKPOINT(iter->next->data)->timestamp) ) | |
285 | num++; | |
286 | iter = iter->next; | |
287 | } | |
288 | return num; | |
289 | } | |
290 | ||
291 | /* | |
292 | * Deletes adjacent points that have the same defined timestamp | |
293 | * Returns the number of points that were deleted | |
294 | */ | |
295 | gulong vik_track_remove_same_time_points ( VikTrack *tr ) | |
296 | { | |
297 | gulong num = 0; | |
298 | GList *iter = tr->trackpoints; | |
299 | while ( iter ) { | |
300 | if ( iter->next && | |
301 | ( VIK_TRACKPOINT(iter->data)->has_timestamp && | |
302 | VIK_TRACKPOINT(iter->next->data)->has_timestamp ) && | |
303 | ( VIK_TRACKPOINT(iter->data)->timestamp == | |
304 | VIK_TRACKPOINT(iter->next->data)->timestamp) ) { | |
305 | ||
306 | num++; | |
307 | ||
308 | // Maintain track segments | |
309 | if ( VIK_TRACKPOINT(iter->next->data)->newsegment && (iter->next)->next ) | |
310 | VIK_TRACKPOINT(((iter->next)->next)->data)->newsegment = TRUE; | |
311 | ||
312 | vik_trackpoint_free ( iter->next->data ); | |
50a14534 EB |
313 | tr->trackpoints = g_list_delete_link ( tr->trackpoints, iter->next ); |
314 | } | |
315 | else | |
316 | iter = iter->next; | |
317 | } | |
4f48c541 | 318 | return num; |
50a14534 EB |
319 | } |
320 | ||
2f5d7ea1 RN |
321 | /* |
322 | * Deletes all 'extra' trackpoint information | |
323 | * such as time stamps, speed, course etc... | |
324 | */ | |
325 | void vik_track_to_routepoints ( VikTrack *tr ) | |
326 | { | |
327 | GList *iter = tr->trackpoints; | |
328 | while ( iter ) { | |
329 | ||
330 | // c.f. with vik_trackpoint_new() | |
331 | ||
332 | VIK_TRACKPOINT(iter->data)->has_timestamp = FALSE; | |
333 | VIK_TRACKPOINT(iter->data)->timestamp = 0; | |
334 | VIK_TRACKPOINT(iter->data)->speed = NAN; | |
335 | VIK_TRACKPOINT(iter->data)->course = NAN; | |
336 | VIK_TRACKPOINT(iter->data)->hdop = VIK_DEFAULT_DOP; | |
337 | VIK_TRACKPOINT(iter->data)->vdop = VIK_DEFAULT_DOP; | |
338 | VIK_TRACKPOINT(iter->data)->pdop = VIK_DEFAULT_DOP; | |
339 | VIK_TRACKPOINT(iter->data)->nsats = 0; | |
340 | VIK_TRACKPOINT(iter->data)->fix_mode = VIK_GPS_MODE_NOT_SEEN; | |
341 | ||
342 | iter = iter->next; | |
343 | } | |
344 | } | |
345 | ||
50a14534 EB |
346 | guint vik_track_get_segment_count(const VikTrack *tr) |
347 | { | |
348 | guint num = 1; | |
349 | GList *iter = tr->trackpoints; | |
350 | if ( !iter ) | |
351 | return 0; | |
352 | while ( (iter = iter->next) ) | |
353 | { | |
354 | if ( VIK_TRACKPOINT(iter->data)->newsegment ) | |
355 | num++; | |
356 | } | |
357 | return num; | |
358 | } | |
359 | ||
360 | VikTrack **vik_track_split_into_segments(VikTrack *t, guint *ret_len) | |
361 | { | |
362 | VikTrack **rv; | |
363 | VikTrack *tr; | |
364 | guint i; | |
365 | guint segs = vik_track_get_segment_count(t); | |
366 | GList *iter; | |
367 | ||
368 | if ( segs < 2 ) | |
369 | { | |
370 | *ret_len = 0; | |
371 | return NULL; | |
372 | } | |
373 | ||
374 | rv = g_malloc ( segs * sizeof(VikTrack *) ); | |
03817fbf | 375 | tr = vik_track_copy ( t, TRUE ); |
50a14534 EB |
376 | rv[0] = tr; |
377 | iter = tr->trackpoints; | |
378 | ||
379 | i = 1; | |
380 | while ( (iter = iter->next) ) | |
381 | { | |
382 | if ( VIK_TRACKPOINT(iter->data)->newsegment ) | |
383 | { | |
384 | iter->prev->next = NULL; | |
385 | iter->prev = NULL; | |
03817fbf | 386 | rv[i] = vik_track_copy ( tr, FALSE ); |
50a14534 EB |
387 | rv[i]->trackpoints = iter; |
388 | i++; | |
389 | } | |
390 | } | |
391 | *ret_len = segs; | |
392 | return rv; | |
393 | } | |
394 | ||
ce1c0489 RN |
395 | /* |
396 | * Simply remove any subsequent segment markers in a track to form one continuous track | |
397 | * Return the number of segments merged | |
398 | */ | |
399 | guint vik_track_merge_segments(VikTrack *tr) | |
400 | { | |
401 | guint num = 0; | |
402 | GList *iter = tr->trackpoints; | |
403 | if ( !iter ) | |
404 | return num; | |
405 | ||
406 | // Always skip the first point as this should be the first segment | |
407 | iter = iter->next; | |
408 | ||
409 | while ( (iter = iter->next) ) | |
410 | { | |
411 | if ( VIK_TRACKPOINT(iter->data)->newsegment ) { | |
412 | VIK_TRACKPOINT(iter->data)->newsegment = FALSE; | |
413 | num++; | |
414 | } | |
415 | } | |
416 | return num; | |
417 | } | |
418 | ||
50a14534 EB |
419 | void vik_track_reverse ( VikTrack *tr ) |
420 | { | |
421 | GList *iter; | |
422 | tr->trackpoints = g_list_reverse(tr->trackpoints); | |
423 | ||
424 | /* fix 'newsegment' */ | |
425 | iter = g_list_last ( tr->trackpoints ); | |
426 | while ( iter ) | |
427 | { | |
428 | if ( ! iter->next ) /* last segment, was first, cancel newsegment. */ | |
429 | VIK_TRACKPOINT(iter->data)->newsegment = FALSE; | |
430 | if ( ! iter->prev ) /* first segment by convention has newsegment flag. */ | |
431 | VIK_TRACKPOINT(iter->data)->newsegment = TRUE; | |
432 | else if ( VIK_TRACKPOINT(iter->data)->newsegment && iter->next ) | |
433 | { | |
434 | VIK_TRACKPOINT(iter->next->data)->newsegment = TRUE; | |
435 | VIK_TRACKPOINT(iter->data)->newsegment = FALSE; | |
436 | } | |
437 | iter = iter->prev; | |
438 | } | |
439 | } | |
440 | ||
441 | gdouble vik_track_get_average_speed(const VikTrack *tr) | |
442 | { | |
443 | gdouble len = 0.0; | |
444 | guint32 time = 0; | |
445 | if ( tr->trackpoints ) | |
446 | { | |
447 | GList *iter = tr->trackpoints->next; | |
448 | while (iter) | |
449 | { | |
450 | if ( VIK_TRACKPOINT(iter->data)->has_timestamp && | |
451 | VIK_TRACKPOINT(iter->prev->data)->has_timestamp && | |
452 | (! VIK_TRACKPOINT(iter->data)->newsegment) ) | |
453 | { | |
454 | len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), | |
455 | &(VIK_TRACKPOINT(iter->prev->data)->coord) ); | |
456 | time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp); | |
457 | } | |
458 | iter = iter->next; | |
459 | } | |
460 | } | |
461 | return (time == 0) ? 0 : ABS(len/time); | |
462 | } | |
463 | ||
4c21c2fa RN |
464 | /** |
465 | * Based on a simple average speed, but with a twist - to give a moving average. | |
466 | * . GPSs often report a moving average in their statistics output | |
467 | * . bicycle speedos often don't factor in time when stopped - hence reporting a moving average for speed | |
468 | * | |
469 | * Often GPS track will record every second but not when stationary | |
470 | * This method doesn't use samples that differ over the specified time limit - effectively skipping that time chunk from the total time | |
471 | * | |
472 | * Suggest to use 60 seconds as the stop length (as the default used in the TrackWaypoint draw stops factor) | |
473 | */ | |
474 | gdouble vik_track_get_average_speed_moving (const VikTrack *tr, int stop_length_seconds) | |
475 | { | |
476 | gdouble len = 0.0; | |
477 | guint32 time = 0; | |
478 | if ( tr->trackpoints ) | |
479 | { | |
480 | GList *iter = tr->trackpoints->next; | |
481 | while (iter) | |
482 | { | |
483 | if ( VIK_TRACKPOINT(iter->data)->has_timestamp && | |
484 | VIK_TRACKPOINT(iter->prev->data)->has_timestamp && | |
485 | (! VIK_TRACKPOINT(iter->data)->newsegment) ) | |
486 | { | |
487 | if ( ( VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp ) < stop_length_seconds ) { | |
488 | len += vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), | |
489 | &(VIK_TRACKPOINT(iter->prev->data)->coord) ); | |
490 | ||
491 | time += ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp); | |
492 | } | |
493 | } | |
494 | iter = iter->next; | |
495 | } | |
496 | } | |
497 | return (time == 0) ? 0 : ABS(len/time); | |
498 | } | |
499 | ||
50a14534 EB |
500 | gdouble vik_track_get_max_speed(const VikTrack *tr) |
501 | { | |
502 | gdouble maxspeed = 0.0, speed = 0.0; | |
503 | if ( tr->trackpoints ) | |
504 | { | |
505 | GList *iter = tr->trackpoints->next; | |
506 | while (iter) | |
507 | { | |
508 | if ( VIK_TRACKPOINT(iter->data)->has_timestamp && | |
509 | VIK_TRACKPOINT(iter->prev->data)->has_timestamp && | |
510 | (! VIK_TRACKPOINT(iter->data)->newsegment) ) | |
511 | { | |
512 | speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) ) | |
513 | / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp); | |
514 | if ( speed > maxspeed ) | |
515 | maxspeed = speed; | |
516 | } | |
517 | iter = iter->next; | |
518 | } | |
519 | } | |
520 | return maxspeed; | |
521 | } | |
522 | ||
523 | void vik_track_convert ( VikTrack *tr, VikCoordMode dest_mode ) | |
524 | { | |
525 | GList *iter = tr->trackpoints; | |
526 | while (iter) | |
527 | { | |
528 | vik_coord_convert ( &(VIK_TRACKPOINT(iter->data)->coord), dest_mode ); | |
529 | iter = iter->next; | |
530 | } | |
531 | } | |
532 | ||
533 | /* I understood this when I wrote it ... maybe ... Basically it eats up the | |
534 | * proper amounts of length on the track and averages elevation over that. */ | |
535 | gdouble *vik_track_make_elevation_map ( const VikTrack *tr, guint16 num_chunks ) | |
536 | { | |
537 | gdouble *pts; | |
538 | gdouble total_length, chunk_length, current_dist, current_area_under_curve, current_seg_length, dist_along_seg = 0.0; | |
539 | gdouble altitude1, altitude2; | |
540 | guint16 current_chunk; | |
541 | gboolean ignore_it = FALSE; | |
542 | ||
543 | GList *iter = tr->trackpoints; | |
544 | ||
c3deba01 | 545 | if (!iter || !iter->next) /* zero- or one-point track */ |
4b31042b QT |
546 | return NULL; |
547 | ||
c79f0206 EB |
548 | { /* test if there's anything worth calculating */ |
549 | gboolean okay = FALSE; | |
550 | while ( iter ) | |
551 | { | |
5c280098 RN |
552 | // Sometimes a GPS device (or indeed any random file) can have stupid numbers for elevations |
553 | // Since when is 9.9999e+24 a valid elevation!! | |
554 | // This can happen when a track (with no elevations) is uploaded to a GPS device and then redownloaded (e.g. using a Garmin Legend EtrexHCx) | |
555 | // Some protection against trying to work with crazily massive numbers (otherwise get SIGFPE, Arithmetic exception) | |
556 | if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE && | |
557 | VIK_TRACKPOINT(iter->data)->altitude < 1E9 ) { | |
c79f0206 EB |
558 | okay = TRUE; break; |
559 | } | |
560 | iter = iter->next; | |
561 | } | |
562 | if ( ! okay ) | |
563 | return NULL; | |
564 | } | |
565 | ||
0f941532 | 566 | iter = tr->trackpoints; |
c79f0206 | 567 | |
50a14534 EB |
568 | g_assert ( num_chunks < 16000 ); |
569 | ||
570 | pts = g_malloc ( sizeof(gdouble) * num_chunks ); | |
571 | ||
572 | total_length = vik_track_get_length_including_gaps ( tr ); | |
573 | chunk_length = total_length / num_chunks; | |
574 | ||
6374e157 | 575 | /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */ |
3b36279c JJ |
576 | if (chunk_length <= 0) { |
577 | g_free(pts); | |
6374e157 | 578 | return NULL; |
3b36279c | 579 | } |
6374e157 | 580 | |
50a14534 EB |
581 | current_dist = 0.0; |
582 | current_area_under_curve = 0; | |
583 | current_chunk = 0; | |
584 | current_seg_length = 0; | |
585 | ||
586 | current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), | |
587 | &(VIK_TRACKPOINT(iter->next->data)->coord) ); | |
588 | altitude1 = VIK_TRACKPOINT(iter->data)->altitude; | |
589 | altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude; | |
590 | dist_along_seg = 0; | |
591 | ||
592 | while ( current_chunk < num_chunks ) { | |
593 | ||
594 | /* go along current seg */ | |
595 | if ( current_seg_length && (current_seg_length - dist_along_seg) > chunk_length ) { | |
596 | dist_along_seg += chunk_length; | |
597 | ||
598 | /* / | |
599 | * pt2 * | |
600 | * /x altitude = alt_at_pt_1 + alt_at_pt_2 / 2 = altitude1 + slope * dist_value_of_pt_inbetween_pt1_and_pt2 | |
601 | * /xx avg altitude = area under curve / chunk len | |
602 | *pt1 *xxx avg altitude = altitude1 + (altitude2-altitude1)/(current_seg_length)*(dist_along_seg + (chunk_len/2)) | |
603 | * / xxx | |
604 | * / xxx | |
605 | **/ | |
606 | ||
607 | if ( ignore_it ) | |
aebc49f4 RN |
608 | // Seemly can't determine average for this section - so use last known good value (much better than just sticking in zero) |
609 | pts[current_chunk] = altitude1; | |
50a14534 | 610 | else |
9903c388 | 611 | pts[current_chunk] = altitude1 + (altitude2-altitude1)*((dist_along_seg - (chunk_length/2))/current_seg_length); |
50a14534 EB |
612 | |
613 | current_chunk++; | |
614 | } else { | |
615 | /* finish current seg */ | |
616 | if ( current_seg_length ) { | |
617 | gdouble altitude_at_dist_along_seg = altitude1 + (altitude2-altitude1)/(current_seg_length)*dist_along_seg; | |
618 | current_dist = current_seg_length - dist_along_seg; | |
619 | current_area_under_curve = current_dist*(altitude_at_dist_along_seg + altitude2)*0.5; | |
620 | } else { current_dist = current_area_under_curve = 0; } /* should only happen if first current_seg_length == 0 */ | |
621 | ||
622 | /* get intervening segs */ | |
623 | iter = iter->next; | |
624 | while ( iter && iter->next ) { | |
625 | current_seg_length = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), | |
626 | &(VIK_TRACKPOINT(iter->next->data)->coord) ); | |
627 | altitude1 = VIK_TRACKPOINT(iter->data)->altitude; | |
628 | altitude2 = VIK_TRACKPOINT(iter->next->data)->altitude; | |
629 | ignore_it = VIK_TRACKPOINT(iter->next->data)->newsegment; | |
630 | ||
631 | if ( chunk_length - current_dist >= current_seg_length ) { | |
632 | current_dist += current_seg_length; | |
633 | current_area_under_curve += current_seg_length * (altitude1+altitude2) * 0.5; | |
634 | iter = iter->next; | |
635 | } else { | |
636 | break; | |
637 | } | |
638 | } | |
639 | ||
640 | /* final seg */ | |
641 | dist_along_seg = chunk_length - current_dist; | |
fa396b8a | 642 | if ( ignore_it || ( iter && !iter->next ) ) { |
50a14534 | 643 | pts[current_chunk] = current_area_under_curve / current_dist; |
61950ef8 QT |
644 | if (!iter->next) { |
645 | int i; | |
646 | for (i = current_chunk + 1; i < num_chunks; i++) | |
647 | pts[i] = pts[current_chunk]; | |
648 | break; | |
649 | } | |
9903c388 | 650 | } |
50a14534 EB |
651 | else { |
652 | current_area_under_curve += dist_along_seg * (altitude1 + (altitude2 - altitude1)*dist_along_seg/current_seg_length); | |
653 | pts[current_chunk] = current_area_under_curve / chunk_length; | |
654 | } | |
655 | ||
656 | current_dist = 0; | |
657 | current_chunk++; | |
658 | } | |
659 | } | |
660 | ||
661 | return pts; | |
662 | } | |
663 | ||
664 | ||
665 | void vik_track_get_total_elevation_gain(const VikTrack *tr, gdouble *up, gdouble *down) | |
666 | { | |
667 | gdouble diff; | |
668 | *up = *down = 0; | |
8c4f1350 | 669 | if ( tr->trackpoints && VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE ) |
50a14534 EB |
670 | { |
671 | GList *iter = tr->trackpoints->next; | |
672 | while (iter) | |
673 | { | |
674 | diff = VIK_TRACKPOINT(iter->data)->altitude - VIK_TRACKPOINT(iter->prev->data)->altitude; | |
675 | if ( diff > 0 ) | |
676 | *up += diff; | |
677 | else | |
678 | *down -= diff; | |
679 | iter = iter->next; | |
680 | } | |
bf35388d EB |
681 | } else |
682 | *up = *down = VIK_DEFAULT_ALTITUDE; | |
683 | } | |
684 | ||
0ba33e1d FA |
685 | gdouble *vik_track_make_gradient_map ( const VikTrack *tr, guint16 num_chunks ) |
686 | { | |
687 | gdouble *pts; | |
688 | gdouble *altitudes; | |
689 | gdouble total_length, chunk_length, current_gradient; | |
690 | gdouble altitude1, altitude2; | |
691 | guint16 current_chunk; | |
692 | ||
693 | g_assert ( num_chunks < 16000 ); | |
694 | ||
695 | total_length = vik_track_get_length_including_gaps ( tr ); | |
696 | chunk_length = total_length / num_chunks; | |
697 | ||
698 | /* Zero chunk_length (eg, track of 2 tp with the same loc) will cause crash */ | |
699 | if (chunk_length <= 0) { | |
700 | return NULL; | |
701 | } | |
702 | ||
703 | altitudes = vik_track_make_elevation_map (tr, num_chunks); | |
704 | if (altitudes == NULL) { | |
705 | return NULL; | |
706 | } | |
707 | ||
708 | current_gradient = 0.0; | |
709 | pts = g_malloc ( sizeof(gdouble) * num_chunks ); | |
710 | for (current_chunk = 0; current_chunk < (num_chunks - 1); current_chunk++) { | |
711 | altitude1 = altitudes[current_chunk]; | |
712 | altitude2 = altitudes[current_chunk + 1]; | |
713 | current_gradient = 100.0 * (altitude2 - altitude1) / chunk_length; | |
714 | ||
715 | pts[current_chunk] = current_gradient; | |
716 | } | |
717 | ||
718 | pts[current_chunk] = current_gradient; | |
719 | ||
720 | return pts; | |
721 | } | |
25e44eac | 722 | |
bf35388d | 723 | /* by Alex Foobarian */ |
25e44eac AF |
724 | gdouble *vik_track_make_speed_map ( const VikTrack *tr, guint16 num_chunks ) |
725 | { | |
bf35388d | 726 | gdouble *v, *s, *t; |
0654760a | 727 | gdouble duration, chunk_dur; |
25e44eac | 728 | time_t t1, t2; |
d03d80e6 | 729 | int i, pt_count, numpts, index; |
bf35388d | 730 | GList *iter; |
25e44eac | 731 | |
24d5c7e2 EB |
732 | if ( ! tr->trackpoints ) |
733 | return NULL; | |
25e44eac | 734 | |
24d5c7e2 | 735 | g_assert ( num_chunks < 16000 ); |
25e44eac AF |
736 | |
737 | t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
738 | t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
739 | duration = t2 - t1; | |
c79f0206 EB |
740 | |
741 | if ( !t1 || !t2 || !duration ) | |
742 | return NULL; | |
743 | ||
25e44eac | 744 | if (duration < 0) { |
4258f4e2 | 745 | g_warning("negative duration: unsorted trackpoint timestamps?"); |
25e44eac AF |
746 | return NULL; |
747 | } | |
bf35388d | 748 | pt_count = vik_track_get_tp_count(tr); |
24d5c7e2 | 749 | |
bf35388d | 750 | v = g_malloc ( sizeof(gdouble) * num_chunks ); |
25e44eac | 751 | chunk_dur = duration / num_chunks; |
bf35388d EB |
752 | |
753 | s = g_malloc(sizeof(double) * pt_count); | |
754 | t = g_malloc(sizeof(double) * pt_count); | |
bf35388d EB |
755 | |
756 | iter = tr->trackpoints->next; | |
757 | numpts = 0; | |
758 | s[0] = 0; | |
e979bdab | 759 | t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; |
bf35388d EB |
760 | numpts++; |
761 | while (iter) { | |
762 | s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); | |
763 | t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; | |
764 | numpts++; | |
765 | iter = iter->next; | |
25e44eac AF |
766 | } |
767 | ||
d03d80e6 AF |
768 | /* In the following computation, we iterate through periods of time of duration chunk_dur. |
769 | * The first period begins at the beginning of the track. The last period ends at the end of the track. | |
bf35388d | 770 | */ |
d03d80e6 AF |
771 | index = 0; /* index of the current trackpoint. */ |
772 | for (i = 0; i < num_chunks; i++) { | |
773 | /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur. | |
774 | * find the first trackpoint outside the current interval, averaging the speeds between intermediate trackpoints. | |
775 | */ | |
776 | if (t[0] + i*chunk_dur >= t[index]) { | |
777 | gdouble acc_t = 0, acc_s = 0; | |
d03d80e6 AF |
778 | while (t[0] + i*chunk_dur >= t[index]) { |
779 | acc_s += (s[index+1]-s[index]); | |
780 | acc_t += (t[index+1]-t[index]); | |
781 | index++; | |
d03d80e6 AF |
782 | } |
783 | v[i] = acc_s/acc_t; | |
784 | } | |
785 | else if (i) { | |
786 | v[i] = v[i-1]; | |
787 | } | |
788 | else { | |
789 | v[i] = 0; | |
bf35388d | 790 | } |
bf35388d EB |
791 | } |
792 | g_free(s); | |
793 | g_free(t); | |
bf35388d | 794 | return v; |
25e44eac | 795 | } |
24d5c7e2 | 796 | |
926c8140 RN |
797 | /** |
798 | * Make a distance/time map, heavily based on the vik_track_make_speed_map method | |
799 | */ | |
800 | gdouble *vik_track_make_distance_map ( const VikTrack *tr, guint16 num_chunks ) | |
801 | { | |
802 | gdouble *v, *s, *t; | |
803 | gdouble duration, chunk_dur; | |
804 | time_t t1, t2; | |
805 | int i, pt_count, numpts, index; | |
806 | GList *iter; | |
807 | ||
808 | if ( ! tr->trackpoints ) | |
809 | return NULL; | |
810 | ||
811 | t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
812 | t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
813 | duration = t2 - t1; | |
814 | ||
815 | if ( !t1 || !t2 || !duration ) | |
816 | return NULL; | |
817 | ||
818 | if (duration < 0) { | |
819 | g_warning("negative duration: unsorted trackpoint timestamps?"); | |
820 | return NULL; | |
821 | } | |
822 | pt_count = vik_track_get_tp_count(tr); | |
823 | ||
824 | v = g_malloc ( sizeof(gdouble) * num_chunks ); | |
825 | chunk_dur = duration / num_chunks; | |
826 | ||
827 | s = g_malloc(sizeof(double) * pt_count); | |
828 | t = g_malloc(sizeof(double) * pt_count); | |
829 | ||
830 | iter = tr->trackpoints->next; | |
831 | numpts = 0; | |
832 | s[0] = 0; | |
e979bdab | 833 | t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; |
926c8140 RN |
834 | numpts++; |
835 | while (iter) { | |
836 | s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); | |
837 | t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; | |
838 | numpts++; | |
839 | iter = iter->next; | |
840 | } | |
841 | ||
842 | /* In the following computation, we iterate through periods of time of duration chunk_dur. | |
843 | * The first period begins at the beginning of the track. The last period ends at the end of the track. | |
844 | */ | |
845 | index = 0; /* index of the current trackpoint. */ | |
846 | for (i = 0; i < num_chunks; i++) { | |
847 | /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur. | |
848 | * find the first trackpoint outside the current interval, averaging the distance between intermediate trackpoints. | |
849 | */ | |
850 | if (t[0] + i*chunk_dur >= t[index]) { | |
851 | gdouble acc_s = 0; // No need for acc_t | |
852 | while (t[0] + i*chunk_dur >= t[index]) { | |
853 | acc_s += (s[index+1]-s[index]); | |
854 | index++; | |
855 | } | |
856 | // The only bit that's really different from the speed map - just keep an accululative record distance | |
857 | v[i] = i ? v[i-1]+acc_s : acc_s; | |
858 | } | |
859 | else if (i) { | |
860 | v[i] = v[i-1]; | |
861 | } | |
862 | else { | |
863 | v[i] = 0; | |
864 | } | |
865 | } | |
866 | g_free(s); | |
867 | g_free(t); | |
868 | return v; | |
869 | } | |
870 | ||
8de26632 RN |
871 | /** |
872 | * This uses the 'time' based method to make the graph, (which is a simpler compared to the elevation/distance) | |
873 | * This results in a slightly blocky graph when it does not have many trackpoints: <60 | |
874 | * NB Somehow the elevation/distance applies some kind of smoothing algorithm, | |
875 | * but I don't think any one understands it any more (I certainly don't ATM) | |
876 | */ | |
877 | gdouble *vik_track_make_elevation_time_map ( const VikTrack *tr, guint16 num_chunks ) | |
878 | { | |
879 | time_t t1, t2; | |
880 | gdouble duration, chunk_dur; | |
881 | GList *iter = tr->trackpoints; | |
882 | ||
883 | if (!iter || !iter->next) /* zero- or one-point track */ | |
884 | return NULL; | |
885 | ||
886 | /* test if there's anything worth calculating */ | |
887 | gboolean okay = FALSE; | |
888 | while ( iter ) { | |
889 | if ( VIK_TRACKPOINT(iter->data)->altitude != VIK_DEFAULT_ALTITUDE ) { | |
890 | okay = TRUE; | |
891 | break; | |
892 | } | |
893 | iter = iter->next; | |
894 | } | |
895 | if ( ! okay ) | |
896 | return NULL; | |
897 | ||
898 | t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
899 | t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
900 | duration = t2 - t1; | |
901 | ||
902 | if ( !t1 || !t2 || !duration ) | |
903 | return NULL; | |
904 | ||
905 | if (duration < 0) { | |
906 | g_warning("negative duration: unsorted trackpoint timestamps?"); | |
907 | return NULL; | |
908 | } | |
909 | gint pt_count = vik_track_get_tp_count(tr); | |
910 | ||
911 | // Reset iterator back to the beginning | |
912 | iter = tr->trackpoints; | |
913 | ||
914 | gdouble *pts = g_malloc ( sizeof(gdouble) * num_chunks ); // The return altitude values | |
915 | gdouble *s = g_malloc(sizeof(double) * pt_count); // calculation altitudes | |
916 | gdouble *t = g_malloc(sizeof(double) * pt_count); // calculation times | |
917 | ||
918 | chunk_dur = duration / num_chunks; | |
919 | ||
920 | s[0] = VIK_TRACKPOINT(iter->data)->altitude; | |
921 | t[0] = VIK_TRACKPOINT(iter->data)->timestamp; | |
922 | iter = tr->trackpoints->next; | |
923 | gint numpts = 1; | |
924 | while (iter) { | |
925 | s[numpts] = VIK_TRACKPOINT(iter->data)->altitude; | |
926 | t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; | |
927 | numpts++; | |
928 | iter = iter->next; | |
929 | } | |
930 | ||
931 | /* In the following computation, we iterate through periods of time of duration chunk_dur. | |
932 | * The first period begins at the beginning of the track. The last period ends at the end of the track. | |
933 | */ | |
934 | gint index = 0; /* index of the current trackpoint. */ | |
935 | gint i; | |
936 | for (i = 0; i < num_chunks; i++) { | |
937 | /* we are now covering the interval from t[0] + i*chunk_dur to t[0] + (i+1)*chunk_dur. | |
938 | * find the first trackpoint outside the current interval, averaging the heights between intermediate trackpoints. | |
939 | */ | |
940 | if (t[0] + i*chunk_dur >= t[index]) { | |
941 | gdouble acc_s = s[index]; // initialise to first point | |
942 | while (t[0] + i*chunk_dur >= t[index]) { | |
943 | acc_s += (s[index+1]-s[index]); | |
944 | index++; | |
945 | } | |
946 | pts[i] = acc_s; | |
947 | } | |
948 | else if (i) { | |
949 | pts[i] = pts[i-1]; | |
950 | } | |
951 | else { | |
952 | pts[i] = 0; | |
953 | } | |
954 | } | |
955 | g_free(s); | |
956 | g_free(t); | |
957 | ||
958 | return pts; | |
959 | } | |
960 | ||
7b624086 RN |
961 | /** |
962 | * Make a speed/distance map | |
963 | */ | |
964 | gdouble *vik_track_make_speed_dist_map ( const VikTrack *tr, guint16 num_chunks ) | |
965 | { | |
966 | gdouble *v, *s, *t; | |
967 | time_t t1, t2; | |
968 | gint i, pt_count, numpts, index; | |
969 | GList *iter; | |
970 | gdouble duration, total_length, chunk_length; | |
971 | ||
972 | if ( ! tr->trackpoints ) | |
973 | return NULL; | |
974 | ||
975 | t1 = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
976 | t2 = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
977 | duration = t2 - t1; | |
978 | ||
979 | if ( !t1 || !t2 || !duration ) | |
980 | return NULL; | |
981 | ||
982 | if (duration < 0) { | |
983 | g_warning("negative duration: unsorted trackpoint timestamps?"); | |
984 | return NULL; | |
985 | } | |
986 | ||
987 | total_length = vik_track_get_length_including_gaps ( tr ); | |
988 | chunk_length = total_length / num_chunks; | |
989 | pt_count = vik_track_get_tp_count(tr); | |
990 | ||
991 | if (chunk_length <= 0) { | |
992 | return NULL; | |
993 | } | |
994 | ||
995 | v = g_malloc ( sizeof(gdouble) * num_chunks ); | |
996 | s = g_malloc ( sizeof(double) * pt_count ); | |
997 | t = g_malloc ( sizeof(double) * pt_count ); | |
998 | ||
999 | // No special handling of segments ATM... | |
1000 | iter = tr->trackpoints->next; | |
1001 | numpts = 0; | |
1002 | s[0] = 0; | |
e979bdab | 1003 | t[0] = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; |
7b624086 RN |
1004 | numpts++; |
1005 | while (iter) { | |
1006 | s[numpts] = s[numpts-1] + vik_coord_diff ( &(VIK_TRACKPOINT(iter->prev->data)->coord), &(VIK_TRACKPOINT(iter->data)->coord) ); | |
1007 | t[numpts] = VIK_TRACKPOINT(iter->data)->timestamp; | |
1008 | numpts++; | |
1009 | iter = iter->next; | |
1010 | } | |
1011 | ||
1012 | // Iterate through a portion of the track to get an average speed for that part | |
1013 | // This will essentially interpolate between segments, which I think is right given the usage of 'get_length_including_gaps' | |
1014 | index = 0; /* index of the current trackpoint. */ | |
1015 | for (i = 0; i < num_chunks; i++) { | |
1016 | // Similar to the make_speed_map, but instead of using a time chunk, use a distance chunk | |
1017 | if (s[0] + i*chunk_length >= s[index]) { | |
1018 | gdouble acc_t = 0, acc_s = 0; | |
1019 | while (s[0] + i*chunk_length >= s[index]) { | |
1020 | acc_s += (s[index+1]-s[index]); | |
1021 | acc_t += (t[index+1]-t[index]); | |
1022 | index++; | |
1023 | } | |
1024 | v[i] = acc_s/acc_t; | |
1025 | } | |
1026 | else if (i) { | |
1027 | v[i] = v[i-1]; | |
1028 | } | |
1029 | else { | |
1030 | v[i] = 0; | |
1031 | } | |
1032 | } | |
1033 | g_free(s); | |
1034 | g_free(t); | |
1035 | return v; | |
1036 | } | |
1037 | ||
bf35388d | 1038 | /* by Alex Foobarian */ |
ddc2372e | 1039 | VikTrackpoint *vik_track_get_closest_tp_by_percentage_dist ( VikTrack *tr, gdouble reldist, gdouble *meters_from_start ) |
24d5c7e2 EB |
1040 | { |
1041 | gdouble dist = vik_track_get_length_including_gaps(tr) * reldist; | |
1042 | gdouble current_dist = 0.0; | |
1043 | gdouble current_inc = 0.0; | |
24d5c7e2 EB |
1044 | if ( tr->trackpoints ) |
1045 | { | |
1046 | GList *iter = tr->trackpoints->next; | |
ecb51018 | 1047 | GList *last_iter = NULL; |
ddc2372e | 1048 | gdouble last_dist = 0.0; |
24d5c7e2 EB |
1049 | while (iter) |
1050 | { | |
1051 | current_inc = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), | |
1052 | &(VIK_TRACKPOINT(iter->prev->data)->coord) ); | |
ddc2372e | 1053 | last_dist = current_dist; |
24d5c7e2 EB |
1054 | current_dist += current_inc; |
1055 | if ( current_dist >= dist ) | |
1056 | break; | |
ecb51018 | 1057 | last_iter = iter; |
24d5c7e2 EB |
1058 | iter = iter->next; |
1059 | } | |
ddc2372e QT |
1060 | if (!iter) { /* passing the end the track */ |
1061 | if (last_iter) { | |
1062 | if (meters_from_start) | |
1063 | *meters_from_start = last_dist; | |
1064 | return(VIK_TRACKPOINT(last_iter->data)); | |
1065 | } | |
1066 | else | |
1067 | return NULL; | |
1068 | } | |
24d5c7e2 EB |
1069 | /* we've gone past the dist already, was prev trackpoint closer? */ |
1070 | /* should do a vik_coord_average_weighted() thingy. */ | |
ddc2372e QT |
1071 | if ( iter->prev && abs(current_dist-current_inc-dist) < abs(current_dist-dist) ) { |
1072 | if (meters_from_start) | |
1073 | *meters_from_start = last_dist; | |
24d5c7e2 | 1074 | iter = iter->prev; |
ddc2372e QT |
1075 | } |
1076 | else | |
1077 | if (meters_from_start) | |
1078 | *meters_from_start = current_dist; | |
24d5c7e2 | 1079 | |
e1e2f2c6 | 1080 | return VIK_TRACKPOINT(iter->data); |
24d5c7e2 EB |
1081 | |
1082 | } | |
1083 | return NULL; | |
1084 | } | |
b42a25ba | 1085 | |
ddc2372e | 1086 | VikTrackpoint *vik_track_get_closest_tp_by_percentage_time ( VikTrack *tr, gdouble reltime, time_t *seconds_from_start ) |
32e48121 QT |
1087 | { |
1088 | time_t t_pos, t_start, t_end, t_total; | |
1089 | t_start = VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
1090 | t_end = VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->timestamp; | |
1091 | t_total = t_end - t_start; | |
1092 | ||
1093 | t_pos = t_start + t_total * reltime; | |
1094 | ||
ddc2372e QT |
1095 | if ( !tr->trackpoints ) |
1096 | return NULL; | |
32e48121 | 1097 | |
ddc2372e QT |
1098 | GList *iter = tr->trackpoints; |
1099 | ||
1100 | while (iter) { | |
1101 | if (VIK_TRACKPOINT(iter->data)->timestamp == t_pos) | |
1102 | break; | |
1103 | if (VIK_TRACKPOINT(iter->data)->timestamp > t_pos) { | |
1104 | if (iter->prev == NULL) /* first trackpoint */ | |
1105 | break; | |
1106 | time_t t_before = t_pos - VIK_TRACKPOINT(iter->prev)->timestamp; | |
1107 | time_t t_after = VIK_TRACKPOINT(iter->data)->timestamp - t_pos; | |
1108 | if (t_before <= t_after) | |
1109 | iter = iter->prev; | |
1110 | break; | |
32e48121 | 1111 | } |
ddc2372e QT |
1112 | else if ((iter->next == NULL) && (t_pos < (VIK_TRACKPOINT(iter->data)->timestamp + 3))) /* last trackpoint: accommodate for round-off */ |
1113 | break; | |
1114 | iter = iter->next; | |
32e48121 | 1115 | } |
ddc2372e QT |
1116 | |
1117 | if (!iter) | |
1118 | return NULL; | |
1119 | if (seconds_from_start) | |
1120 | *seconds_from_start = VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(tr->trackpoints->data)->timestamp; | |
1121 | return VIK_TRACKPOINT(iter->data); | |
32e48121 QT |
1122 | } |
1123 | ||
03e7da75 RN |
1124 | VikTrackpoint* vik_track_get_tp_by_max_speed ( const VikTrack *tr ) |
1125 | { | |
1126 | gdouble maxspeed = 0.0, speed = 0.0; | |
1127 | ||
1128 | if ( !tr->trackpoints ) | |
1129 | return NULL; | |
1130 | ||
1131 | GList *iter = tr->trackpoints; | |
1132 | VikTrackpoint *max_speed_tp = NULL; | |
1133 | ||
1134 | while (iter) { | |
7d7acd6d | 1135 | if (iter->prev) { |
03e7da75 RN |
1136 | if ( VIK_TRACKPOINT(iter->data)->has_timestamp && |
1137 | VIK_TRACKPOINT(iter->prev->data)->has_timestamp && | |
1138 | (! VIK_TRACKPOINT(iter->data)->newsegment) ) { | |
1139 | speed = vik_coord_diff ( &(VIK_TRACKPOINT(iter->data)->coord), &(VIK_TRACKPOINT(iter->prev->data)->coord) ) | |
1140 | / ABS(VIK_TRACKPOINT(iter->data)->timestamp - VIK_TRACKPOINT(iter->prev->data)->timestamp); | |
1141 | if ( speed > maxspeed ) { | |
1142 | maxspeed = speed; | |
1143 | max_speed_tp = VIK_TRACKPOINT(iter->data); | |
1144 | } | |
1145 | } | |
1146 | } | |
1147 | iter = iter->next; | |
1148 | } | |
1149 | ||
1150 | if (!max_speed_tp) | |
1151 | return NULL; | |
1152 | ||
1153 | return max_speed_tp; | |
1154 | } | |
1155 | ||
c28faca8 RN |
1156 | VikTrackpoint* vik_track_get_tp_by_max_alt ( const VikTrack *tr ) |
1157 | { | |
1158 | gdouble maxalt = -5000.0; | |
1159 | if ( !tr->trackpoints ) | |
1160 | return NULL; | |
1161 | ||
1162 | GList *iter = tr->trackpoints; | |
1163 | VikTrackpoint *max_alt_tp = NULL; | |
1164 | ||
1165 | while (iter) { | |
1166 | if ( VIK_TRACKPOINT(iter->data)->altitude > maxalt ) { | |
1167 | maxalt = VIK_TRACKPOINT(iter->data)->altitude; | |
1168 | max_alt_tp = VIK_TRACKPOINT(iter->data); | |
1169 | } | |
1170 | iter = iter->next; | |
1171 | } | |
1172 | ||
1173 | if (!max_alt_tp) | |
1174 | return NULL; | |
1175 | ||
1176 | return max_alt_tp; | |
1177 | } | |
1178 | ||
1179 | VikTrackpoint* vik_track_get_tp_by_min_alt ( const VikTrack *tr ) | |
1180 | { | |
1181 | gdouble minalt = 25000.0; | |
1182 | if ( !tr->trackpoints ) | |
1183 | return NULL; | |
1184 | ||
1185 | GList *iter = tr->trackpoints; | |
1186 | VikTrackpoint *min_alt_tp = NULL; | |
1187 | ||
1188 | while (iter) { | |
1189 | if ( VIK_TRACKPOINT(iter->data)->altitude < minalt ) { | |
1190 | minalt = VIK_TRACKPOINT(iter->data)->altitude; | |
1191 | min_alt_tp = VIK_TRACKPOINT(iter->data); | |
1192 | } | |
1193 | iter = iter->next; | |
1194 | } | |
1195 | ||
1196 | if (!min_alt_tp) | |
1197 | return NULL; | |
1198 | ||
1199 | return min_alt_tp; | |
1200 | } | |
1201 | ||
b42a25ba EB |
1202 | gboolean vik_track_get_minmax_alt ( const VikTrack *tr, gdouble *min_alt, gdouble *max_alt ) |
1203 | { | |
1204 | *min_alt = 25000; | |
1205 | *max_alt = -5000; | |
1206 | if ( tr && tr->trackpoints && tr->trackpoints->data && (VIK_TRACKPOINT(tr->trackpoints->data)->altitude != VIK_DEFAULT_ALTITUDE) ) { | |
1207 | GList *iter = tr->trackpoints->next; | |
1208 | gdouble tmp_alt; | |
1209 | while (iter) | |
1210 | { | |
1211 | tmp_alt = VIK_TRACKPOINT(iter->data)->altitude; | |
1212 | if ( tmp_alt > *max_alt ) | |
1213 | *max_alt = tmp_alt; | |
1214 | if ( tmp_alt < *min_alt ) | |
1215 | *min_alt = tmp_alt; | |
1216 | iter = iter->next; | |
1217 | } | |
1218 | return TRUE; | |
1219 | } | |
1220 | return FALSE; | |
1221 | } | |
ddc47a46 AF |
1222 | |
1223 | void vik_track_marshall ( VikTrack *tr, guint8 **data, guint *datalen) | |
1224 | { | |
1225 | GList *tps; | |
1226 | GByteArray *b = g_byte_array_new(); | |
1227 | guint len; | |
1228 | guint intp, ntp; | |
1229 | ||
1230 | g_byte_array_append(b, (guint8 *)tr, sizeof(*tr)); | |
1231 | ||
1232 | /* we'll fill out number of trackpoints later */ | |
1233 | intp = b->len; | |
1234 | g_byte_array_append(b, (guint8 *)&len, sizeof(len)); | |
1235 | ||
1236 | tps = tr->trackpoints; | |
1237 | ntp = 0; | |
1238 | while (tps) { | |
1239 | g_byte_array_append(b, (guint8 *)tps->data, sizeof(VikTrackpoint)); | |
1240 | tps = tps->next; | |
1241 | ntp++; | |
1242 | } | |
1243 | *(guint *)(b->data + intp) = ntp; | |
1244 | ||
6b2f262e RN |
1245 | // This allocates space for variant sized strings |
1246 | // and copies that amount of data from the track to byte array | |
1247 | #define vtm_append(s) \ | |
1248 | len = (s) ? strlen(s)+1 : 0; \ | |
1249 | g_byte_array_append(b, (guint8 *)&len, sizeof(len)); \ | |
1250 | if (s) g_byte_array_append(b, (guint8 *)s, len); | |
ce4bd1cf | 1251 | |
6b2f262e RN |
1252 | vtm_append(tr->name); |
1253 | vtm_append(tr->comment); | |
1254 | vtm_append(tr->description); | |
ddc47a46 AF |
1255 | |
1256 | *data = b->data; | |
1257 | *datalen = b->len; | |
1258 | g_byte_array_free(b, FALSE); | |
1259 | } | |
1260 | ||
6b2f262e RN |
1261 | /* |
1262 | * Take a byte array and convert it into a Track | |
1263 | */ | |
ddc47a46 AF |
1264 | VikTrack *vik_track_unmarshall (guint8 *data, guint datalen) |
1265 | { | |
1266 | guint len; | |
1267 | VikTrack *new_tr = vik_track_new(); | |
1268 | VikTrackpoint *new_tp; | |
1269 | guint ntp; | |
1270 | gint i; | |
1271 | ||
0d2b891f | 1272 | /* basic properties: */ |
ddc47a46 | 1273 | new_tr->visible = ((VikTrack *)data)->visible; |
0d2b891f | 1274 | new_tr->is_route = ((VikTrack *)data)->is_route; |
b1453c16 RN |
1275 | new_tr->has_color = ((VikTrack *)data)->has_color; |
1276 | new_tr->color = ((VikTrack *)data)->color; | |
0d2b891f | 1277 | |
ddc47a46 AF |
1278 | data += sizeof(*new_tr); |
1279 | ||
1280 | ntp = *(guint *)data; | |
1281 | data += sizeof(ntp); | |
1282 | ||
1283 | for (i=0; i<ntp; i++) { | |
1284 | new_tp = vik_trackpoint_new(); | |
1285 | memcpy(new_tp, data, sizeof(*new_tp)); | |
1286 | data += sizeof(*new_tp); | |
1287 | new_tr->trackpoints = g_list_append(new_tr->trackpoints, new_tp); | |
1288 | } | |
1289 | ||
6b2f262e RN |
1290 | #define vtu_get(s) \ |
1291 | len = *(guint *)data; \ | |
1292 | data += sizeof(len); \ | |
1293 | if (len) { \ | |
1294 | (s) = g_strdup((gchar *)data); \ | |
1295 | } else { \ | |
1296 | (s) = NULL; \ | |
1297 | } \ | |
ce4bd1cf RN |
1298 | data += len; |
1299 | ||
6b2f262e RN |
1300 | vtu_get(new_tr->name); |
1301 | vtu_get(new_tr->comment); | |
1302 | vtu_get(new_tr->description); | |
1303 | ||
ddc47a46 AF |
1304 | return new_tr; |
1305 | } | |
ad0a8c2d | 1306 | |
55906514 | 1307 | void vik_track_apply_dem_data ( VikTrack *tr ) |
ad0a8c2d EB |
1308 | { |
1309 | GList *tp_iter; | |
1310 | gint16 elev; | |
ad0a8c2d EB |
1311 | tp_iter = tr->trackpoints; |
1312 | while ( tp_iter ) { | |
1313 | /* TODO: of the 4 possible choices we have for choosing an elevation | |
1314 | * (trackpoint in between samples), choose the one with the least elevation change | |
1315 | * as the last */ | |
5ef1d57e | 1316 | elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(tp_iter->data)->coord), VIK_DEM_INTERPOL_BEST ); |
ad0a8c2d EB |
1317 | if ( elev != VIK_DEM_INVALID_ELEVATION ) |
1318 | VIK_TRACKPOINT(tp_iter->data)->altitude = elev; | |
1319 | tp_iter = tp_iter->next; | |
1320 | } | |
1321 | } | |
bddd2056 | 1322 | |
e85535ea RN |
1323 | /* |
1324 | * Apply DEM data (if available) - to only the last trackpoint | |
1325 | */ | |
1326 | void vik_track_apply_dem_data_last_trackpoint ( VikTrack *tr ) | |
1327 | { | |
1328 | gint16 elev; | |
1329 | if ( tr->trackpoints ) { | |
1330 | /* As in vik_track_apply_dem_data above - use 'best' interpolation method */ | |
1331 | elev = a_dems_get_elev_by_coord ( &(VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->coord), VIK_DEM_INTERPOL_BEST ); | |
1332 | if ( elev != VIK_DEM_INVALID_ELEVATION ) | |
1333 | VIK_TRACKPOINT(g_list_last(tr->trackpoints)->data)->altitude = elev; | |
1334 | } | |
1335 | } | |
1336 | ||
bddd2056 EB |
1337 | /* appends t2 to t1, leaving t2 with no trackpoints */ |
1338 | void vik_track_steal_and_append_trackpoints ( VikTrack *t1, VikTrack *t2 ) | |
1339 | { | |
1340 | if ( t1->trackpoints ) { | |
1341 | GList *tpiter = t1->trackpoints; | |
1342 | while ( tpiter->next ) | |
1343 | tpiter = tpiter->next; | |
1344 | tpiter->next = t2->trackpoints; | |
1345 | t2->trackpoints->prev = tpiter; | |
1346 | } else | |
1347 | t1->trackpoints = t2->trackpoints; | |
1348 | t2->trackpoints = NULL; | |
1349 | } | |
c3deba01 EB |
1350 | |
1351 | /* starting at the end, looks backwards for the last "double point", a duplicate trackpoint. | |
7ff7d728 RN |
1352 | * If there is no double point, deletes all the trackpoints. |
1353 | * Returns the new end of the track (or the start if there are no double points) | |
c3deba01 EB |
1354 | */ |
1355 | VikCoord *vik_track_cut_back_to_double_point ( VikTrack *tr ) | |
1356 | { | |
1357 | GList *iter = tr->trackpoints; | |
1358 | VikCoord *rv; | |
1359 | ||
1360 | if ( !iter ) | |
1361 | return NULL; | |
1362 | while ( iter->next ) | |
1363 | iter = iter->next; | |
1364 | ||
1365 | ||
1366 | while ( iter->prev ) { | |
1367 | if ( vik_coord_equals((VikCoord *)iter->data, (VikCoord *)iter->prev->data) ) { | |
1368 | GList *prev = iter->prev; | |
1369 | ||
1370 | rv = g_malloc(sizeof(VikCoord)); | |
1371 | *rv = *((VikCoord *) iter->data); | |
1372 | ||
1373 | /* truncate trackpoint list */ | |
1374 | iter->prev = NULL; /* pretend it's the end */ | |
1375 | g_list_foreach ( iter, (GFunc) g_free, NULL ); | |
1376 | g_list_free( iter ); | |
1377 | ||
1378 | prev->next = NULL; | |
1379 | ||
1380 | return rv; | |
1381 | } | |
1382 | iter = iter->prev; | |
1383 | } | |
1384 | ||
1385 | /* no double point found! */ | |
1386 | rv = g_malloc(sizeof(VikCoord)); | |
1387 | *rv = *((VikCoord *) tr->trackpoints->data); | |
1388 | g_list_foreach ( tr->trackpoints, (GFunc) g_free, NULL ); | |
1389 | g_list_free( tr->trackpoints ); | |
1390 | tr->trackpoints = NULL; | |
1391 | return rv; | |
1392 | } | |
1393 |