-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathSIFTImageManager.cu
1162 lines (921 loc) · 42.7 KB
/
SIFTImageManager.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 "SIFTImageManager.h"
#include "cudaUtil.h"
#include "CUDATimer.h"
#include "cuda_kabsch.h"
#include "cuda_svd3.h"
#include "cuda_surfaceArea.h"
#define SORT_NUM_BLOCK_THREADS_X (MAX_MATCHES_PER_IMAGE_PAIR_RAW / 2)
int CheckErrorCUDA(const char* location)
{
#if (defined(_DEBUG) || defined(DEBUG))
cudaDeviceSynchronize();
cudaError_t e = cudaGetLastError();
if (e)
{
if (location) fprintf(stderr, "%s:\t", location);
fprintf(stderr, "%s\n", cudaGetErrorString(e));
assert(0);
return 1;
}
else
{
return 0;
}
#else
return 0;
#endif
}
__device__ bool cmpAndSawp(
volatile float* dist0,
volatile uint2* idx0,
volatile float* dist1,
volatile uint2* idx1
)
{
if (dist0[0] > dist1[0]) {
float tmpDist = dist0[0];
dist0[0] = dist1[0];
dist1[0] = tmpDist;
const unsigned int tmpIdxX = idx0[0].x;
idx0[0].x = idx1[0].x;
idx1[0].x = tmpIdxX;
const unsigned int tmpIdxY = idx0[0].y;
idx0[0].y = idx1[0].y;
idx1[0].y = tmpIdxY;
return true;
}
else {
return false;
}
}
//we launch 1 thread for two array entries
void __global__ SortKeyPointMatchesCU_Kernel(
unsigned int curFrame,
unsigned int startFrameOffset,
const int* d_numMatchesPerImagePair,
float* d_matchDistancesGlobal,
uint2* d_matchKeyPointIndicesGlobal)
{
unsigned int imagePairIdx = blockIdx.x + startFrameOffset;
if (imagePairIdx == curFrame) return;
unsigned int tidx = threadIdx.x;
float* d_matchDistances = &d_matchDistancesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_RAW];
uint2* d_matchKeyPointIndices = &d_matchKeyPointIndicesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_RAW];
const unsigned int numMatches = min(MAX_MATCHES_PER_IMAGE_PAIR_RAW, d_numMatchesPerImagePair[imagePairIdx]);
if (numMatches == 0) return;
__shared__ float matchDistances[MAX_MATCHES_PER_IMAGE_PAIR_RAW];
__shared__ uint2 matchKeyPointIndices[MAX_MATCHES_PER_IMAGE_PAIR_RAW];
if (2 * tidx < numMatches) {
matchDistances[2 * tidx + 0] = d_matchDistances[2 * tidx + 0];
matchKeyPointIndices[2 * tidx + 0] = d_matchKeyPointIndices[2 * tidx + 0];
if (2 * tidx + 1 < numMatches) {
matchDistances[2 * tidx + 1] = d_matchDistances[2 * tidx + 1];
matchKeyPointIndices[2 * tidx + 1] = d_matchKeyPointIndices[2 * tidx + 1];
}
else {
matchDistances[2 * tidx + 1] = 999.0f;
matchKeyPointIndices[2 * tidx + 1] = make_uint2((unsigned int)-1, (unsigned int)-1);
}
}
else {
matchDistances[2 * tidx + 0] = 999.0f;
matchKeyPointIndices[2 * tidx + 0] = make_uint2((unsigned int)-1, (unsigned int)-1);
matchDistances[2 * tidx + 1] = 999.0f;
matchKeyPointIndices[2 * tidx + 1] = make_uint2((unsigned int)-1, (unsigned int)-1);
}
#if !(SORT_NUM_BLOCK_THREADS_X == 32)
__syncthreads();
#endif
__shared__ bool swapped;
swapped = true;
unsigned int run = 0;
//at least one odd phase is required
while (swapped || run < 2) {
swapped = false;
unsigned int idx0 = 2 * tidx + 0;
unsigned int idx1 = 2 * tidx + 1;
//odd phase
if (run & 0x1) {
idx0 += 1;
idx1 += 1;
}
bool res = false;
if (idx1 < MAX_MATCHES_PER_IMAGE_PAIR_RAW) {
res = cmpAndSawp(&matchDistances[idx0], &matchKeyPointIndices[idx0], &matchDistances[idx1], &matchKeyPointIndices[idx1]);
}
if (res) swapped = true;
run++;
#if !(SORT_NUM_BLOCK_THREADS_X == 32)
__syncthreads();
#endif
}
//write results back
if (2 * tidx < numMatches) {
d_matchDistances[2 * tidx + 0] = matchDistances[2 * tidx + 0];
d_matchKeyPointIndices[2 * tidx + 0] = matchKeyPointIndices[2 * tidx + 0];
if (2 * tidx + 1 < numMatches) {
d_matchDistances[2 * tidx + 1] = matchDistances[2 * tidx + 1];
d_matchKeyPointIndices[2 * tidx + 1] = matchKeyPointIndices[2 * tidx + 1];
}
}
}
void SIFTImageManager::SortKeyPointMatchesCU(unsigned int curFrame, unsigned int startFrame, unsigned int numFrames) {
if (numFrames == 0) return;
dim3 grid(numFrames - startFrame);
dim3 block(SORT_NUM_BLOCK_THREADS_X);
if (m_timer) m_timer->startEvent(__FUNCTION__);
SortKeyPointMatchesCU_Kernel << <grid, block >> >(curFrame, startFrame,
d_currNumMatchesPerImagePair, d_currMatchDistances, d_currMatchKeyPointIndices);
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
//std::vector<uint2> indices(numImagePairs * MAX_MATCHES_PER_IMAGE_PAIR_RAW, make_uint2((unsigned int)-1,(unsigned int)-1));
//std::vector<float> distances(numImagePairs * MAX_MATCHES_PER_IMAGE_PAIR_RAW, -1.0f);
//std::vector<unsigned int> numMatchesPerImagePair(numImagePairs * MAX_MATCHES_PER_IMAGE_PAIR_RAW, 0);
//cutilSafeCall(cudaMemcpy(distances.data(), d_matchDistances, sizeof(float) * numImagePairs * MAX_MATCHES_PER_IMAGE_PAIR_RAW, cudaMemcpyDeviceToHost));
//cutilSafeCall(cudaMemcpy(indices.data(), d_matchKeyPointIndices, sizeof(uint2) * numImagePairs * MAX_MATCHES_PER_IMAGE_PAIR_RAW, cudaMemcpyDeviceToHost));
//cutilSafeCall(cudaMemcpy(numMatchesPerImagePair.data(), d_numMatchesPerImagePair, sizeof(unsigned int) * MAX_MATCHES_PER_IMAGE_PAIR_RAW, cudaMemcpyDeviceToHost));
//std::cout << "\n\nafter:\n";
//for (size_t i = 0; i < numMatchesPerImagePair.size(); i++) {
// unsigned int numMatches = numMatchesPerImagePair[i];
// unsigned int baseIdx = i * MAX_MATCHES_PER_IMAGE_PAIR_RAW;
// for (size_t j = 0; j < numMatches; j++) {
// std::cout << distances[baseIdx + j] << " ";
// }
//}
//std::cout << std::endl << std::endl;
}
#define FILTER_NUM_BLOCK_THREADS_X MAX_MATCHES_PER_IMAGE_PAIR_RAW
//we launch 1 thread for two array entries
void __global__ FilterKeyPointMatchesCU_Kernel(
unsigned int curFrame,
unsigned int startFrame,
const SIFTKeyPoint* d_keyPointsGlobal,
const int* d_numMatchesPerImagePair,
const float* d_matchDistancesGlobal,
const uint2* d_matchKeyPointIndicesGlobal,
int* d_numFilteredMatchesPerImagePair,
float* d_filteredMatchDistancesGlobal,
uint2* d_filteredMatchKeyPointIndicesGlobal,
float4x4* d_filteredTransforms,
float4x4* d_filteredTransformsInv,
float4x4 siftIntrinsicsInv,
unsigned int minNumMatches,
float maxKabschRes2)
{
const unsigned int imagePairIdx = blockIdx.x + startFrame;
if (imagePairIdx == curFrame) return;
const unsigned int tidx = threadIdx.x;
const float* d_matchDistances = &d_matchDistancesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_RAW];
const uint2* d_matchKeyPointIndices = &d_matchKeyPointIndicesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_RAW];
unsigned int numMatches = min(MAX_MATCHES_PER_IMAGE_PAIR_RAW, d_numMatchesPerImagePair[imagePairIdx]);
if (numMatches == 0) {
if (tidx == 0) {
d_numFilteredMatchesPerImagePair[imagePairIdx] = 0;
}
return;
}
__shared__ float matchDistances[MAX_MATCHES_PER_IMAGE_PAIR_RAW];
__shared__ uint2 matchKeyPointIndices[MAX_MATCHES_PER_IMAGE_PAIR_RAW];
if (tidx < numMatches) {
matchDistances[tidx] = d_matchDistances[tidx];
matchKeyPointIndices[tidx] = d_matchKeyPointIndices[tidx];
}
else {
matchDistances[tidx] = 999.0f;
matchKeyPointIndices[tidx] = make_uint2((unsigned int)-1, (unsigned int)-1);
}
#if !(FILTER_NUM_BLOCK_THREADS_X == 32)
__syncthreads();
#endif
__shared__ unsigned int numFilteredMatches;
if (tidx == 0) {
float4x4 trans;
unsigned int curr = filterKeyPointMatches(d_keyPointsGlobal, matchKeyPointIndices, matchDistances, numMatches,
trans, siftIntrinsicsInv, minNumMatches, maxKabschRes2);//, (imagePairIdx == 63 && curFrame == 76));
numFilteredMatches = curr;
d_filteredTransforms[imagePairIdx] = trans;
d_filteredTransformsInv[imagePairIdx] = trans.getInverse();
}
#if !(FILTER_NUM_BLOCK_THREADS_X == 32)
__syncthreads();
#endif
//write results back
if (tidx == 0) {
d_numFilteredMatchesPerImagePair[imagePairIdx] = numFilteredMatches;
}
if (tidx < numFilteredMatches) {
d_filteredMatchDistancesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_FILTERED + tidx] = matchDistances[tidx];
d_filteredMatchKeyPointIndicesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_FILTERED + tidx] = matchKeyPointIndices[tidx];
}
else if (tidx < MAX_MATCHES_PER_IMAGE_PAIR_FILTERED) {
d_filteredMatchDistancesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_FILTERED + tidx] = 999.0f;
d_filteredMatchKeyPointIndicesGlobal[imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_FILTERED + tidx] = make_uint2((unsigned int)-1, (unsigned int)-1);
}
}
void SIFTImageManager::FilterKeyPointMatchesCU(unsigned int curFrame, unsigned int startFrame, unsigned int numFrames, const float4x4& siftIntrinsicsInv, unsigned int minNumMatches, float maxKabschRes2) {
if (numFrames == 0) return;
dim3 grid(numFrames - startFrame);
dim3 block(FILTER_NUM_BLOCK_THREADS_X);
if (m_timer) m_timer->startEvent(__FUNCTION__);
FilterKeyPointMatchesCU_Kernel << <grid, block >> >(
curFrame,
startFrame,
d_keyPoints,
d_currNumMatchesPerImagePair,
d_currMatchDistances,
d_currMatchKeyPointIndices,
d_currNumFilteredMatchesPerImagePair,
d_currFilteredMatchDistances,
d_currFilteredMatchKeyPointIndices,
d_currFilteredTransforms,
d_currFilteredTransformsInv,
siftIntrinsicsInv,
minNumMatches,
maxKabschRes2);
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
//DEBUG
//{
// std::vector<int> numMatches(numCurrImagePairs);
// std::vector<float> matchDistancesGlob(MAX_MATCHES_PER_IMAGE_PAIR_FILTERED*numCurrImagePairs);
// std::vector<float4x4> transforms(numCurrImagePairs);
// cutilSafeCall(cudaMemcpy(numMatches.data(), d_currNumFilteredMatchesPerImagePair, sizeof(int)*numCurrImagePairs, cudaMemcpyDeviceToHost));
// cutilSafeCall(cudaMemcpy(matchDistancesGlob.data(), d_currFilteredMatchDistances, sizeof(float)*numCurrImagePairs*MAX_MATCHES_PER_IMAGE_PAIR_FILTERED, cudaMemcpyDeviceToHost));
// cutilSafeCall(cudaMemcpy(transforms.data(), d_currFilteredTransforms, sizeof(float4x4)*numCurrImagePairs, cudaMemcpyDeviceToHost));
// for (unsigned int i = 0; i < numCurrImagePairs; i++) {
// unsigned int newNumMatches = numMatches[i];
// float checkSum = 0.0f;
// for (unsigned int k = 0; k < newNumMatches; k++) {
// checkSum += matchDistancesGlob[MAX_MATCHES_PER_IMAGE_PAIR_FILTERED*i + k];
// }
// std::cout << "checkSum: " << checkSum << std::endl;
// }
// for (unsigned int i = 0; i < numCurrImagePairs; i++) {
// transforms[i].print();
// }
//}
}
//we launch 1 thread for two array entries
void __global__ FilterMatchesBySurfaceAreaCU_Kernel(
unsigned int curFrame,
unsigned int startFrame,
const SIFTKeyPoint* d_keyPointsGlobal,
int* d_numFilteredMatchesPerImagePair,
const uint2* d_filteredMatchKeyPointIndicesGlobal,
const float4x4 colorIntrinsicsInv,
float areaThresh)
{
const unsigned int imagePairIdx = blockIdx.x + startFrame;
if (imagePairIdx == curFrame) return;
const unsigned int numMatches = d_numFilteredMatchesPerImagePair[imagePairIdx];
if (numMatches == 0) return;
const uint2* d_keyPointMatchIndices = d_filteredMatchKeyPointIndicesGlobal + imagePairIdx * MAX_MATCHES_PER_IMAGE_PAIR_FILTERED;
float area0 = 0.0f;
float area1 = 0.0f;
// compute area image 0
unsigned int which = 0;
computeKeyPointMatchesCovariance(d_keyPointsGlobal, d_keyPointMatchIndices, numMatches, colorIntrinsicsInv, which);
float3 evs, ev0, ev1, ev2;
bool res;
res = MYEIGEN::eigenSystem(V, evs, ev0, ev1, ev2);
__shared__ float2 pointsProj[MAX_MATCHES_PER_IMAGE_PAIR_FILTERED];
if (res) { // project
projectKeysToPlane(pointsProj, d_keyPointsGlobal, d_keyPointMatchIndices, numMatches, colorIntrinsicsInv, which, ev0, ev1, ev2, mean);
area0 = computeAreaOrientedBoundingBox2(pointsProj, numMatches);
}
////if (threadIdx.x == 0)
//{
// float3x3 V_ = V;
// float3 mean_ = mean;
// __syncthreads();
// printf("matches: %d\n", numMatches);
// V_.print();
// printf("mean: [%f %f %f]\n", mean_.x, mean_.y, mean_.z);
// printf("evs [%f | %f | %f]\n", evs.x, evs.y, evs.z);
// printf("ev0 [%f | %f | %f]\n", ev0.x, ev0.y, ev0.z);
// printf("ev1 [%f | %f | %f]\n", ev1.x, ev1.y, ev1.z);
// printf("ev2 [%f | %f | %f]\n", ev2.x, ev2.y, ev2.z);
// printf("res %d\n", (int)res);
// printf("area0: %f\n", area0);
//}
//if (threadIdx.x == 0) {
// colorIntrinsicsInv.print();
// for (unsigned int i = 0; i < numMatches; i++) {
// printf("pointsProj[%d] = [%f | %f] \t\t idx: [%d | %d]\n", i, pointsProj[i].x, pointsProj[i].y, d_keyPointMatchIndices[i].x, d_keyPointMatchIndices[i].y);
// }
//}
// compute area image 1
which = 1;
computeKeyPointMatchesCovariance(d_keyPointsGlobal, d_keyPointMatchIndices, numMatches, colorIntrinsicsInv, which);
res = MYEIGEN::eigenSystem(V, evs, ev0, ev1, ev2);
if (res) {// project
projectKeysToPlane(pointsProj, d_keyPointsGlobal, d_keyPointMatchIndices, numMatches, colorIntrinsicsInv, which, ev0, ev1, ev2, mean);
area1 = computeAreaOrientedBoundingBox2(pointsProj, numMatches);
}
if (threadIdx.x == 0) {
if (area0 < areaThresh && area1 < areaThresh) {
//printf("INVALID AREA [%d %d] (%f %f)\n", imagePairIdx, gridDim.x, area0, area1);
d_numFilteredMatchesPerImagePair[imagePairIdx] = 0;
}
}
}
void SIFTImageManager::FilterMatchesBySurfaceAreaCU(unsigned int curFrame, unsigned int startFrame, unsigned int numFrames, const float4x4& colorIntrinsicsInv, float areaThresh) {
if (numFrames == 0) return;
dim3 grid(numFrames - startFrame);
const unsigned int threadsPerBlock = ((MAX_MATCHES_PER_IMAGE_PAIR_FILTERED + 31) / 32) * 32;
dim3 block(threadsPerBlock);
if (m_timer) m_timer->startEvent(__FUNCTION__);
FilterMatchesBySurfaceAreaCU_Kernel << <grid, block >> >(
curFrame,
startFrame,
d_keyPoints,
d_currNumFilteredMatchesPerImagePair,
d_currFilteredMatchKeyPointIndices,
colorIntrinsicsInv,
areaThresh);
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
}
#define FILTER_DENSE_VERIFY_THREAD_SPLIT 32
#ifdef CUDACACHE_FLOAT_NORMALS
__device__ float3 computeProjError(unsigned int idx, unsigned int imageWidth, unsigned int imageHeight,
float distThresh, float normalThresh, float colorThresh, const float4x4& transform, const float4x4& intrinsics,
const float* d_inputDepth, const float4* d_inputCamPos, const float4* d_inputNormal, const float* d_inputColor,
const float* d_modelDepth, const float4* d_modelCamPos, const float4* d_modelNormal, const float* d_modelColor,
float sensorDepthMin, float sensorDepthMax)
#elif defined(CUDACACHE_UCHAR_NORMALS)
__device__ float3 computeProjError(unsigned int idx, unsigned int imageWidth, unsigned int imageHeight,
float distThresh, float normalThresh, float colorThresh, const float4x4& transform, const float4x4& intrinsics,
const float* d_inputDepth, const float4* d_inputCamPos, const uchar4* d_inputNormal, const float* d_inputColor,
const float* d_modelDepth, const float4* d_modelCamPos, const uchar4* d_modelNormal, const float* d_modelColor,
float sensorDepthMin, float sensorDepthMax)
#endif
{
float3 out = make_float3(0.0f);
float4 pInput = d_inputCamPos[idx]; // point
#ifdef CUDACACHE_FLOAT_NORMALS
float4 nInput = d_inputNormal[idx]; nInput.w = 0.0f; // vector
#else
float4 nInput = make_float4(MINF);
uchar4 nInputU4 = d_inputNormal[idx];
if (*(int*)(&nInputU4) != 0) nInput = make_float4(make_float3(nInputU4.x, nInputU4.y, nInputU4.z) / 255.0f * 2.0f - 1.0f, 0.0f); // vector
#endif
float dInput = d_inputDepth[idx];
//float cInput = d_inputColor[idx];
if (pInput.x != MINF && nInput.x != MINF && dInput >= sensorDepthMin && dInput <= sensorDepthMax) {
const float4 pTransInput = transform * pInput;
const float4 nTransInput = transform * nInput;
float3 tmp = intrinsics * make_float3(pTransInput.x, pTransInput.y, pTransInput.z);
const int2 screenPos = make_int2((int)roundf(tmp.x / tmp.z), (int)roundf(tmp.y / tmp.z)); // subsampled space
if (screenPos.x >= 0 && screenPos.y >= 0 && screenPos.x < (int)imageWidth && screenPos.y < (int)imageHeight) {
float4 pTarget = d_modelCamPos[screenPos.y * imageWidth + screenPos.x]; //getBestCorrespondence1x1
//float cTarget = d_modelColor[screenPos.y * imageWidth + screenPos.x];
#ifdef CUDACACHE_FLOAT_NORMALS
float4 nTarget = d_modelNormal[screenPos.y * imageWidth + screenPos.x];
#else
float4 nTarget = make_float4(MINF);
uchar4 nTargetU4 = d_modelNormal[idx];
if (*(int*)(&nTargetU4) != 0) nTarget = make_float4(make_float3(nTargetU4.x, nTargetU4.y, nTargetU4.z) / 255.0f * 2.0f - 1.0f, 0.0f); // vector
#endif
if (pTarget.x != MINF && nTarget.x != MINF) {
float d = length(pTransInput - pTarget);
float dNormal = dot(make_float3(nTransInput.x, nTransInput.y, nTransInput.z), make_float3(nTarget.x, nTarget.y, nTarget.z)); // should be able to do dot(nTransInput, nTarget)
//float c = length(cInput - cTarget);
//float projInputDepth = (intrinsics * make_float3(pTransInput.x, pTransInput.y, pTransInput.z)).z;
float projInputDepth = pTransInput.z;
float tgtDepth = d_modelDepth[screenPos.y * imageWidth + screenPos.x];
if (tgtDepth >= sensorDepthMin && tgtDepth <= sensorDepthMax) {
bool b = ((tgtDepth != MINF && projInputDepth < tgtDepth) && d > distThresh); // bad matches that are known
if ((dNormal >= normalThresh && d <= distThresh /*&& c <= colorThresh*/) || b) { // if normal/pos/color correspond or known bad match
const float cameraToKinectProjZ = (pTransInput.z - sensorDepthMin) / (sensorDepthMax - sensorDepthMin);
const float weight = max(0.0f, 0.5f*((1.0f - d / distThresh) + (1.0f - cameraToKinectProjZ))); // for weighted ICP;
out.x = d; //residual
out.y = weight; //corr weight
out.z = 1.0f;
}
} // target depth within sensor min/max
} // projected to valid depth
} // inside image
}
return out;
}
//we launch 1 thread for two array entries
void __global__ FilterMatchesByDenseVerifyCU_Kernel(unsigned int curImageIdx, unsigned int startFrame, unsigned int imageWidth, unsigned int imageHeight, const float4x4 intrinsics,
int* d_currNumFilteredMatchesPerImagePair, const float4x4* d_currFilteredTransforms, const float4x4* d_currFilteredTransformsInv, const CUDACachedFrame* d_cachedFrames,
float distThresh, float normalThresh, float colorThresh, float errThresh, float corrThresh, float sensorDepthMin, float sensorDepthMax)
{
const unsigned int imagePairIdx = blockIdx.x + startFrame; // prev image idx
if (imagePairIdx == curImageIdx) return;
const unsigned int numMatches = d_currNumFilteredMatchesPerImagePair[imagePairIdx];
if (numMatches == 0) {
//if (threadIdx.x == 0 && threadIdx.y == 0) printf("no matches between %d, %d\n", imagePairIdx, curImageIdx);
return;
}
const float* d_inputDepth = d_cachedFrames[imagePairIdx].d_depthDownsampled;
const float4* d_inputCamPos = d_cachedFrames[imagePairIdx].d_cameraposDownsampled;
const float* d_inputColor = d_cachedFrames[imagePairIdx].d_intensityDownsampled;
const float* d_modelDepth = d_cachedFrames[curImageIdx].d_depthDownsampled;
const float4* d_modelCamPos = d_cachedFrames[curImageIdx].d_cameraposDownsampled;
const float* d_modelColor = d_cachedFrames[curImageIdx].d_intensityDownsampled;
//TODO HERE ANGIE
#ifdef CUDACACHE_FLOAT_NORMALS
const float4* d_inputNormal = d_cachedFrames[imagePairIdx].d_normalsDownsampled;
const float4* d_modelNormal = d_cachedFrames[curImageIdx].d_normalsDownsampled;
#elif defined(CUDACACHE_UCHAR_NORMALS)
const uchar4* d_inputNormal = d_cachedFrames[imagePairIdx].d_normalsDownsampledUCHAR4;
const uchar4* d_modelNormal = d_cachedFrames[curImageIdx].d_normalsDownsampledUCHAR4;
#endif
const float4x4 transform = d_currFilteredTransforms[imagePairIdx];
float local_sumResidual = 0.0f;
float local_sumWeight = 0.0f;
float local_numCorr = 0.0f;
for (unsigned int i = 0; i < FILTER_DENSE_VERIFY_THREAD_SPLIT; i++) {
const unsigned int idxX = threadIdx.x;
const unsigned int idxY = threadIdx.y*FILTER_DENSE_VERIFY_THREAD_SPLIT + i;
if (idxY < imageHeight) {
const unsigned int idx = idxY * imageWidth + idxX;
float3 inputToModel = computeProjError(idx, imageWidth, imageHeight, distThresh, normalThresh, colorThresh, transform, intrinsics,
d_inputDepth, d_inputCamPos, d_inputNormal, d_inputColor,
d_modelDepth, d_modelCamPos, d_modelNormal, d_modelColor, sensorDepthMin, sensorDepthMax);
float3 modelToInput = computeProjError(idx, imageWidth, imageHeight, distThresh, normalThresh, colorThresh, transform.getInverse(), intrinsics,
d_modelDepth, d_modelCamPos, d_modelNormal, d_modelColor,
d_inputDepth, d_inputCamPos, d_inputNormal, d_inputColor, sensorDepthMin, sensorDepthMax);
local_sumResidual += inputToModel.x + modelToInput.x; //residual
local_sumWeight += inputToModel.y + modelToInput.y; //corr weight
local_numCorr += inputToModel.z + modelToInput.z; //corr number
}
}
__shared__ float sumResidual;
__shared__ float sumWeight;
__shared__ float numCorr;
if (threadIdx.x == 0 && threadIdx.y == 0) {
sumResidual = 0.0f;
sumWeight = 0.0f;
numCorr = 0;
}
__syncthreads();
//atomicAdd(&sumResidual, local_sumResidual);
//atomicAdd(&sumWeight, local_sumWeight);
//atomicAdd(&numCorr, local_numCorr);
local_sumResidual = warpReduceSum(local_sumResidual);
local_sumWeight = warpReduceSum(local_sumWeight);
local_numCorr = warpReduceSum(local_numCorr);
if (threadIdx.x % warpSize == 0) {
atomicAdd(&sumResidual, local_sumResidual);
atomicAdd(&sumWeight, local_sumWeight);
atomicAdd(&numCorr, local_numCorr);
}
__syncthreads();
//write results back
if (threadIdx.x == 0 && threadIdx.y == 0) {
float err = sumResidual / sumWeight;
float corr = 0.5f * numCorr / (float)(imageWidth * imageHeight);
//debugging
//bool debugPrint = imagePairIdx == 63 && curImageIdx == 76;
if (corr < corrThresh || err > errThresh || isnan(err)) { // invalid!
//if (debugPrint) printf("[%d-%d]: %f %f INVALID\n", imagePairIdx, curImageIdx, err, corr);
d_currNumFilteredMatchesPerImagePair[imagePairIdx] = 0;
}
//else if (debugPrint) printf("[%d-%d]: %f %f\n", imagePairIdx, curImageIdx, err, corr);
}
}
void SIFTImageManager::FilterMatchesByDenseVerifyCU(unsigned int curFrame, unsigned int startFrame, unsigned int numFrames, unsigned int imageWidth, unsigned int imageHeight,
const float4x4& intrinsics, const CUDACachedFrame* d_cachedFrames,
float distThresh, float normalThresh, float colorThresh, float errThresh, float corrThresh, float sensorDepthMin, float sensorDepthMax)
{
if (numFrames == 0) return;
dim3 grid(numFrames - startFrame);
dim3 block(imageWidth, (imageHeight + FILTER_DENSE_VERIFY_THREAD_SPLIT - 1) / FILTER_DENSE_VERIFY_THREAD_SPLIT);
if (m_timer) m_timer->startEvent(__FUNCTION__);
FilterMatchesByDenseVerifyCU_Kernel << <grid, block >> >(
curFrame, startFrame, imageWidth, imageHeight, intrinsics,
d_currNumFilteredMatchesPerImagePair, d_currFilteredTransforms, d_currFilteredTransformsInv, d_cachedFrames,
distThresh, normalThresh, colorThresh, errThresh, corrThresh,
sensorDepthMin, sensorDepthMax);
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
}
void __global__ AddCurrToResidualsCU_Kernel(
unsigned int curFrame,
unsigned int startFrame,
EntryJ* d_globMatches,
uint2* d_globMatchesKeyPointIndices,
int* d_globNumImagePairs,
const int* d_currNumFilteredMatchesPerImagePair,
const uint2* d_currFilteredMatchKeyPointIndices,
const SIFTKeyPoint* d_keyPoints,
const unsigned int maxKeyPointsPerImage,
const float4x4 colorIntrinsicsInv
)
{
const unsigned int imagePairIdx = blockIdx.x + startFrame;
if (imagePairIdx == curFrame) return;
const unsigned int tidx = threadIdx.x;
const unsigned int numMatches = d_currNumFilteredMatchesPerImagePair[imagePairIdx];
__shared__ unsigned int basePtr;
if (tidx == 0) {
basePtr = atomicAdd(&d_globNumImagePairs[0], numMatches);
}
__syncthreads();
//if (tidx == 0) {
// printf("[%d] baseAddr=%d\n", imagePairIdx, basePtr);
//}
if (tidx < numMatches) {
const unsigned int srcAddr = imagePairIdx*MAX_MATCHES_PER_IMAGE_PAIR_FILTERED + tidx;
uint2 currFilteredMachtKeyPointIndices = d_currFilteredMatchKeyPointIndices[srcAddr];
//printf("[%d] = [%d %d]\n", imagePairIdx, currFilteredMachtKeyPointIndices.x, currFilteredMachtKeyPointIndices.y);
const SIFTKeyPoint& k_i = d_keyPoints[currFilteredMachtKeyPointIndices.x];
const SIFTKeyPoint& k_j = d_keyPoints[currFilteredMachtKeyPointIndices.y];
EntryJ e;
const unsigned int imageIdx0 = imagePairIdx;
const unsigned int imageIdx1 = curFrame;
e.imgIdx_i = imageIdx0;
e.imgIdx_j = imageIdx1;
e.pos_i = colorIntrinsicsInv * (k_i.depth * make_float3(k_i.pos.x, k_i.pos.y, 1.0f));
e.pos_j = colorIntrinsicsInv * (k_j.depth * make_float3(k_j.pos.x, k_j.pos.y, 1.0f));
d_globMatches[basePtr + tidx] = e;
d_globMatchesKeyPointIndices[basePtr + tidx] = currFilteredMachtKeyPointIndices;
}
}
void SIFTImageManager::AddCurrToResidualsCU(unsigned int curFrame, unsigned int startFrame, unsigned int numFrames, const float4x4& colorIntrinsicsInv) {
if (numFrames == 0) return;
dim3 grid(numFrames - startFrame);
const unsigned int threadsPerBlock = ((MAX_MATCHES_PER_IMAGE_PAIR_FILTERED + 31) / 32) * 32;
dim3 block(threadsPerBlock);
if (m_timer) m_timer->startEvent(__FUNCTION__);
AddCurrToResidualsCU_Kernel << <grid, block >> >(
curFrame,
startFrame,
d_globMatches,
d_globMatchesKeyPointIndices,
d_globNumResiduals,
d_currNumFilteredMatchesPerImagePair,
d_currFilteredMatchKeyPointIndices,
d_keyPoints,
m_maxKeyPointsPerImage,
colorIntrinsicsInv
);
cutilSafeCall(cudaMemcpy(&m_globNumResiduals, d_globNumResiduals, sizeof(unsigned int), cudaMemcpyDeviceToHost));
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
}
#define INVALIDATEIMAGE_TO_IMAGE_KERNEL_THREADS_X 128
void __global__ InvalidateImageToImageCU_Kernel(EntryJ* d_globMatches, unsigned int globNumResiduals, uint2 imageToImageIdx)
{
const unsigned int idx = blockDim.x*blockIdx.x + threadIdx.x;
if (idx < globNumResiduals) {
if (d_globMatches[idx].imgIdx_i == imageToImageIdx.x &&
d_globMatches[idx].imgIdx_j == imageToImageIdx.y) {
d_globMatches[idx].setInvalid();
}
}
}
void SIFTImageManager::InvalidateImageToImageCU(const uint2& imageToImageIdx) {
const unsigned int threadsPerBlock = INVALIDATEIMAGE_TO_IMAGE_KERNEL_THREADS_X;
dim3 grid((m_globNumResiduals + threadsPerBlock - 1) / threadsPerBlock);
dim3 block(threadsPerBlock);
if (m_timer) m_timer->startEvent(__FUNCTION__);
InvalidateImageToImageCU_Kernel << <grid, block >> >(d_globMatches, m_globNumResiduals, imageToImageIdx);
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
}
#define CHECK_FOR_INVALID_FRAMES_X 128
#define CHECK_FOR_INVALID_FRAMES_THREADS_X 16
void __global__ CheckForInvalidFramesCU_Kernel(const int* d_varToCorrNumEntriesPerRow, int* d_validImages, unsigned int numVars,
EntryJ* d_globMatches, unsigned int numGlobResiduals)
{
const unsigned int resIdx = blockDim.x*blockIdx.x + blockIdx.y;
const unsigned int varIdx = gridDim.x*threadIdx.x + threadIdx.y;
if (varIdx < numVars && resIdx < numGlobResiduals) {
if (d_varToCorrNumEntriesPerRow[varIdx] == 0) { // no connections!
if (d_globMatches[resIdx].isValid() && (d_globMatches[resIdx].imgIdx_i == varIdx || d_globMatches[resIdx].imgIdx_j == varIdx)) { // invalidate residuals
d_globMatches[resIdx].setInvalid();
}
if (d_validImages[varIdx] != 0) {
if (varIdx == 0) printf("ERROR ERROR INVALIDATING THE FIRST FRAME\n");
//printf("[CheckForInvalidFramesCU] invalidating frame %d\n", varIdx);
d_validImages[varIdx] = 0;
}
}
}
}
//TODO CHECK grid/block dim (too many threads?)
void SIFTImageManager::CheckForInvalidFramesCU(const int* d_varToCorrNumEntriesPerRow, unsigned int numVars)
{
dim3 block((m_globNumResiduals + CHECK_FOR_INVALID_FRAMES_X - 1) / CHECK_FOR_INVALID_FRAMES_X, CHECK_FOR_INVALID_FRAMES_X);
dim3 threadsPerBlock((numVars + CHECK_FOR_INVALID_FRAMES_THREADS_X - 1) / CHECK_FOR_INVALID_FRAMES_THREADS_X, CHECK_FOR_INVALID_FRAMES_THREADS_X);
if (m_timer) m_timer->startEvent(__FUNCTION__);
cutilSafeCall(cudaMemcpy(d_validImages, m_validImages.data(), sizeof(int) * numVars, cudaMemcpyHostToDevice));
CheckForInvalidFramesCU_Kernel << <block, threadsPerBlock >> >(d_varToCorrNumEntriesPerRow, d_validImages, numVars, d_globMatches, m_globNumResiduals);
cutilSafeCall(cudaMemcpy(m_validImages.data(), d_validImages, sizeof(int) * numVars, cudaMemcpyDeviceToHost));
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
}
void __global__ CheckForInvalidFramesSimpleCU_Kernel(const int* d_varToCorrNumEntriesPerRow, int* d_validImages, unsigned int numVars)
{
const unsigned int idx = blockDim.x*blockIdx.x + threadIdx.x;
if (idx < numVars) {
if (d_varToCorrNumEntriesPerRow[idx] == 0) { // no connections!
//printf("[CheckForInvalidFramesCU] invalidating frame %d\n", idx);
d_validImages[idx] = 0;
}
}
}
//TODO CHECK grid/block dim (too many threads?)
void SIFTImageManager::CheckForInvalidFramesSimpleCU(const int* d_varToCorrNumEntriesPerRow, unsigned int numVars)
{
const unsigned int threadsPerBlock = CHECK_FOR_INVALID_FRAMES_THREADS_X;
dim3 grid((numVars + threadsPerBlock - 1) / threadsPerBlock);
dim3 block(threadsPerBlock);
if (m_timer) m_timer->startEvent(__FUNCTION__);
cutilSafeCall(cudaMemcpy(d_validImages, m_validImages.data(), sizeof(int) * numVars, cudaMemcpyHostToDevice));
CheckForInvalidFramesSimpleCU_Kernel << <grid, block >> >(d_varToCorrNumEntriesPerRow, d_validImages, numVars);
cutilSafeCall(cudaMemcpy(m_validImages.data(), d_validImages, sizeof(int) * numVars, cudaMemcpyDeviceToHost));
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
}
/*
#define MARK_FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X 128
#define FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X 512
void __global__ FuseToGlobalKeyCU_Kernel(unsigned int maxNumKeysAll, int* d_fuseGlobalKeyMarker,
const SIFTKeyPoint* d_allLocalKeyPoints, const SIFTKeyPointDesc* d_allLocalKeyPointDescs,
SIFTKeyPoint* d_curGlobalKeyPoints, SIFTKeyPointDesc* d_curGlobalKeyPointsDescs,
const float4x4* transforms, float4x4 colorIntrinsics, float4x4 colorIntrinsicsInv, int* d_fuseGlobalKeyCount,
unsigned int maxNumKeysPerImage)
{
const unsigned int idx = blockDim.x*blockIdx.x + threadIdx.x;
if (idx < maxNumKeysAll) {
if (d_fuseGlobalKeyMarker[idx] > 0) {
int addr = atomicAdd(d_fuseGlobalKeyCount, 1);
if (addr < maxNumKeysPerImage) {
const unsigned int imgIdx = d_fuseGlobalKeyMarker[idx] - 1;
const SIFTKeyPoint key = d_allLocalKeyPoints[idx];
float3 pos = colorIntrinsicsInv * (key.depth * make_float3(key.pos.x, key.pos.y, 1.0f));
float3 projPos = colorIntrinsics * (transforms[imgIdx] * pos);
float2 loc = make_float2(projPos.x / projPos.z, projPos.y / projPos.z);
SIFTKeyPoint newKey;
newKey.pos = loc;
newKey.scale = key.scale;
newKey.depth = projPos.z;
d_curGlobalKeyPoints[addr] = newKey;
d_curGlobalKeyPointsDescs[addr] = d_allLocalKeyPointDescs[idx];
}
// unmark for next time
d_fuseGlobalKeyMarker[idx] = 0;
} // marked
}
}
void __global__ MarkKeysToFuseToGlobalKeyCU_Kernel(unsigned int globNumResiduals, const EntryJ* d_correspondences, uint2* d_correspondenceKeyIndices,
int* d_fuseGlobalKeyMarker)
{
const unsigned int idx = blockDim.x*blockIdx.x + threadIdx.x;
if (idx < globNumResiduals) {
const EntryJ& corr = d_correspondences[idx];
if (corr.isValid()) {
const uint2 keyIndices = d_correspondenceKeyIndices[idx];
d_fuseGlobalKeyMarker[keyIndices.x] = corr.imgIdx_i + 1; // just pick the first one (offset by 1 since 0 invalid)
} // valid corr
} // residual/correspondence
}
unsigned int SIFTImageManager::FuseToGlobalKeyCU(SIFTImageGPU& globalImage, const float4x4* transforms,
const float4x4& colorIntrinsics, const float4x4& colorIntrinsicsInv)
{
dim3 gridMark((m_globNumResiduals + MARK_FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X - 1) / MARK_FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X);
dim3 blockMark(MARK_FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X);
if (m_timer) m_timer->startEvent(__FUNCTION__);
MarkKeysToFuseToGlobalKeyCU_Kernel << <gridMark, blockMark >> >(m_globNumResiduals, d_globMatches,
d_globMatchesKeyPointIndices, d_fuseGlobalKeyMarker);
CheckErrorCUDA(__FUNCTION__);
const unsigned int maxNumKeysAll = m_submapSize * m_maxKeyPointsPerImage;
dim3 gridFuse((maxNumKeysAll + FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X - 1) / FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X);
dim3 blockFuse(FUSE_TO_GLOBAL_KEY_KERNEL_THREADS_X);
cutilSafeCall(cudaMemset(d_fuseGlobalKeyCount, 0, sizeof(int)));
FuseToGlobalKeyCU_Kernel << <gridFuse, blockFuse >> >(maxNumKeysAll, d_fuseGlobalKeyMarker,
d_keyPoints, d_keyPointDescs, globalImage.d_keyPoints, globalImage.d_keyPointDescs,
transforms, colorIntrinsics, colorIntrinsicsInv, d_fuseGlobalKeyCount, m_maxKeyPointsPerImage);
CheckErrorCUDA(__FUNCTION__);
unsigned int numKeys;
cutilSafeCall(cudaMemcpy(&numKeys, d_fuseGlobalKeyCount, sizeof(int), cudaMemcpyDeviceToHost));
if (numKeys > m_maxKeyPointsPerImage) numKeys = m_maxKeyPointsPerImage;
if (m_timer) m_timer->endEvent();
CheckErrorCUDA(__FUNCTION__);
return numKeys;
}
*/
/*
__global__ void getSiftTransformCU_Kernel(unsigned int curFrameIndex,
const float4x4* d_completeTrajectory, unsigned int lastValidCompleteTransform,
float4x4* d_siftTrajectory, unsigned int curFrameIndexAll,
const int* d_numFilteredMatchesPerImagePair,
const float4x4* d_filteredTransformsInv, float4x4* d_currIntegrateTrans)
{
for (int i = (int)curFrameIndex - 1; i >= 0; i--) {
if (d_numFilteredMatchesPerImagePair[i] > 0) {
float4x4 transform;
const unsigned int idxPrevSiftKnown = curFrameIndexAll - (curFrameIndex - i);
d_siftTrajectory[curFrameIndexAll] = d_siftTrajectory[idxPrevSiftKnown] * d_filteredTransformsInv[i];
if (lastValidCompleteTransform == 0) {
transform = d_siftTrajectory[curFrameIndexAll];
}
else if (idxPrevSiftKnown < lastValidCompleteTransform) {
transform = d_completeTrajectory[idxPrevSiftKnown] * d_filteredTransformsInv[i];
}
else {
const float4x4 offset = d_siftTrajectory[lastValidCompleteTransform].getInverse() * d_siftTrajectory[idxPrevSiftKnown];
transform = d_completeTrajectory[lastValidCompleteTransform] * offset * d_filteredTransformsInv[i];
}
//!!!debugging
//if (d_siftTrajectory[curFrameIndexAll][0] == MINF) printf("INVALID AT FRAME %d,%d (last valid %d)\n", curFrameIndex, curFrameIndexAll, lastValidCompleteTransform);
//{
// printf("INVALID AT FRAME %d,%d (last valid %d)\n", curFrameIndex, curFrameIndexAll, lastValidCompleteTransform);
// printf("(%d, %d) -> prev sift known %d, %d, last valid %d\n", curFrameIndexAll, curFrameIndex,
// idxPrevSiftKnown, i, lastValidCompleteTransform);
// printf("sift at prev known:\n");
// d_siftTrajectory[idxPrevSiftKnown].print();
// printf("sift at last valid:\n");
// d_siftTrajectory[lastValidCompleteTransform].print();
// printf("complete at last valid:\n");
// d_completeTrajectory[lastValidCompleteTransform].print();
// printf("delta:\n");
// d_filteredTransformsInv[i].print();
// printf("transform:\n");
// transform.print();
//}
//!!!debugging
d_currIntegrateTrans[0] = transform;
break;
}
}
}
void SIFTImageManager::computeSiftTransformCU(const float4x4* d_completeTrajectory, unsigned int lastValidCompleteTransform,
float4x4* d_siftTrajectory, unsigned int curFrameIndexAll, unsigned int curFrameIndex, float4x4* d_currIntegrateTrans)
{
if (curFrameIndex == 0) return;
getSiftTransformCU_Kernel << <1, 1 >> >(curFrameIndex,
d_completeTrajectory, lastValidCompleteTransform,
d_siftTrajectory, curFrameIndexAll,
d_currNumFilteredMatchesPerImagePair, d_currFilteredTransformsInv,
d_currIntegrateTrans);
#ifdef _DEBUG
cutilSafeCall(cudaDeviceSynchronize());
cutilCheckMsg(__FUNCTION__);
#endif
}
*/
void __global__ TestSVDDebugCU_Kernel(float3x3* d_m, float3x3* d_u, float3x3* d_s, float3x3* d_v) {
float3x3 m = d_m[0];
//printf("InKernel\n");
//m.print();
float3x3 u, s, v;
s.setZero();
v.setZero();
//SVD::decompose3x3((float*)&m, (float*)&s, (float*)&v);
for (unsigned int i = 0; i < 100; i++) {
SVD::svd(m, u, s, v);
}
//svd(m, u, s, v);
d_u[0] = u;
d_s[0] = s;
d_v[0] = v;
//printf("\n");
//u.print();
//printf("\n");
//s.print();
//printf("\n");
//v.print();
//printf("\n");
//float3x3 res = u * s * v.getTranspose();
//res.print();
}
void SIFTImageManager::TestSVDDebugCU(const float3x3& m) {
dim3 grid(1);
dim3 block(1);
float3x3* d_m, *d_u, *d_s, *d_v;
cutilSafeCall(cudaMalloc(&d_m, sizeof(float3x3)));
cutilSafeCall(cudaMalloc(&d_u, sizeof(float3x3)));
cutilSafeCall(cudaMalloc(&d_s, sizeof(float3x3)));
cutilSafeCall(cudaMalloc(&d_v, sizeof(float3x3)));