-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv_handler.cxx
928 lines (833 loc) · 29.2 KB
/
csv_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
// C++ STL
#include <cmath>
#include <array>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <utility>
#include <limits>
// CGV framework core
#include <cgv/base/register.h>
#include <cgv/utils/file.h>
#include <cgv/utils/scan.h>
#include <cgv/utils/advanced_scan.h>
// 3rd party libs
#include <WGS84toCartesian.hpp>
// local includes
#include "regulargrid.h"
// implemented header
#include "csv_handler.h"
#include "csv_handler_detail.h"
////
// Class implementation - csv_descriptor
struct csv_descriptor::Impl
{
// fields
csv_descriptor::csv_properties props;
std::vector<attribute> attribs;
std::string name, separators;
};
csv_descriptor::csv_descriptor() : pimpl(nullptr)
{
pimpl = new Impl;
}
csv_descriptor::csv_descriptor(const csv_descriptor &other) : csv_descriptor()
{
pimpl->name = other.pimpl->name;
pimpl->props = other.pimpl->props;
pimpl->attribs = other.pimpl->attribs;
pimpl->separators = other.pimpl->separators;
}
csv_descriptor::csv_descriptor(csv_descriptor &&other) : pimpl(other.pimpl)
{
other.pimpl = nullptr;
}
csv_descriptor::csv_descriptor(
const std::string &name, const std::string &separators, const std::vector<attribute> &attributes
)
: csv_descriptor()
{
pimpl->name = name;
pimpl->separators = separators;
auto &attribs = pimpl->attribs = attributes;
pimpl->props = infer_properties(attribs);
}
csv_descriptor::csv_descriptor(
const std::string &name, std::string &&separators, const std::vector<attribute> &attributes
)
: csv_descriptor()
{
pimpl->name = name;
pimpl->separators = std::move(separators);
auto &attribs = pimpl->attribs = attributes;
pimpl->props = infer_properties(attribs);
}
csv_descriptor::csv_descriptor(
const std::string &name, const std::string &separators, std::vector<attribute> &&attributes
)
: csv_descriptor()
{
pimpl->name = name;
pimpl->separators = separators;
auto &attribs = pimpl->attribs = std::move(attributes);
pimpl->props = infer_properties(attribs);
}
csv_descriptor::csv_descriptor(
const std::string &name, std::string &&separators, std::vector<attribute> &&attributes
)
: csv_descriptor()
{
pimpl->name = name;
pimpl->separators = std::move(separators);
auto &attribs = pimpl->attribs = std::move(attributes);
pimpl->props = infer_properties(attribs);
}
csv_descriptor::~csv_descriptor()
{
if (pimpl)
delete pimpl;
}
csv_descriptor& csv_descriptor::operator= (const csv_descriptor &other)
{
pimpl->name = other.pimpl->name;
pimpl->props = other.pimpl->props;
pimpl->attribs = other.pimpl->attribs;
pimpl->separators = other.pimpl->separators;
return *this;
}
csv_descriptor& csv_descriptor::operator= (csv_descriptor &&other)
{
this->~csv_descriptor();
pimpl = other.pimpl;
other.pimpl = nullptr;
return *this;
}
const std::string& csv_descriptor::name (void) const
{
return pimpl->name;
}
const std::string& csv_descriptor::separators (void) const
{
return pimpl->separators;
}
const std::vector<csv_descriptor::attribute>& csv_descriptor::attributes (void) const
{
return pimpl->attribs;
}
const csv_descriptor::csv_properties& csv_descriptor::properties (void) const
{
return pimpl->props;
}
csv_descriptor::csv_properties csv_descriptor::infer_properties (const std::vector<attribute> &attributes)
{
csv_properties ret;
ret.header = false;
ret.multi_traj = false;
signed num_cols = 0, max_col_id = 0;
bool all_cols_have_ids_defined = true; // TODO: used for internal logic sanity check, remove once validated
for (unsigned i=0; i<attributes.size(); i++)
{
const auto &attrib = attributes[i];
num_cols += (unsigned)attrib.columns.size();
if (attrib.semantics == CSV::POS)
ret.pos_id = i;
else if (attrib.semantics == CSV::TRAJ_ID)
{
ret.multi_traj = true;
ret.traj_id = i;
}
for (const auto &col : attrib.columns)
{
max_col_id = std::max(col.number, max_col_id);
if (col.number < 0) {
ret.header = true;
all_cols_have_ids_defined = false;
}
}
}
ret.max_col_id = ret.header ?
(unsigned)std::max(num_cols-1, max_col_id)
: (unsigned)max_col_id;
assert(ret.header == !all_cols_have_ids_defined && "INTERNAL CONTROL LOGIC ERROR");
return ret;
}
////
// Class implementation - csv_handler
template <class flt_type>
csv_handler<flt_type>::csv_handler() : pimpl(nullptr)
{
pimpl = new Impl;
}
template <class flt_type>
csv_handler<flt_type>::csv_handler(
const csv_descriptor &csv_desc, const visual_attribute_mapping<real> &vmap_hints
)
: csv_handler()
{
// shortcut for saving one indirection
auto &impl = *pimpl;
// commit name and descriptor
impl.fmt_name = "CSV - "+csv_desc.name();
impl.csv_desc = csv_desc;
impl.vmap_hints = vmap_hints;
impl.common_init();
}
template <class flt_type>
csv_handler<flt_type>::csv_handler(
csv_descriptor &&csv_desc, const visual_attribute_mapping<real> &vmap_hints
)
: csv_handler()
{
// shortcut for saving one indirection
auto &impl = *pimpl;
// commit name and descriptor
impl.fmt_name = "CSV - "+csv_desc.name();
impl.csv_desc = std::move(csv_desc);
impl.vmap_hints = vmap_hints;
impl.common_init();
}
template <class flt_type>
csv_handler<flt_type>::~csv_handler()
{
if (pimpl)
delete pimpl;
}
template <class flt_type>
const std::string& csv_handler<flt_type>::format_name (void) const {
return pimpl->fmt_name;
}
template <class flt_type>
const std::vector<std::string>& csv_handler<flt_type>::handled_extensions (void) const
{
// for now, we don't claim any file extensions
// ToDo: add option to csv_descriptor to specify file extensions, which will then be reported to callers here
return traj_format_handler<flt_type>::handled_extensions();
}
template <class flt_type>
void csv_handler<flt_type>::cleanup (void)
{
// shortcut for saving one indirection
auto &impl = *pimpl;
// reset our private fields
impl.has_data = false;
impl.avg_dist = 0;
}
template <class flt_type>
bool csv_handler<flt_type>::can_handle (std::istream &contents) const
{
// shortcut for saving one indirection
auto &impl = *pimpl;
// init
std::string line;
const stream_pos_guard g(contents);
// check for tell-tale stream contents
// - parse first row and check if there are enough columns
const std::string &separators = impl.csv_desc.separators();
const auto &props = impl.csv_desc.properties();
std::vector<std::string> columns;
std::vector<cgv::utils::token> tokens;
if ( Impl::read_next_nonempty_line(&line, &tokens, separators, contents, &columns)
< props.max_col_id)
return false;
Impl::remove_enclosing_quotes(columns);
// - inspect contents more closely
if (props.header)
{
if (impl.check_header(columns))
// header matches up, so at this point we don't do any further tests and just assume it'll work
return true;
return false;
}
else
{
// also actually process up to two lines in the stream to see if we can actually interpret the text-encoded data
if (impl.is_header(columns))
{
// even if we don't need a header - in case the file does have one, we will require it to match up
if (!impl.check_header(columns))
return false;
// header checked out ok, read in first actual data row
Impl::read_next_nonempty_line(&line, &tokens, separators, contents, &columns);
}
if (impl.special_fields_not_readable(columns))
return false;
}
// apparently a valid .csv that we can interpret
return true;
}
template <class flt_type>
traj_dataset<flt_type> csv_handler<flt_type>::read (
std::istream &contents, DatasetOrigin source, const std::string &path
)
{
// shortcut for saving one indirection
auto &impl = *pimpl;
// file parsing database
const std::string &separators = impl.csv_desc.separators();
std::string line;
std::vector<std::string> fields;
std::vector<cgv::utils::token> tokens;
// read in first row and do initial pre-processing
Impl::read_next_nonempty_line(&line, &tokens, separators, contents, &fields);
std::set<unsigned> undeclared_cols;
for (unsigned i=0; i<fields.size(); i++)
undeclared_cols.emplace_hint(undeclared_cols.end(), i);
bool header_present;
// route .csv file columns to declared attributes
const auto &props = impl.csv_desc.properties();
const auto &csv_attribs = impl.csv_desc.attributes();
std::vector<typename Impl::declared_attrib> declared_attribs;
declared_attribs.reserve(csv_attribs.size());
int timestamp_id = -1;
if (props.header)
{
// we know that we have a header because our attribute declaration requires one
header_present = true;
Impl::remove_enclosing_quotes(fields);
// find actual .csv columns belonging to each declared attribute
for (const auto &csv_attrib : csv_attribs)
{
if (csv_attrib.semantics == CSV::TIMESTAMP)
timestamp_id = (int)declared_attribs.size();
declared_attribs.emplace_back(csv_attrib);
auto &attrib = declared_attribs.back();
// for each colum declaration, search the corresponding field in the actual .csv header row
for (const auto &col : csv_attribs[props.pos_id].columns)
for (unsigned i=0; i<(unsigned)fields.size(); i++)
{
if ( (col.case_sensitive && fields[i].compare(col.name) == 0)
|| ( !(col.case_sensitive)
&& cgv::utils::to_lower(fields[i]).compare(cgv::utils::to_lower(col.name)) == 0))
{
attrib.field_ids.emplace_back(i);
undeclared_cols.erase(i);
}
}
}
}
else
{
// store whether or not the file has a header
header_present = Impl::is_header(fields);
if(header_present)
Impl::remove_enclosing_quotes(fields);
// just commit the user-declared column numbers
for (const auto &csv_attrib : csv_attribs)
{
if (csv_attrib.semantics == CSV::TIMESTAMP)
timestamp_id = (int)declared_attribs.size();
declared_attribs.emplace_back(csv_attrib);
auto &attrib = declared_attribs.back();
for (const auto &col : csv_attrib.columns)
{
attrib.field_ids.emplace_back(col.number);
undeclared_cols.erase(col.number);
}
}
}
// route remaining .csv file columns to undeclared attributes
std::vector<typename Impl::undeclared_attrib> undeclared_attribs;
undeclared_attribs.reserve(undeclared_cols.size());
for (unsigned i : undeclared_cols)
{
undeclared_attribs.emplace_back();
auto &attrib = undeclared_attribs.back();
if (header_present)
attrib.name = fields[i];
else
attrib.name = "attr_"+std::to_string(i);
attrib.field_id = i;
}
undeclared_cols.clear(); // <-- we don't need these from here on
// ensure we're at the first actual data row
if (header_present)
Impl::read_next_nonempty_line(&line, &tokens, separators, contents, &fields);
// prepare timestamp parsing
auto timestamp_format = Impl::TimeFmt::UNKNOWN;
if (timestamp_id > -1)
timestamp_format = Impl::guess_timestamp_format(fields[declared_attribs[timestamp_id].field_ids.front()]);
///////////
/// XXX: Hack to get absolute values of vorticity vector components for Paraview-exported Streamline datasets
/// Note: has more custom hacks inside main parser loop to take absolute value
static const csv_descriptor::attribute abs_attribs[] = {
{"|Vorticity.x|", {"Vorticity:0", false, 5}},
{"|Vorticity.y|", {"Vorticity:1", false, 6}},
{"|Vorticity.z|", {"Vorticity:2", false, 7}},
{"|p|", {"p", false, 3}}
};
bool is_paraview_streamline = false;
if (impl.csv_desc.name().compare("Paraview Streamline") == 0)
{
for (const auto &vattr : abs_attribs)
{
declared_attribs.emplace_back(vattr);
auto &attrib = declared_attribs.back();
for (const auto &col : vattr.columns)
{
attrib.field_ids.emplace_back(col.number);
undeclared_cols.erase(col.number);
}
}
is_paraview_streamline = true;
}
/// \END hack
///////////
// parse the stream until EOF
bool nothing_loaded = true;
real dist_accum = 0;
unsigned running_traj_id = 0;
double first_timestamp = 0, prev_timestamp = 0;
bool first = true;
while (!contents.eof())
{
// read in timestamps if present
double t;
if(timestamp_id > -1) {
auto &ts_csv = declared_attribs[timestamp_id];
t = Impl::parse_timestamp_field(
timestamp_format, fields[ts_csv.field_ids.front()]
);
if (first && !props.multi_traj)
first_timestamp = t;
}
// determine trajectory id of this row
unsigned traj_id;
if (props.multi_traj)
{
const int traj_id_field = declared_attribs[props.traj_id].field_ids.front();
traj_id = std::atoi(fields[traj_id_field].c_str());
}
else if(timestamp_id > -1)
{
if(!first) {
if(prev_timestamp > t)
++running_traj_id;
}
first = false;
prev_timestamp = t;
traj_id = running_traj_id;
}
else
traj_id = 0;
// make sure position traj exists for various kinds of forward queries
auto &P = Impl::ensure_traj(declared_attribs[props.pos_id].trajs, traj_id, 3).template get_data<Vec3>().values;
// commit timestamp as actual data point if present
real t_mod;
if(timestamp_id > -1) {
auto &ts_csv = declared_attribs[timestamp_id];
auto &ts_attrib = Impl::ensure_traj(ts_csv.trajs, traj_id, 1);
t_mod = (real)(t - first_timestamp);
ts_attrib.template get_data<real>().append(t_mod, t_mod);
} else {
t_mod = (real)(t = (real)P.size());
}
// read in all declared attributes
for (auto &attrib : declared_attribs)
{
if (attrib.desc.semantics == CSV::TIMESTAMP)
// timestamps are handled above
continue;
switch (attrib.field_ids.size())
{
case 1:
{
auto &a = Impl::ensure_traj(attrib.trajs, traj_id, 1);
///////////
/// XXX: Hack to get absolute values of vorticity vector components for Paraview-exported Streamline datasets
/// Note: also has a preparatory custom hack in the initialization phase
if (is_paraview_streamline && [&attrib] {
for (const auto &vattr : abs_attribs)
if (&attrib.desc == &vattr) // <-- this works because attrib just references the underlying csv_desc
return true;
return false;
}()) {
a.template get_data<real>().append(
std::abs(Impl::parse_field(fields[attrib.field_ids.front()])), (real)t_mod
);
}
else
/// \END hack
///////////
a.template get_data<real>().append(
Impl::parse_field(fields[attrib.field_ids.front()]), (real)t_mod
);
continue;
}
case 2:
{
auto &a = Impl::ensure_traj(attrib.trajs, traj_id, 2);
a.template get_data<Vec2>().append(
std::move(Impl::template parse_fields<2>(fields, attrib.field_ids)), (real)t_mod
);
continue;
}
case 3:
{
auto &a = Impl::ensure_traj(attrib.trajs, traj_id, 3);
a.template get_data<Vec3>().append(
std::move(Impl::template parse_fields<3>(fields, attrib.field_ids)), (real)t_mod
);
nothing_loaded = false; // this is guaranteed to capture the position attribute, since positions are always Vec3
continue;
}
case 4:
{
auto &a = Impl::ensure_traj(attrib.trajs, traj_id, 4);
a.template get_data<Vec4>().append(
std::move(Impl::template parse_fields<4>(fields, attrib.field_ids)), (real)t_mod
);
continue;
}
default:
/* DoNothing() */;
}
}
// read in all undeclared attributes
for (auto &attrib : undeclared_attribs)
{
auto &a = Impl::ensure_traj(attrib.trajs, traj_id, 1);
a.template get_data<real>().append(Impl::parse_field(fields[attrib.field_id]), (real)t_mod);
}
// update segment length counter
if (P.size() > 1)
dist_accum += (P.back() - P[P.size()-2]).length();
// read in next data row
Impl::read_next_nonempty_line(&line, &tokens, separators, contents, &fields);
}
// did we succeed at loading anything?
traj_dataset<real> ret;
if (nothing_loaded)
return std::move(ret);
// transform from intermediate storage to final data layout and transfer into output dataset
for (auto &attr : declared_attribs)
{
std::vector<range> ds_trajs;
// special treatment for first trajectory
auto it = attr.trajs.begin();
attr.ds_attrib = std::move(it->second);
ds_trajs.emplace_back(range{ 0, attr.ds_attrib.num() });
it++; for (; it!=attr.trajs.end(); it++)
{
const auto &traj_attrib = it->second;
// generate range
ds_trajs.emplace_back(range{ attr.ds_attrib.num(), traj_attrib.num() });
// copy data points
Impl::concat_attribute_containers(attr.ds_attrib, traj_attrib);
}
// commit to dataset
traj_format_handler<flt_type>::trajectories(ret, attr.ds_attrib) = std::move(ds_trajs);
traj_format_handler<flt_type>::attributes(ret).emplace(attr.desc.name, std::move(attr.ds_attrib));
}
for (auto &attr : undeclared_attribs)
{
std::vector<range> ds_trajs;
// special treatment for first trajectory
auto it = attr.trajs.begin();
attr.ds_attrib = std::move(it->second);
ds_trajs.emplace_back(range{ 0, attr.ds_attrib.num() });
// copy datapoints from remaining trajectories
it++; for (; it!=attr.trajs.end(); it++)
{
const auto &traj_attrib = it->second;
// generate range
ds_trajs.emplace_back(range{ attr.ds_attrib.num(), traj_attrib.num() });
// copy data points
Impl::concat_attribute_containers(attr.ds_attrib, traj_attrib);
}
// commit to dataset
traj_format_handler<flt_type>::trajectories(ret, attr.ds_attrib) = std::move(ds_trajs);
traj_format_handler<flt_type>::attributes(ret).emplace(attr.name, std::move(attr.ds_attrib));
}
// prepare invented radii
auto R = traj_format_handler<flt_type>::template add_attribute<real>(ret, "_radius");
// set visual mapping
visual_attribute_mapping<real> vamap(impl.vmap_hints);
if (!vamap.is_mapped(VisualAttrib::POSITION))
// ToDo: perform more fine-grained / smart test to retain any hinted at transformation if
// pre-mapped name and position attribute name from the csv table description match up
vamap.map_attribute(VisualAttrib::POSITION, csv_attribs[props.pos_id].name);
ret.set_mapping(std::move(vamap));
// determine remaining stats
const auto &P = traj_format_handler<flt_type>::positions(ret);
const unsigned
num_samples = P.data.num(),
num_segs = std::max<int>(num_samples-(unsigned)traj_format_handler<flt_type>::trajectories(ret, P.attrib).size(), 1);
traj_format_handler<flt_type>::set_avg_segment_length(ret, real(dist_accum / double(num_segs)));
// invent radii now that all stats are known
R.data.values = std::vector<real>(num_samples, ret.avg_segment_length()*real(.25));
R.data.timestamps = P.data.timestamps;
traj_format_handler<flt_type>::trajectories(ret, R.attrib) = traj_format_handler<flt_type>::trajectories(ret, P.attrib);
// set dataset name (we use the filename since our .csv model does not support other kinds of fields outside the actual data
// which could contain the name)
traj_format_handler<flt_type>::name(ret) = cgv::utils::file::drop_extension(cgv::utils::file::get_file_name(path));
// print stats
const unsigned num_trajs = (unsigned)declared_attribs[props.pos_id].trajs.size();
std::cout << "csv_handler: loading completed! Stats:" << std::endl
<< " " << num_samples<<" samples" << std::endl
<< " " << num_segs<<" segments" << std::endl
<< " " << num_trajs<<(num_trajs>1?" trajectories":" trajectory") << std::endl
<< std::endl;
// done
return std::move(ret);
}
template <class flt_type>
bool csv_handler<flt_type>::is_csv_descriptor_valid (const csv_descriptor &csv_desc)
{
bool pos_found=false, traj_id_found=false, timestamp_found=false;
for (const auto &attrib : csv_desc.attributes())
{
if (attrib.semantics == CSV::POS)
{
pos_found = true;
if (attrib.columns.empty() || attrib.columns.size() > 3)
return false;
}
else if (attrib.semantics == CSV::TRAJ_ID)
{
traj_id_found = true;
if (attrib.columns.empty() || attrib.columns.size() > 1)
return false;
}
else if (attrib.semantics == CSV::TIMESTAMP)
{
timestamp_found = true;
if (attrib.columns.empty() || attrib.columns.size() > 1)
return false;
}
else if ( (attrib.semantics == CSV::POS && pos_found)
|| (attrib.semantics == CSV::TRAJ_ID && traj_id_found)
|| (attrib.semantics == CSV::TIMESTAMP && timestamp_found))
return false;
}
return pos_found;
}
////
// Explicit template instantiations
// Only float and double variants are intended
template class csv_handler<float>;
template class csv_handler<double>;
////
// Object registration
// Register example handler for the IML multi-user study .csv files
static const csv_descriptor csv_imluser_desc("IML user trajectory", ",", {
{ "timestamp", {"timestamp", false, 0}, CSV::TIMESTAMP },
{ "id", {"id", false, 1}, CSV::TRAJ_ID },
{ "position", {{"pos_1", false, 2}, {"pos_2", false, 3}, {"pos_3", false, 4}}, CSV::POS }}
),
csv_imldevice_desc("IML device trajectory", ",", {
{ "timestamp", {"timestamp", false, 0}, CSV::TIMESTAMP },
{ "userid", {"userid", false, 1}, CSV::TRAJ_ID },
{ "position", {{"spacePos_1", false, 4}, {"spacePos_2", false, 5}, {"spacePos_3", false, 6}}, CSV::POS }}
);
cgv::base::object_registration_2<
csv_handler<float>, csv_descriptor, visual_attribute_mapping<float>
> csv_imluser_reg(
csv_imluser_desc,
visual_attribute_mapping<float>({
{VisualAttrib::POSITION, {
// we scale up the dataset to get more sensible numbers (mitigates floating point rounding errors etc.)
"position", attrib_transform<float>::vec3_to_vec3(
[](csv_handler<float>::Vec3 &out, const csv_handler<float>::Vec3 &in) {
out = 128.f * in;
}
)
}},
{VisualAttrib::RADIUS, {
// we scale up the dataset to get more sensible numbers (mitigates floating point rounding errors etc.)
"_radius", attrib_transform<float>::real_to_real(
[](float &out, const float &in) {
out = 128 * in;
}
)
}},
{VisualAttrib::COLOR, {
// ids are either 1 or 2, so by substracting 1 we can use them as input to a color scale
"id", attrib_transform<float>::real_to_real(
[](float &out, const float &in) { out = in - 1; }
)
}}},
// make user trajectories a green-ish color, with the first user being a bit lighter than the second
colormap({{ 178.f/255.f, 223.f/255.f, 138.f/255.f }, { 51.f/255.f, 160.f/255.f, 44.f/255.f }})
),
"csv handler (float) - "+csv_imluser_desc.name()
),
csv_imldevice_reg(
csv_imldevice_desc,
visual_attribute_mapping<float>({
{VisualAttrib::POSITION, {
// we flip the direction of the x and z axes, as the tracking system for the devices in the IML
// study used a different coordinate system, also scale up to get more sensible numbers (mitigates
// floating point rounding errors etc.)
"position", attrib_transform<float>::vec3_to_vec3(
[](csv_handler<float>::Vec3 &out, const csv_handler<float>::Vec3 &in) {
out.x() = -128 * in.x();
out.y() = 128 * in.y();
out.z() = -128 * in.z();
}
)
}},
{VisualAttrib::RADIUS, {
// we scale up the dataset to get more sensible numbers (mitigates floating point rounding errors etc.)
"_radius", attrib_transform<float>::real_to_real(
[](float &out, const float &in) {
out = 128 * in;
}
)
}},
{VisualAttrib::COLOR, {
// user ids are either 1 or 2, so by substracting 1 we can use them as input to a color scale
"userid", attrib_transform<float>::real_to_real(
[](float &out, const float &in) { out = in - 1; }
)
}}},
// make device trajectories a blue-ish color, with the first user being a bit lighter than the second
colormap({{ 166.f/255.f, 206.f/255.f, 227.f/255.f }, { 31.f/255.f, 120.f/255.f, 180.f/255.f }})
),
"csv handler (float) - "+csv_imldevice_desc.name()
);
// Register handler for streamline .csv files exported from paraview
static const csv_descriptor csv_paraview_streamline_desc("Paraview Streamline", ",", {
{ "t", {"IntegrationTime", false, 4}, CSV::TIMESTAMP },
{ "Position", {{"Points:0", false, 13}, {"Points:1", false, 14}, {"Points:2", false, 15}}, CSV::POS },
{ "Velocity", {{"U:0", false, 0}, {"U:1", false, 1}, {"U:2", false, 2}} },
{ "p", {"p", false, 3} },
{ "Vorticity", {{"Vorticity:0", false, 5}, {"Vorticity:1", false, 6}, {"Vorticity:2", false, 7}} },
{ "Vorticity.x", {"Vorticity:0", false, 5} }, // make individual
{ "Vorticity.y", {"Vorticity:1", false, 6} }, // components accessible
{ "Vorticity.z", {"Vorticity:2", false, 7} }, // also
{ "Normal", {{"Normals:0", false, 10}, {"Normals:1", false, 11}, {"Normals:2", false, 12}} },
{ "Normal.x", {"Normals:0", false, 10} }, // make individual
{ "Normal.y", {"Normals:1", false, 11} }, // components accessible
{ "Normal.z", {"Normals:2", false, 12} } // also
});
//static const csv_descriptor csv_paraview_streamline_desc("Paraview Streamline", ",", {
// { "timestamp", {"\"IntegrationTime\"", false, 4}, CSV::TIMESTAMP },
// { "position", {{"\"Points:0\"", false, 13}, {"\"Points:1\"", false, 14}, {"\"Points:2\"", false, 15}}, CSV::POS },
// { "velocity", {{"\"U:0\"", false, 0}, {"\"U:1\"", false, 1}, {"\"U:2\"", false, 2}} } }
//);
cgv::base::object_registration_2<
csv_handler<float>, csv_descriptor, visual_attribute_mapping<float>
> csv_paraview_streamline_reg(
csv_paraview_streamline_desc,
visual_attribute_mapping<float>({
{VisualAttrib::POSITION, {
// scale up dataset to make intersectors more numerically stable
"Position", attrib_transform<float>::vec3_to_vec3(
[](csv_handler<float>::Vec3& out, const csv_handler<float>::Vec3& in) {
out = 100.f*in;
}
)
}},
{VisualAttrib::RADIUS, {
// scale up radius accordingly but not as much to reduce overlapping tubes
"_radius", attrib_transform<float>::real_to_real(
[](float &out, const float &in) {
out = 75.f*in;
}
)
}}}
),
"csv handler (float) - "+csv_paraview_streamline_desc.name()
);
// Register handler for package delivery drone trajectory .csv files
static const csv_descriptor csv_pkg_drone_streamline_desc("Delivery Drone Trajectory", ",", {
{ "timestamp", {"time", false, 1}, CSV::TIMESTAMP },
{ "id", {"id", false, 0}, CSV::TRAJ_ID },
{ "position", {{"position_x", false, 6}, {"position_y", false, 7}, {"position_z", false, 8}}, CSV::POS },
{ "velocity", {{"velocity_x", false, 13}, {"velocity_y", false, 14}, {"velocity_z", false, 15}} },
{ "linear_acceleration", {{"linear_acceleration_x", false, 19}, {"linear_acceleration_y", false, 20}, {"linear_acceleration_z", false, 21}} },
// for reading raw files with GPS coordinates
/*{ "timestamp", {"time", false, 0}, CSV::TIMESTAMP },
{ "position", {{"position_x", false, 5}, {"position_y", false, 6}, {"position_z", false, 7}}, CSV::POS },
{ "velocity", {{"velocity_x", false, 12}, {"velocity_y", false, 13}, {"velocity_z", false, 14}} },
{ "linear_acceleration", {{"linear_acceleration_x", false, 18}, {"linear_acceleration_y", false, 19}, {"linear_acceleration_z", false, 20}} },*/
}
);
cgv::base::object_registration_2<
csv_handler<float>, csv_descriptor, visual_attribute_mapping<float>
> csv_pkg_drone_streamline_reg(
csv_pkg_drone_streamline_desc,
visual_attribute_mapping<float>({
{VisualAttrib::RADIUS, {
// increase radius a bit to get thicker tubes with more visible area
"_radius", attrib_transform<float>::real_to_real(
[](float &out, const float &in) {
out = 4.0f*in;
}
)
}}}
),
"csv handler (float) - " + csv_pkg_drone_streamline_desc.name()
);
// Register handler for RTLola trace export .csv files
static const csv_descriptor csv_rtlola_desc("RTLola Trace", ",", {
{ "time", {"time", false, 0}, CSV::TIMESTAMP },
{ "position", {{"lat", false, 1}, {"lon", false, 2}, {"altitude", false, 3}}, CSV::POS } }
);
cgv::base::object_registration_2<
csv_handler<float>, csv_descriptor, visual_attribute_mapping<float>
> csv_rtlola_reg(
csv_rtlola_desc,
visual_attribute_mapping<float>({
{VisualAttrib::POSITION, {
// transform lat/long/alt to cartesian coordinates using mercator projection
"position", attrib_transform<float>::vec3_to_vec3(
[](csv_handler<float>::Vec3 &out, const csv_handler<float>::Vec3 &in) {
typedef std::array<double, 2> latlong;
const static latlong refpos = {in.x(), in.y()};
//
const static latlong llmin = {-35.37080874562025f, 149.13721750526548f},
llmax = {-35.34067082104956f, 149.18727521267593f},
pmin = wgs84::toCartesian(refpos, llmin),
pmax = wgs84::toCartesian(refpos, llmax);
//
const auto mercator = wgs84::toCartesian(refpos, latlong{in.x(), in.y()});
out.set((float)mercator[0], in.z(), (float)mercator[1]);
}
)
}},
{VisualAttrib::RADIUS, {
// increase radius a bit to get thicker tubes with more visible area
"_radius", attrib_transform<float>::real_to_real(
[](float &out, const float &in) {
out = in;
}
)
}}}
),
"csv handler (float) - " + csv_rtlola_desc.name()
);
// Register handler for SimpleDebugTrace .csv files
static const csv_descriptor csv_dbg_trace_desc("SimpleDebugTrace", ",", {
{ "time", {"time", true, 0}, CSV::TIMESTAMP },
{ "pos", {{"pos_x", true, 1}, {"pos_y", true, 2}, {"pos_z", true, 3}}, CSV::POS },
{ "vel", {{"vel_x", true, 4}, {"vel_y", true, 5}, {"vel_z", true, 6}} },
{ "attrib0", {"attrib0", true, 7}} }
);
cgv::base::object_registration_2<
csv_handler<float>, csv_descriptor, visual_attribute_mapping<float>
> csv_dbg_trace_reg(
csv_dbg_trace_desc,
visual_attribute_mapping<float>({
{VisualAttrib::RADIUS, {
// increase radius a bit to get thicker tubes with more visible area
"_radius", attrib_transform<float>::real_to_real(
[](float &out, const float &in) {
out = in;
}
)
}},
{VisualAttrib::TANGENT, {
"vel", attrib_transform<float>::vec3_to_vec4(
[](csv_handler<float>::Vec4 &out, const csv_handler<float>::Vec3 &in) {
out = csv_handler<float>::Vec4(in, 0.f);
}
)
}}
}),
"csv handler (float) - " + csv_dbg_trace_desc.name()
);