-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglyph_compiler.h
904 lines (741 loc) · 32.3 KB
/
glyph_compiler.h
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
#pragma once
// CGV framework core
//#include <cgv/render/render_types.h>
// local includes
#include "arclen_helper.h"
#include "traj_loader.h"
#include "glyph_layer_manager.h"
class glyph_compiler {
public:
using vec4 = cgv::vec4;
using uvec2 = cgv::uvec2;
using mat4 = cgv::mat4;
protected:
// helper struct for range entries with start index i0 and count n
struct irange { int i0, n; };
// helper struct for glyph attributes
struct glyph_attributes {
size_t count = 0;
std::vector<float> data;
bool empty() const { return size() == 0; }
size_t size() const { return data.size(); }
size_t glyph_count() const {
return size() / (2 + count);
}
void add(const float& x) {
data.push_back(x);
}
float& operator [](int idx) {
return data[idx];
}
float operator [](int idx) const {
return data[idx];
}
float last_glyph_s() const {
if(size() > 0)
return data[size() - 1 - 1 - count];
else
return 0.0f;
}
};
// clamp value v to range [r.x,r.y] and remap to [r.z,r.w]
float clamp_remap(float v, const vec4& r) {
v = cgv::math::clamp(v, r.x(), r.y());
float t = 0.0f;
if(abs(r.x() - r.y()) > std::numeric_limits<float>::epsilon())
t = (v - r.x()) / (r.y() - r.x());
return cgv::math::lerp(r.z(), r.w(), t);
}
struct layer_compile_info {
const glyph_shape* current_shape;
std::vector<const traj_attribute<float>*> mapped_attribs;
std::vector<const std::vector<range>*> attribs_trajs;
size_t attrib_count;
std::vector<irange> ranges;
glyph_attributes attribs;
layer_compile_info(const glyph_shape* shape_ptr) : current_shape(shape_ptr) {}
void update_attribute_count() {
attrib_count = mapped_attribs.size();
attribs.count = attrib_count;
}
};
// generate a glyph at every attribute sample location (interpolates attributes if more than one is mapped in this layer)
void compile_glyphs_front_at_samples(const traj_attribute<float>& P, const std::vector<range>& tube_trajs, const std::vector<mat4>& arc_length, const glyph_layer_manager::configuration::layer_configuration& layer_config, layer_compile_info& lci) {
// convenience shorthands
const size_t attrib_count = lci.attrib_count;
const auto& mapped_attribs = lci.mapped_attribs;
const auto& attribs_trajs = lci.attribs_trajs;
auto& ranges = lci.ranges;
auto& attribs = lci.attribs;
const auto& alen = arc_length;
// create an index for each attribute
std::vector<unsigned> attrib_indices(attrib_count, 0);
// create storage for attribute and glyph parameter values
std::vector<traj_attribute<float>::datapoint_mag> data_points(attrib_count);
std::vector<float> attrib_values(attrib_count);
std::vector<bool> has_sample(attrib_count);
std::vector<float> glyph_params(lci.current_shape->num_size_attribs());
// - compile data
unsigned traj_offset = 0;
for(unsigned trj = 0; trj < (unsigned)tube_trajs.size(); trj++) {
const auto &tube_traj = tube_trajs[trj];
//const auto *alen = render.arclen_data.data();
const unsigned num_segments = tube_traj.n - 1;
const unsigned attribs_traj_offset = (unsigned)attribs.glyph_count();
// make sure there is exactly one 'range' entry per segment
ranges.resize(traj_offset + num_segments); // takes care of zero-initializing each entry
float prev_glyph_size = 0.0f;
float last_commited_s = 0.0f;
// index for the current segment
unsigned seg = 0;
// reset the sample availability status
std::fill(has_sample.begin(), has_sample.end(), false);
// stores the minimum t over all current attribute sample points in each iteration
float min_t;
// stores the index of the attribute with the minimum t
unsigned min_a_idx = 0;
bool run = true;
for(size_t i = 0; i < attrib_indices.size(); ++i) {
const auto &traj_range = attribs_trajs[i]->at(trj);
attrib_indices[i] = traj_range.i0;
if(traj_range.n < 2) // single-sample trajectory, assignment doesn't make sense here
run &= false;
}
run &= seg < num_segments;
// following variable only needed for debugging
unsigned glyph_idx = 0;
while(run) {
//if(i > 0) // enforce monotonicity
// // TODO: this fails when using the debug-size dataset
// assert(a.t >= mapped_attribs[0]->signed_magnitude_at(i - 1).t);
min_t = std::numeric_limits<float>::max();
for(size_t i = 0; i < attrib_count; ++i) {
auto a = mapped_attribs[i]->signed_magnitude_at(attrib_indices[i]);
data_points[i] = a;
if(a.t < min_t) {
min_a_idx = (unsigned)i;
min_t = a.t;
}
}
// advance segment pointer
auto segtime = segment_time_get(P, tube_traj, seg);
while(min_t >= segtime.t1) {
if(seg >= num_segments - 1)
break;
segtime = segment_time_get(P, tube_traj, ++seg);
// handle overlap from previous segment
const unsigned global_seg = traj_offset + seg;
if(ranges[global_seg - 1].n > 0) {
// using half size of previous glyph
if(prev_glyph_size < 0.0f) {
// "glyphs" with a negative size value are possibly infinite in size and always overlap onto the next segment
ranges[global_seg].i0 = (int)attribs.glyph_count() - 1;
ranges[global_seg].n = 1;
} else {
if(alen[global_seg][0] < attribs.last_glyph_s() + 0.5f*prev_glyph_size) {
ranges[global_seg].i0 = (int)attribs.glyph_count() - 1;
ranges[global_seg].n = 1;
}
}
}
}
const unsigned global_seg = traj_offset + seg;
// commit the attribute if it falls into the current segment
//if((min_t >= segtime.t0 && min_t < segtime.t1)
// || (seg == num_segments - 1 && min_t <= segtime.t1)) {
if((seg == num_segments - 1 || min_t >= segtime.t0) && min_t <= segtime.t1) {
// compute segment-relative t and arclength
const float t_seg = (min_t - segtime.t0) / (segtime.t1 - segtime.t0),
s = arclen::eval(alen[global_seg], t_seg);
for(size_t i = 0; i < attrib_count; ++i) {
unsigned attrib_idx = attrib_indices[i];
const auto& a_curr = data_points[i];
float val = a_curr.val;
// TODO: make epsilon adjustable
bool found_sample = abs(min_t - a_curr.t) < 0.001f;
has_sample[i] = found_sample;
if(!found_sample && attrib_idx > 0) {
// get interpolated value
auto a_prev = mapped_attribs[i]->signed_magnitude_at(attrib_idx - 1);
float t = (min_t - a_prev.t) / (a_curr.t - a_prev.t);
val = cgv::math::lerp(a_prev.val, val, t);
}
attrib_values[i] = val;
}
// setup parameters of potential glyph
for(size_t i = 0; i < layer_config.glyph_mapping_parameters.size(); ++i) {
const auto& triple = layer_config.glyph_mapping_parameters[i];
if(triple.type == 0) {
// constant attribute
glyph_params[i] = (*triple.v)[3];
} else {
// mapped attribute
const vec4& ranges = *(triple.v);
// use windowing and remapping to get the value of the glyph parameter
glyph_params[i] = clamp_remap(attrib_values[triple.idx], ranges);
}
}
float new_glyph_size = lci.current_shape->get_size(glyph_params);
new_glyph_size /= length_scale;
// infer potential glyph extents
const float min_dist = attribs.size() > 0 ?
std::max(new_glyph_size, prev_glyph_size) :
new_glyph_size;
bool include_glyph = attribs.glyph_count() == attribs_traj_offset || s >= last_commited_s + min_dist;
include_glyph |= min_dist < 0.0f;
if(include_glyph || include_hidden_glyphs) {
auto &cur_range = ranges[global_seg];
if(cur_range.n < 1) {
// first free attribute that falls into this segment
cur_range.i0 = (unsigned)attribs.glyph_count();
cur_range.n = 1;
// handle overlap to previous segment (this only works for a single previous segment)
/*if(seg > 0 && alen[global_seg - 1][15] > s - 0.5f*new_glyph_size) {
// if there have been no glyphs comitted to the previous segment until now, also update its start index
if(ranges[global_seg - 1].n == 0)
ranges[global_seg - 1].i0 = cur_range.i0;
ranges[global_seg - 1].n++;
}*/
// handle overlap to the previous segments
if(seg > 0) {
int prev_seg = static_cast<int>(global_seg - 1);
int min_global_seg = static_cast<int>(traj_offset);
float min_s = s - 0.5f*new_glyph_size;
if(min_s >= 0.0f) {
while(prev_seg >= min_global_seg && alen[prev_seg][15] > min_s) {
// if there have been no glyphs comitted to the previous segment until now, also update its start index
auto& prev_range = ranges[prev_seg];
if(prev_range.n == 0)
prev_range.i0 = cur_range.i0;
prev_range.n++;
prev_seg--;
}
}
}
} else {
// one more free attribute that falls into this segment
cur_range.n++;
// for infinitely sized "glyphs" there always will have been overlap from the previous segment, so the above branch won't have been executed
if(global_seg > 0 && new_glyph_size < 0.0) {
// "glyphs" with a negative size value are possibly infinite in size and always overlap onto the previous segment
ranges[global_seg - 1].n++;
}
}
// store the new glyph
attribs.add(s);
int debug_info = include_glyph ? 0 : 1;
attribs.add(*reinterpret_cast<float*>(&debug_info));
std::copy(attrib_values.begin(), attrib_values.end(), std::back_inserter(attribs.data));
}
//store the size when this glyph is actually placed
if(include_glyph) {
prev_glyph_size = new_glyph_size;
last_commited_s = s;
}
} else {
// If the attrib does not fall into the current segment something is out of order.
// We just increment the attribute index with the minimal timestamp.
has_sample[min_a_idx] = true;
}
// increment indices and check whether the indices of all attributes have reached the end
for(size_t i = 0; i < attrib_count; ++i) {
const auto &traj_range = attribs_trajs[i]->at(trj);
const unsigned max_attrib_index = traj_range.i0 + traj_range.n;
// only increment indices of attributes that have a sample at the current location (min_a.t)
if(has_sample[i])
attrib_indices[i] = std::min(max_attrib_index, ++attrib_indices[i]);
if(attrib_indices[i] >= max_attrib_index)
run &= false;
}
run &= seg < num_segments;
++glyph_idx;
//if(glyph_idx >= max_glyph_count)
// run = false;
}
// update auxiliary indices
traj_offset += num_segments;
}
// fill the attribute buffer with one glyph entry if it is empty (will cause crash otherwise)
if(attribs.empty()) {
attribs.add(0.0f);
attribs.add(0.0f);
for(size_t i = 0; i < attrib_count; ++i)
attribs.add(0.0f);
}
}
// generate a glyph at uniformly spaced time steps by interpolating attributes
void compile_glyphs_front_uniform_time(const traj_attribute<float>& P, const std::vector<range>& tube_trajs, const std::vector<mat4>& arc_length, const glyph_layer_manager::configuration::layer_configuration& layer_config, layer_compile_info& lci) {
// convenience shorthands
const size_t attrib_count = lci.attrib_count;
const auto& mapped_attribs = lci.mapped_attribs;
const auto& attribs_trajs = lci.attribs_trajs;
auto& ranges = lci.ranges;
auto& attribs = lci.attribs;
const auto& alen = arc_length;
// stores an index pair for each attribute
std::vector<uvec2> attrib_indices(attrib_count, uvec2(0, 1));
// stores the count of each attribute
std::vector<unsigned> attrib_index_counts(attrib_count, 0);
// create storage for attribute and glyph parameter values
std::vector<float> attrib_values(attrib_count);
std::vector<float> glyph_params(lci.current_shape->num_size_attribs());
// - compile data
unsigned traj_offset = 0;
for(unsigned trj = 0; trj < (unsigned)tube_trajs.size(); trj++) {
const auto &tube_traj = tube_trajs[trj];
const unsigned num_segments = tube_traj.n - 1;
const unsigned attribs_traj_offset = (unsigned)attribs.glyph_count();
// make sure there is exactly one 'range' entry per segment
ranges.resize(traj_offset + num_segments); // takes care of zero-initializing each entry
float prev_glyph_size = 0.0f;
float last_commited_s = 0.0f;
// index for the current segment
unsigned seg = 0;
// stores the current t at which we want to sample the attributes
float sample_t = 0.0f;
const float sample_step = layer_config.sampling_step;
// initializa all attribute index pairs to point to the first two attributes
// and gather the total count of attributes for this trajectory
bool run = true;
for(size_t i = 0; i < attrib_count; ++i) {
const auto &traj_range = attribs_trajs[i]->at(trj);
unsigned idx = traj_range.i0;
attrib_indices[i] = uvec2(idx, idx + 1);
attrib_index_counts[i] = traj_range.i0 + traj_range.n;
if(traj_range.n < 2) // single-sample trajectory, assignment doesn't make sense here
run &= false;
}
run &= seg < num_segments;
// TODO: make this adapt to data set?
if(sample_step < 0.005) {
std::cout << "sample step too low" << std::endl;
run = false;
}
// test if the first sample time point is before the first attribute sample for each attribute
for(size_t i = 0; i < attrib_count; ++i) {
auto a = mapped_attribs[i]->signed_magnitude_at(attrib_indices[i].x());
if(sample_t < a.t) // if yes, set the indices of this attribute to both point to the very first sample
attrib_indices[i].y() = attrib_indices[i].x();
}
// following variable only needed for debugging
unsigned glyph_idx = 0;
while(run) {
//if(i > 0) // enforce monotonicity
// // TODO: this fails when using the debug-size dataset
// assert(a.t >= mapped_attribs[0]->signed_magnitude_at(i - 1).t);
// iterate over each attribute
for(size_t i = 0; i < attrib_count; ++i) {
const auto& mapped_attrib = mapped_attribs[i];
uvec2& indices = attrib_indices[i];
const unsigned count = attrib_index_counts[i];
// increment indices until the sample time point lies between the first and second attribute sample
while(
sample_t > mapped_attrib->signed_magnitude_at(indices.y()).t &&
indices.x() < count - 1
) {
indices.x() = indices.y();
indices.y() = std::min(indices.x() + 1, count - 1);
}
}
// advance segment pointer
auto segtime = segment_time_get(P, tube_traj, seg);
while(sample_t >= segtime.t1) {
if(seg >= num_segments - 1)
break;
segtime = segment_time_get(P, tube_traj, ++seg);
// handle overlap from previous segment
const unsigned global_seg = traj_offset + seg;
if(ranges[global_seg - 1].n > 0) {
// using half size of previous glyph
if(prev_glyph_size < 0.0f) {
// "glyphs" with a negative size value are possibly infinite in size and always overlap onto the next segment
ranges[global_seg].i0 = (int)attribs.glyph_count() - 1;
ranges[global_seg].n = 1;
} else {
if(alen[global_seg][0] < attribs.last_glyph_s() + 0.5f*prev_glyph_size) {
ranges[global_seg].i0 = (int)attribs.glyph_count() - 1;
ranges[global_seg].n = 1;
}
}
}
}
const unsigned global_seg = traj_offset + seg;
// commit the attribute if it falls into the current segment
if((seg == num_segments - 1 || sample_t >= segtime.t0) && sample_t <= segtime.t1) {
// compute segment-relative t and arclength
const float t_seg = (sample_t - segtime.t0) / (segtime.t1 - segtime.t0),
s = arclen::eval(alen[global_seg], t_seg);
// interpolate each mapped attribute value
for(size_t i = 0; i < attrib_count; ++i) {
const uvec2& attrib_idx = attrib_indices[i];
auto a0 = mapped_attribs[i]->signed_magnitude_at(attrib_idx.x());
auto a1 = mapped_attribs[i]->signed_magnitude_at(attrib_idx.y());
float denom = a1.t - a0.t;
float t = 0.0f;
if(abs(denom) > std::numeric_limits<float>::epsilon())
t = (sample_t - a0.t) / denom;
attrib_values[i] = cgv::math::lerp(a0.val, a1.val, t);
}
// setup parameters of potential glyph
for(size_t i = 0; i < layer_config.glyph_mapping_parameters.size(); ++i) {
const auto& triple = layer_config.glyph_mapping_parameters[i];
if(triple.type == 0) {
// constant attribute
glyph_params[i] = (*triple.v)[3];
} else {
// mapped attribute
const vec4& ranges = *(triple.v);
// use windowing and remapping to get the value of the glyph parameter
glyph_params[i] = clamp_remap(attrib_values[triple.idx], ranges);
}
}
float new_glyph_size = lci.current_shape->get_size(glyph_params);
new_glyph_size /= length_scale;
// infer potential glyph extents
const float min_dist = attribs.size() > 0 ?
std::max(new_glyph_size, prev_glyph_size) :
new_glyph_size;
bool include_glyph = attribs.glyph_count() == attribs_traj_offset || s >= last_commited_s + min_dist;
include_glyph |= min_dist < 0.0f;
if(include_glyph || include_hidden_glyphs) {
auto &cur_range = ranges[global_seg];
if(cur_range.n < 1) {
// first free attribute that falls into this segment
cur_range.i0 = (unsigned)attribs.glyph_count();
cur_range.n = 1;
// handle overlap to the previous segments
if(seg > 0) {
int prev_seg = static_cast<int>(global_seg - 1);
int min_global_seg = static_cast<int>(traj_offset);
float min_s = s - 0.5f*new_glyph_size;
if(min_s >= 0.0f) {
while(prev_seg >= min_global_seg && alen[prev_seg][15] > min_s) {
// if there have been no glyphs comitted to the previous segment until now, also update its start index
auto& prev_range = ranges[prev_seg];
if(prev_range.n == 0)
prev_range.i0 = cur_range.i0;
prev_range.n++;
prev_seg--;
}
}
}
} else {
// one more free attribute that falls into this segment
cur_range.n++;
// for infinitely sized "glyphs" there always will have been overlap from the previous segment, so the above branch won't have been executed
if(global_seg > 0 && new_glyph_size < 0.0) {
// "glyphs" with a negative size value are possibly infinite in size and always overlap onto the previous segment
ranges[global_seg - 1].n++;
}
}
// store the new glyph
attribs.add(s);
int debug_info = include_glyph ? 0 : 1;
attribs.add(*reinterpret_cast<float*>(&debug_info));
std::copy(attrib_values.begin(), attrib_values.end(), std::back_inserter(attribs.data));
}
//store the size when this glyph is actually placed
if(include_glyph) {
prev_glyph_size = new_glyph_size;
last_commited_s = s;
}
}
// check whether the indices of all attributes have reached the end
for(size_t i = 0; i < attrib_count; ++i) {
if(attrib_indices[i].x() >= attrib_index_counts[i] - 1)
run &= false;
}
run &= seg < num_segments;
//++glyph_idx;
//if(glyph_idx >= max_glyph_count)
// run = false;
// increment the sample time point
sample_t += sample_step;
}
// update auxiliary indices
traj_offset += num_segments;
}
}
// generate a glyph at uniformly spaced time steps by interpolating attributes
void compile_glyphs_front_equidistant(const traj_attribute<float> &P, const std::vector<range> &tube_trajs, const arclen::parametrization ¶m, const glyph_layer_manager::configuration::layer_configuration &layer_config, layer_compile_info &lci) {
// convenience shorthands
const size_t attrib_count = lci.attrib_count;
const auto& mapped_attribs = lci.mapped_attribs;
const auto& attribs_trajs = lci.attribs_trajs;
auto& ranges = lci.ranges;
auto& attribs = lci.attribs;
// stores an index pair for each attribute
std::vector<uvec2> attrib_indices(attrib_count, uvec2(0, 1));
// stores the count of each attribute
std::vector<unsigned> attrib_index_counts(attrib_count, 0);
// create storage for attribute and glyph parameter values
std::vector<float> attrib_values(attrib_count);
std::vector<float> glyph_params(lci.current_shape->num_size_attribs());
// - compile data
unsigned traj_offset = 0;
for(unsigned trj = 0; trj < (unsigned)tube_trajs.size(); trj++) {
const auto &tube_traj = tube_trajs[trj];
const unsigned num_segments = tube_traj.n - 1;
const unsigned attribs_traj_offset = (unsigned)attribs.glyph_count();
// make sure there is exactly one 'range' entry per segment
ranges.resize(traj_offset + num_segments); // takes care of zero-initializing each entry
float prev_glyph_size = 0.0f;
float last_commited_s = 0.0f;
// index for the current segment
unsigned seg = 0;
// stores the current t at which we want to sample the attributes
float sample_t = 0, sample_s = 0;
const float sample_step = layer_config.sampling_step;
// initializa all attribute index pairs to point to the first two attributes
// and gather the total count of attributes for this trajectory
bool run = true;
for(size_t i = 0; i < attrib_count; ++i) {
const auto &traj_range = attribs_trajs[i]->at(trj);
unsigned idx = traj_range.i0;
attrib_indices[i] = uvec2(idx, idx + 1);
attrib_index_counts[i] = traj_range.i0 + traj_range.n;
if(traj_range.n < 2) // single-sample trajectory, assignment doesn't make sense here
run &= false;
}
run &= seg < num_segments;
// TODO: make this adapt to data set?
if(sample_step < 0.005) {
std::cout << "sample step too low" << std::endl;
run = false;
}
// test if the first sample time point is before the first attribute sample for each attribute
for(size_t i = 0; i < attrib_count; ++i) {
auto a = mapped_attribs[i]->signed_magnitude_at(attrib_indices[i].x());
if(sample_t < a.t) // if yes, set the indices of this attribute to both point to the very first sample
attrib_indices[i].y() = attrib_indices[i].x();
}
// following variable only needed for debugging
unsigned glyph_idx = 0;
while(run) {
//if(i > 0) // enforce monotonicity
// // TODO: this fails when using the debug-size dataset
// assert(a.t >= mapped_attribs[0]->signed_magnitude_at(i - 1).t);
// iterate over each attribute
for(size_t i = 0; i < attrib_count; ++i) {
const auto& mapped_attrib = mapped_attribs[i];
uvec2& indices = attrib_indices[i];
const unsigned count = attrib_index_counts[i];
// increment indices until the sample time point lies between the first and second attribute sample
while(
sample_t > mapped_attrib->signed_magnitude_at(indices.y()).t &&
indices.x() < count - 1
) {
indices.x() = indices.y();
indices.y() = std::min(indices.x() + 1, count - 1);
}
}
// advance segment pointer
auto segtime = segment_time_get(P, tube_traj, seg);
while(sample_t >= segtime.t1) {
if(seg >= num_segments - 1)
break;
segtime = segment_time_get(P, tube_traj, ++seg);
// handle overlap from previous segment
const unsigned global_seg = traj_offset + seg;
if(ranges[global_seg - 1].n > 0) {
// using half size of previous glyph
if(prev_glyph_size < 0.0f) {
// "glyphs" with a negative size value are possibly infinite in size and always overlap onto the next segment
ranges[global_seg].i0 = (int)attribs.glyph_count() - 1;
ranges[global_seg].n = 1;
} else {
if(param.t_to_s[global_seg][0] < attribs.last_glyph_s() + 0.5f*prev_glyph_size) {
ranges[global_seg].i0 = (int)attribs.glyph_count() - 1;
ranges[global_seg].n = 1;
}
}
}
}
const unsigned global_seg = traj_offset + seg;
// commit the attribute if it falls into the current segment
if((seg == num_segments - 1 || sample_t >= segtime.t0) && sample_t <= segtime.t1) {
// compute segment-relative t and arclength
const float t_seg = (sample_t - segtime.t0) / (segtime.t1 - segtime.t0),
s = arclen::eval(param.t_to_s[global_seg], t_seg);
// interpolate each mapped attribute value
for(size_t i = 0; i < attrib_count; ++i) {
const uvec2& attrib_idx = attrib_indices[i];
auto a0 = mapped_attribs[i]->signed_magnitude_at(attrib_idx.x());
auto a1 = mapped_attribs[i]->signed_magnitude_at(attrib_idx.y());
float denom = a1.t - a0.t;
float t = 0.0f;
if(abs(denom) > std::numeric_limits<float>::epsilon())
t = (sample_t - a0.t) / denom;
attrib_values[i] = cgv::math::lerp(a0.val, a1.val, t);
}
// setup parameters of potential glyph
for(size_t i = 0; i < layer_config.glyph_mapping_parameters.size(); ++i) {
const auto& triple = layer_config.glyph_mapping_parameters[i];
if(triple.type == 0) {
// constant attribute
glyph_params[i] = (*triple.v)[3];
} else {
// mapped attribute
const vec4& ranges = *(triple.v);
// use windowing and remapping to get the value of the glyph parameter
glyph_params[i] = clamp_remap(attrib_values[triple.idx], ranges);
}
}
float new_glyph_size = lci.current_shape->get_size(glyph_params);
new_glyph_size /= length_scale;
// infer potential glyph extents
const float min_dist = attribs.size() > 0 ?
std::max(new_glyph_size, prev_glyph_size) :
new_glyph_size;
bool include_glyph = attribs.glyph_count() == attribs_traj_offset || s >= last_commited_s + min_dist;
include_glyph |= min_dist < 0.0f;
if(include_glyph || include_hidden_glyphs) {
auto &cur_range = ranges[global_seg];
if(cur_range.n < 1) {
// first free attribute that falls into this segment
cur_range.i0 = (unsigned)attribs.glyph_count();
cur_range.n = 1;
// handle overlap to the previous segments
if(seg > 0) {
int prev_seg = static_cast<int>(global_seg - 1);
int min_global_seg = static_cast<int>(traj_offset);
float min_s = s - 0.5f*new_glyph_size;
if(min_s >= 0.0f) {
while(prev_seg >= min_global_seg && param.t_to_s[prev_seg][15] > min_s) {
// if there have been no glyphs comitted to the previous segment until now, also update its start index
auto& prev_range = ranges[prev_seg];
if(prev_range.n == 0)
prev_range.i0 = cur_range.i0;
prev_range.n++;
prev_seg--;
}
}
}
} else {
// one more free attribute that falls into this segment
cur_range.n++;
// for infinitely sized "glyphs" there always will have been overlap from the previous segment, so the above branch won't have been executed
if(global_seg > 0 && new_glyph_size < 0.0) {
// "glyphs" with a negative size value are possibly infinite in size and always overlap onto the previous segment
ranges[global_seg - 1].n++;
}
}
// store the new glyph
attribs.add(s);
int debug_info = include_glyph ? 0 : 1;
attribs.add(*reinterpret_cast<float*>(&debug_info));
std::copy(attrib_values.begin(), attrib_values.end(), std::back_inserter(attribs.data));
}
//store the size when this glyph is actually placed
if(include_glyph) {
prev_glyph_size = new_glyph_size;
last_commited_s = s;
}
}
// check whether the indices of all attributes have reached the end
for(size_t i = 0; i < attrib_count; ++i) {
if(attrib_indices[i].x() >= attrib_index_counts[i] - 1)
run &= false;
}
run &= seg < num_segments;
//++glyph_idx;
//if(glyph_idx >= max_glyph_count)
// run = false;
// increment the sample point
sample_s += sample_step;
// update current sample_t
unsigned next_seg = seg, next_seg_global = global_seg;
// - find segment the next point is in
while (next_seg < num_segments && sample_s > param.t_to_s[next_seg_global][15])
{ next_seg++; next_seg_global++; }
// - terminate immediatly if next sample point is beyond trajectory bound
if (next_seg >= num_segments) break;
// - query arclenght parametrization for segment t and offset to get actual global timestamp
const float sample_t_local = arclen::map(param.t_to_s[next_seg_global], param.s_to_t[next_seg_global], sample_s);
segtime = segment_time_get(P, tube_traj, next_seg);
sample_t = segtime.t0 + sample_t_local*(segtime.t1-segtime.t0);
}
// update auxiliary indices
traj_offset += num_segments;
}
}
void compile_glyph_layer(size_t layer_idx, const traj_dataset<float>& data_set, const arclen::parametrization ¶metrization, const std::vector<std::string>& attrib_names, const glyph_layer_manager::configuration::layer_configuration& layer_config, const traj_attribute<float>& P, const std::vector<range>& tube_trajs) {
const AttributeSamplingStrategy sampling_strategy = layer_config.sampling_strategy;
layer_compile_info lci(layer_config.shape_ptr);
for(size_t i = 0; i < layer_config.mapped_attributes.size(); ++i) {
int attrib_idx = layer_config.mapped_attributes[i];
if(attrib_idx < 0 || attrib_idx >= attrib_names.size()) {
std::cout << "Error: glyph_compiler::compile_glyph_layer - attribute index out of range" << std::endl;
continue;
}
// from the docs: returns an explicitly invalid attribute interface that acts "empty" on all relevant queries
// if no attribute of the given name exists in the dataset
const traj_attribute<float>& attrib = data_set.attribute(attrib_names[attrib_idx]);
lci.mapped_attribs.push_back(&attrib);
// from the docs: returns an explicitly invalid range that indicates zero samples in the trajectory if the dataset has
// no trajectory information for the attribute
lci.attribs_trajs.push_back(&data_set.trajectories(attrib));
}
lci.update_attribute_count();
// reserve memory (might not make a huge or even noticeable difference)
//lci.attribs.reserve(attribs.size() + ...);
// reserve the maximum amount of possible segments; actual segment count may be less if some nodes are used multiple times
lci.ranges.reserve(P.num() - tube_trajs.size());
switch (sampling_strategy)
{
case ASS_UNIFORM:
compile_glyphs_front_uniform_time(P, tube_trajs, parametrization.t_to_s, layer_config, lci);
break;
case ASS_EQUIDIST:
compile_glyphs_front_equidistant(P, tube_trajs, parametrization, layer_config, lci);
break;
case ASS_AT_SAMPLES:
compile_glyphs_front_at_samples(P, tube_trajs, parametrization.t_to_s, layer_config, lci);
default:
/* DoNothing() */;
}
layer_filled[layer_idx] = true;
layer_ranges[layer_idx] = lci.ranges;
layer_attribs[layer_idx] = lci.attribs;
}
void compile_glyph_attributes_impl(const traj_dataset<float> &data_set, const arclen::parametrization ¶metrization, const glyph_layer_manager::configuration &layers_config) {
const auto &P = data_set.positions().attrib;
const auto &tube_trajs = data_set.trajectories(P);
auto attrib_names = data_set.get_attribute_names();
size_t layer_count = layers_config.layer_configs.size();
layer_filled.resize(layer_count, false);
layer_ranges.resize(layer_count, std::vector<irange>());
layer_attribs.resize(layer_count, glyph_attributes());
// build seperate range and attribs buffers for each glyph layer
// could be parallelized here but only gives very moderate speedup (and only if more than one alyer is used)
// parallelization of trajectory loop in each layer might be a better bet to improve performance but is non-trivial to implement
//#pragma omp parallel for
// for(int layer_idx = 0; layer_idx < layer_count; ++layer_idx) {
for(size_t layer_idx = 0; layer_idx < layer_count; ++layer_idx) {
const auto& layer_config = layers_config.layer_configs[layer_idx];
// skip this layer if it does not have any mapped attributes
if(layer_config.mapped_attributes.size() == 0) {
continue;
}
compile_glyph_layer(layer_idx, data_set, parametrization, attrib_names, layer_config, P, tube_trajs);
}
}
public:
glyph_compiler() {}
std::vector<bool> layer_filled;
std::vector<std::vector<irange>> layer_ranges;
std::vector<glyph_attributes> layer_attribs;
bool include_hidden_glyphs;
float length_scale;
bool compile_glyph_attributes(const traj_dataset<float> &data_set, const arclen::parametrization ¶metrization, const glyph_layer_manager::configuration &layers_config) {
bool success = false;
layer_filled.clear();
layer_ranges.clear();
layer_attribs.clear();
if(layers_config.layer_configs.size() > 0) {
compile_glyph_attributes_impl(data_set, parametrization, layers_config);
success = true;
}
return success;
}
};