-
Notifications
You must be signed in to change notification settings - Fork 106
/
gipuma.cu
1970 lines (1741 loc) · 72.5 KB
/
gipuma.cu
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
//#include <helper_math.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <stdint.h> // for uint8_t
#include "globalstate.h"
#include "algorithmparameters.h"
#include "cameraparameters.h"
#include "linestate.h"
#include "imageinfo.h"
#include "config.h"
#include <vector_types.h> // float4
#include <math.h>
#include <cuda.h>
#include <vector>
#include <string>
#include <iostream>
#include <curand_kernel.h>
#include "vector_operations.h"
#include "helper_cuda.h"
//#define CENSUS
#define SHARED
//#define NOTEXTURE_CHECK
#define WIN_INCREMENT 2
// uses smaller (but more) kernels, use if windows watchdog is enabled or if you want frequent display updates
// (might avoid crashes due to timeout on windows but possibly slows down marginally)
// only implements EXTRAPOINTFAR (not EXTRAPOINT or EXTRAPOINT2)
#define SMALLKERNEL
#define EXTRAPOINTFAR
#define EXTRAPOINT
#define EXTRAPOINT2
//#define FORCEINLINE_GIPUMA __FORCEINLINE_GIPUMA__
//#define FORCEINLINE_GIPUMA
__device__ float K[16];
__device__ float K_inv[16];
#ifndef SHARED_HARDCODED
__managed__ int SHARED_SIZE_W_m;
__constant__ int SHARED_SIZE_W;
__managed__ int SHARED_SIZE_H;
__managed__ int SHARED_SIZE = 0;
__managed__ int WIN_RADIUS_W;
__managed__ int WIN_RADIUS_H;
__managed__ int TILE_W;
__managed__ int TILE_H;
#endif
/*__device__ FORCEINLINE_GIPUMA __constant__ float4 camerasK[32];*/
/* compute depth value from disparity or disparity value from depth
* Input: f - focal length in pixel
* baseline - baseline between cameras (in meters)
* d - either disparity or depth value
* Output: either depth or disparity value
*/
__device__ FORCEINLINE_GIPUMA float disparityDepthConversion_cu ( const float &f, const float &baseline, const float &d ) {
return f * baseline / d;
}
// CHECKED
__device__ FORCEINLINE_GIPUMA void get3Dpoint_cu ( float4 * __restrict__ ptX, const Camera_cu &cam, const int2 &p, const float &depth ) {
// in case camera matrix is not normalized: see page 162, then depth might not be the real depth but w and depth needs to be computed from that first
const float4 pt = make_float4 (
depth * (float)p.x - cam.P_col34.x,
depth * (float)p.y - cam.P_col34.y,
depth - cam.P_col34.z,
0);
matvecmul4 (cam.M_inv, pt, ptX);
}
__device__ FORCEINLINE_GIPUMA void get3Dpoint_cu1 ( float4 * __restrict__ ptX, const Camera_cu &cam, const int2 &p) {
// in case camera matrix is not normalized: see page 162, then depth might not be the real depth but w and depth needs to be computed from that first
float4 pt;
pt.x = (float)p.x - cam.P_col34.x;
pt.y = (float)p.y - cam.P_col34.y;
pt.z = 1.0f - cam.P_col34.z;
matvecmul4 (cam.M_inv, pt, ptX);
}
// CHECKED
//get d parameter of plane pi = [nT, d]T, which is the distance of the plane to the camera center
__device__ FORCEINLINE_GIPUMA float getPlaneDistance_cu ( const float4 &normal, const float4 &X ) {
return -(dot4(normal,X));
}
// CHECKED
__device__ FORCEINLINE_GIPUMA static float getD_cu ( const float4 &normal,
const int2 &p,
const float &depth,
const Camera_cu &cam ) {
/*float4 pt;*/
/*get3Dpoint_cu ( &pt, cam, (float)x0, (float)y0, depth );*/
float4 pt,ptX;
pt.x = depth * (float)(p.x) - cam.P_col34.x;
pt.y = depth * (float)(p.y) - cam.P_col34.y;
pt.z = depth - cam.P_col34.z;
matvecmul4 (cam.M_inv, pt, (&ptX));
return -(dot4(normal,ptX));
/*return getPlaneDistance_cu (normal, ptX);*/
}
// CHECKED
__device__ FORCEINLINE_GIPUMA void normalize_cu (float4 * __restrict__ v)
{
const float normSquared = pow2(v->x) + pow2(v->y) + pow2(v->z);
const float inverse_sqrt = rsqrtf (normSquared);
v->x *= inverse_sqrt;
v->y *= inverse_sqrt;
v->z *= inverse_sqrt;
}
//CHECKED
__device__ FORCEINLINE_GIPUMA void getViewVector_cu (float4 * __restrict__ v, const Camera_cu &camera, const int2 &p)
{
get3Dpoint_cu1 (v, camera, p);
sub((*v), camera.C4);
normalize_cu(v);
//v->x=0;
//v->y=0;
//v->z=1;
}
__device__ FORCEINLINE_GIPUMA static void vecOnHemisphere_cu ( float4 * __restrict__ v, const float4 &viewVector ) {
const float dp = dot4 ( (*v), viewVector );
if ( dp > 0.0f ) {
negate4(v);
}
return;
}
__device__ FORCEINLINE_GIPUMA float curand_between (curandState *cs, const float &min, const float &max)
{
return (curand_uniform(cs) * (max-min) + min);
}
/* compute random 3D unit vector
* notes on how to do that: http://mathworld.wolfram.com/SpherePointPicking.html
* this method uses the last approach in the link by Muller 1959, Marsaglia 1972: three Gaussian random values for x,y,z
* Output: random unit vector
*/
// CHECKED
__device__ FORCEINLINE_GIPUMA static void rndUnitVectorSphereMarsaglia_cu (float4 *v, curandState *cs) {
float x = 1.0f;
float y = 1.0f;
float sum = 2.0f;
while ( sum>=1.0f ) {
x = curand_between (cs, -1.0f, 1.0f);
y = curand_between (cs, -1.0f, 1.0f);
sum = get_pow2_norm(x,y);
}
const float sq = sqrtf ( 1.0f-sum );
v->x = 2.0f*x*sq;
v->y = 2.0f*y*sq;
v->z = 1.0f-2.0f*sum;
//v->x = 0;
//v->y = 0;
//v->z = -1;
}
//CHECKED
__device__ FORCEINLINE_GIPUMA static void rndUnitVectorOnHemisphere_cu ( float4 *v, const float4 &viewVector, curandState *cs ) {
rndUnitVectorSphereMarsaglia_cu (v, cs);
vecOnHemisphere_cu ( v,viewVector );
};
__device__ FORCEINLINE_GIPUMA float l1_norm(float f) {
return fabsf(f);
}
__device__ FORCEINLINE_GIPUMA float l1_norm(float4 f) {
return ( fabsf (f.x) +
fabsf (f.y) +
fabsf (f.z))*0.3333333f;
}
__device__ FORCEINLINE_GIPUMA float l1_norm2(float4 f) {
return ( fabsf (f.x) +
fabsf (f.y) +
fabsf (f.z));
}
template< typename T >
__device__ FORCEINLINE_GIPUMA float weight_cu ( const T &c1, const T &c2, const float &gamma )
{
const float colorDis = l1_norm ( c1 - c2 );
return expf ( -colorDis / gamma ); ///[>0.33333333f));
/*return __expf ( -colorDis / gamma ); ///[>0.33333333f));*/
/*return expf_cache[c1-c2+256];*/
}
// CHECKED
__device__ FORCEINLINE_GIPUMA void getCorrespondingHomographyPt_cu ( const float * __restrict__ H, int x, int y, float4 *ptf) {
float4 pt;
pt.x = __int2float_rn (x);
pt.y = __int2float_rn (y);
pt.z = 1.0f;
matvecmul4(H,pt,ptf); //ptf = H * pt;
vecdiv4(ptf,ptf->z); //ptf = ptf / ptf[2];
return ;
}
// CHECKED
__device__ FORCEINLINE_GIPUMA void getCorrespondingPoint_cu ( const int2 &p, const float * __restrict__ H, float4 * __restrict__ ptf ) {
/*getCorrespondingHomographyPt_cu ( (const float * )H, x , y , pt );*/
float4 pt;
pt.x = __int2float_rn (p.x);
pt.y = __int2float_rn (p.y);
/*pt.z = 1.0f;*/
matvecmul4noz(H,pt,ptf); //ptf = H * pt;
vecdiv4(ptf,ptf->z); //ptf = ptf / ptf[2];
return ;
}
__device__ FORCEINLINE_GIPUMA float colorDifferenceL1_cu ( float c1, float c2 )
{
return abs ( c1-c2 );
}
template< typename T >
__device__ FORCEINLINE_GIPUMA float pmCostComputation_shared (
const cudaTextureObject_t &l,
const T * __restrict__ tile_left,
const cudaTextureObject_t &r,
const T &leftValue,
const int2 &pI,
const float4 &pt_r,
const float &tau_color,
const float &tau_gradient,
const float &alpha,
const float &w )
{
/*XXX*/
/*if ( pt_r.x >= 0 && */
/*pt_r.x < cols && */
/*pt_r.y >= 0 && */
/*pt_r.y < rows ) */
{
/*float dis = dissimilarity ( l, r, pt_l, pt_r, gradX1, gradY1, gradX2, gradY2, alpha, tau_color, tau_gradient );*/
/*float colDiff = colorDifferenceL1_cu ( texatpt4(l,pt_l), texatpt4(r,pt_r) );*/
/*if (*/
/*pt_li.x == 100 && */
/*pt_li.y == 100)*/
/*printf ("PMCOSTCOMPUTATION I and J are %d %d and II JJ are %d %d value is %f and tile cache is %f\n", I, J, II, JJ, leftValue, tile_left[I][J]);*/
const T gx2 = tex2D<T> (r, pt_r.x+1 + 0.5f, pt_r.y + 0.5f) - tex2D<T> (r, pt_r.x-1 + 0.5f, pt_r.y + 0.5f);
const T gy2 = tex2D<T> (r, pt_r.x + 0.5f, pt_r.y+1 + 0.5f) - tex2D<T> (r, pt_r.x + 0.5f, pt_r.y-1 + 0.5f);
const float colDiff = l1_norm ( leftValue - tex2D<T>(r, pt_r.x + 0.5f, pt_r.y + 0.5f) );
const T up = tile_left[ pI.x + SHARED_SIZE_W * (pI.y-1)];
const T down = tile_left[ pI.x + SHARED_SIZE_W * (pI.y+1)];
const T left = tile_left[ pI.x-1 + SHARED_SIZE_W * pI.y ];
const T right = tile_left[ pI.x+1 + SHARED_SIZE_W * pI.y ];
const T gx1 = right - left;
const T gy1 = down - up;
/*float gradX = texatpt4(gradX1,pt_l) - texatpt4(gradX2,pt_r);*/
/*float gradY = texatpt4(gradY1,pt_l) - texatpt4(gradY2,pt_r);*/
const T gradX = (gx1 - gx2);
const T gradY = (gy1 - gy2);
//gradient dissimilarity (L1) in x and y direction (multiplication by 0.5 to use tauGrad from PatchMatch stereo paper)
const float gradDis = fminf ( ( l1_norm ( gradX ) + l1_norm ( gradY ) ) * 0.0625f, tau_gradient );
//gradient dissimilarity only in x direction
//float gradDis = min(abs(gradX),tau_gradient);
const float colDis = fminf ( colDiff, tau_color );
const float dis = ( 1.f - alpha ) * colDis + alpha * gradDis;
//const float dis = gradDis;
return w * dis;
}
//return 3.0;
}
template< typename T >
__device__ FORCEINLINE_GIPUMA float pmCostComputation (
const cudaTextureObject_t &l,
const T * __restrict__ tile_left,
const cudaTextureObject_t &r,
const float4 &pt_l,
const float4 &pt_r,
const int &rows,
const int &cols,
const float &tau_color,
const float &tau_gradient,
const float &alpha,
const float &w )
{
/*XXX*/
/*if ( pt_r.x >= 0 && */
/*pt_r.x < cols && */
/*pt_r.y >= 0 && */
/*pt_r.y < rows ) */
{
/*float dis = dissimilarity ( l, r, pt_l, pt_r, gradX1, gradY1, gradX2, gradY2, alpha, tau_color, tau_gradient );*/
const float colDiff = l1_norm ( tex2D<T>(l,pt_l.x + 0.5f,pt_l.y + 0.5f) - tex2D<T>(r,pt_r.x + 0.5f, pt_r.y + 0.5f) );
const float colDis = fminf ( colDiff, tau_color );
const T gx1 = tex2D<T> (l, pt_l.x+1 + 0.5f, pt_l.y + 0.5f) - tex2D<T> (l, pt_l.x-1 + 0.5f, pt_l.y + 0.5f);
const T gy1 = tex2D<T> (l, pt_l.x + 0.5f, pt_l.y+1 + 0.5f) - tex2D<T> (l, pt_l.x + 0.5f, pt_l.y-1 + 0.5f);
const T gx2 = tex2D<T> (r, pt_r.x+1 + 0.5f, pt_r.y + 0.5f) - tex2D<T> (r, pt_r.x-1 + 0.5f, pt_r.y + 0.5f);
const T gy2 = tex2D<T> (r, pt_r.x + 0.5f, pt_r.y+1 + 0.5f) - tex2D<T> (r, pt_r.x + 0.5f, pt_r.y-1 + 0.5f);
const T gradX = (gx1 - gx2);
const T gradY = (gy1 - gy2);
//gradient dissimilarity (L1) in x and y direction (multiplication by 0.5 to use tauGrad from PatchMatch stereo paper)
const float gradDis = fminf ( ( l1_norm ( gradX ) + l1_norm ( gradY ) ) * 0.0625f, tau_gradient );
//gradient dissimilarity only in x direction
//float gradDis = min(abs(gradX),tau_gradient);
const float dis = ( 1.f - alpha ) * colDis + alpha * gradDis;
return w * dis;
}
//return 3.0;
}
__device__ FORCEINLINE_GIPUMA void getHomography_real (const float *K1_inv,
const float *K2,
const float *R,
const float4 t,
const float4 n,
const float d,
float *H )
{
/*print_matrix(R,"R");*/
float tmp[16];
float tmp2[16];
outer_product4(t, n, tmp); // tmp = t * n'
matdivide(tmp, d); // tmp / d
matmatsub2(R, tmp); // tmp = R - tmp;
matmul_cu(tmp,K1_inv,tmp2); // tmp2=tmp*Kinv
matmul_cu(K2,tmp2,H);// H = tmp * K2
return;
}
__device__ FORCEINLINE_GIPUMA void getHomography_cu ( const Camera_cu &from, const Camera_cu &to,
const float * __restrict__ K1_inv, const float * __restrict__ K2,
const float4 &n, const float &d, float * __restrict__ H )
{
//if ( !to.reference )
{
/*getHomography_real( K1_inv, K2, to.R, to.t4, n, d, H );*/
/*float tmp[16];*/
float tmp2[16];
outer_product4(to.t4, n, H); // tmp = t * n'
matdivide(H, d); // tmp / d
matmatsub2(to.R, H); // tmp = R - tmp;
matmul_cu(H,K1_inv,tmp2); // tmp2=tmp*Kinv
matmul_cu(K2,tmp2,H);// H = tmp * K2
}
return;
}
/* census transform, get value based on weather intensity is smaller or higher than center intensity
* Input: p - intensity of center pixel
* pNb - intensity of current nb pixel in the kernel
* epsilon - threshold for classifying as the same intensity (for original ct no epsilon is used --> epsilon=0)
*/
__device__ FORCEINLINE_GIPUMA uint8_t getCTbit_cu ( float p, float pNb, float eps ) {
uint8_t bit = 1;
if ( p - pNb > eps )
bit = 0;
else if ( pNb - p > eps )
bit = 2;
return bit;
}
__device__ FORCEINLINE_GIPUMA float ct_Arma_cu ( const cudaTextureObject_t &l,
const cudaTextureObject_t &r,
const int2 &p,
const int vRad,
const int hRad,
const float intensityCenterLeft,
const float intensityCenterRight,
const float eps,
const float* __restrict__ H
)
{
float4 pt;
getCorrespondingPoint_cu ( p, H, &pt );
//default cost if pt outside of image
float c = 1.0f;
//if ( pt ( 0 ) > 0 &&
//pt ( 0 ) < ( float ) ( l.cols - 1 ) &&
//pt ( 1 ) > 0 &&
//pt ( 1 ) < ( float ) ( l.rows - 1 ) )
{
float intensityL = texatpt4(l, p);
float intensityR = texatpt4(r,pt);
if ( getCTbit_cu ( intensityCenterLeft, intensityL, eps ) == getCTbit_cu ( intensityCenterRight, intensityR, eps ) )
c = 0.0f;
else
c = 1.0f;
}
return c;
}
/* census transform cost computation for search window
* check if intensity of neighboring pixel is lower or higher than intensity of center pixel
*/
__device__ float censusTransform_Arma_cu ( const cudaTextureObject_t &l,
const cudaTextureObject_t &r,
const int2 &p,
const float &d,
const int &vRad,
const int &hRad,
const float &eps,
const float* __restrict__ H)
{
float cost = 0.0f;
float4 pt;
getCorrespondingPoint_cu ( p, H, &pt );
/*if ( pt_c ( 0 ) <= 0.0f || */
/*pt_c ( 0 ) >= ( float ) ( l.cols - 1 ) || */
/*pt_c ( 1 ) <= 0.0f || */
/*pt_c ( 1 ) >= ( float ) ( l.rows - 1 ) ||*/
/*pt_c ( 0 ) != pt_c (0) ||*/
/*pt_c ( 1 ) != pt_c (1) )*/
/*{*/
/*return ( float ) ( hRad * 2 + 1 ) * ( vRad * 2 + 1 );*/
/*}*/
float intensityCenterLeft = texatpt4(l, p);
float intensityCenterRight = texatpt4(r,pt);
//if (blockIdx.x ==0 && blockIdx.y ==0) printf("color is %f %f\n", intensityCenterLeft, intensityCenterRight);
//use non-border values for disparity computation
//subtract i and j by half kernel size since disparity is without border
for ( int i = p.x - hRad; i <= p.x + hRad; i++ ) {
for ( int j = p.y - vRad; j <= p.y + vRad; j++ ) {
if ( i == p.x && j == p.y )
continue;
float c = ct_Arma_cu ( l, r, make_int2(i, j), vRad, hRad, intensityCenterLeft, intensityCenterRight, eps, H);
//w = weight_cu ( leftValue, centerValue, gamma);
cost = cost + c;
}
}
return cost;
}
/*
* cost computation of different cost functions
*/
template< typename T >
__device__ FORCEINLINE_GIPUMA static float pmCost (
const cudaTextureObject_t &l,
const T * __restrict__ tile_left,
const int2 tile_offset,
const cudaTextureObject_t &r,
const int &x,
const int &y,
const float4 &normal,
const int &vRad,
const int &hRad,
const AlgorithmParameters &algParam,
const CameraParameters_cu &camParams,
const int &camTo )
{
const int cols = camParams.cols;
const int rows = camParams.rows;
const float alpha = algParam.alpha;
const float tau_color = algParam.tau_color;
const float tau_gradient = algParam.tau_gradient;
const float gamma = algParam.gamma;
float4 pt_c;
float H[16];
/*float H[3*3];*/
getHomography_cu ( camParams.cameras[REFERENCE], camParams.cameras[camTo], camParams.cameras[REFERENCE].K_inv, camParams.cameras[camTo].K, normal, normal.w, H );
getCorrespondingPoint_cu ( make_int2(x, y), H, &pt_c );
// XXX to review
//if ( pt_c.x < hRad ||
//pt_c.x >= ( float ) ( cols - hRad - 1 ) ||
//pt_c.y < ( float ) vRad ||
//pt_c.y >= ( float ) ( rows - vRad - 1 ) ) {
//return 1000; // XXX
//}
{
float cost = 0;
//float weightSum = 0.0f;
for ( int i = -hRad; i < hRad + 1; i+=WIN_INCREMENT ) {
for ( int j = -vRad; j < vRad + 1; j+=WIN_INCREMENT ) {
const int xTemp = x + i;
const int yTemp = y + j;
float4 pt_l;
pt_l.x = __int2float_rn(xTemp);
pt_l.y = __int2float_rn(yTemp);
int2 pt_li = make_int2(xTemp, yTemp);
float w;
w = weight_cu<T> ( tex2D<T>(l, pt_l.x + 0.5f, pt_l.y + 0.5f), tex2D<T>(l,x + 0.5f,y + 0.5f), gamma);
float4 pt;
getCorrespondingPoint_cu ( make_int2(xTemp, yTemp),
H,
&pt );
cost = cost + pmCostComputation<T> ( l, tile_left, r, pt_l, pt, rows, cols, tau_color, tau_gradient, alpha, w );
//weightSum = weightSum + w;
}
}
return cost;
}
}
template< typename T >
__device__ FORCEINLINE_GIPUMA static float hasImageTexture (
const cudaTextureObject_t &l,
const int2 &p,
const int &vRad,
const int &hRad,
const AlgorithmParameters &algParam)
{
const float gamma = algParam.gamma;
int count_similar_pixel = 0;
for ( int i = -hRad; i < hRad + 1; i += WIN_INCREMENT ) {
for ( int j = -vRad; j < vRad + 1; j += WIN_INCREMENT ) {
const int xTemp = p.x + i;
const int yTemp = p.y + j;
float4 pt_l;
pt_l.x = __int2float_rn( xTemp );
pt_l.y = __int2float_rn( yTemp );
const float w = weight_cu<T> ( tex2D <T> (l, pt_l.x + 0.5f, pt_l.y + 0.5f ), tex2D <T> ( l, p.x + 0.5f, p.y + 0.5f ), gamma);
if (w > algParam.no_texture_sim)
count_similar_pixel++;
}
}
if (count_similar_pixel > hRad*vRad*4/(WIN_INCREMENT * WIN_INCREMENT)*algParam.no_texture_per)
return false;
return true;
}
template< typename T >
__device__ FORCEINLINE_GIPUMA static float hasImageTexture_shared (
const cudaTextureObject_t &l,
const T * __restrict__ tile_left,
const int2 tile_offset,
const int2 &p,
const int &vRad,
const int &hRad,
const AlgorithmParameters &algParam)
{
const T centerValue = tile_left[ p.x-tile_offset.x + SHARED_SIZE_W * ( p.y - tile_offset.y ) ];
int count_similar_pixel = 0;
for ( int i = -hRad; i < hRad + 1; i += WIN_INCREMENT) {
for ( int j = -vRad; j < vRad + 1; j += WIN_INCREMENT) {
const int2 pI = make_int2 ( p.x + i - tile_offset.x, p.y + j - tile_offset.y);
const T leftValue = tile_left[ pI.x + SHARED_SIZE_W * pI.y ];
const float w = weight_cu<T> ( leftValue,
centerValue,
algParam.gamma);
//if (p.x == 440 && p.y == 307 )
//printf("Weight is %f\tValues are %f and %f\n", w, centerValue, leftValue);
if (w > algParam.no_texture_sim)
count_similar_pixel++;
}
}
//if (p.x == 440 && p.y == 307 ) {
//printf("Count similar pixel is %d\n", count_similar_pixel);
////printf("Limit is %f\n", (float) 4*hRad*vRad/(WIN_INCREMENT * WIN_INCREMENT)*algParam.no_texture_per);
////printf("Hrad is %d\n", vRad);
//}
if (count_similar_pixel > hRad*vRad*4/(WIN_INCREMENT * WIN_INCREMENT)*algParam.no_texture_per)
return false;
return true;
}
template< typename T >
__device__ FORCEINLINE_GIPUMA static float pmCost_shared (
const cudaTextureObject_t &l,
const T * __restrict__ tile_left,
const int2 tile_offset,
const cudaTextureObject_t &r,
const int2 &p,
const float4 &normal,
const int &vRad,
const int &hRad,
const AlgorithmParameters &algParam,
const CameraParameters_cu &camParams,
const int &camTo )
{
const float alpha = algParam.alpha;
const float tau_color = algParam.tau_color;
const float tau_gradient = algParam.tau_gradient;
const float gamma = algParam.gamma;
/*float4 pt_c;*/
float H[16];
/*float H[3*3];*/
//getHomography_cu ( camParams.cameras[REFERENCE], camParams.cameras[camTo], camParams.K_inv, camParams.K, normal, normal.w, H );
getHomography_cu ( camParams.cameras[REFERENCE], camParams.cameras[camTo], camParams.cameras[REFERENCE].K_inv, camParams.cameras[camTo].K, normal, normal.w, H );
/*getCorrespondingPoint_cu ( x, y, H, &pt_c );*/
// XXX to review
//if ( pt_c.x < hRad ||
//pt_c.x >= ( float ) ( cols - hRad - 1 ) ||
//pt_c.y < ( float ) vRad ||
//pt_c.y >= ( float ) ( rows - vRad - 1 ) ) {
//return 1000; // XXX
//}
{
float cost = 0;
//float weightSum = 0.0f;
//const int Ic = x - tile_offset.x;
//const int Jc = y - tile_offset.y;
const T centerValue = tile_left[p.x-tile_offset.x + SHARED_SIZE_W*(p.y-tile_offset.y)];
#ifdef CENSUS
cost = censusTransform_Arma_cu (l, r, p, normal.w, vRad, hRad, algParam.census_epsilon, H);
return cost;
#endif
for ( int i = -hRad; i < hRad + 1; i+=WIN_INCREMENT) {
for ( int j = -vRad; j < vRad + 1; j+=WIN_INCREMENT) {
const int2 pTemp = make_int2(p.x +i, p.y + j);
//const int xTemp = p.x + i;
//const int yTemp = p.y + j;
const int2 pI = make_int2 ( p.x + i - tile_offset.x, p.y + j - tile_offset.y);
float w;
#if 0
if (tile_offset.x !=0 &&
xTemp <12 &&
yTemp < 12
)
{
if (texatpt4(l,pt_l) != tile_left[I+SHARED_SIZE_W*J])
{
//printf("PMCOST x %d %d Xtemp %d %d \t\tI %d J %d tilecoords %d %d offset is %d %d blockIdx %d %d tile_offset.x %d tile_offset.y %d\n", x, y, xTemp, yTemp, I, J, xTemp-tile_offset.x, yTemp-tile_offset.y, tile_offset.x, tile_offset.y, blockIdx.x, blockIdx.y, tile_offset.x, tile_offset.y);
printf("Tex is %f, caache is %f\nPMCOST x %d %d Xtemp %d %d \t\tI %d J %d tilecoords %d %d offset is %d %d blockIdx %d %d tile_offset.x %d tile_offset.y %d\n", texatpt4(l, pt_l), tile_left[I+SHARED_SIZE_W*J], x, y, xTemp, yTemp, I, J, xTemp-tile_offset.x, yTemp-tile_offset.y, tile_offset.x, tile_offset.y, blockIdx.x, blockIdx.y, tile_offset.x, tile_offset.y);
}
}
#endif
const T leftValue = tile_left[pI.x + SHARED_SIZE_W*pI.y];
/*if (tile_offset.x !=0 && */
/*xTemp == 100 && */
/*yTemp == 100) {*/
/*printf ("I and J are %d %d and value is %f\n", I, J,leftValue);*/
/*}*/
w = weight_cu<T> ( leftValue,
centerValue,
gamma);
//if( p.x == 446 && p.y == 307)
//printf("weigth is %f\n", w);
//const float w = weight_cu ( tile_left[xTemp - SHARED_SIZE_H][], pt_l), texat(l,x,y), gamma);
float4 pt;
getCorrespondingPoint_cu ( pTemp,
H,
&pt );
cost = cost + pmCostComputation_shared<T> ( l, tile_left, r, leftValue, pI, pt, tau_color, tau_gradient, alpha, w );
//weightSum = weightSum + w;
}
}
return cost;
}
}
// via https://stackoverflow.com/questions/2786899/fastest-sort-of-fixed-length-6-int-array
static __device__ FORCEINLINE_GIPUMA void sort_small(float * __restrict__ d,const int n)
{
int j;
for (int i = 1; i < n; i++) {
float tmp = d[i];
for (j = i; j >= 1 && tmp < d[j-1]; j--)
d[j] = d[j-1];
d[j] = tmp;
}
}
__device__ FORCEINLINE_GIPUMA float getDepthFromPlane3_cu (const Camera_cu &cam,
const float4 &n,
const float &d,
const int2 &p)
{
return -d*cam.fx/(
(n.x*(p.x-cam.K[2]))
+
(n.y*(p.y-cam.K[2+3]))
*cam.alpha +
n.z*cam.fx);
}
__device__ FORCEINLINE_GIPUMA float getDisparity_cu ( const float4 &normal,
const float &d,
const int2 &p,
const Camera_cu &cam )
{
if ( d != d )
return 1000;
return getDepthFromPlane3_cu (cam, normal, d, p);
}
/* cost computation for multiple images
* combines cost of all ref-to-img correspondences
*/
template< typename T >
__device__ FORCEINLINE_GIPUMA static float pmCostMultiview_cu (
const cudaTextureObject_t *images,
const T * __restrict__ tile_left,
const int2 tile_offset,
const int2 p,
const float4 &normal,
const int &vRad,
const int &hRad,
const AlgorithmParameters &algParam,
const CameraParameters_cu &camParams,
const float4 * __restrict__ state,
const int point)
{
// iterate over all other images and compute cost
//const int numImages = camParams.viewSelectionSubsetNumber; // CACHE
float costVector[32];
float cost = 0.0f;
int numValidViews = 0;
int cost_count=0;
for ( int i = 0; i < camParams.viewSelectionSubsetNumber; i++ ) {
int idxCurr = camParams.viewSelectionSubset[i];
/*if ( idxCurr != REFERENCE ) */
{
float c = 0;
#ifdef SHARED
if (tile_offset.x!= 0 )
c = pmCost_shared<T> ( images[REFERENCE],
tile_left,
tile_offset,
images[idxCurr],
p,
normal,
vRad, hRad,
algParam, camParams,
idxCurr );
else
#endif
c = pmCost<T> ( images[REFERENCE],
tile_left,
tile_offset,
images[idxCurr],
p.x, p.y,
normal,
vRad, hRad,
algParam, camParams,
idxCurr );
// only add to cost vector if viewable
if ( c < MAXCOST )
numValidViews++;
else
c = MAXCOST; // in order to not get an overflow when accumulating
costVector[i] = c;
cost_count++;
}
}
sort_small(costVector,cost_count);
//for some robustness only consider best n cost values (n dependent on number of images)
int numBest = numValidViews; //numImages-1;
if ( algParam.cost_comb == COMB_BEST_N )
numBest = min ( numBest, algParam.n_best );
if ( algParam.cost_comb == COMB_GOOD )
numBest = camParams.viewSelectionSubsetNumber ;
float costThresh = costVector[0] * algParam.good_factor;
int numConsidered = 0;
for ( int i = 0; i < numBest; i++ ) {
numConsidered++;
float c = costVector[i];
if ( algParam.cost_comb == COMB_GOOD ) {
c = fminf ( c, costThresh );
}
cost = cost + c;
}
cost = cost / ( ( float ) numConsidered);
if ( numConsidered < 1 )
cost = MAXCOST;
if ( cost != cost || cost > MAXCOST || cost < 0 )
cost = MAXCOST;
return cost;
}
__device__ FORCEINLINE_GIPUMA float get_smoothness_at2 ( const float4 * __restrict__ state,
const float4 &norm,
const float &depth,
const int2 p,
const int2 p_other,
const int cols,
const Camera_cu &cam )
{
float4 norm_other = state [p_other.x + p_other.y*cols];
const float depth_other = getDisparity_cu (norm_other, norm_other.w, p_other, cam);
float4 X_other;
float4 X;
get3Dpoint_cu (&X, cam, p, depth);
get3Dpoint_cu (&X_other, cam, p_other, depth_other);
return (1.0f - fabsf(dot4(norm,norm_other)) + 1.0f);
}
#define ISDISPDEPTHWITHINBORDERS(disp,camParams,camIdx,algParams) \
disp >= camParams.cameras[REFERENCE].depthMin && disp <= camParams.cameras[REFERENCE].depthMax
template< typename T >
__device__ FORCEINLINE_GIPUMA void spatialPropagation_cu ( const cudaTextureObject_t *imgs,
const T * __restrict__ tile_left,
const int2 &tile_offset,
const int2 &p,
const int &box_hrad, const int &box_vrad,
const AlgorithmParameters &algParams,
const CameraParameters_cu &camParams,
float *cost_now,
float4 *norm_now,
const float4 norm_before,
float *disp_now,
const float4 * __restrict__ state,
const int point
)
{
// previous image values
const float d_before = norm_before.w;
const float disp_before = getDisparity_cu (norm_before, d_before, p, camParams.cameras[REFERENCE] );
float cost_before = pmCostMultiview_cu<T> ( imgs,
tile_left,
tile_offset,
p,
norm_before,
box_vrad,
box_hrad,
algParams,
camParams,
state,
point);
if ( ISDISPDEPTHWITHINBORDERS(disp_before,camParams,REFERENCE,algParams) )
{
if ( cost_before < *cost_now ) {
*disp_now = disp_before;
*norm_now = norm_before;
*cost_now = cost_before;
}
}
return;
}
/* compute random disparity and unit vector within given intervals, used for plane refinement step
* interval is limited by image border and general disparity range [0 maxDisparity]
* Input: x - current column x
* disp - old disparity value
* norm - old normal
* maxDeltaZ - range radius for disparity [disp-maxDeltaZ,disp+maxDeltaZ]
* maxDeltaN - range radius for normal
* maxDisparity - maximum disparity value
* cols - number of columns of the image
* dir - disparity to the left or right of x
* limit - defines maximal value for |[nx ny]T| so that only plane tilts to a certain degree are possible
* Output: dispOut - new disparity
* normOut - new normal
*/
__device__ FORCEINLINE_GIPUMA void getRndDispAndUnitVector_cu (
float disp,
const float4 norm,
float &dispOut,
float4 * __restrict__ normOut,
const float maxDeltaZ,
const float maxDeltaN,
const float minDisparity,
const float maxDisparity,
curandState *cs,
CameraParameters_cu &camParams,
const float baseline,
const float4 viewVector) {
//convert depth to disparity and back for non-rectified approach
disp = disparityDepthConversion_cu ( camParams.f, baseline, disp );
//delta min limited by disp=0 and image border
//delta max limited by disp=maxDisparity and image border
float minDelta, maxDelta;
minDelta = -min ( maxDeltaZ, minDisparity + disp ); //limit new disp>=0
maxDelta = min ( maxDeltaZ, maxDisparity - disp ); //limit new disp < maxDisparity
/*minDelta ; -minDelta;*/
float deltaZ = curand_between(cs, minDelta, maxDelta);
//get new disparity value within valid range [0 maxDisparity]
dispOut = fminf ( fmaxf ( disp + deltaZ, minDisparity ), maxDisparity );
dispOut = disparityDepthConversion_cu ( camParams.f, baseline, dispOut );
//get normal
normOut->x = norm.x + curand_between (cs, -maxDeltaN, maxDeltaN );
normOut->y = norm.y + curand_between (cs, -maxDeltaN, maxDeltaN );
normOut->z = norm.z + curand_between (cs, -maxDeltaN, maxDeltaN );
normalize_cu ( normOut );
vecOnHemisphere_cu ( normOut, viewVector );
}
template< typename T >
__device__ FORCEINLINE_GIPUMA static void planeRefinement_cu (
const cudaTextureObject_t *images,
const T * __restrict__ tile_left,
const int2 &p,
const int2 &tile_offset,
const int &box_hrad,
const int &box_vrad,
const AlgorithmParameters &algParams,
CameraParameters_cu &camParams,
const int camIdx,
float * __restrict__ cost_now,
float4 * __restrict__ norm_now,
float * __restrict__ disp_now,
curandState *cs,
const float4 * __restrict__ state)
{
float deltaN = 1.0f;
float4 viewVector;
getViewVector_cu (&viewVector, camParams.cameras[0], p);
// divide delta by 4 instead of 2 for less iterations (for higher disparity range)
// iteration is done over disparity values even for multi-view case in order to have approximately unifom sampling along epipolar line
/*for ( float deltaZ = ( float ) algParams.max_disparity / 2.0f; deltaZ >= 0.1f; deltaZ = deltaZ / 4.0f ) {*/
float4 norm_temp;
float dispTemp_L;
float dTemp_L;
float costTempL;
const float maxdisp=algParams.max_disparity / 2.0f; // temp variable
for ( float deltaZ = maxdisp; deltaZ >= 0.01f; deltaZ = deltaZ / 10.0f ) {
getRndDispAndUnitVector_cu (
*disp_now, *norm_now,
dispTemp_L, &norm_temp,
deltaZ, deltaN,
algParams.min_disparity, algParams.max_disparity,
cs,
camParams, camParams.cameras[0].baseline,
viewVector);
dTemp_L = getD_cu ( norm_temp,
p,
dispTemp_L, camParams.cameras[camIdx] );
norm_temp.w = dTemp_L; // TODO might save a variable here
costTempL = pmCostMultiview_cu<T> ( images,
tile_left,
tile_offset,
p,
norm_temp,
box_vrad, box_hrad,
algParams, camParams,
state,
0);
//if (dTemp_L==dTemp_L && dTemp_L!= 0) // XXX
{
if ( costTempL < *cost_now ) {
*cost_now = costTempL;
*disp_now = dispTemp_L;
*norm_now = norm_temp;
}
}
deltaN = deltaN / 4.0f;
}
}
template< typename T >
__global__ void gipuma_init_cu2(GlobalState &gs)
{
const int2 p = make_int2 ( blockIdx.x * blockDim.x + threadIdx.x, blockIdx.y * blockDim.y + threadIdx.y );
const int rows = gs.cameras->rows;