-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsepia_handler.cxx
1326 lines (1238 loc) · 50.8 KB
/
sepia_handler.cxx
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// C++ STL
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <utility>
#include <limits>
#include <ctime>
// CGV framework core
#include <cgv/base/register.h>
// CGV framework utilities
#include <cgv/utils/scan.h>
#include <cgv/utils/advanced_scan.h>
#include <cgv/utils/file.h>
// 3rd party libs
#include <peridetic.h>
#include <WGS84toCartesian.hpp>
// implemented header
#include "sepia_handler.h"
/////
// Some constant defines
/// min supported version
#define SEPIA_CSV_MIN_VERSION 5
/// max supported version
#define SEPIA_CSV_MAX_VERSION 5
/// identifyier to use for position data
#define SEPIA_POSITION_ATTRIB_NAME "DR_Gps"
/// identifyier to use for radius data
#define SEPIA_RADIUS_ATTRIB_NAME "radius"
/// identifyier to use for timestamp attribute
#define SEPIA_TIME_ATTRIB_NAME "time"
// whether to use ECEF coordinates instead of Mercator cartesian + altitude
#define SEPIA_USE_ECEF_COORDINATES 0
////
// Local types and variables
// anonymous namespace begin
namespace {
// some no-op expression that can be optimized away
#define DO_NOTHING (0)
// enum specifying the type of SEPIA trajectory file
struct DatasetInfo
{
typedef unsigned version_t;
enum Type { SINGLE, COLLECTION, UNKNOWN } type;
version_t version;
};
// convenience shorthands
typedef DatasetInfo DI;
typedef DatasetInfo::Type DT;
// struct representing some property
template <class T>
struct sepia_traj_prop
{
typedef T prop_type;
T val;
bool encountered=false;
};
// double-like type for representing a timestamp
struct double_time
{
// the actual time value
double t;
// convenience conversion
inline operator double (void) { return t; }
};
// SePIA sample data types. Even though many of them share the same underlying integral type, they all need different parsing.
// To make the template magic work, they all get their distinct, compiler-discernable type.
// sample of an on/off state
template <class flt_type>
struct sample_on_off
{
bool val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of an active/inactive state
template <class flt_type>
struct sample_active_inactive
{
bool val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of an active/inactive regulation state
template <class flt_type>
struct sample_reg_notreg
{
bool val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of a hit / not hit state of a control (e.g. the breaks)
template <class flt_type>
struct sample_hit_nothit
{
bool val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of handbrake state
template <class flt_type>
struct sample_handbreak
{
bool val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of seatbelt state
template <class flt_type>
struct sample_seatbelt
{
bool val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of an open/closed state
template <class flt_type>
struct sample_open_closed
{
bool val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of a left/center/right state (negative means left, positive right)
template <class flt_type>
struct sample_left_right
{
int val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of gear selection indicator state (negative is reverse gears, crawling is not supported)
template <class flt_type>
struct sample_gearsel
{
int val;
inline operator flt_type (void) const { return (flt_type)val; }
};
// sample of scalar acceleration state. Needs its own special handling as acceleration can be stored in different units
template <class flt_type>
struct sample_accel
{
flt_type val;
inline operator flt_type (void) const { return val; }
};
// ordered timestamp -> sample map (mainly for closest-point queries)
template <class T>
using ordered_samples = std::map<double, T>;
// name -> ordered attribute samples dictionary
template <class T>
using ordered_samples_dict = std::unordered_map<std::string, ordered_samples<T>>;
// internal trajectory representation
template <class flt_type>
struct trajectory
{
// types
typedef flt_type real;
typedef cgv::math::fvec<double, 2> gpsvec;
typedef cgv::math::fvec<real, 3> vec3;
// fields
bool loaded;
double start_time, toffset;
ordered_samples<gpsvec> gps;
ordered_samples_dict<flt_type> attribs_scalar;
ordered_samples_dict<vec3> attribs_vec3;
// methods
trajectory() : loaded(false), start_time(0), toffset(0) {}
trajectory(const trajectory &other)
: loaded(other.loaded), start_time(other.start_time), toffset(other.toffset),
gps(other.gps), attribs_vec3(other.attribs_vec3), attribs_scalar(other.attribs_scalar)
{}
trajectory(trajectory &&other)
: loaded(other.loaded), start_time(other.start_time), toffset(other.toffset),
gps(std::move(other.gps)), attribs_vec3(std::move(other.attribs_vec3)), attribs_scalar(std::move(other.attribs_scalar))
{
other.loaded = false;
other.toffset = other.start_time = 0;
}
trajectory& operator= (const trajectory &other)
{
loaded = other.loaded;
start_time = other.start_time;
toffset = other.toffset;
gps = other.gps;
attribs_scalar = other.attribs_scalar;
attribs_vec3 = other.attribs_vec3;
return *this;
}
trajectory& operator= (trajectory &&other)
{
loaded = other.loaded; other.loaded = false;
start_time = other.start_time; other.start_time = 0;
toffset = other.toffset; other.toffset = 0;
gps = std::move(other.gps);
attribs_scalar = std::move(other.attribs_scalar);
attribs_vec3 = std::move(other.attribs_vec3);
return *this;
}
};
// Anonymous namespace end
}
////
// Class implementation
template <class flt_type>
struct sepia_handler<flt_type>::Impl {
// types
typedef flt_type real;
typedef sepia_handler::Vec2 Vec2;
typedef sepia_handler::Vec3 Vec3;
typedef sepia_handler::Vec4 Vec4;
// fields
static const visual_attribute_mapping<real> attrmap;
inline static const std::string collection_seps=" \t", single_seps="|";
// helper methods
// ToDo: factor out all generic parser utils into own module
// infers type of file from the given header fields.
inline static bool check_version (unsigned version)
{
return version >= SEPIA_CSV_MIN_VERSION && version <= SEPIA_CSV_MAX_VERSION;
}
static DatasetInfo check_type (const std::vector<std::string> &fields)
{
// return value for unknown files
static const DatasetInfo unknown = {DT::UNKNOWN, DI::version_t(-1)};
// check for collection
if ( fields.size() == 3
&& fields[0].compare("SEPIA") == 0 && fields[1].compare("TRAJECTORY") == 0 && fields[2].compare("COLLECTION") == 0)
return DatasetInfo{DT::COLLECTION, 0};
// check for individual trajectory
if (fields.size() == 1)
{
std::vector<cgv::utils::token> tokens;
std::vector<std::string> vfields;
cgv::utils::split_to_tokens(fields[0], tokens, single_seps, false);
Impl::read_fields(tokens, single_seps, &vfields);
// check for known file structure
if (vfields.size() == 2 && vfields[0].compare("Version") == 0)
{
unsigned version;
if (parse_value(&version, vfields[1]))
return DatasetInfo{DT::SINGLE, version};
}
// not an individual SEPIA trajectory after all
return unknown;
}
// it's neither!
return unknown;
}
static bool is_separator (const cgv::utils::token &token, const std::string &separators)
{
// only 1-char tokens can be separators
if (token.end-token.begin != 1)
return false;
// check against each given separator
for (const char sep : separators)
if (sep == *token.begin)
return true;
return false;
}
static unsigned read_fields (
const std::vector<cgv::utils::token> &tokens, const std::string &separators,
std::vector<std::string> *out=nullptr
)
{
// count each token that is not a separator as a field
unsigned count = 0;
std::vector<std::string> fields;
for (const auto &token : tokens)
if (!is_separator(token, separators))
{
count++;
if (out)
fields.emplace_back(std::string(token.begin, size_t(token.end - token.begin)));
}
// account for empty last field (indicated by last token being a separator)
if (count > 0 && is_separator(tokens.back(), separators))
{
count++;
if (out)
fields.emplace_back();
}
// Commit field contents if requested
if (out)
*out = std::move(fields);
// done
return count;
}
static unsigned read_next_nonempty_line (
std::string *line_out, std::vector<cgv::utils::token> *tokens, const std::string &separators,
const std::string &whitespaces, std::istream &contents, std::vector<std::string>* fields_out=nullptr
)
{
tokens->clear();
unsigned num_tokens = 0;
do {
std::getline(contents, *line_out);
if (!line_out->empty())
{
cgv::utils::split_to_tokens(*line_out, *tokens, separators, false, "", "", whitespaces);
num_tokens = (unsigned)tokens->size();
}
}
while (!contents.eof() && num_tokens < 1);
return read_fields(*tokens, separators, fields_out);
}
inline static unsigned read_next_nonempty_line (
std::string *line_out, std::vector<cgv::utils::token> *tokens, const std::string &separators,
std::istream &contents, std::vector<std::string>* fields_out=nullptr
)
{
return read_next_nonempty_line(line_out, tokens, separators, " \t", contents, fields_out);
}
inline static bool field_starts_comment (const std::string &field)
{
return field.size() >= 2 && field[0] == '/' && field[1] == '/';
}
// Timestamp utils
enum class TimeFmt
{
UNKNOWN, SIMPLE_NUMBER, HMS, HMSms, YMW_HMS
};
inline static TimeFmt guess_timestamp_format (const std::string &field)
{
static const std::string seperators = ":._";
std::vector<cgv::utils::token> tokens;
cgv::utils::split_to_tokens(field, tokens, seperators, false);
unsigned num_fields = 0;
for (const auto &token : tokens)
{
if (is_separator(token, seperators))
continue;
num_fields++;
}
if (tokens.size() == 1)
return TimeFmt::SIMPLE_NUMBER;
else if (num_fields == 3 && tokens.size() > 4)
return TimeFmt::HMS;
else if (num_fields == 4 && tokens.size() > 6)
return TimeFmt::HMSms;
else if ( num_fields == 2 && tokens.size() > 2
&& tokens[0].get_length() == 8 && tokens[2].get_length() == 6)
return TimeFmt::YMW_HMS;
return TimeFmt::UNKNOWN;
}
template <class T>
inline static T string_to_value (const std::string &str, size_t *pos) { return (T)std::stoll(str, pos); }
template <>
inline static float string_to_value<float> (const std::string &str, size_t *pos) { return std::stof(str, pos); }
template <>
inline static double string_to_value<double> (const std::string &str, size_t *pos) { return std::stod(str, pos); }
template <>
inline static sample_on_off<real> string_to_value<sample_on_off<real>> (const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
bool val;
if (str_lower.compare("an")==0 || str_lower.compare("on")==0)
val = true;
else if (str_lower.compare("aus")==0 || str_lower.compare("off")==0)
val = false;
else
throw std::invalid_argument("string contains no known representation for 'on' or 'off'");
*pos = str.length();
return {val};
}
template <>
inline static sample_active_inactive<real> string_to_value<sample_active_inactive<real>> (const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
bool val;
if (str_lower.compare("aktiv")==0 || str_lower.compare("active")==0)
val = true;
else if (str_lower.compare("nicht aktiv")==0 || str_lower.compare("inactive")==0)
val = false;
else
throw std::invalid_argument("string contains no known representation for 'active' or 'inactive'");
*pos = str.length();
return {val};
}
template <>
inline static sample_reg_notreg<real> string_to_value<sample_reg_notreg<real>>(const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
bool val;
if (str_lower.compare("regelt") == 0)
val = true;
else if (str_lower.compare("regelt nicht") == 0)
val = false;
else
throw std::invalid_argument("string contains no known representation for 'active' or 'inactive'");
*pos = str.length();
return { val };
}
template <>
inline static sample_hit_nothit<real> string_to_value<sample_hit_nothit<real>>(const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
bool val;
if (str_lower.compare("betaetigt")==0 || str_lower.compare("bet�tigt")==0)
val = true;
else if (str_lower.compare("nicht betaetigt")==0 || str_lower.compare("nicht bet�tigt")==0)
val = false;
else
throw std::invalid_argument("string contains no known representation for 'betaetigt' or 'nicht betaetigt'");
*pos = str.length();
return {val};
}
template <>
inline static sample_handbreak<real> string_to_value<sample_handbreak<real>>(const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
bool val;
// ToDo: find out what the flip the proper terms used by SePIA for an engaged handbrake are...
if (str_lower.compare("gezogen")==0 || str_lower.compare("angezogen")==0 || str_lower.compare("betaetigt")==0 || str_lower.compare("bet�tigt")==0)
val = true;
else if (str_lower.compare("geloest") == 0)
val = false;
else
throw std::invalid_argument("string contains no known representation for 'active' or 'inactive'");
*pos = str.length();
return { val };
}
template <>
inline static sample_seatbelt<real> string_to_value<sample_seatbelt<real>>(const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
bool val;
// ToDo: find out what the flip the proper terms used by SePIA for an engaged handbrake are...
if (str_lower.compare("gesteckt") == 0)
val = true;
/*else if (str_lower.compare("geloest") == 0)
val = false;*/
else
throw std::invalid_argument("string contains no known representation for 'gesteckt' or 'nicht gesteckt'");
*pos = str.length();
return { val };
}
template <>
inline static sample_open_closed<real> string_to_value<sample_open_closed<real>>(const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
bool val;
// ToDo: find out what the flip the proper terms used by SePIA for an engaged handbrake are...
if (str_lower.compare("offen")==0 || str_lower.compare("open")==0 || str_lower.compare("opened")==0)
val = true;
else if (str_lower.compare("geschlossen")==0 || str_lower.compare("closed")==0)
val = false;
else
throw std::invalid_argument("string contains no known representation for 'offen' or 'geschlossen'");
*pos = str.length();
return { val };
}
template <>
inline static sample_left_right<real> string_to_value<sample_left_right<real>> (const std::string &str, size_t *pos)
{
const std::string str_lower(cgv::utils::to_lower(str));
int val;
if (str_lower.compare("links") == 0 || str_lower.compare("left") == 0)
val = -1;
else if (str_lower.compare("rechts") == 0 || str_lower.compare("right") == 0)
val = 1;
// ToDo: find out what, if any, terms SePIA uses to represent dead-center
else if (str_lower.compare("mitte") == 0 || str_lower.compare("neutral") == 0 || str_lower.compare("zentral") == 0 || str_lower.compare("center") == 0)
val = 0;
else
throw std::invalid_argument("string contains no known representation for 'active' or 'inactive'");
*pos = str.length();
return {val};
}
template <>
inline static sample_gearsel<real> string_to_value<sample_gearsel<real>>(const std::string &str, size_t *pos)
{
static const std::string seperators = " \t";
std::vector<cgv::utils::token> tokens;
std::vector<std::string> fields;
cgv::utils::split_to_tokens(str, tokens, seperators, false);
const auto num_fields = Impl::read_fields(tokens, seperators, &fields);
if (num_fields < 1 || num_fields > 2)
throw std::invalid_argument("invalid number of tokens");
int val;
size_t pos_local;
if (num_fields == 1)
if (fields[0][0] == '-')
{
val = 0;
pos_local = 1;
}
else val = std::stoi(fields[0], &pos_local);
else if (fields[0][0]=='m' || fields[0][0]=='a') // for now, we don't care whether gearbox operates in auto or manual
{
const auto pos_tmp = tokens[0].get_length() + tokens[1].get_length();
val = std::stoi(fields[1], &pos_local);
pos_local = pos_tmp + pos_local;
}
else
throw std::invalid_argument("string contains no known gear selection descriptor");
*pos = pos_local;
return {val};
}
template <>
inline static double_time string_to_value<double_time> (const std::string &str, size_t *pos)
{
switch (guess_timestamp_format(str))
{
case TimeFmt::SIMPLE_NUMBER:
{
double val;
try { val = (double)std::stod(str, pos); }
catch (const std::out_of_range&) { val = std::numeric_limits<double>::infinity(); }
catch (...) { val = -0; }
return { val };
}
case TimeFmt::HMS:
{
static const std::string seperators = ":._";
std::vector<cgv::utils::token> tokens;
cgv::utils::split_to_tokens(str, tokens, seperators, false);
double val[3];
size_t local_pos = 0;
for (unsigned i=0; i<3; i++)
{
const auto &token = tokens[i*2];
val[i] = string_to_value<double>(std::string(token.begin, size_t(token.end - token.begin)), &local_pos);
*pos += local_pos + (i>0 ? tokens[i*2-1].get_length() : 0);
}
return { val[0]*60*60 + val[1]*60 + val[2] };
}
case TimeFmt::HMSms:
{
static const std::string seperators = ":._";
std::vector<cgv::utils::token> tokens;
cgv::utils::split_to_tokens(str, tokens, seperators, false);
double val[4];
size_t local_pos = 0;
for (unsigned i=0; i<4; i++)
{
const auto &token = tokens[i*2];
val[i] = string_to_value<double>(std::string(token.begin, size_t(token.end - token.begin)), &local_pos);
*pos += local_pos + (i>0 ? tokens[i*2-1].get_length() : 0);
}
return { val[0]*60*60 + val[1]*60 + val[2] + val[3]/1000 };
}
case TimeFmt::YMW_HMS:
{
static const std::string seperators = ":._";
std::vector<cgv::utils::token> tokens;
cgv::utils::split_to_tokens(str, tokens, seperators, false);
std::tm date;
size_t local_pos = 0;
date.tm_year = string_to_value<int>(std::string(tokens[0].begin, 4), &local_pos)-1900; *pos += local_pos;
date.tm_mon = string_to_value<int>(std::string(tokens[0].begin+4, 2), &local_pos)-1; *pos += local_pos;
date.tm_mday = string_to_value<int>(std::string(tokens[0].begin+6, 2), &local_pos); *pos += local_pos;
*pos += tokens[1].get_length();
date.tm_hour = string_to_value<int>(std::string(tokens[2].begin, 2), &local_pos)-1; *pos += local_pos;
date.tm_min = string_to_value<int>(std::string(tokens[2].begin+2, 2), &local_pos)-1; *pos += local_pos;
date.tm_sec = string_to_value<int>(std::string(tokens[2].begin+4, 2), &local_pos)-1; *pos += local_pos;
date.tm_wday = 0;
date.tm_yday = 0;
date.tm_isdst = 0;
double ymdhms = (double)mktime(&date);
return { ymdhms };
}
default:
throw std::invalid_argument("unrecognized timestamp format");
}
}
template <class T>
inline static bool parse_value (T *out, const std::string &field) noexcept
{
T v;
size_t pos = 0;
try { v = string_to_value<T>(field, &pos); }
catch (...) { return false; }
const bool success = pos > 0 && pos == field.size();
if (success)
*out = v;
return success;
}
inline static bool collection_parse_switch (
bool *out, const char *name, const std::vector<std::string> &fields, const std::string &line
) noexcept
{
if (cgv::utils::to_lower(fields[0].c_str()+1).compare(name) == 0)
{
if (fields.size() > 1)
std::cerr << "[sepia_handler] WARNING: switch '"<<name<<"' has additional arguments specified, ignoring! Line: "<<line << std::endl;
*out = true;
return true;
}
return false;
}
inline static bool traj_ignore_prop (const char* name, const std::vector<std::string> &fields) noexcept
{
return cgv::utils::to_lower(fields[0]).compare(cgv::utils::to_lower(name)) == 0;
}
template <class prop_type>
inline static bool traj_parse_prop(
prop_type *out, const char* name, const std::vector<std::string> &fields, const std::string &line
) noexcept
{
if (cgv::utils::to_lower(fields[0]).compare(cgv::utils::to_lower(name)) == 0)
{
if (fields.size() >= 2 && parse_value(&(out->val), fields[1]))
{
if (fields.size() > 2)
std::cerr << "[sepia_handler] WARNING: property '"<<name<<"' has additional arguments specified, ignoring! Line: "<<line << std::endl;
if (out->encountered)
std::cerr << "[sepia_handler] WARNING: property '"<<name<<"' specified more than once! Using new value: "<<out->val << std::endl;
out->encountered = true;
}
else
std::cerr << "[sepia_handler] WARNING: '"<<name<<"' value could not be parsed! Line: "<<line << std::endl;
// we were responsible for this field, so return 'true' even in case we couldn't parse it
return true;
}
return false;
}
template <>
inline static bool traj_parse_prop(
sepia_traj_prop<typename trajectory<real>::gpsvec> *out, const char* name, const std::vector<std::string> &fields, const std::string &line
) noexcept
{
if (cgv::utils::to_lower(fields[0]).compare(cgv::utils::to_lower(name)) == 0)
{
typename trajectory<real>::gpsvec::value_type c0, c1;
if (fields.size() >= 4 && parse_value(&c0, fields[2]) && parse_value(&c1, fields[3]))
{
out->val.set(c0, c1);
if (fields.size() > 4)
std::cerr << "[sepia_handler] WARNING: property '"<<name<<"' has additional arguments specified, ignoring! Line: "<<line << std::endl;
if (out->encountered)
std::cerr << "[sepia_handler] WARNING: property '"<<name<<"' specified more than once! Using new value: "<<out->val << std::endl;
out->encountered = true;
}
else
std::cerr << "[sepia_handler] WARNING: '"<<name<<"' value could not be parsed! Line: "<<line << std::endl;
// we were responsible for this field, so return 'true' even in case we couldn't parse it
return true;
}
return false;
}
inline static bool traj_ignore_sample (const char* name, const std::vector<std::string> &fields) noexcept
{
return cgv::utils::to_lower(fields[1]).compare(cgv::utils::to_lower(name)) == 0;
}
template <class sample_type>
inline static unsigned traj_parse_sample (
double *ts, sample_type *out, const char* name, const std::vector<std::string> &fields, const std::string &line
) noexcept
{
if (cgv::utils::to_lower(fields[1]).compare(cgv::utils::to_lower(name)) == 0)
{
if (fields.size() > 2)
{
if (fields.size() > 4)
std::cerr << "[sepia_handler] WARNING: too many columns in '"<<name<<"' sample! Line: "<<line << std::endl;
double ts_local;
if (Impl::parse_value(&ts_local, fields[0]) && Impl::parse_value(out, fields[2]))
{
*ts = ts_local; // commit timestamp
return 1;
}
}
std::cerr << "[sepia_handler] WARNING: '"<<name<<"' value could not be parsed! Line: "<<line << std::endl;
// we were responsible for this field, so return non-zero even in case we couldn't parse it
return 2;
}
return 0;
}
template <>
inline static unsigned traj_parse_sample<sample_accel<real>> (
double *ts, sample_accel<real> *out, const char* name, const std::vector<std::string> &fields, const std::string &line
) noexcept
{
if (cgv::utils::to_lower(fields[1]).compare(cgv::utils::to_lower(name)) == 0)
{
if (fields.size() > 2)
{
if (fields.size() > 4)
std::cerr << "[sepia_handler] WARNING: too many columns in '"<<name<<"' sample! Line: "<<line << std::endl;
double ts_local;
real val;
if (Impl::parse_value(&ts_local, fields[0]) && Impl::parse_value(&val, fields[2]))
{
if (fields.size() > 3)
{
std::string unit = cgv::utils::to_lower(fields[3]);
if (unit.compare("g") == 0)
val *= real(9.8067);
else
std::cerr << "[sepia_handler] WARNING: unknown unit specified for '"<<name<<"', committing value as-is. Line: "<<line << std::endl;
}
else
std::cerr << "[sepia_handler] WARNING: no unit specified for '"<<name<<"', committing value as-is. Line: "<<line << std::endl;
// commit values
*ts = ts_local;
out->val = val;
return 1;
}
}
std::cerr << "[sepia_handler] WARNING: '"<<name<<"' value could not be parsed! Line: "<<line << std::endl;
// we were responsible for this field, so return non-zero even in case we couldn't parse it
return 2;
}
return 0;
}
template <class T, class sample_type>
inline static unsigned traj_parse_sample_and_commit (
ordered_samples_dict<T> *attrib_store, sample_type ®, const char *name, const std::vector<std::string> &fields, const std::string &line
) noexcept
{
double ts_local;
unsigned parse_result = traj_parse_sample(&ts_local, ®, name, fields, line);
if (parse_result == 1)
{
// parsing successful, commmit to storage
auto &attrib = (*attrib_store)[name];
auto it = attrib.find(ts_local);
if (it == attrib.end())
attrib.emplace(ts_local, (T)reg);
else
{
std::cerr << "[sepia_handler] WARNING: duplicate sample timestamp in attribute '"<<name<<"', overwriting previous value" << std::endl;
it->second = (T)reg;
}
return 1;
}
// just pass on parsing result
return parse_result;
}
static trajectory<real> load_trajectory (unsigned format_version, std::istream &contents)
{
// parsing workspace
std::string line;
std::vector<cgv::utils::token> tokens;
std::vector<std::string> fields;
// properties
sepia_traj_prop<double_time> time0, timeN;
sepia_traj_prop<typename trajectory<real>::gpsvec> gps0, gpsN;
// trajectory properties
while ( !contents.eof()
&& Impl::read_next_nonempty_line(&line, &tokens, Impl::single_seps, "", contents, &fields))
{
// parse handled properties
if (Impl::traj_parse_prop(&time0, "Timestamp Start", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_prop(&timeN, "Timestamp Ende", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_prop(&gps0, "GPS Start", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_prop(&gpsN, "GPS Ende", fields, line)) DO_NOTHING;
// skip ignored properties
else if (Impl::traj_ignore_prop("Triggernummer", fields)) DO_NOTHING;
else if (Impl::traj_ignore_prop("Trigger-Timestamp", fields)) DO_NOTHING;
else if (Impl::traj_ignore_prop("DD-Sensor", fields)) DO_NOTHING;
else if (Impl::traj_ignore_prop("Konfiguration", fields)) DO_NOTHING;
else if (Impl::traj_ignore_prop("Triggergrund", fields)) DO_NOTHING;
else if (Impl::traj_ignore_prop("GPS Trigger", fields)) DO_NOTHING;
else if (Impl::traj_ignore_prop("GPS alt", fields)) DO_NOTHING;
// check if header is finished
else if (cgv::utils::to_lower(line).compare("zeit|signal|wert|einheit") == 0)
break;
/// unknown declaration, print warning
else
std::cerr << "[sepia_handler] WARNING: unknown property: '"<<fields[0]<<"', ignoring" << std::endl;
}
if (contents.eof())
{
std::cerr << "[sepia_handler] ERROR: reached end-of-file before any actual data!" << std::endl;
return trajectory<real>();
}
// samples
// - typed "registers"
double ts, dbl;
real scl;
int ival;
sample_on_off<real> oo;
sample_active_inactive<real> ai;
sample_reg_notreg<real> rn;
sample_hit_nothit<real> hn;
sample_handbreak<real> hb;
sample_seatbelt<real> sb;
sample_open_closed<real> oc;
sample_left_right<real> lr;
sample_gearsel<real> gs;
sample_accel<real> a;
// - temporary attribute storage
trajectory<real> traj;
std::unordered_set<std::string> unknowns;
// - set relevant properties from header
if (time0.encountered)
traj.start_time = time0.val;
if (gps0.encountered)
traj.gps[0] = gps0.val;
while ( !contents.eof()
&& Impl::read_next_nonempty_line(&line, &tokens, Impl::single_seps, "", contents, &fields))
{
// parse handled sample types
// - boolean states
if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Motor_KL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Blinker_L_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Blinker_R_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Blinker_Warnblinker_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Licht_Abblend_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Licht_Fern_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Licht_Hupe_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Licht_Nebelschluss_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Licht_Stand_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_GW_Gurt_WL_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_GW_Nebelschluss_KL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, rn, "ABS_E", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, rn, "ABS_E_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "ABS_WL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "GW_ABS_WL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "GW_ABS_WL_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, ai, "ESP_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, ai, "ESP_S_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, rn, "ESP_E", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, rn, "ESP_E_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "ESP_KL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "GW_ESP_KL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, ai, "Zusatz_Bremse_Licht_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, hn, "Zusatz_Kickdown", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, hn, "Bremse_Pedal_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, hn, "Bremse_Pedal_S_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, hn, "Bremse_Pedal_S_2", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, hb, "Bremse_Hand", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Airbag_WL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Airbag_WL_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, sb, "Gurt_VL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_GW_Gurt_WL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_GW_Batterie_WL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_GW_KuehlMtemp_WL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_GW_Oeldruck_WL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_GW_Werkstatt_KL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "GW_ESP_KL_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "GW_ESP_KL_2", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_Motor_Kuehlung_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_Anlassschalter", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_Heckscheibe_Heizung", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_Klimaanlage", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oo, "Zusatz_Lueftung_Innenraum_S", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oc, "Zusatz_Tuer_links", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, oc, "Zusatz_Tuer_rechts", fields, line)) DO_NOTHING;
// - discrete state
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, ival, "Gang_Wahl", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, ival, "Gang_Getriebe", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, gs, "Zusatz_Gang_Anzeige_KI", fields, line)) DO_NOTHING;
// - scalar values
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Motor_n", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Motor_n_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Zusatz_Motor_Temperatur", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Rad_n_VL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Rad_n_VR", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Rad_n_HL", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Rad_n_HR", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "FZ_v", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "FZ_v_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, a, "FZ_a_y", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, a, "FZ_a_y_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, a, "FZ_a_y_2", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "FZ_Gierrate", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "DR_Gps_v", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "DR_Gierrate", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Zusatz_Bremse_Moment", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Batterie_U", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Temperatur_Aussen", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Temperatur_Aussen_1", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "KM", fields, line)) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "KM_1", fields, line)) DO_NOTHING;
// - percentages
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Gaspedal_W", fields, line) == 1) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Zusatz_Tankinhalt", fields, line) == 1) DO_NOTHING;
else if (Impl::traj_parse_sample_and_commit(&traj.attribs_scalar, scl, "Zusatz_Licht_Instrumente", fields, line) == 1) DO_NOTHING;
// - scalars that need additional attributes for deciding the sign
else if (Impl::traj_parse_sample(&ts, &scl, "Lenkrad_W_B", fields, line) == 1) {
auto &steering_pos = traj.attribs_scalar["Lenkrad_W"];
if (steering_pos.find(ts) == steering_pos.end())
steering_pos[ts] = scl;
else
steering_pos[ts] *= scl;
}
else if (Impl::traj_parse_sample(&ts, &lr, "Lenkrad_W_R", fields, line) == 1) {
auto &steering_pos = traj.attribs_scalar["Lenkrad_W"];
if (steering_pos.find(ts) == steering_pos.end())
steering_pos[ts] = lr;
else
steering_pos[ts] *= lr;
}
else if (Impl::traj_parse_sample(&ts, &scl, "Lenkrad_W_v_B", fields, line) == 1) {
auto &steering_vel = traj.attribs_scalar["Lenkrad_W_v"];
if (steering_vel.find(ts) == steering_vel.end())
steering_vel[ts] = scl;
else
steering_vel[ts] *= scl;
}
else if (Impl::traj_parse_sample(&ts, &lr, "Lenkrad_W_v_R", fields, line) == 1) {
auto &steering_vel = traj.attribs_scalar["Lenkrad_W_v"];
if (steering_vel.find(ts) == steering_vel.end())
steering_vel[ts] = lr;
else
steering_vel[ts] *= lr;
}
// - vectors
else if (Impl::traj_parse_sample(&ts, &a, "DR_a_x", fields, line) == 1) traj.attribs_vec3["DR_a"][ts].x() = a.val;
else if (Impl::traj_parse_sample(&ts, &a, "DR_a_y", fields, line) == 1) traj.attribs_vec3["DR_a"][ts].y() = a.val;
else if (Impl::traj_parse_sample(&ts, &a, "DR_a_z", fields, line) == 1) traj.attribs_vec3["DR_a"][ts].z() = a.val;
else if (Impl::traj_parse_sample(&ts, &dbl, "DR_Gps_Latitude", fields, line) == 1) traj.gps[ts].x() = dbl;
else if (Impl::traj_parse_sample(&ts, &dbl, "DR_Gps_Longitude", fields, line) == 1) traj.gps[ts].y() = dbl;
// skip ignored properties
else if (Impl::traj_ignore_sample("Rad_n_VL_1", fields)) DO_NOTHING;
else if (Impl::traj_ignore_sample("Bremse_DOT_S", fields)) DO_NOTHING;
else if (Impl::traj_ignore_sample("DR_Gps_Date", fields)) DO_NOTHING;
else if (Impl::traj_ignore_sample("DR_Gps_Time", fields)) DO_NOTHING;
else if (Impl::traj_ignore_sample("DR_Trigger", fields)) DO_NOTHING;
else if (Impl::traj_ignore_sample("Zusatz_Fahr_S", fields)) DO_NOTHING;
else if (Impl::traj_ignore_sample("Zusatz_Parksperre", fields)) DO_NOTHING;
else if (Impl::traj_ignore_sample("Zusatz_Zuendung_Ganganzeige", fields)) DO_NOTHING;
// unknown sample type, print warning
else
{
// some logic to make sure we print this warning only once
if (unknowns.find(fields[1]) == unknowns.end())
{
std::cerr << "[sepia_handler] WARNING: unknown sample type: '"<<fields[1]<<"', ignoring" << std::endl;
unknowns.emplace(fields[1]);
}
}
// for setting end-of-loop breakpoints
std::cerr.flush();
}
// post-cleanup
if (traj.gps.size() > 2)
{
// quick-and-dirty hack to get rid of duplicate first GPS sample in some datasets
auto it_first = traj.gps.cbegin(), it_second = it_first; it_second++;
if ((it_second->second - it_first->second).sqr_length() < std::numeric_limits<float>::epsilon()*2)
traj.gps.erase(it_second);
}
// finally, we introduce the timestamps of the least-frequently sampled scalar attribute as its own attribute
auto it_lf = traj.attribs_scalar.cbegin();
unsigned num_samples = std::numeric_limits<unsigned>::max();
for (auto it=traj.attribs_scalar.cbegin(); it!=traj.attribs_scalar.cend(); it++)
if (it->second.size() < num_samples)
{