-
Notifications
You must be signed in to change notification settings - Fork 14
/
APRFilter.hpp
1351 lines (1060 loc) · 58.7 KB
/
APRFilter.hpp
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
//
// Created by cheesema on 31.10.18.
//
#ifndef LIBAPR_APRFILTER_HPP
#define LIBAPR_APRFILTER_HPP
#ifndef M_PI
#define M_PI 3.14159265358979323846 //added for clang-cl windows compilation as cmath.h does not define this in clang (not part of the standard)
#endif
#include "numerics/APRStencil.hpp"
#include "numerics/APRTreeNumerics.hpp"
#include<cmath>
template<typename T>
class ImageBuffer {
public:
int y_num;
int x_num;
int z_num;
std::vector<T> mesh;
ImageBuffer() {
init(0, 0, 0);
}
ImageBuffer(int aSizeOfY, int aSizeOfX, int aSizeOfZ) {
init(aSizeOfY, aSizeOfX, aSizeOfZ);
}
void init(int aSizeOfY, int aSizeOfX, int aSizeOfZ) {
y_num = aSizeOfY;
x_num = aSizeOfX;
z_num = aSizeOfZ;
mesh.resize((size_t) y_num * x_num * z_num);
}
T& at(int y, int x, int z) {
size_t idx = (size_t) z * x_num * y_num + x * y_num + y;
return mesh[idx];
}
};
namespace APRFilter {
/**
* Applies APR convolution by constructing isotropic regions using a temporary buffer of size
* (Sz, x_num, y_num), where Sz is the stencil dimensions in z, x_num and y_num are the size
* of the image domain in x and y.
*
* @tparam ParticleDataTypeInput
* @tparam T
* @tparam ParticleDataTypeOutput
* @param apr
* @param stencils std::vector containing one or more PixelData stencils. If multiple stencils are supplied,
* stencils[0] is applied to the finest resolution particles, stencils[1] to one level coarser
* particles, and so on. The last stencil is applied to all remaining levels.
* @param particle_input input particles
* @param particle_output output particles
* @param reflect_boundary use reflective boundary condition? (default: true)
*/
template<typename ParticleDataTypeInput, typename T,typename ParticleDataTypeOutput>
void convolve(APR &apr, const std::vector<PixelData<T>>& stencils, const ParticleDataTypeInput &particle_input,
ParticleDataTypeOutput &particle_output, bool reflect_boundary=true);
/**
* Applies APR convolution by constructing isotropic regions using a temporary buffer of size
* (Sz, Sx, y_num), where Sz and Sx are the stencil dimensions in z and x, and y_num is the
* size of the image domain in y. Produces the same result as APRFilter::convolve, but requires
* less memory and is often faster for large image domains.
*
* @tparam ParticleDataTypeInput
* @tparam T
* @tparam ParticleDataTypeOutput
* @param apr
* @param stencils std::vector containing one or more PixelData stencils. If multiple stencils are supplied,
* stencils[0] is applied to the finest resolution particles, stencils[1] to one level coarser
* particles, and so on. The last stencil is applied to all remaining levels.
* @param particle_input input particles
* @param particle_output output particles
* @param reflect_boundary use reflective boundary condition? (default: true)
*/
template<typename ParticleDataTypeInput, typename T,typename ParticleDataTypeOutput>
void convolve_pencil(APR &apr, const std::vector<PixelData<T>>& stencils, const ParticleDataTypeInput &particle_input,
ParticleDataTypeOutput &particle_output, bool reflect_boundary=true);
/**
* Prepares a single PixelData stencil and applies APR convolution (APRFilter::convolve).
* @tparam ParticleDataTypeInput
* @tparam T
* @tparam ParticleDataTypeOutput
* @param apr
* @param stencil
* @param particle_input
* @param particle_output
* @param reflect_boundary
* @param use_stencil_downsample if true, restricts the stencil at lower resolution levels to make the result
* consistent with applying the input stencil to a reconstructed pixel image and
* then resampling the particle values. if false, applies the input stencil to all
* particles. (default: true)
* @param normalize should the stencil(s) be normalized to sum to unity? (default: false)
*/
template<typename ParticleDataTypeInput, typename T,typename ParticleDataTypeOutput>
void convolve(APR &apr, const PixelData<T>& stencil, const ParticleDataTypeInput &particle_input,
ParticleDataTypeOutput &particle_output, const bool reflect_boundary=true,
const bool use_stencil_downsample=true, const bool normalize=false) {
int num_levels = use_stencil_downsample ? apr.level_max() - apr.level_min() : 1;
std::vector<PixelData<T>> stencil_vec;
APRStencil::get_downsampled_stencils(stencil, stencil_vec, num_levels, normalize);
convolve(apr, stencil_vec, particle_input, particle_output, reflect_boundary);
}
/**
* Prepares a single PixelData stencil and applies APR convolution (APRFilter::convolve_pencil).
* @tparam ParticleDataTypeInput
* @tparam T
* @tparam ParticleDataTypeOutput
* @param apr
* @param stencil
* @param particle_input
* @param particle_output
* @param reflect_boundary
* @param use_stencil_downsample if true, restricts the stencil at lower resolution levels to make the result
* consistent with applying the input stencil to a reconstructed pixel image and
* then resampling the particle values. if false, applies the input stencil to all
* particles. (default: true)
* @param normalize should the stencil(s) be normalized to sum to unity? (default: false)
*/
template<typename ParticleDataTypeInput, typename T,typename ParticleDataTypeOutput>
void convolve_pencil(APR &apr, const PixelData<T>& stencil, const ParticleDataTypeInput &particle_input,
ParticleDataTypeOutput &particle_output, const bool reflect_boundary=true,
const bool use_stencil_downsample=true, const bool normalize=false) {
int num_levels = use_stencil_downsample ? apr.level_max() - apr.level_min() : 1;
std::vector<PixelData<T>> stencil_vec;
APRStencil::get_downsampled_stencils(stencil, stencil_vec, num_levels, normalize);
convolve_pencil(apr, stencil_vec, particle_input, particle_output, reflect_boundary);
}
template<typename ParticleDataTypeInput, typename T,typename ParticleDataTypeOutput, typename TreeDataType>
void convolve_pencil(APR &apr,
const std::vector<PixelData<T>>& stencils,
const ParticleDataTypeInput &particle_input,
const TreeDataType &tree_data,
ParticleDataTypeOutput &particle_output,
bool reflect_boundary);
template<typename ParticleDataTypeInput, typename T,typename ParticleDataTypeOutput, typename TreeDataType>
void convolve_pencil(APR &apr,
const PixelData<T>& stencil,
const ParticleDataTypeInput &particle_input,
const TreeDataType &tree_data,
ParticleDataTypeOutput &particle_output,
const bool reflect_boundary=true,
const bool use_stencil_downsample=true,
const bool normalize=false) {
int num_levels = use_stencil_downsample ? apr.level_max() - apr.level_min() : 1;
std::vector<PixelData<T>> stencil_vec;
APRStencil::get_downsampled_stencils(stencil, stencil_vec, num_levels, normalize);
convolve_pencil(apr, stencil_vec, particle_input, tree_data, particle_output, reflect_boundary);
}
/**
* Applies three successive convolutions, in order:
* stencil_y -> stencil_x -> stencil_z
* There is no check for stencil dimensions, but the typical use case would be a separable convolution,
* using three one-dimensional stencils.
*
* NOTE: Separable convolutions on the APR does not produce exactly the same result as directly
* applying the corresponding dense convolution, due to differences between resolution levels.
* TODO: look into this analytically
*/
template<typename InputType, typename StencilType, typename OutputType>
void convolve_sep(APR &apr,
const std::vector<PixelData<StencilType>>& stencils_y,
const std::vector<PixelData<StencilType>>& stencils_x,
const std::vector<PixelData<StencilType>>& stencils_z,
const ParticleData<InputType> &particle_input,
ParticleData<OutputType> &particle_output,
const bool reflect_boundary=true) {
ParticleData<OutputType> tmp;
convolve_pencil(apr, stencils_y, particle_input, particle_output, reflect_boundary);
convolve_pencil(apr, stencils_x, particle_output, tmp, reflect_boundary);
convolve_pencil(apr, stencils_z, tmp, particle_output, reflect_boundary);
}
/**
* Separable convolution using three potentially different stencils, supplied as PixelData objects.
* Note: it's up to the user to make sure that the dimensions of each stencil are correct
*/
template<typename InputType, typename StencilType, typename OutputType>
void convolve_sep(APR &apr,
const PixelData<StencilType>& stencil_y,
const PixelData<StencilType>& stencil_x,
const PixelData<StencilType>& stencil_z,
const ParticleData<InputType> &particle_input,
ParticleData<OutputType> &particle_output,
const bool reflect_boundary=true,
const bool use_stencil_downsample=true,
const bool normalize=false) {
int num_levels = use_stencil_downsample ? apr.level_max() - apr.level_min() : 1;
std::vector<PixelData<StencilType>> stencil_vec_y, stencil_vec_x, stencil_vec_z;
APRStencil::get_downsampled_stencils(stencil_y, stencil_vec_y, num_levels, normalize);
APRStencil::get_downsampled_stencils(stencil_x, stencil_vec_x, num_levels, normalize);
APRStencil::get_downsampled_stencils(stencil_z, stencil_vec_z, num_levels, normalize);
convolve_sep(apr, stencil_vec_y, stencil_vec_x, stencil_vec_z, particle_input, particle_output, reflect_boundary);
}
/**
* Apply separable convolution using a single 1D stencil in each dimension, supplied as a PixelData object
*/
template<typename InputType, typename StencilType, typename OutputType>
void convolve_sep(APR &apr,
const PixelData<StencilType>& stencil,
const ParticleData<InputType> &particle_input,
ParticleData<OutputType> &particle_output,
const bool reflect_boundary=true,
const bool use_stencil_downsample=true,
const bool normalize=false) {
int ndim = (stencil.z_num > 1) + (stencil.x_num > 1) + (stencil.y_num > 1);
if(ndim != 1) {
throw std::invalid_argument("APRFilter::convolve_sep expects an input stencil with exactly one non-singleton dimension");
}
int ksize = stencil.mesh.size();
PixelData<StencilType> stencil_y(ksize, 1, 1);
PixelData<StencilType> stencil_x(1, ksize, 1);
PixelData<StencilType> stencil_z(1, 1, ksize);
std::copy(stencil.mesh.begin(), stencil.mesh.end(), stencil_y.mesh.begin());
std::copy(stencil.mesh.begin(), stencil.mesh.end(), stencil_x.mesh.begin());
std::copy(stencil.mesh.begin(), stencil.mesh.end(), stencil_z.mesh.begin());
convolve_sep(apr, stencil_y, stencil_x, stencil_z, particle_input,
particle_output, reflect_boundary, use_stencil_downsample, normalize);
}
/**
* Separable convolution using a single 1D stencil in each dimension, supplied as an std::vector
*/
template<typename InputType, typename StencilType, typename OutputType>
void convolve_sep(APR &apr,
const std::vector<StencilType>& stencil,
const ParticleData<InputType> &particle_input,
ParticleData<OutputType> &particle_output,
const bool reflect_boundary=true,
const bool use_stencil_downsample=true,
const bool normalize=false) {
int ksize = stencil.size();
PixelData<StencilType> stencil_y(ksize, 1, 1);
PixelData<StencilType> stencil_x(1, ksize, 1);
PixelData<StencilType> stencil_z(1, 1, ksize);
std::copy(stencil.begin(), stencil.end(), stencil_y.mesh.begin());
std::copy(stencil.begin(), stencil.end(), stencil_x.mesh.begin());
std::copy(stencil.begin(), stencil.end(), stencil_z.mesh.begin());
convolve_sep(apr, stencil_y, stencil_x, stencil_z, particle_input,
particle_output, reflect_boundary, use_stencil_downsample, normalize);
}
template<typename T>
void apply_boundary_conditions_xy(const int z, ImageBuffer<T> &temp_vec, const bool reflect_boundary,
const std::vector<int>& stencil_half, const std::vector<int> &stencil_shape){
const int x_num = temp_vec.x_num;
const int y_num = temp_vec.y_num;
const uint64_t base_offset = (uint64_t) (z % stencil_shape[2]) * x_num * y_num;
if(reflect_boundary){
//x reflection (0 -> stencil_half)
for (int x = 0; x < stencil_half[1]; ++x) {
uint64_t index_in = (stencil_half[1]+x+1) * y_num + base_offset;
uint64_t index_out = (stencil_half[1]-x-1) * y_num + base_offset;
std::copy(temp_vec.mesh.begin() + index_in + stencil_half[0],
temp_vec.mesh.begin() + index_in + y_num - stencil_half[0],
temp_vec.mesh.begin() + index_out + stencil_half[0]);
}
//x reflection (x_num - 1 -> x_num - 1 - stencil_half)
for (int x = 0; x < stencil_half[1]; ++x) {
uint64_t index_in = (x_num - stencil_half[1] - 2 - x) * y_num + base_offset;
uint64_t index_out = (x_num - stencil_half[1] + x) * y_num + base_offset;
std::copy(temp_vec.mesh.begin() + index_in + stencil_half[0],
temp_vec.mesh.begin() + index_in + y_num - stencil_half[0],
temp_vec.mesh.begin() + index_out + stencil_half[0]);
}
// y reflection (0 -> stencil_half)
#ifdef HAVE_OPENMP
#pragma omp parallel default(shared)
#endif
for (int x = 0; x < x_num; ++x) {
uint64_t offset = stencil_half[0] + x * y_num + base_offset;
for (int y = 0; y < stencil_half[0]; ++y) {
temp_vec.mesh[offset - 1 - y] = temp_vec.mesh[offset + y + 1];
}
}
//y reflection (y_num - stencil_half -> y_num)
#ifdef HAVE_OPENMP
#pragma omp parallel default(shared)
#endif
for (int x = 0; x < x_num; ++x) {
uint64_t offset = y_num - stencil_half[0] + x * y_num + base_offset;
for (int y = 0; y < stencil_half[0]; ++y) {
temp_vec.mesh[offset + y] = temp_vec.mesh[offset - 2 - y];
}
}
} else { // zero pad
//first pad y (x = 0 -> stencil_half)
for (int x = 0; x < stencil_half[1]; ++x) {
uint64_t index_start = x * y_num + base_offset;
std::fill(temp_vec.mesh.begin() + index_start, temp_vec.mesh.begin() + index_start + y_num, 0);
}
//first pad y (x = x_num - stencil_half -> x_num)
for (int x = 0; x < stencil_half[1]; ++x) {
uint64_t index_start = (x_num - stencil_half[1] + x) * y_num + base_offset;
std::fill(temp_vec.mesh.begin() + index_start, temp_vec.mesh.begin() + index_start + y_num, 0);
}
//then pad x (y = 0 -> stencil_half)
#ifdef HAVE_OPENMP
#pragma omp parallel default(shared)
#endif
for(int x = 0; x < x_num; ++x) {
uint64_t offset = x * y_num + base_offset;
for(int y = 0; y < stencil_half[0]; ++y) {
temp_vec.mesh[offset + y] = 0;
}
}
// then pad x (y = y_num - stencil_half -> ynum)
#ifdef HAVE_OPENMP
#pragma omp parallel default(shared)
#endif
for(int x = 0; x < x_num; ++x) {
uint64_t offset = y_num - stencil_half[0] + x * y_num + base_offset;
for(int y = 0; y < stencil_half[0]; ++y) {
temp_vec.mesh[offset + y] = 0;
}
}
}
}
template<typename T>
void apply_boundary_conditions_z(const int z, const int z_num, ImageBuffer<T> &temp_vec, const bool reflect_boundary,
const bool low_end, const std::vector<int>& stencil_half,const std::vector<int> &stencil_shape) {
const int x_num = temp_vec.x_num;
const int y_num = temp_vec.y_num;
uint64_t out_offset = (z % stencil_shape[2]) * x_num * y_num;
if(reflect_boundary) {
if(low_end) {
uint64_t z_in = (stencil_half[2] + (stencil_half[2] - z)) % stencil_shape[2];
uint64_t in_offset = z_in * x_num * y_num;
// copy slice at z_in to z
for(int x = 0; x < x_num; ++x) {
std::copy(temp_vec.mesh.begin() + in_offset + x * y_num,
temp_vec.mesh.begin() + in_offset + (x+1) * y_num,
temp_vec.mesh.begin() + out_offset + x *y_num);
}
} else {
uint64_t r = z_num - 1 + stencil_half[2]; // z index of the boundary
uint64_t z_in = (r - (z-r)) % stencil_shape[2];
uint64_t in_offset = z_in * x_num * y_num;
// copy slice at z_in to z
for(int x = 0; x < x_num; ++x) {
std::copy(temp_vec.mesh.begin() + in_offset + x * y_num,
temp_vec.mesh.begin() + in_offset + (x+1) * y_num,
temp_vec.mesh.begin() + out_offset + x * y_num);
}
}
} else { // zero padding
// fill slice at z with zeroes
T pad_value = 0;
for(int x = 0; x < x_num; ++x) {
std::fill(temp_vec.mesh.begin() + out_offset + x * y_num,
temp_vec.mesh.begin() + out_offset + (x+1) * y_num,
pad_value);
}
}
}
template<typename T>
void apply_boundary_conditions_x(const int x, const int x_num, ImageBuffer<T> &temp_vec, const bool reflect_boundary,
const bool low_end, const std::vector<int>& stencil_half,const std::vector<int> &stencil_shape) {
const uint64_t y_num = temp_vec.y_num;
const uint64_t xy_num = temp_vec.x_num * y_num;
if(reflect_boundary) {
if(low_end) {
for(int z = 0; z < stencil_shape[2]; ++z) {
uint64_t x_in = (stencil_half[1] + (stencil_half[1] - x)) % stencil_shape[1];
uint64_t in_offset = z * xy_num + x_in * y_num;
uint64_t out_offset = z * xy_num + (x % stencil_shape[1]) * y_num;
std::copy(temp_vec.mesh.begin() + in_offset,
temp_vec.mesh.begin() + in_offset + y_num,
temp_vec.mesh.begin() + out_offset);
}
} else {
for(int z = 0; z < stencil_shape[2]; ++z) {
uint64_t r = x_num - 1 + stencil_half[1]; // x index of the boundary
uint64_t x_in = (r - (x-r)) % stencil_shape[1];
uint64_t in_offset = z * xy_num + x_in * y_num;
uint64_t out_offset = z * xy_num + (x % stencil_shape[1]) * y_num;
std::copy(temp_vec.mesh.begin() + in_offset,
temp_vec.mesh.begin() + in_offset + y_num,
temp_vec.mesh.begin() + out_offset);
}
}
} else { // zero padding
for(int z = 0; z < stencil_shape[2]; ++z) {
uint64_t out_offset = z * xy_num + (x % stencil_shape[1]) * y_num;
std::fill(temp_vec.mesh.begin() + out_offset,
temp_vec.mesh.begin() + out_offset + y_num,
0);
}
}
}
template<typename T>
inline void apply_boundary_conditions_y(const int z, const int x,ImageBuffer<T> &temp_vec, const bool reflect_boundary,
const std::vector<int> &stencil_half,const std::vector<int> &stencil_shape){
const size_t y_num = temp_vec.y_num;
const uint64_t base_offset = (z % stencil_shape[2]) * temp_vec.x_num * y_num + (x % stencil_shape[1]) * y_num;
if(reflect_boundary){
for(int y = 0; y < stencil_half[0]; ++y) {
temp_vec.mesh[base_offset + stencil_half[0] - 1 - y] = temp_vec.mesh[base_offset + stencil_half[0] + 1 + y];
}
for(int y = 0; y < stencil_half[0]; ++y) {
const uint64_t r = y_num - 1 - stencil_half[0];
temp_vec.mesh[base_offset + r + 1 + y] = temp_vec.mesh[base_offset + r - 1 - y];
}
} else { // zero pad
for(int y = 0; y < stencil_half[0]; ++y) {
temp_vec.mesh[base_offset + y] = 0;
}
for(int y = 0; y < stencil_half[0]; ++y) {
temp_vec.mesh[base_offset + y_num - stencil_half[0] + y] = 0;
}
}
}
/**
* Fills a pixel image with the particle values at a given level and depth (z), where the particles exactly match
* the pixels.
*/
template<typename T, typename ParticleDataType>
void update_same_level(LinearIterator apr_it,
const int level,
const int z,
ImageBuffer<T> &temp_vec,
ParticleDataType &inputParticles,
std::vector<int> stencil_half,
std::vector<int> stencil_shape){
const int x_num_m = temp_vec.x_num;
const int y_num_m = temp_vec.y_num;
uint64_t base_offset = stencil_half[0] + (uint64_t) x_num_m * y_num_m * ((z + stencil_half[2]) % stencil_shape[2]);
#ifdef HAVE_OPENMP
#pragma omp parallel for schedule(dynamic) firstprivate(apr_it)
#endif
for (int x = 0; x < apr_it.x_num(level); ++x) {
uint64_t mesh_offset = base_offset + (x + stencil_half[1]) * y_num_m;
for (apr_it.begin(level, z, x); apr_it < apr_it.end(); apr_it++) {
temp_vec.mesh[apr_it.y() + mesh_offset] = inputParticles[apr_it];
}
}
}
/**
* Fills a pixel image with the particle values from one level below a given level and depth (z), that is, the
* particles correspond to groups of 2^dim pixels.
*/
template< typename T, typename ParticleDataType>
void update_higher_level(LinearIterator apr_it,
const int level,
const int z,
ImageBuffer<T> &temp_vec,
ParticleDataType &inputParticles,
const std::vector<int> &stencil_half,
const std::vector<int> &stencil_shape) {
const int x_num_m = temp_vec.x_num;
const int y_num_m = temp_vec.y_num;
uint64_t base_offset = stencil_half[0] + (uint64_t) x_num_m * y_num_m * ((z + stencil_half[2]) % stencil_shape[2]);
#ifdef HAVE_OPENMP
#pragma omp parallel for schedule(dynamic) firstprivate(apr_it)
#endif
for (int x = 0; x < apr_it.x_num(level); ++x) {
uint64_t mesh_offset = base_offset + (x + stencil_half[1]) * y_num_m;
for (apr_it.begin(level-1, z/2, x/2); apr_it < apr_it.end(); ++apr_it) {
int y_m = std::min(2 * apr_it.y() + 1, (int) apr_it.y_num(level) - 1); // 2y+1+offset
temp_vec.mesh[2*apr_it.y() + mesh_offset] = inputParticles[apr_it];
temp_vec.mesh[y_m + mesh_offset] = inputParticles[apr_it];
}
}
}
template<typename T, typename ParticleDataType>
void update_higher_level(LinearIterator apr_it,
const int level,
const int z,
ImageBuffer<T> &temp_vec,
ParticleDataType &inputParticles,
const std::vector<int> &stencil_half,
const std::vector<int> &stencil_shape,
const int num_parent_levels) {
const int x_num_m = temp_vec.x_num;
const int y_num_m = temp_vec.y_num;
uint64_t base_offset = stencil_half[0] + (uint64_t) x_num_m * y_num_m * ((z + stencil_half[2]) % stencil_shape[2]);
#ifdef HAVE_OPENMP
#pragma omp parallel for schedule(dynamic) firstprivate(apr_it) collapse(2)
#endif
for(int dlevel = 1; dlevel <= num_parent_levels; ++dlevel) {
for (int x = 0; x < apr_it.x_num(level); ++x) {
uint64_t mesh_offset = base_offset + (x + stencil_half[1]) * y_num_m;
int step_size = std::pow(2, dlevel);
for (apr_it.begin(level - dlevel, z / step_size, x / step_size); apr_it < apr_it.end(); ++apr_it) {
int y = step_size * apr_it.y();
int y_m = std::min(y + step_size, (int) apr_it.y_num(level));
std::fill(temp_vec.mesh.begin() + mesh_offset + y, temp_vec.mesh.begin() + mesh_offset + y_m, (T)inputParticles[apr_it]);
}
}
}
}
/**
* Fills a pixel image with the particle values from one level above a given level and depth (z), that is, the
* pixels correspond to groups of 2^dim particles. The values must be precomputed (e.g., through APRTreeNumerics::fill_tree_mean)
* and passed to the function through tree_data
*/
template<typename T, typename ParticleDataType>
void update_lower_level(LinearIterator tree_it,
const int level,
const int z,
ImageBuffer<T> &temp_vec,
ParticleDataType &tree_data,
const std::vector<int> &stencil_half,
const std::vector<int> &stencil_shape) {
const int x_num_m = temp_vec.x_num;
const int y_num_m = temp_vec.y_num;
uint64_t base_offset = stencil_half[0] + (uint64_t) x_num_m * y_num_m * ((z + stencil_half[2]) % stencil_shape[2]);
#ifdef HAVE_OPENMP
#pragma omp parallel for schedule(dynamic) firstprivate(tree_it)
#endif
for (int x = 0; x < tree_it.x_num(level); ++x) {
uint64_t mesh_offset = base_offset + (x + stencil_half[1]) * y_num_m;
for (tree_it.begin(level, z, x); tree_it < tree_it.end(); tree_it++) {
temp_vec.mesh[tree_it.y() + mesh_offset] = tree_data[tree_it];
}
}
}
/**
* Reconstruct isotropic neighborhoods around the particles at a given level and depth (z) in a pixel image.
*/
template<typename T, typename ParticleDataType, typename ParticleTreeDataType>
void update_dense_array(LinearIterator apr_it,
LinearIterator tree_it,
const int level,
const int z,
ParticleDataType &tree_data,
ImageBuffer<T> &temp_vec,
ParticleTreeDataType &inputParticles,
const std::vector<int> &stencil_shape,
const std::vector<int> &stencil_half,
const int num_parent_levels,
const bool reflect_boundary) {
update_same_level(apr_it, level, z, temp_vec, inputParticles, stencil_half, stencil_shape);
if (level > apr_it.level_min()) {
update_higher_level(apr_it, level, z, temp_vec, inputParticles, stencil_half, stencil_shape, num_parent_levels);
}
if (level < apr_it.level_max()) {
update_lower_level(tree_it, level, z, temp_vec, tree_data, stencil_half, stencil_shape);
}
apply_boundary_conditions_xy(z+stencil_half[2], temp_vec, reflect_boundary, stencil_half, stencil_shape);
}
template<typename T, typename ParticleDataType>
void run_convolution(LinearIterator apr_it, const int z, const int level, const ImageBuffer<T> &temp_vec, const PixelData<T> &stencil,
ParticleDataType &outputParticles, const std::vector<int> &stencil_half, const std::vector<int> &stencil_shape){
const int x_num = temp_vec.x_num;
const int y_num = temp_vec.y_num;
int x;
#ifdef HAVE_OPENMP
#pragma omp parallel for schedule(dynamic) private(x) firstprivate(apr_it)
#endif
for (x = 0; x < apr_it.x_num(level); ++x) {
for (apr_it.begin(level, z, x); apr_it < apr_it.end(); ++apr_it) {
T val = 0;
int y = apr_it.y();
size_t counter = 0;
// compute the value TODO: optimize this
for(int iz = 0; iz < stencil_shape[2]; ++iz) {
uint64_t offset = ((z + iz) % stencil_shape[2]) * x_num * y_num + x * y_num + y;
for(int ix = 0; ix < stencil_shape[1]; ++ix) {
for(int iy = 0; iy < stencil_shape[0]; ++ iy) {
val += temp_vec.mesh[offset + ix * y_num + iy] * stencil.mesh[counter++];
}
}
}
outputParticles[apr_it] = val;
}
}
}
template<typename T, typename ParticleDataType>
void run_convolution_pencil(LinearIterator &apr_it, const int level, const int z, const int x,
const ImageBuffer<T> &temp_vec, const PixelData<T> &stencil,
ParticleDataType &outputParticles, const std::vector<int> &stencil_half,
const std::vector<int> &stencil_shape){
const int y_num = temp_vec.y_num;
const int xy_num = temp_vec.x_num * y_num;
for (apr_it.begin(level, z, x); apr_it < apr_it.end(); ++apr_it) {
T val = 0;
int y = apr_it.y();
size_t counter = 0;
// compute the value TODO: optimize this
for(int iz = 0; iz < stencil_shape[2]; ++iz) {
uint64_t base_offset = ((z + iz) % stencil_shape[2]) * xy_num + y;
for(int ix = 0; ix < stencil_shape[1]; ++ix) {
uint64_t offset = base_offset + ((x + ix) % stencil_shape[1]) * y_num;
for(int iy = 0; iy < stencil_shape[0]; ++ iy) {
val += temp_vec.mesh[offset + iy] * stencil.mesh[counter++];
}
}
}
outputParticles[apr_it] = val;
}
}
/**
* Fills a pixel image with the particle values at a given level and depth (z), where the particles exactly match
* the pixels.
*/
template<typename T, typename ParticleDataType>
inline void update_same_level(const int level,
const int z,
const int x,
LinearIterator &apr_it,
ImageBuffer<T> &temp_vec,
ParticleDataType &inputParticles,
std::vector<int> stencil_half,
std::vector<int> stencil_shape){
uint64_t mesh_offset = ((z + stencil_half[2]) % stencil_shape[2]) * temp_vec.x_num * temp_vec.y_num +
((x + stencil_half[1]) % stencil_shape[1]) * temp_vec.y_num +
stencil_half[0];
for (apr_it.begin(level, z, x); apr_it < apr_it.end(); ++apr_it) {
temp_vec.mesh[apr_it.y() + mesh_offset] = inputParticles[apr_it];
}
}
template<typename T, typename ParticleDataType>
inline void update_higher_level(const int level,
const int z,
const int x,
LinearIterator &apr_it,
ImageBuffer<T> &temp_vec,
ParticleDataType &inputParticles,
const std::vector<int> &stencil_half,
const std::vector<int> &stencil_shape,
const int num_parent_levels) {
uint64_t mesh_offset = ((z + stencil_half[2]) % stencil_shape[2]) * temp_vec.x_num * temp_vec.y_num +
((x + stencil_half[1]) % stencil_shape[1]) * temp_vec.y_num +
stencil_half[0];
for(int dlevel = 1; dlevel <= num_parent_levels; ++dlevel) {
int step_size = std::pow(2, dlevel);
for (apr_it.begin(level - dlevel, z / step_size, x / step_size); apr_it < apr_it.end(); ++apr_it) {
int y = step_size * apr_it.y();
int y_m = std::min(y + step_size, apr_it.y_num(level));
if(y < y_m) {
std::fill(temp_vec.mesh.begin() + mesh_offset + y, temp_vec.mesh.begin() + mesh_offset + y_m,
(T) inputParticles[apr_it]);
}
}
}
}
/**
* Fills a pixel image with the particle values from one level above a given level and depth (z), that is, the
* pixels correspond to groups of 2^dim particles. The values must be precomputed (e.g., through APRTreeNumerics::fill_tree_mean)
* and passed to the function through tree_data
*/
template<typename T, typename ParticleDataType>
inline void update_lower_level(const int level,
const int z,
const int x,
LinearIterator &tree_it,
ImageBuffer<T> &temp_vec,
ParticleDataType &tree_data,
const std::vector<int> &stencil_half,
const std::vector<int> &stencil_shape) {
uint64_t mesh_offset = ((z + stencil_half[2]) % stencil_shape[2]) * temp_vec.x_num * temp_vec.y_num +
((x + stencil_half[1]) % stencil_shape[1]) * temp_vec.y_num +
stencil_half[0];
for (tree_it.begin(level, z, x); tree_it < tree_it.end(); ++tree_it) {
temp_vec.mesh[tree_it.y() + mesh_offset] = tree_data[tree_it];
}
}
/**
* Reconstruct isotropic neighborhoods around the particles at a given level and depth (z) in a pixel image.
*/
template<typename T, typename ParticleDataType, typename ParticleTreeDataType>
void update_dense_array(LinearIterator &apr_it,
LinearIterator &tree_it,
const int level,
const int z,
const int x,
ParticleDataType &tree_data,
ImageBuffer<T> &temp_vec,
ParticleTreeDataType &inputParticles,
const std::vector<int> &stencil_shape,
const std::vector<int> &stencil_half,
const bool reflect_boundary,
const int num_parent_levels) {
update_same_level(level, z, x, apr_it, temp_vec, inputParticles, stencil_half, stencil_shape);
if (level > apr_it.level_min()) {
update_higher_level(level, z, x, apr_it, temp_vec, inputParticles, stencil_half, stencil_shape, num_parent_levels);
}
if (level < apr_it.level_max()) {
update_lower_level(level, z, x, tree_it, temp_vec, tree_data, stencil_half, stencil_shape);
}
apply_boundary_conditions_y(z+stencil_half[2], x+stencil_half[1], temp_vec, reflect_boundary, stencil_half, stencil_shape);
}
template<typename inputType, typename outputType, typename stencilType>
void convolve_pixel(PixelData<inputType>& input, PixelData<outputType>& output, PixelData<stencilType>& stencil) {
output.init(input);
int y_num = input.y_num;
int x_num = input.x_num;
int z_num = input.z_num;
const std::vector<int> stencil_shape = {(int) stencil.y_num,
(int) stencil.x_num,
(int) stencil.z_num};
const std::vector<int> stencil_half = {(stencil_shape[0] - 1)/2,
(stencil_shape[1] - 1)/2,
(stencil_shape[2] - 1)/2};
int z;
#ifdef HAVE_OPENMP
#pragma omp parallel for private(z)
#endif
for (z = 0; z < z_num; ++z) {
for (int x = 0; x < x_num; ++x) {
for (int y = 0; y < y_num; ++y) {
float neighbour_sum=0;
int counter = 0;
for (int l = -stencil_half[2]; l < stencil_half[2]+1; ++l) {
for (int q = -stencil_half[1]; q < stencil_half[1]+1; ++q) {
for (int w = -stencil_half[0]; w < stencil_half[0]+1; ++w) {
if( ((y+w)>=0) && ((y+w) < y_num) ){
if( ((x+q)>=0) && ((x+q) < x_num) ) {
if(((z+l)>=0) & ((z+l) < z_num) ) {
neighbour_sum += stencil.mesh[counter] * input.at(y+w, x+q, z+l);
}
}
}
counter++;
}
}
}
output.at(y,x,z) = neighbour_sum;
}
}
}
}
template<typename T, T filter(std::vector<T>&), int size_y, int size_x, int size_z>
void apply_filter(LinearIterator &apr_it, const int level, const int z, const int x,
const ImageBuffer<T> &patch_buffer, ParticleData<T> &outputParticles,
std::vector<T> &temp_vec);
template<typename T, typename U, typename V, V filter(std::vector<V>&), int size_y, int size_x, int size_z>
void generic_filter(APR &apr,
const ParticleData<T> &particle_input,
const ParticleData<U> &tree_data,
ParticleData<V> &particle_output,
const bool reflect_boundary);
template<typename T>
T median(std::vector<T>& v) {
size_t n = v.size() / 2;
std::nth_element(v.begin(), v.begin()+n, v.end());
return v[n];
}
template<int size_y, int size_x, int size_z, typename T, typename U>
void median_filter(APR &apr,
const ParticleData<T> &particle_input,
ParticleData<U> &particle_output) {
ParticleData<U> tree_data;
APRTreeNumerics::fill_tree_mean(apr, particle_input, tree_data);
generic_filter<T, U, U, median<U>, size_y, size_x, size_z>(apr, particle_input, tree_data, particle_output, true);
}
template<typename T>
T compute_min(std::vector<T>& v) {
return *std::min_element(v.begin(), v.end());
}
template<int size_y, int size_x, int size_z, typename T, typename U>
void min_filter(APR &apr,
const ParticleData<T> &particle_input,
ParticleData<U> &particle_output) {
ParticleData<U> tree_data;
APRTreeNumerics::fill_tree_min(apr, particle_input, tree_data);
generic_filter<T, U, U, compute_min<U>, size_y, size_x, size_z>(apr, particle_input, tree_data, particle_output, true);
}
template<typename T>
T compute_max(std::vector<T>& v) {
return *std::max_element(v.begin(), v.end());
}
template<int size_y, int size_x, int size_z, typename T, typename U>
void max_filter(APR &apr,
const ParticleData<T> &particle_input,
ParticleData<U> &particle_output) {
ParticleData<U> tree_data;
APRTreeNumerics::fill_tree_max(apr, particle_input, tree_data);
generic_filter<T, U, U, compute_max<U>, size_y, size_x, size_z>(apr, particle_input, tree_data, particle_output, true);
}
}
template<typename ParticleDataTypeInput, typename T,typename ParticleDataTypeOutput>
void APRFilter::convolve(APR &apr, const std::vector<PixelData<T>>& stencils, const ParticleDataTypeInput &particle_input,
ParticleDataTypeOutput &particle_output, const bool reflect_boundary) {
particle_output.init(apr);
auto apr_it = apr.iterator();
auto tree_it = apr.tree_iterator();
/**** initialize and fill the apr tree ****/
ParticleData<T> tree_data;
APRTreeNumerics::fill_tree_mean(apr, particle_input, tree_data);
/// allocate image buffer with pad for isotropic patch reconstruction
// this is reused for lower levels -- assumes that the stencils are not increasing in size !
int y_num_m = (apr.org_dims(0) > 1) ? apr.y_num(apr.level_max()) + stencils[0].y_num - 1 : 1;
int x_num_m = (apr.org_dims(1) > 1) ? apr.x_num(apr.level_max()) + stencils[0].x_num - 1 : 1;
ImageBuffer<T> temp_vec(y_num_m, x_num_m, stencils[0].z_num);
for (int level = apr.level_max(); level >= apr.level_min(); --level) {
int stencil_num = std::min((int)stencils.size()-1,(int)(apr.level_max()-level));
const std::vector<int> stencil_shape = {(int) stencils[stencil_num].y_num,
(int) stencils[stencil_num].x_num,
(int) stencils[stencil_num].z_num};
const std::vector<int> stencil_half = {(stencil_shape[0] - 1) / 2,
(stencil_shape[1] - 1) / 2,
(stencil_shape[2] - 1) / 2};
int max_stencil_radius = *std::max_element(stencil_half.begin(), stencil_half.end());