-
Notifications
You must be signed in to change notification settings - Fork 127
/
filter_vecs.cc
432 lines (392 loc) · 11.2 KB
/
filter_vecs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Describe vectors containing filter operations.
Copyright (C) 2002-2014 Robert Lipe, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "filter_vecs.h"
#include <QByteArray> // for QByteArray
#include <QString> // for QString
#include <QStringList> // for QStringList
#include <QVector> // for QVector
#include <Qt> // for CaseInsensitive
#include <QtGlobal> // for qPrintable
#include <algorithm> // for sort
#include <cassert> // for assert
#include <cstdio> // for printf
#include <type_traits> // for is_base_of
#include "arcdist.h" // for ArcDistanceFilter
#include "bend.h" // for BendFilter
#include "defs.h" // for arglist_t, CSTR, xfree, ARGTYPE_HIDDEN, gbFatal, global_options, global_opts, ARGTYPE_REQUIRED
#include "discard.h" // for DiscardFilter
#include "duplicate.h" // for DuplicateFilter
#include "filter.h" // for Filter
#include "gbversion.h" // for WEB_DOC_DIR
#include "height.h" // for HeightFilter
#include "inifile.h" // for inifile_readstr
#include "interpolate.h" // for InterpolateFilter
#include "nukedata.h" // for NukeDataFilter
#include "polygon.h" // for PolygonFilter
#include "position.h" // for PositionFilter
#include "radius.h" // for RadiusFilter
#include "resample.h" // for ResampleFilter
#include "reverse_route.h" // for ReverseRouteFilter
#include "smplrout.h" // for SimplifyRouteFilter
#include "sort.h" // for SortFilter
#include "stackfilter.h" // for StackFilter
#include "swapdata.h" // for SwapDataFilter
#include "trackfilter.h" // for TrackFilter
#include "transform.h" // for TransformFilter
#include "validate.h" // for ValidateFilter
#include "vecs.h" // for Vecs
template <typename T>
Filter* fltfactory()
{
static_assert(std::is_base_of_v<Filter, T>, "T must be derived from Filter");
return new T();
}
struct FilterVecs::Impl {
StackFilter stackfilt;
const QVector<fl_vecs_t> filter_vec_list = {
#if FILTERS_ENABLED
{
nullptr,
"arc",
"Include Only Points Within Distance of Arc",
&fltfactory<ArcDistanceFilter>
},
{
nullptr,
"bend",
"Add points before and after bends in routes",
&fltfactory<BendFilter>
},
{
nullptr,
"discard",
"Remove unreliable points with high hdop or vdop",
&fltfactory<DiscardFilter>
},
{
nullptr,
"duplicate",
"Remove Duplicates",
&fltfactory<DuplicateFilter>
},
{
nullptr,
"interpolate",
"Interpolate between trackpoints",
&fltfactory<InterpolateFilter>
},
{
nullptr,
"nuketypes",
"Remove all waypoints, tracks, or routes",
&fltfactory<NukeDataFilter>
},
{
nullptr,
"polygon",
"Include Only Points Inside Polygon",
&fltfactory<PolygonFilter>
},
{
nullptr,
"position",
"Remove Points Within Distance",
&fltfactory<PositionFilter>
},
{
nullptr,
"radius",
"Include Only Points Within Radius",
&fltfactory<RadiusFilter>
},
{
nullptr,
"resample",
"Resample Track",
&fltfactory<ResampleFilter>
},
{
nullptr,
"simplify",
"Simplify routes",
&fltfactory<SimplifyRouteFilter>
},
{
nullptr,
"sort",
"Rearrange waypoints, routes and/or tracks by resorting",
&fltfactory<SortFilter>
},
{
&stackfilt,
"stack",
"Save and restore waypoint lists",
nullptr
},
{
nullptr,
"reverse",
"Reverse stops within routes",
&fltfactory<ReverseRouteFilter>
},
{
nullptr,
"track",
"Manipulate track lists",
&fltfactory<TrackFilter>
},
{
nullptr,
"transform",
"Transform waypoints into a route, tracks into routes, ...",
&fltfactory<TransformFilter>
},
{
nullptr,
"height",
"Manipulate altitudes",
&fltfactory<HeightFilter>
},
{
nullptr,
"swap",
"Swap latitude and longitude of all loaded points",
&fltfactory<SwapDataFilter>
},
{
nullptr,
"validate",
"Validate internal data structures",
&fltfactory<ValidateFilter>
}
#elif defined (MINIMAL_FILTERS)
{
nullptr,
"track",
"Manipulate track lists",
&fltfactory<TrackFilter>
}
#endif
};
};
FilterVecs& FilterVecs::Instance()
{
static Impl impl;
static FilterVecs instance(&impl);
return instance;
}
void FilterVecs::prepare_filter(const fltinfo_t& fltdata)
{
QVector<arglist_t>* args = fltdata->get_args();
Vecs::validate_options(fltdata.options, args, fltdata.fltname);
/* step 1: initialize by inifile or default values */
if (args && !args->isEmpty()) {
assert(args->isDetached());
for (auto& arg : *args) {
QString qtemp = inifile_readstr(global_opts.inifile, fltdata.fltname, arg.argstring);
if (qtemp.isNull()) {
qtemp = inifile_readstr(global_opts.inifile, "Common filter settings", arg.argstring);
}
if (qtemp.isNull()) {
Vecs::assign_option(fltdata.fltname, arg, arg.defaultvalue);
} else {
Vecs::assign_option(fltdata.fltname, arg, qtemp);
}
}
}
/* step 2: override settings with command-line values */
if (!fltdata.options.isEmpty()) {
if (args && !args->isEmpty()) {
assert(args->isDetached());
for (auto& arg : *args) {
const QString opt = Vecs::get_option(fltdata.options, arg.argstring);
if (!opt.isNull()) {
Vecs::assign_option(fltdata.fltname, arg, opt);
}
}
}
}
if (global_opts.debug_level >= 1) {
Vecs::disp_vec_options(fltdata.fltname, args);
}
}
FilterVecs::fltinfo_t FilterVecs::find_filter_vec(const QString& fltargstring)
{
QStringList options = fltargstring.split(',');
if (options.isEmpty()) {
gbFatal("A filter name is required.\n");
}
const QString fltname = options.takeFirst();
for (const auto& vec : d_ptr_->filter_vec_list) {
if (fltname.compare(vec.name, Qt::CaseInsensitive) != 0) {
continue;
}
return {vec.vec, vec.name, options, vec.factory};
}
/*
* Not found.
*/
return {};
}
void FilterVecs::free_filter_vec(Filter* flt)
{
Vecs::free_options(flt->get_args());
}
void FilterVecs::init_filter_vec(Filter* flt, const QString& fltname)
{
QVector<arglist_t>* args = flt->get_args();
if (args && !args->isEmpty()) {
assert(args->isDetached());
for (auto& arg : *args) {
if (arg.argval != nullptr) {
arg.argval->reset();
QString id = QStringLiteral("%1(%2)").arg(fltname, arg.argstring);
arg.argval->init(id);
}
}
}
}
void FilterVecs::init_filter_vecs()
{
for (const auto& vec : d_ptr_->filter_vec_list) {
if (vec.vec != nullptr) {
init_filter_vec(vec.vec, vec.name);
}
}
}
void FilterVecs::exit_filter_vec(Filter* flt)
{
(flt->exit)();
Vecs::free_options(flt->get_args());
}
void FilterVecs::exit_filter_vecs()
{
for (const auto& vec : d_ptr_->filter_vec_list) {
if (vec.vec != nullptr) {
exit_filter_vec(vec.vec);
}
}
}
/*
* Display the available formats in a format that's easy for humans to
* parse for help on available command line options.
*/
void FilterVecs::disp_filter_vec(const QString& vecname) const
{
for (const auto& vec : d_ptr_->filter_vec_list) {
/*
* Display info for all filter is vecname is empty,
* otherwise just display info for the matching filter.
*/
if (!vecname.isEmpty() && (vecname.compare(vec.name, Qt::CaseInsensitive) != 0)) {
continue;
}
Filter* flt = (vec.factory != nullptr)? vec.factory() : vec.vec;
printf(" %-20.20s %-50.50s\n",
qPrintable(vec.name), qPrintable(vec.desc));
const QVector<arglist_t>* args = flt->get_args();
if (args) {
for (const auto& arg : *args) {
if (!(arg.argtype & ARGTYPE_HIDDEN)) {
printf(" %-18.18s %-.50s %s\n",
qPrintable(arg.argstring), qPrintable(arg.helpstring),
(arg.argtype & ARGTYPE_REQUIRED) ? "(required)" : "");
}
}
}
if (vec.factory != nullptr) {
delete flt;
}
}
}
void FilterVecs::disp_help_url(const fl_vecs_t& vec, const arglist_t* arg)
{
printf("\t" WEB_DOC_DIR "/filter_%s.html", CSTR(vec.name));
if (arg) {
printf("#fmt_%s_o_%s", CSTR(vec.name), CSTR(arg->argstring));
}
}
void FilterVecs::disp_v1(const fl_vecs_t& vec)
{
Filter* flt = (vec.factory != nullptr)? vec.factory() : vec.vec;
disp_help_url(vec, nullptr);
printf("\n");
const QVector<arglist_t>* args = flt->get_args();
if (args) {
for (const auto& arg : *args) {
if (!(arg.argtype & ARGTYPE_HIDDEN)) {
printf("option\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
CSTR(vec.name),
CSTR(arg.argstring),
CSTR(arg.helpstring),
Vecs::name_option(arg.argtype),
CSTR(arg.defaultvalue),
CSTR(arg.minvalue),
CSTR(arg.maxvalue));
disp_help_url(vec, &arg);
printf("\n");
}
}
}
if (vec.factory != nullptr) {
delete flt;
}
}
/*
* Display the available formats in a format that's easy to machine
* parse. Typically invoked by programs like graphical wrappers to
* determine what formats are supported.
*/
void FilterVecs::disp_filters(int version) const
{
auto sorted_filter_vec_list = d_ptr_->filter_vec_list;
auto alpha = [](const fl_vecs_t& a, const fl_vecs_t& b)->bool {
return QString::compare(a.desc, b.desc, Qt::CaseInsensitive) < 0;
};
std::sort(sorted_filter_vec_list.begin(), sorted_filter_vec_list.end(), alpha);
switch (version) {
case 0:
case 1:
for (const auto& vec : sorted_filter_vec_list) {
if (version == 0) {
printf("%s\t%s\n", CSTR(vec.name), CSTR(vec.desc));
} else {
printf("%s\t%s", CSTR(vec.name), CSTR(vec.desc));
disp_v1(vec);
}
}
break;
default:
;
}
}
bool FilterVecs::validate_filter_vec(const fl_vecs_t& vec)
{
Filter* flt = (vec.factory != nullptr)? vec.factory() : vec.vec;
bool ok = Vecs::validate_args(vec.name, flt->get_args());
if (vec.factory != nullptr) {
delete flt;
}
return ok;
}
bool FilterVecs::validate_filters() const
{
bool ok = true;
for (const auto& vec : d_ptr_->filter_vec_list) {
ok = validate_filter_vec(vec) && ok;
}
return ok;
}