You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

interpolate.cc 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Interpolate filter
  3. Copyright (C) 2002 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "filterdefs.h"
  18. #include "grtcirc.h"
  19. #include <stdlib.h>
  20. #if FILTERS_ENABLED
  21. #define MYNAME "Interpolate filter"
  22. static char* opt_interval = NULL;
  23. unsigned int interval = 0;
  24. static char* opt_dist = NULL;
  25. double dist = 0;
  26. static char* opt_route = NULL;
  27. static
  28. arglist_t interpfilt_args[] = {
  29. {
  30. "time", &opt_interval, "Time interval in seconds", NULL,
  31. ARGTYPE_BEGIN_EXCL | ARGTYPE_BEGIN_REQ | ARGTYPE_INT,
  32. "0", NULL
  33. },
  34. {
  35. "distance", &opt_dist, "Distance interval in miles or kilometers",
  36. NULL, ARGTYPE_END_EXCL | ARGTYPE_END_REQ | ARGTYPE_STRING,
  37. ARG_NOMINMAX
  38. },
  39. {
  40. "route", &opt_route, "Interpolate routes instead", NULL,
  41. ARGTYPE_BOOL, ARG_NOMINMAX
  42. },
  43. ARG_TERMINATOR
  44. };
  45. void
  46. interpfilt_process(void)
  47. {
  48. queue* backuproute = NULL;
  49. queue* elem, *tmp, *elem2, *tmp2;
  50. route_head* rte_new;
  51. int count = 0;
  52. int first = 0;
  53. double lat1 = 0, lon1 = 0;
  54. unsigned int time1 = 0;
  55. unsigned int timen;
  56. double distn;
  57. double curdist;
  58. double rt1, rn1, rt2, rn2;
  59. if (opt_route) {
  60. route_backup(&count, &backuproute);
  61. route_flush_all_routes();
  62. } else {
  63. track_backup(&count, &backuproute);
  64. route_flush_all_tracks();
  65. }
  66. if (count == 0) {
  67. fatal(MYNAME ": Found no routes or tracks to operate on.\n");
  68. }
  69. QUEUE_FOR_EACH(backuproute, elem, tmp) {
  70. route_head* rte_old = (route_head*)elem;
  71. rte_new = route_head_alloc();
  72. rte_new->rte_name = rte_old->rte_name;
  73. rte_new->rte_desc = rte_old->rte_desc;
  74. rte_new->fs = fs_chain_copy(rte_old->fs);
  75. rte_new->rte_num = rte_old->rte_num;
  76. if (opt_route) {
  77. route_add_head(rte_new);
  78. } else {
  79. track_add_head(rte_new);
  80. }
  81. first = 1;
  82. QUEUE_FOR_EACH(&rte_old->waypoint_list, elem2, tmp2) {
  83. Waypoint* wpt = (Waypoint*)elem2;
  84. if (first) {
  85. first = 0;
  86. } else {
  87. if (opt_interval &&
  88. wpt->creation_time.toTime_t() - time1 > interval) {
  89. for (timen = time1+interval;
  90. timen < wpt->creation_time.toTime_t();
  91. timen += interval) {
  92. Waypoint* wpt_new = new Waypoint(*wpt);
  93. wpt_new->SetCreationTime(timen);
  94. wpt_new->shortname = QString();
  95. wpt_new->description = QString();
  96. linepart(lat1, lon1,
  97. wpt->latitude, wpt->longitude,
  98. (double)(timen-time1)/
  99. (double)(wpt->creation_time.toTime_t() - time1),
  100. &wpt_new->latitude,
  101. &wpt_new->longitude);
  102. if (opt_route) {
  103. route_add_wpt(rte_new, wpt_new);
  104. } else {
  105. track_add_wpt(rte_new, wpt_new);
  106. }
  107. }
  108. } else if (opt_dist) {
  109. rt1 = RAD(lat1);
  110. rn1 = RAD(lon1);
  111. rt2 = RAD(wpt->latitude);
  112. rn2 = RAD(wpt->longitude);
  113. curdist = gcdist(rt1, rn1, rt2, rn2);
  114. curdist = radtomiles(curdist);
  115. if (curdist > dist) {
  116. for (distn = dist;
  117. distn < curdist;
  118. distn += dist) {
  119. Waypoint* wpt_new = new Waypoint(*wpt);
  120. wpt_new->SetCreationTime(distn/curdist*
  121. (wpt->creation_time.toTime_t() - time1) + time1);
  122. wpt_new->shortname = QString();
  123. wpt_new->description = QString();
  124. linepart(lat1, lon1,
  125. wpt->latitude, wpt->longitude,
  126. distn/curdist,
  127. &wpt_new->latitude,
  128. &wpt_new->longitude);
  129. if (opt_route) {
  130. route_add_wpt(rte_new, wpt_new);
  131. } else {
  132. track_add_wpt(rte_new, wpt_new);
  133. }
  134. }
  135. }
  136. }
  137. }
  138. if (opt_route) {
  139. route_add_wpt(rte_new, new Waypoint(*wpt));
  140. } else {
  141. track_add_wpt(rte_new, new Waypoint(*wpt));
  142. }
  143. lat1 = wpt->latitude;
  144. lon1 = wpt->longitude;
  145. time1 = wpt->creation_time.toTime_t();
  146. }
  147. }
  148. route_flush(backuproute);
  149. xfree(backuproute);
  150. }
  151. void
  152. interpfilt_init(const char* args)
  153. {
  154. char* fm;
  155. if (opt_interval && opt_dist) {
  156. fatal(MYNAME ": Can't interpolate on both time and distance.\n");
  157. } else if (opt_interval && opt_route) {
  158. fatal(MYNAME ": Can't interpolate routes on time.\n");
  159. } else if (opt_interval) {
  160. interval = atoi(opt_interval);
  161. } else if (opt_dist) {
  162. dist = strtod(opt_dist, &fm);
  163. if ((*fm == 'k') || (*fm == 'K')) {
  164. /* distance is kilometers, convert to miles */
  165. dist *= .6214;
  166. }
  167. } else {
  168. fatal(MYNAME ": No interval specified.\n");
  169. }
  170. }
  171. filter_vecs_t interpolatefilt_vecs = {
  172. interpfilt_init,
  173. interpfilt_process,
  174. NULL,
  175. NULL,
  176. interpfilt_args
  177. };
  178. #endif // FILTERS_ENABLED