-
Notifications
You must be signed in to change notification settings - Fork 29
/
ACMP.cu
1246 lines (1107 loc) · 51 KB
/
ACMP.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 "ACMP.h"
__device__ void sort_small(float *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__ void sort_small_weighted(float *d, float *w, int n)
{
int j;
for (int i = 1; i < n; i++) {
float tmp = d[i];
float tmp_w = w[i];
for (j = i; j >= 1 && tmp < d[j - 1]; j--) {
d[j] = d[j - 1];
w[j] = w[j - 1];
}
d[j] = tmp;
w[j] = tmp_w;
}
}
__device__ int FindMinCostIndex(const float *costs, const int n)
{
float min_cost = costs[0];
int min_cost_idx = 0;
for (int idx = 1; idx < n; ++idx) {
if (costs[idx] <= min_cost) {
min_cost = costs[idx];
min_cost_idx = idx;
}
}
return min_cost_idx;
}
__device__ int FindMaxCostIndex(const float *costs, const int n)
{
float max_cost = costs[0];
int max_cost_idx = 0;
for (int idx = 1; idx < n; ++idx) {
if (costs[idx] >= max_cost) {
max_cost = costs[idx];
max_cost_idx = idx;
}
}
return max_cost_idx;
}
__device__ void setBit(unsigned int &input, const unsigned int n)
{
input |= (unsigned int)(1 << n);
}
__device__ int isSet(unsigned int input, const unsigned int n)
{
return (input >> n) & 1;
}
__device__ void Mat33DotVec3(const float mat[9], const float4 vec, float4 *result)
{
result->x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z;
result->y = mat[3] * vec.x + mat[4] * vec.y + mat[5] * vec.z;
result->z = mat[6] * vec.x + mat[7] * vec.y + mat[8] * vec.z;
}
__device__ float Vec3DotVec3(const float4 vec1, const float4 vec2)
{
return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z;
}
__device__ void NormalizeVec3 (float4 *vec)
{
const float normSquared = vec->x * vec->x + vec->y * vec->y + vec->z * vec->z;
const float inverse_sqrt = rsqrtf (normSquared);
vec->x *= inverse_sqrt;
vec->y *= inverse_sqrt;
vec->z *= inverse_sqrt;
}
__device__ void TransformPDFToCDF(float* probs, const int num_probs)
{
float prob_sum = 0.0f;
for (int i = 0; i < num_probs; ++i) {
prob_sum += probs[i];
}
const float inv_prob_sum = 1.0f / prob_sum;
float cum_prob = 0.0f;
for (int i = 0; i < num_probs; ++i) {
const float prob = probs[i] * inv_prob_sum;
cum_prob += prob;
probs[i] = cum_prob;
}
}
__device__ void Get3DPoint(const Camera camera, const int2 p, const float depth, float *X)
{
X[0] = depth * (p.x - camera.K[2]) / camera.K[0];
X[1] = depth * (p.y - camera.K[5]) / camera.K[4];
X[2] = depth;
}
__device__ float4 GetViewDirection(const Camera camera, const int2 p, const float depth)
{
float X[3];
Get3DPoint(camera, p, depth, X);
float norm = sqrt(X[0] * X[0] + X[1] * X[1] + X[2] * X[2]);
float4 view_direction;
view_direction.x = X[0] / norm;
view_direction.y = X[1] / norm;
view_direction.z = X[2] / norm;
view_direction.w = 0;
return view_direction;
}
__device__ float GetDistance2Origin(const Camera camera, const int2 p, const float depth, const float4 normal)
{
float X[3];
Get3DPoint(camera, p, depth, X);
return -(normal.x * X[0] + normal.y * X[1] + normal.z * X[2]);
}
__device__ float ComputeDepthfromPlaneHypothesis(const Camera camera, const float4 plane_hypothesis, const int2 p)
{
return -plane_hypothesis.w * camera.K[0] / ((p.x - camera.K[2]) * plane_hypothesis.x + (camera.K[0] / camera.K[4]) * (p.y - camera.K[5]) * plane_hypothesis.y + camera.K[0] * plane_hypothesis.z);
}
__device__ float4 GenerateRandomNormal(const Camera camera, const int2 p, curandState *rand_state, const float depth)
{
float4 normal;
float q1 = 1.0f;
float q2 = 1.0f;
float s = 2.0f;
while (s >= 1.0f) {
q1 = 2.0f * curand_uniform(rand_state) -1.0f;
q2 = 2.0f * curand_uniform(rand_state) - 1.0f;
s = q1 * q1 + q2 * q2;
}
const float sq = sqrt(1.0f - s);
normal.x = 2.0f * q1 * sq;
normal.y = 2.0f * q2 * sq;
normal.z = 1.0f - 2.0f * s;
normal.w = 0;
float4 view_direction = GetViewDirection(camera, p, depth);
float dot_product = normal.x * view_direction.x + normal.y * view_direction.y + normal.z * view_direction.z;
if (dot_product > 0.0f) {
normal.x = -normal.x;
normal.y = -normal.y;
normal.z = - normal.z;
}
NormalizeVec3(&normal);
return normal;
}
__device__ float4 GeneratePerturbedNormal(const Camera camera, const int2 p, const float4 normal, curandState *rand_state, const float perturbation)
{
float4 view_direction = GetViewDirection(camera, p, 1.0f);
const float a1 = (curand_uniform(rand_state) - 0.5f) * perturbation;
const float a2 = (curand_uniform(rand_state) - 0.5f) * perturbation;
const float a3 = (curand_uniform(rand_state) - 0.5f) * perturbation;
const float sin_a1 = sin(a1);
const float sin_a2 = sin(a2);
const float sin_a3 = sin(a3);
const float cos_a1 = cos(a1);
const float cos_a2 = cos(a2);
const float cos_a3 = cos(a3);
float R[9];
R[0] = cos_a2 * cos_a3;
R[1] = cos_a3 * sin_a1 * sin_a2 - cos_a1 * sin_a3;
R[2] = sin_a1 * sin_a3 + cos_a1 * cos_a3 * sin_a2;
R[3] = cos_a2 * sin_a3;
R[4] = cos_a1 * cos_a3 + sin_a1 * sin_a2 * sin_a3;
R[5] = cos_a1 * sin_a2 * sin_a3 - cos_a3 * sin_a1;
R[6] = -sin_a2;
R[7] = cos_a2 * sin_a1;
R[8] = cos_a1 * cos_a2;
float4 normal_perturbed;
Mat33DotVec3(R, normal, &normal_perturbed);
if (Vec3DotVec3(normal_perturbed, view_direction) >= 0.0f) {
normal_perturbed = normal;
}
NormalizeVec3(&normal_perturbed);
return normal_perturbed;
}
__device__ float4 GenerateRandomPlaneHypothesis(const Camera camera, const int2 p, curandState *rand_state, const float depth_min, const float depth_max)
{
float depth = curand_uniform(rand_state) * (depth_max - depth_min) + depth_min;
float4 plane_hypothesis = GenerateRandomNormal(camera, p, rand_state, depth);
plane_hypothesis.w = GetDistance2Origin(camera, p, depth, plane_hypothesis);
return plane_hypothesis;
}
__device__ float4 GeneratePertubedPlaneHypothesis(const Camera camera, const int2 p, curandState *rand_state, const float perturbation, const float4 plane_hypothesis_now, const float depth_now, const float depth_min, const float depth_max)
{
float depth_perturbed = depth_now;
float dist_perturbed = plane_hypothesis_now.w;
const float dist_min_perturbed = (1 - perturbation) * dist_perturbed;
const float dist_max_perturbed = (1 + perturbation) * dist_perturbed;
float4 plane_hypothesis_temp = plane_hypothesis_now;
do {
dist_perturbed = curand_uniform(rand_state) * (dist_max_perturbed - dist_min_perturbed) + dist_min_perturbed;
plane_hypothesis_temp.w = dist_perturbed;
depth_perturbed = ComputeDepthfromPlaneHypothesis(camera, plane_hypothesis_temp, p);
} while (depth_perturbed < depth_min && depth_perturbed > depth_max);
float4 plane_hypothesis = GeneratePerturbedNormal(camera, p, plane_hypothesis_now, rand_state, perturbation * M_PI);
plane_hypothesis.w = dist_perturbed;
return plane_hypothesis;
}
__device__ void ComputeHomography(const Camera ref_camera, const Camera src_camera, const float4 plane_hypothesis, float *H)
{
float ref_C[3];
float src_C[3];
ref_C[0] = -(ref_camera.R[0] * ref_camera.t[0] + ref_camera.R[3] * ref_camera.t[1] + ref_camera.R[6] * ref_camera.t[2]);
ref_C[1] = -(ref_camera.R[1] * ref_camera.t[0] + ref_camera.R[4] * ref_camera.t[1] + ref_camera.R[7] * ref_camera.t[2]);
ref_C[2] = -(ref_camera.R[2] * ref_camera.t[0] + ref_camera.R[5] * ref_camera.t[1] + ref_camera.R[8] * ref_camera.t[2]);
src_C[0] = -(src_camera.R[0] * src_camera.t[0] + src_camera.R[3] * src_camera.t[1] + src_camera.R[6] * src_camera.t[2]);
src_C[1] = -(src_camera.R[1] * src_camera.t[0] + src_camera.R[4] * src_camera.t[1] + src_camera.R[7] * src_camera.t[2]);
src_C[2] = -(src_camera.R[2] * src_camera.t[0] + src_camera.R[5] * src_camera.t[1] + src_camera.R[8] * src_camera.t[2]);
float R_relative[9];
float C_relative[3];
float t_relative[3];
R_relative[0] = src_camera.R[0] * ref_camera.R[0] + src_camera.R[1] * ref_camera.R[1] + src_camera.R[2] *ref_camera.R[2];
R_relative[1] = src_camera.R[0] * ref_camera.R[3] + src_camera.R[1] * ref_camera.R[4] + src_camera.R[2] *ref_camera.R[5];
R_relative[2] = src_camera.R[0] * ref_camera.R[6] + src_camera.R[1] * ref_camera.R[7] + src_camera.R[2] *ref_camera.R[8];
R_relative[3] = src_camera.R[3] * ref_camera.R[0] + src_camera.R[4] * ref_camera.R[1] + src_camera.R[5] *ref_camera.R[2];
R_relative[4] = src_camera.R[3] * ref_camera.R[3] + src_camera.R[4] * ref_camera.R[4] + src_camera.R[5] *ref_camera.R[5];
R_relative[5] = src_camera.R[3] * ref_camera.R[6] + src_camera.R[4] * ref_camera.R[7] + src_camera.R[5] *ref_camera.R[8];
R_relative[6] = src_camera.R[6] * ref_camera.R[0] + src_camera.R[7] * ref_camera.R[1] + src_camera.R[8] *ref_camera.R[2];
R_relative[7] = src_camera.R[6] * ref_camera.R[3] + src_camera.R[7] * ref_camera.R[4] + src_camera.R[8] *ref_camera.R[5];
R_relative[8] = src_camera.R[6] * ref_camera.R[6] + src_camera.R[7] * ref_camera.R[7] + src_camera.R[8] *ref_camera.R[8];
C_relative[0] = (ref_C[0] - src_C[0]);
C_relative[1] = (ref_C[1] - src_C[1]);
C_relative[2] = (ref_C[2] - src_C[2]);
t_relative[0] = src_camera.R[0] * C_relative[0] + src_camera.R[1] * C_relative[1] + src_camera.R[2] * C_relative[2];
t_relative[1] = src_camera.R[3] * C_relative[0] + src_camera.R[4] * C_relative[1] + src_camera.R[5] * C_relative[2];
t_relative[2] = src_camera.R[6] * C_relative[0] + src_camera.R[7] * C_relative[1] + src_camera.R[8] * C_relative[2];
H[0] = R_relative[0] - t_relative[0] * plane_hypothesis.x / plane_hypothesis.w;
H[1] = R_relative[1] - t_relative[0] * plane_hypothesis.y / plane_hypothesis.w;
H[2] = R_relative[2] - t_relative[0] * plane_hypothesis.z / plane_hypothesis.w;
H[3] = R_relative[3] - t_relative[1] * plane_hypothesis.x / plane_hypothesis.w;
H[4] = R_relative[4] - t_relative[1] * plane_hypothesis.y / plane_hypothesis.w;
H[5] = R_relative[5] - t_relative[1] * plane_hypothesis.z / plane_hypothesis.w;
H[6] = R_relative[6] - t_relative[2] * plane_hypothesis.x / plane_hypothesis.w;
H[7] = R_relative[7] - t_relative[2] * plane_hypothesis.y / plane_hypothesis.w;
H[8] = R_relative[8] - t_relative[2] * plane_hypothesis.z / plane_hypothesis.w;
float tmp[9];
tmp[0] = H[0] / ref_camera.K[0];
tmp[1] = H[1] / ref_camera.K[4];
tmp[2] = -H[0] * ref_camera.K[2] / ref_camera.K[0] - H[1] * ref_camera.K[5] / ref_camera.K[4] + H[2];
tmp[3] = H[3] / ref_camera.K[0];
tmp[4] = H[4] / ref_camera.K[4];
tmp[5] = -H[3] * ref_camera.K[2] / ref_camera.K[0] - H[4] * ref_camera.K[5] / ref_camera.K[4] + H[5];
tmp[6] = H[6] / ref_camera.K[0];
tmp[7] = H[7] / ref_camera.K[4];
tmp[8] = -H[6] * ref_camera.K[2] / ref_camera.K[0] - H[7] * ref_camera.K[5] / ref_camera.K[4] + H[8];
H[0] = src_camera.K[0] * tmp[0] + src_camera.K[2] * tmp[6];
H[1] = src_camera.K[0] * tmp[1] + src_camera.K[2] * tmp[7];
H[2] = src_camera.K[0] * tmp[2] + src_camera.K[2] * tmp[8];
H[3] = src_camera.K[4] * tmp[3] + src_camera.K[5] * tmp[6];
H[4] = src_camera.K[4] * tmp[4] + src_camera.K[5] * tmp[7];
H[5] = src_camera.K[4] * tmp[5] + src_camera.K[5] * tmp[8];
H[6] = src_camera.K[8] * tmp[6];
H[7] = src_camera.K[8] * tmp[7];
H[8] = src_camera.K[8] * tmp[8];
}
__device__ float2 ComputeCorrespondingPoint(const float *H, const int2 p)
{
float3 pt;
pt.x = H[0] * p.x + H[1] * p.y + H[2];
pt.y = H[3] * p.x + H[4] * p.y + H[5];
pt.z = H[6] * p.x + H[7] * p.y + H[8];
return make_float2(pt.x / pt.z, pt.y / pt.z);
}
__device__ float4 TransformNormal(const Camera camera, float4 plane_hypothesis)
{
float4 transformed_normal;
transformed_normal.x = camera.R[0] * plane_hypothesis.x + camera.R[3] * plane_hypothesis.y + camera.R[6] * plane_hypothesis.z;
transformed_normal.y = camera.R[1] * plane_hypothesis.x + camera.R[4] * plane_hypothesis.y + camera.R[7] * plane_hypothesis.z;
transformed_normal.z = camera.R[2] * plane_hypothesis.x + camera.R[5] * plane_hypothesis.y + camera.R[8] * plane_hypothesis.z;
transformed_normal.w = plane_hypothesis.w;
return transformed_normal;
}
__device__ float4 TransformNormal2RefCam(const Camera camera, float4 plane_hypothesis)
{
float4 transformed_normal;
transformed_normal.x = camera.R[0] * plane_hypothesis.x + camera.R[1] * plane_hypothesis.y + camera.R[2] * plane_hypothesis.z;
transformed_normal.y = camera.R[3] * plane_hypothesis.x + camera.R[4] * plane_hypothesis.y + camera.R[5] * plane_hypothesis.z;
transformed_normal.z = camera.R[6] * plane_hypothesis.x + camera.R[7] * plane_hypothesis.y + camera.R[8] * plane_hypothesis.z;
transformed_normal.w = plane_hypothesis.w;
return transformed_normal;
}
__device__ float ComputeBilateralWeight(const float x_dist, const float y_dist, const float pix, const float center_pix, const float sigma_spatial, const float sigma_color)
{
const float spatial_dist = sqrt(x_dist * x_dist + y_dist * y_dist);
const float color_dist = fabs(pix - center_pix);
return exp(-spatial_dist / (2.0f * sigma_spatial* sigma_spatial) - color_dist / (2.0f * sigma_color * sigma_color));
}
__device__ float ComputeBilateralNCC(const cudaTextureObject_t ref_image, const Camera ref_camera, const cudaTextureObject_t src_image, const Camera src_camera, const int2 p, const float4 plane_hypothesis, const PatchMatchParams params)
{
const float cost_max = 2.0f;
int radius = params.patch_size / 2;
float H[9];
ComputeHomography(ref_camera, src_camera, plane_hypothesis, H);
float2 pt = ComputeCorrespondingPoint(H, p);
if (pt.x >= src_camera.width || pt.x < 0.0f || pt.y >= src_camera.height || pt.y < 0.0f) {
return cost_max;
}
float cost = 0.0f;
{
float sum_ref = 0.0f;
float sum_ref_ref = 0.0f;
float sum_src = 0.0f;
float sum_src_src = 0.0f;
float sum_ref_src = 0.0f;
float bilateral_weight_sum = 0.0f;
const float ref_center_pix = tex2D<float>(ref_image, p.x + 0.5f, p.y + 0.5f);
for (int i = -radius; i < radius + 1; i += params.radius_increment) {
float sum_ref_row = 0.0f;
float sum_src_row = 0.0f;
float sum_ref_ref_row = 0.0f;
float sum_src_src_row = 0.0f;
float sum_ref_src_row = 0.0f;
float bilateral_weight_sum_row = 0.0f;
for (int j = -radius; j < radius + 1; j += params.radius_increment) {
const int2 ref_pt = make_int2(p.x + i, p.y + j);
const float ref_pix = tex2D<float>(ref_image, ref_pt.x + 0.5f, ref_pt.y + 0.5f);
float2 src_pt = ComputeCorrespondingPoint(H, ref_pt);
const float src_pix = tex2D<float>(src_image, src_pt.x + 0.5f, src_pt.y + 0.5f);
float weight = ComputeBilateralWeight(i, j, ref_pix, ref_center_pix, params.sigma_spatial, params.sigma_color);
sum_ref_row += weight * ref_pix;
sum_ref_ref_row += weight * ref_pix * ref_pix;
sum_src_row += weight * src_pix;
sum_src_src_row += weight * src_pix * src_pix;
sum_ref_src_row += weight * ref_pix * src_pix;
bilateral_weight_sum_row += weight;
}
sum_ref += sum_ref_row;
sum_ref_ref += sum_ref_ref_row;
sum_src += sum_src_row;
sum_src_src += sum_src_src_row;
sum_ref_src += sum_ref_src_row;
bilateral_weight_sum += bilateral_weight_sum_row;
}
const float inv_bilateral_weight_sum = 1.0f / bilateral_weight_sum;
sum_ref *= inv_bilateral_weight_sum;
sum_ref_ref *= inv_bilateral_weight_sum;
sum_src *= inv_bilateral_weight_sum;
sum_src_src *= inv_bilateral_weight_sum;
sum_ref_src *= inv_bilateral_weight_sum;
const float var_ref = sum_ref_ref - sum_ref * sum_ref;
const float var_src = sum_src_src - sum_src * sum_src;
const float kMinVar = 1e-5f;
if (var_ref < kMinVar || var_src < kMinVar) {
return cost = cost_max;
} else {
const float covar_src_ref = sum_ref_src - sum_ref * sum_src;
const float var_ref_src = sqrt(var_ref * var_src);
return cost = max(0.0f, min(cost_max, 1.0f - covar_src_ref / var_ref_src));
}
}
}
__device__ float ComputeMultiViewInitialCostandSelectedViews(const cudaTextureObject_t *images, const Camera *cameras, const int2 p, const float4 plane_hypothesis, unsigned int *selected_views, const PatchMatchParams params)
{
float cost_max = 2.0f;
float cost_vector[32] = {2.0f};
float cost_vector_copy[32] = {2.0f};
int cost_count = 0;
int num_valid_views = 0;
for (int i = 1; i < params.num_images; ++i) {
float c = ComputeBilateralNCC(images[0], cameras[0], images[i], cameras[i], p, plane_hypothesis, params);
cost_vector[i - 1] = c;
cost_vector_copy[i - 1] = c;
cost_count++;
if (c < cost_max) {
num_valid_views++;
}
}
sort_small(cost_vector, cost_count);
*selected_views = 0;
int top_k = min(num_valid_views, params.top_k);
if (top_k > 0) {
float cost = 0.0f;
for (int i = 0; i < top_k; ++i) {
cost += cost_vector[i];
}
float cost_threshold = cost_vector[top_k - 1];
for (int i = 0; i < params.num_images - 1; ++i) {
if (cost_vector_copy[i] <= cost_threshold) {
setBit(*selected_views, i);
}
}
return cost / top_k;
} else {
return cost_max;
}
}
__device__ void ComputeMultiViewCostVector(const cudaTextureObject_t *images, const Camera *cameras, const int2 p, const float4 plane_hypothesis, float *cost_vector, const PatchMatchParams params)
{
for (int i = 1; i < params.num_images; ++i) {
cost_vector[i - 1] = ComputeBilateralNCC(images[0], cameras[0], images[i], cameras[i], p, plane_hypothesis, params);
}
}
__device__ float3 Get3DPointonWorld_cu(const float x, const float y, const float depth, const Camera camera)
{
float3 pointX;
float3 tmpX;
// Reprojection
pointX.x = depth * (x - camera.K[2]) / camera.K[0];
pointX.y = depth * (y - camera.K[5]) / camera.K[4];
pointX.z = depth;
// Rotation
tmpX.x = camera.R[0] * pointX.x + camera.R[3] * pointX.y + camera.R[6] * pointX.z;
tmpX.y = camera.R[1] * pointX.x + camera.R[4] * pointX.y + camera.R[7] * pointX.z;
tmpX.z = camera.R[2] * pointX.x + camera.R[5] * pointX.y + camera.R[8] * pointX.z;
// Transformation
float3 C;
C.x = -(camera.R[0] * camera.t[0] + camera.R[3] * camera.t[1] + camera.R[6] * camera.t[2]);
C.y = -(camera.R[1] * camera.t[0] + camera.R[4] * camera.t[1] + camera.R[7] * camera.t[2]);
C.z = -(camera.R[2] * camera.t[0] + camera.R[5] * camera.t[1] + camera.R[8] * camera.t[2]);
pointX.x = tmpX.x + C.x;
pointX.y = tmpX.y + C.y;
pointX.z = tmpX.z + C.z;
return pointX;
}
__device__ void ProjectonCamera_cu(const float3 PointX, const Camera camera, float2 &point, float &depth)
{
float3 tmp;
tmp.x = camera.R[0] * PointX.x + camera.R[1] * PointX.y + camera.R[2] * PointX.z + camera.t[0];
tmp.y = camera.R[3] * PointX.x + camera.R[4] * PointX.y + camera.R[5] * PointX.z + camera.t[1];
tmp.z = camera.R[6] * PointX.x + camera.R[7] * PointX.y + camera.R[8] * PointX.z + camera.t[2];
depth = camera.K[6] * tmp.x + camera.K[7] * tmp.y + camera.K[8] * tmp.z;
point.x = (camera.K[0] * tmp.x + camera.K[1] * tmp.y + camera.K[2] * tmp.z) / depth;
point.y = (camera.K[3] * tmp.x + camera.K[4] * tmp.y + camera.K[5] * tmp.z) / depth;
}
__device__ float ComputeGeomConsistencyCost(const cudaTextureObject_t depth_image, const Camera ref_camera, const Camera src_camera, const float4 plane_hypothesis, const int2 p)
{
const float max_cost = 5.0f;
float depth = ComputeDepthfromPlaneHypothesis(ref_camera, plane_hypothesis, p);
float3 forward_point = Get3DPointonWorld_cu(p.x, p.y, depth, ref_camera);
float2 src_pt;
float src_d;
ProjectonCamera_cu(forward_point, src_camera, src_pt, src_d);
const float src_depth = tex2D<float>(depth_image, (int)src_pt.x + 0.5f, (int)src_pt.y + 0.5f);
if (src_depth == 0.0f) {
return max_cost;
}
float3 src_3D_pt = Get3DPointonWorld_cu(src_pt.x, src_pt.y, src_depth, src_camera);
float2 backward_point;
float ref_d;
ProjectonCamera_cu(src_3D_pt, ref_camera, backward_point, ref_d);
const float diff_col = p.x - backward_point.x;
const float diff_row = p.y - backward_point.y;
return min(max_cost, sqrt(diff_col * diff_col + diff_row * diff_row));
}
__global__ void RandomInitialization(cudaTextureObjects *texture_objects, Camera *cameras, float4 *plane_hypotheses, float *costs, curandState *rand_states, unsigned int *selected_views, float4 *prior_planes, unsigned int *plane_masks, const PatchMatchParams params)
{
const int2 p = make_int2(blockIdx.x * blockDim.x + threadIdx.x, blockIdx.y * blockDim.y + threadIdx.y);
int width = cameras[0].width;
int height = cameras[0].height;
if (p.x >= width || p.y >= height) {
return;
}
const int center = p.y * width + p.x;
curand_init(clock64(), p.y, p.x, &rand_states[center]);
if (params.geom_consistency) {
float4 plane_hypothesis = plane_hypotheses[center];
plane_hypothesis = TransformNormal2RefCam(cameras[0], plane_hypothesis);
float depth = plane_hypothesis.w;
plane_hypothesis.w = GetDistance2Origin(cameras[0], p, depth, plane_hypothesis);
plane_hypotheses[center] = plane_hypothesis;
costs[center] = ComputeMultiViewInitialCostandSelectedViews(texture_objects[0].images, cameras, p, plane_hypotheses[center], &selected_views[center], params);
}
else if (params.planar_prior) {
if (plane_masks[center] > 0 && costs[center] >= 0.1f) {
float perturbation = 0.02f;
float4 plane_hypothesis = prior_planes[center];
float depth_perturbed = plane_hypothesis.w;
const float depth_min_perturbed = (1 - 3 * perturbation) * depth_perturbed;
const float depth_max_perturbed = (1 + 3 * perturbation) * depth_perturbed;
depth_perturbed = curand_uniform(&rand_states[center]) * (depth_max_perturbed - depth_min_perturbed) + depth_min_perturbed;
float4 plane_hypothesis_perturbed = GeneratePerturbedNormal(cameras[0], p, plane_hypothesis, &rand_states[center], 3 * perturbation * M_PI);
plane_hypothesis_perturbed.w = depth_perturbed;
plane_hypotheses[center] = plane_hypothesis_perturbed;
costs[center] = ComputeMultiViewInitialCostandSelectedViews(texture_objects[0].images, cameras, p, plane_hypotheses[center], &selected_views[center], params);
}
else {
float4 plane_hypothesis = plane_hypotheses[center];
float depth = plane_hypothesis.w;
plane_hypothesis.w = GetDistance2Origin(cameras[0], p, depth, plane_hypothesis);
plane_hypotheses[center] = plane_hypothesis;
costs[center] = ComputeMultiViewInitialCostandSelectedViews(texture_objects[0].images, cameras, p, plane_hypotheses[center], &selected_views[center], params);
}
}
else {
plane_hypotheses[center] = GenerateRandomPlaneHypothesis(cameras[0], p, &rand_states[center], params.depth_min, params.depth_max);
costs[center] = ComputeMultiViewInitialCostandSelectedViews(texture_objects[0].images, cameras, p, plane_hypotheses[center], &selected_views[center], params);
}
}
__device__ void PlaneHypothesisRefinement(const cudaTextureObject_t *images, const cudaTextureObject_t *depth_images, const Camera *cameras, float4 *plane_hypothesis, float *depth, float *cost, curandState *rand_state, const float *view_weights, const float weight_norm, float4 *prior_planes, unsigned int *plane_masks, float *restricted_cost, const int2 p, const PatchMatchParams params)
{
float perturbation = 0.02f;
const int center = p.y * cameras[0].width + p.x;
float gamma = 0.5f;
float depth_sigma = (params.depth_max - params.depth_min) / 64.0f;
float two_depth_sigma_squared = 2 * depth_sigma * depth_sigma;
float angle_sigma = M_PI * (5.0f / 180.0f);
float two_angle_sigma_squared = 2 * angle_sigma * angle_sigma;
float beta = 0.18f;
float depth_prior = 0.0f;
float depth_rand;
float4 plane_hypothesis_rand;
if (params.planar_prior && plane_masks[center] > 0) {
depth_prior = ComputeDepthfromPlaneHypothesis(cameras[0], prior_planes[center], p);
depth_rand = curand_uniform(rand_state) * 6 * depth_sigma + (depth_prior - 3 * depth_sigma);
plane_hypothesis_rand = GeneratePerturbedNormal(cameras[0], p, prior_planes[center], rand_state, angle_sigma);
}
else {
depth_rand = curand_uniform(rand_state) * (params.depth_max - params.depth_min) + params.depth_min;
plane_hypothesis_rand = GenerateRandomNormal(cameras[0], p, rand_state, *depth);
}
float depth_perturbed = *depth;
const float depth_min_perturbed = (1 - perturbation) * depth_perturbed;
const float depth_max_perturbed = (1 + perturbation) * depth_perturbed;
do {
depth_perturbed = curand_uniform(rand_state) * (depth_max_perturbed - depth_min_perturbed) + depth_min_perturbed;
} while (depth_perturbed < params.depth_min && depth_perturbed > params.depth_max);
float4 plane_hypothesis_perturbed = GeneratePerturbedNormal(cameras[0], p, *plane_hypothesis, rand_state, perturbation * M_PI); // GeneratePertubedPlaneHypothesis(cameras[0], p, rand_state, perturbation, *plane_hypothesis, *depth, params.depth_min, params.depth_max);
const int num_planes = 5;
float depths[num_planes] = {depth_rand, *depth, depth_rand, *depth, depth_perturbed};
float4 normals[num_planes] = {*plane_hypothesis, plane_hypothesis_rand, plane_hypothesis_rand, plane_hypothesis_perturbed, *plane_hypothesis};
for (int i = 0; i < num_planes; ++i) {
float cost_vector[32] = {2.0f};
float4 temp_plane_hypothesis = normals[i];
temp_plane_hypothesis.w = GetDistance2Origin(cameras[0], p, depths[i], temp_plane_hypothesis); // dists[i];
ComputeMultiViewCostVector(images, cameras, p, temp_plane_hypothesis, cost_vector, params);
float temp_cost = 0.0f;
for (int j = 0; j < params.num_images - 1; ++j) {
if (view_weights[j] > 0) {
if (params.geom_consistency) {
temp_cost += view_weights[j] * (cost_vector[j] + 0.1f * ComputeGeomConsistencyCost(depth_images[j+1], cameras[0], cameras[j+1], temp_plane_hypothesis, p));
}
else {
temp_cost += view_weights[j] * cost_vector[j];
}
}
}
temp_cost /= weight_norm;
float depth_before = ComputeDepthfromPlaneHypothesis(cameras[0], temp_plane_hypothesis, p);
if (params.planar_prior && plane_masks[center] > 0) {
float depth_diff = depths[i] - depth_prior;
float angle_cos = Vec3DotVec3(prior_planes[center], temp_plane_hypothesis);
float angle_diff = acos(angle_cos);
float prior = gamma + exp(- depth_diff * depth_diff / two_depth_sigma_squared) * exp(- angle_diff * angle_diff / two_angle_sigma_squared);
float restricted_temp_cost = exp(-temp_cost * temp_cost / beta) * prior;
if (depth_before >= params.depth_min && depth_before <= params.depth_max && restricted_temp_cost > *restricted_cost) {
*depth = depth_before;
*plane_hypothesis = temp_plane_hypothesis;
*cost = temp_cost;
*restricted_cost = restricted_temp_cost;
}
}
else {
if (depth_before >= params.depth_min && depth_before <= params.depth_max && temp_cost < *cost) {
*depth = depth_before;
*plane_hypothesis = temp_plane_hypothesis;
*cost = temp_cost;
}
}
}
}
__device__ void CheckerboardPropagation(const cudaTextureObject_t *images, const cudaTextureObject_t *depths, const Camera *cameras, float4 *plane_hypotheses, float *costs, curandState *rand_states, unsigned int *selected_views, float4 *prior_planes, unsigned int *plane_masks, const int2 p, const PatchMatchParams params, const int iter)
{
int width = cameras[0].width;
int height = cameras[0].height;
if (p.x >= width || p.y >= height) {
return;
}
const int center = p.y * width + p.x;
int left_near = center - 1;
int left_far = center - 3;
int right_near = center + 1;
int right_far = center + 3;
int up_near = center - width;
int up_far = center - 3 * width;
int down_near = center + width;
int down_far = center + 3 * width;
// Adaptive Checkerboard Sampling
float cost_array[8][32] = {2.0f};
// 0 -- up_near, 1 -- up_far, 2 -- down_near, 3 -- down_far, 4 -- left_near, 5 -- left_far, 6 -- right_near, 7 -- right_far
bool flag[8] = {false};
int num_valid_pixels = 0;
float costMin;
int costMinPoint;
// up_far
if (p.y > 2) {
flag[1] = true;
num_valid_pixels++;
costMin = costs[up_far];
costMinPoint = up_far;
for (int i = 1; i < 11; ++i) {
if (p.y > 2 + 2 * i) {
int pointTemp = up_far - 2 * i * width;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
up_far = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[up_far], cost_array[1], params);
}
// dwon_far
if (p.y < height - 3) {
flag[3] = true;
num_valid_pixels++;
costMin = costs[down_far];
costMinPoint = down_far;
for (int i = 1; i < 11; ++i) {
if (p.y < height - 3 - 2 * i) {
int pointTemp = down_far + 2 * i * width;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
down_far = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[down_far], cost_array[3], params);
}
// left_far
if (p.x > 2) {
flag[5] = true;
num_valid_pixels++;
costMin = costs[left_far];
costMinPoint = left_far;
for (int i = 1; i < 11; ++i) {
if (p.x > 2 + 2 * i) {
int pointTemp = left_far - 2 * i;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
left_far = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[left_far], cost_array[5], params);
}
// right_far
if (p.x < width - 3) {
flag[7] = true;
num_valid_pixels++;
costMin = costs[right_far];
costMinPoint = right_far;
for (int i = 1; i < 11; ++i) {
if (p.x < width - 3 - 2 * i) {
int pointTemp = right_far + 2 * i;
if (costMin < costs[pointTemp]) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
right_far = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[right_far], cost_array[7], params);
}
// up_near
if (p.y > 0) {
flag[0] = true;
num_valid_pixels++;
costMin = costs[up_near];
costMinPoint = up_near;
for (int i = 0; i < 3; ++i) {
if (p.y > 1 + i && p.x > i) {
int pointTemp = up_near - (1 + i) * width - i;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
if (p.y > 1 + i && p.x < width - 1 - i) {
int pointTemp = up_near - (1 + i) * width + i;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
up_near = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[up_near], cost_array[0], params);
}
// down_near
if (p.y < height - 1) {
flag[2] = true;
num_valid_pixels++;
costMin = costs[down_near];
costMinPoint = down_near;
for (int i = 0; i < 3; ++i) {
if (p.y < height - 2 - i && p.x > i) {
int pointTemp = down_near + (1 + i) * width - i;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
if (p.y < height - 2 - i && p.x < width - 1 - i) {
int pointTemp = down_near + (1 + i) * width + i;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
down_near = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[down_near], cost_array[2], params);
}
// left_near
if (p.x > 0) {
flag[4] = true;
num_valid_pixels++;
costMin = costs[left_near];
costMinPoint = left_near;
for (int i = 0; i < 3; ++i) {
if (p.x > 1 + i && p.y > i) {
int pointTemp = left_near - (1 + i) - i * width;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
if (p.x > 1 + i && p.y < height - 1 - i) {
int pointTemp = left_near - (1 + i) + i * width;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
left_near = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[left_near], cost_array[4], params);
}
// right_near
if (p.x < width - 1) {
flag[6] = true;
num_valid_pixels++;
costMin = costs[right_near];
costMinPoint = right_near;
for (int i = 0; i < 3; ++i) {
if (p.x < width - 2 - i && p.y > i) {
int pointTemp = right_near + (1 + i) - i * width;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
if (p.x < width - 2 - i && p.y < height - 1- i) {
int pointTemp = right_near + (1 + i) + i * width;
if (costs[pointTemp] < costMin) {
costMin = costs[pointTemp];
costMinPoint = pointTemp;
}
}
}
right_near = costMinPoint;
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[right_near], cost_array[6], params);
}
const int positions[8] = {up_near, up_far, down_near, down_far, left_near, left_far, right_near, right_far};
// Multi-hypothesis Joint View Selection
float view_weights[32] = {0.0f};
float view_selection_priors[32] = {0.0f};
int neighbor_positions[4] = {center - width, center + width, center - 1, center + 1};
for (int i = 0; i < 4; ++i) {
if (flag[2 * i]) {
for (int j = 0; j < params.num_images - 1; ++j) {
if (isSet(selected_views[neighbor_positions[i]], j) == 1) {
view_selection_priors[j] += 0.9f;
} else {
view_selection_priors[j] += 0.1f;
}
}
}
}
float sampling_probs[32] = {0.0f};
float cost_threshold = 0.8 * expf((iter) * (iter) / (-90.0f));
for (int i = 0; i < params.num_images - 1; i++) {
float count = 0;
int count_false = 0;
float tmpw = 0;
for (int j = 0; j < 8; j++) {
if (cost_array[j][i] < cost_threshold) {
tmpw += expf(cost_array[j][i] * cost_array[j][i] / (-0.18f));
count++;
}
if (cost_array[j][i] > 1.2f) {
count_false++;
}
}
if (count > 2 && count_false < 3) {
sampling_probs[i] = tmpw / count;
}
else if (count_false < 3) {
sampling_probs[i] = expf(cost_threshold * cost_threshold / (-0.32f));
}
sampling_probs[i] = sampling_probs[i] * view_selection_priors[i];
}
TransformPDFToCDF(sampling_probs, params.num_images - 1);
for (int sample = 0; sample < 15; ++sample) {
const float rand_prob = curand_uniform(&rand_states[center]) - FLT_EPSILON;
for (int image_id = 0; image_id < params.num_images - 1; ++image_id) {
const float prob = sampling_probs[image_id];
if (prob > rand_prob) {
view_weights[image_id] += 1.0f;
break;
}
}
}
unsigned int temp_selected_views = 0;
int num_selected_view = 0;
float weight_norm = 0;
for (int i = 0; i < params.num_images - 1; ++i) {
if (view_weights[i] > 0) {
setBit(temp_selected_views, i);
weight_norm += view_weights[i];
num_selected_view++;
}
}
float final_costs[8] = {0.0f};
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < params.num_images - 1; ++j) {
if (view_weights[j] > 0) {
if (params.geom_consistency) {
if (flag[i]) {
final_costs[i] += view_weights[j] * (cost_array[i][j] + 0.1f * ComputeGeomConsistencyCost(depths[j+1], cameras[0], cameras[j+1], plane_hypotheses[positions[i]], p));
}
else {
final_costs[i] += view_weights[j] * (cost_array[i][j] + 0.1f * 5.0f);
}
}
else {
final_costs[i] += view_weights[j] * cost_array[i][j];
}
}
}
final_costs[i] /= weight_norm;
}
const int min_cost_idx = FindMinCostIndex(final_costs, 8);
float cost_vector_now[32] = {2.0f};
ComputeMultiViewCostVector(images, cameras, p, plane_hypotheses[center], cost_vector_now, params);
float cost_now = 0.0f;
for (int i = 0; i < params.num_images - 1; ++i) {
if (params.geom_consistency) {
cost_now += view_weights[i] * (cost_vector_now[i] + 0.1f * ComputeGeomConsistencyCost(depths[i+1], cameras[0], cameras[i+1], plane_hypotheses[center], p));
}
else {
cost_now += view_weights[i] * cost_vector_now[i];
}
}
cost_now /= weight_norm;
costs[center] = cost_now;
float depth_now = ComputeDepthfromPlaneHypothesis(cameras[0], plane_hypotheses[center], p);
float restricted_cost = 0.0f;
if (params.planar_prior) {
float restricted_final_costs[8] = {0.0f};
float gamma = 0.5f;
float depth_sigma = (params.depth_max - params.depth_min) / 64.0f;
float two_depth_sigma_squared = 2 * depth_sigma * depth_sigma;
float angle_sigma = M_PI * (5.0f / 180.0f);
float two_angle_sigma_squared = 2 * angle_sigma * angle_sigma;
float depth_prior = ComputeDepthfromPlaneHypothesis(cameras[0], prior_planes[center], p);
float beta = 0.18f;
if (plane_masks[center] > 0) {
for (int i = 0; i < 8; i++) {
if (flag[i]) {
float depth_now = ComputeDepthfromPlaneHypothesis(cameras[0], plane_hypotheses[positions[i]], p);
float depth_diff = depth_now - depth_prior;
float angle_cos = Vec3DotVec3(prior_planes[center], plane_hypotheses[positions[i]]);
float angle_diff = acos(angle_cos);
float prior = gamma + exp(- depth_diff * depth_diff / two_depth_sigma_squared) * exp(- angle_diff * angle_diff / two_angle_sigma_squared);
restricted_final_costs[i] = exp(-final_costs[i] * final_costs[i] / beta) * prior;
}
}
const int max_cost_idx = FindMaxCostIndex(restricted_final_costs, 8);
float restricted_cost_now = 0.0f;
float depth_now = ComputeDepthfromPlaneHypothesis(cameras[0], plane_hypotheses[center], p);
float depth_diff = depth_now - depth_prior;
float angle_cos = Vec3DotVec3(prior_planes[center], plane_hypotheses[center]);
float angle_diff = acos(angle_cos);
float prior = gamma + exp(- depth_diff * depth_diff / two_depth_sigma_squared) * exp(- angle_diff * angle_diff / two_angle_sigma_squared);
restricted_cost_now = exp(-cost_now * cost_now / beta) * prior;
if (flag[max_cost_idx]) {
float depth_before = ComputeDepthfromPlaneHypothesis(cameras[0], plane_hypotheses[positions[max_cost_idx]], p);
if (depth_before >= params.depth_min && depth_before <= params.depth_max && restricted_final_costs[max_cost_idx] > restricted_cost_now) {
depth_now = depth_before;
plane_hypotheses[center] = plane_hypotheses[positions[max_cost_idx]];
costs[center] = final_costs[max_cost_idx];
restricted_cost = restricted_final_costs[max_cost_idx];
selected_views[center] = temp_selected_views;
}
}
}
else if (flag[min_cost_idx]) {
float depth_before = ComputeDepthfromPlaneHypothesis(cameras[0], plane_hypotheses[positions[min_cost_idx]], p);
if (depth_before >= params.depth_min && depth_before <= params.depth_max && final_costs[min_cost_idx] < cost_now) {
depth_now = depth_before;
plane_hypotheses[center] = plane_hypotheses[positions[min_cost_idx]];
costs[center] = final_costs[min_cost_idx];
}
}
}