-
Notifications
You must be signed in to change notification settings - Fork 4
/
RANSAC.cu
722 lines (594 loc) · 20.8 KB
/
RANSAC.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
#include "RANSAC.hpp"
#include <unistd.h>
#include <random>
#include <iomanip>
#include <algorithm>
#include <set>
#include <cmath>
#include "cutil_math.h"
#include <thrust/extrema.h>
__host__ __device__ __forceinline__
float accurateSqrt(float x)
{
return x * rsqrt(x);
}
inline __host__ __device__ float norm(float3 v)
{
return sqrtf(dot(v, v));
}
__host__ __device__ __forceinline__
float dist2(float x, float y, float z)
{
return x*x+y*y+z*z;
}
template <typename Vector>
void print_vector(const std::string& name, const Vector& v)
{
typedef typename Vector::value_type T;
std::cout << " " << std::setw(20) << name << " ";
thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << std::endl;
}
__host__ __device__ __forceinline__
void printMat3( float a11, float a12, float a13,
float a21, float a22, float a23,
float a31, float a32, float a33)
{
printf("%f %f %f \n", a11, a12, a13);
printf("%f %f %f \n", a21, a22, a23);
printf("%f %f %f \n", a31, a32, a33);
}
__global__ void computeRT(
const uint offset,
const float* vertices_source,
const float* vertices_target,
const uint* selection_source,
const uint* selection_target,
RT* candidate_solutions,
uint num_candidates)
{
uint thread = blockIdx.x *blockDim.x + threadIdx.x;
if(thread + offset >= num_candidates) return;
float3 vs[MODEL_SIZE];
float3 vt[MODEL_SIZE];
for (int i = 0; i < MODEL_SIZE; ++i)
{
vs[i] = make_float3(vertices_source[ selection_source[ (offset + thread) * MODEL_SIZE + i] * 3 + 0],
vertices_source[ selection_source[ (offset + thread) * MODEL_SIZE + i] * 3 + 1],
vertices_source[ selection_source[ (offset + thread) * MODEL_SIZE + i] * 3 + 2]);
vt[i] = make_float3(vertices_target[ selection_target[ (offset + thread) * MODEL_SIZE + i] * 3 + 0],
vertices_target[ selection_target[ (offset + thread) * MODEL_SIZE + i] * 3 + 1],
vertices_target[ selection_target[ (offset + thread) * MODEL_SIZE + i] * 3 + 2]);
}
float3 s_mean = make_float3(0,0,0);
float3 t_mean = make_float3(0,0,0);
for (int i = 0; i < MODEL_SIZE; ++i)
{
s_mean += vs[i];
t_mean += vt[i];
}
s_mean /= MODEL_SIZE;
t_mean /= MODEL_SIZE;
for (int i = 0; i < MODEL_SIZE; ++i)
{
vs[i] -= s_mean;
vt[i] -= t_mean;
}
float3 rs_2 = vs[2] - vs[0];
float3 rs_1 = vs[1] - vs[0];
float3 rt_2 = vt[2] - vt[0];
float3 rt_1 = vt[1] - vt[0];
float3 ns = cross(rs_2, rs_1);
float3 ns_n = normalize(ns);
float3 nt = cross(rt_2, rt_1);
float3 nt_n = normalize(nt);
float3 a = normalize(cross(ns,nt));
//phi is the out-of-plane rotation along the intersection between the two planes formed by the vertex triplets vs and vt
float sin_phi = norm(cross(ns_n, nt_n));
float cos_phi = dot(ns_n, nt_n);
float t = 1.0 - cos_phi;
float ra00 = cos_phi + a.x*a.x*t;
float ra11 = cos_phi + a.y*a.y*t;
float ra22 = cos_phi + a.z*a.z*t;
float tmp1 = a.x*a.y*t;
float tmp2 = a.z*sin_phi;
float ra10 = tmp1 + tmp2;
float ra01 = tmp1 - tmp2;
tmp1 = a.x*a.z*t;
tmp2 = a.y*sin_phi;
float ra20 = tmp1 - tmp2;
float ra02 = tmp1 + tmp2;
tmp1 = a.y*a.z*t;
tmp2 = a.x*sin_phi;
float ra21 = tmp1 + tmp2;
float ra12 = tmp1 - tmp2;
float S = 0;
float C = 0;
for (int i = 0; i < MODEL_SIZE; ++i)
{
float3 vsr = vs[i]*cos_phi + cross(a,vs[i])*sin_phi + a * ( dot(a,vs[i]) * (1.0f-cos_phi) );
S += dot(cross(vt[i], vsr), nt_n);
C += dot(vt[i], vsr);
}
float sin_theta = S/sqrt(S*S + C*C);
float cos_theta = C/sqrt(S*S + C*C);
t = 1.0 - cos_theta;
float3 b = nt_n;
float rb00 = cos_theta + b.x*b.x*t;
float rb11 = cos_theta + b.y*b.y*t;
float rb22 = cos_theta + b.z*b.z*t;
tmp1 = b.x*b.y*t;
tmp2 = b.z*sin_theta;
float rb10 = tmp1 + tmp2;
float rb01 = tmp1 - tmp2;
tmp1 = b.x*b.z*t;
tmp2 = b.y*sin_theta;
float rb20 = tmp1 - tmp2;
float rb02 = tmp1 + tmp2;
tmp1 = b.y*b.z*t;
tmp2 = b.x*sin_theta;
float rb21 = tmp1 + tmp2;
float rb12 = tmp1 - tmp2;
float R00 = ra00*rb00 + ra01*rb10 + ra02*rb20; float R01 = ra00*rb01 + ra01*rb11 + ra02*rb21; float R02 = ra00*rb02 + ra01*rb12 + ra02*rb22;
float R10 = ra10*rb00 + ra11*rb10 + ra12*rb20; float R11 = ra10*rb01 + ra11*rb11 + ra12*rb21; float R12 = ra10*rb02 + ra11*rb12 + ra12*rb22;
float R20 = ra20*rb00 + ra21*rb10 + ra22*rb20; float R21 = ra20*rb01 + ra21*rb11 + ra22*rb21; float R22 = ra20*rb02 + ra21*rb12 + ra22*rb22;
RT a_solution;
a_solution.R[0] = make_float3(R00, R10, R20);
a_solution.R[1] = make_float3(R01, R11, R21);
a_solution.R[2] = make_float3(R02, R12, R22);
// solve for the translation vector
// T = Ym - R*Xm
float3 T = t_mean - make_float3(R00 * s_mean.x + R01 * s_mean.y + R02 * s_mean.z,
R10 * s_mean.x + R11 * s_mean.y + R12 * s_mean.z,
R20 * s_mean.x + R21 * s_mean.y + R22 * s_mean.z);
a_solution.T = T;
candidate_solutions[thread] = a_solution;
/*
if (thread==0)
{
printf("selection =\n");
for (int i = 0; i < MODEL_SIZE; ++i)
{
printf("%d %d %d = %f %f %f \n",
offset,
(offset + thread) * MODEL_SIZE + i,
selection_source[ (offset + thread) * MODEL_SIZE + i],
vertices_source[ selection_source[ (offset + thread) * MODEL_SIZE + i] * 3 + 0],
vertices_source[ selection_source[ (offset + thread) * MODEL_SIZE + i] * 3 + 1],
vertices_source[ selection_source[ (offset + thread) * MODEL_SIZE + i] * 3 + 2]
);
}
printf("s_mean = %f %f %f \n", s_mean.x, s_mean.y, s_mean.z);
printf("t_mean = %f %f %f \n", t_mean.x, t_mean.y, t_mean.z);
printf("T = %f %f %f \n", a_solution.T.x, a_solution.T.y, a_solution.T.z);
printMat3( R_11, R_12, R_13,
R_21, R_22, R_23,
R_31, R_32, R_33);
}
*/
}
__global__ void computeRT(
const uint offset,
const float3* vertices_source,
const float3* vertices_target,
const uint* selection_source,
const uint* selection_target,
RT* candidate_solutions,
uint num_candidates)
{
uint thread = blockIdx.x *blockDim.x + threadIdx.x;
if( offset + thread >= num_candidates) return;
float3 vs[MODEL_SIZE];
float3 vt[MODEL_SIZE];
for (int i = 0; i < MODEL_SIZE; ++i)
{
vs[i] = vertices_source[ selection_source[ (offset + thread) * MODEL_SIZE + i]];
vt[i] = vertices_target[ selection_target[ (offset + thread) * MODEL_SIZE + i]];
}
float3 s_mean = make_float3(0,0,0);
float3 t_mean = make_float3(0,0,0);
for (int i = 0; i < MODEL_SIZE; ++i)
{
s_mean += vs[i];
t_mean += vt[i];
}
s_mean /= MODEL_SIZE;
t_mean /= MODEL_SIZE;
for (int i = 0; i < MODEL_SIZE; ++i)
{
vs[i] -= s_mean;
vt[i] -= t_mean;
}
float3 rs_2 = vs[2] - vs[0];
float3 rs_1 = vs[1] - vs[0];
float3 rt_2 = vt[2] - vt[0];
float3 rt_1 = vt[1] - vt[0];
float3 ns = cross(rs_2, rs_1);
float3 ns_n = normalize(ns);
float3 nt = cross(rt_2, rt_1);
float3 nt_n = normalize(nt);
float3 a = normalize(cross(ns,nt));
//phi is the out-of-plane rotation along the intersection between the two planes formed by the vertex triplets vs and vt
float sin_phi = norm(cross(ns_n, nt_n));
float cos_phi = dot(ns_n, nt_n);
float t = 1.0 - cos_phi;
float ra00 = cos_phi + a.x*a.x*t;
float ra11 = cos_phi + a.y*a.y*t;
float ra22 = cos_phi + a.z*a.z*t;
float tmp1 = a.x*a.y*t;
float tmp2 = a.z*sin_phi;
float ra10 = tmp1 + tmp2;
float ra01 = tmp1 - tmp2;
tmp1 = a.x*a.z*t;
tmp2 = a.y*sin_phi;
float ra20 = tmp1 - tmp2;
float ra02 = tmp1 + tmp2;
tmp1 = a.y*a.z*t;
tmp2 = a.x*sin_phi;
float ra21 = tmp1 + tmp2;
float ra12 = tmp1 - tmp2;
float S = 0;
float C = 0;
for (int i = 0; i < MODEL_SIZE; ++i)
{
float3 vsr = vs[i]*cos_phi + cross(a,vs[i])*sin_phi + a * ( dot(a,vs[i]) * (1.0f-cos_phi) );
S += dot(cross(vt[i], vsr), nt_n);
C += dot(vt[i], vsr);
}
float sin_theta = S/sqrt(S*S + C*C);
float cos_theta = C/sqrt(S*S + C*C);
t = 1.0 - cos_theta;
float3 b = nt_n;
float rb00 = cos_theta + b.x*b.x*t;
float rb11 = cos_theta + b.y*b.y*t;
float rb22 = cos_theta + b.z*b.z*t;
tmp1 = b.x*b.y*t;
tmp2 = b.z*sin_theta;
float rb10 = tmp1 + tmp2;
float rb01 = tmp1 - tmp2;
tmp1 = b.x*b.z*t;
tmp2 = b.y*sin_theta;
float rb20 = tmp1 - tmp2;
float rb02 = tmp1 + tmp2;
tmp1 = b.y*b.z*t;
tmp2 = b.x*sin_theta;
float rb21 = tmp1 + tmp2;
float rb12 = tmp1 - tmp2;
float R00 = ra00*rb00 + ra01*rb10 + ra02*rb20; float R01 = ra00*rb01 + ra01*rb11 + ra02*rb21; float R02 = ra00*rb02 + ra01*rb12 + ra02*rb22;
float R10 = ra10*rb00 + ra11*rb10 + ra12*rb20; float R11 = ra10*rb01 + ra11*rb11 + ra12*rb21; float R12 = ra10*rb02 + ra11*rb12 + ra12*rb22;
float R20 = ra20*rb00 + ra21*rb10 + ra22*rb20; float R21 = ra20*rb01 + ra21*rb11 + ra22*rb21; float R22 = ra20*rb02 + ra21*rb12 + ra22*rb22;
RT a_solution;
a_solution.R[0] = make_float3(R00, R10, R20);
a_solution.R[1] = make_float3(R01, R11, R21);
a_solution.R[2] = make_float3(R02, R12, R22);
// solve for the translation vector
// T = Ym - R*Xm
float3 T = t_mean - make_float3(R00 * s_mean.x + R01 * s_mean.y + R02 * s_mean.z,
R10 * s_mean.x + R11 * s_mean.y + R12 * s_mean.z,
R20 * s_mean.x + R21 * s_mean.y + R22 * s_mean.z);
a_solution.T = T;
candidate_solutions[thread] = a_solution;
}
__global__ void computeFitnessInverse(
float * score,
const RT* candidate_solutions,
const float3* vertices_source,
const uint num_verts_source,
const float3* vertices_target,
const uint num_verts_target,
const float error_tolerance_dist,
const float reject_model_if_below_this)
{
uint thread = blockIdx.x *blockDim.x + threadIdx.x;
RT warp = candidate_solutions[thread];
uint inlier_count = 0;
float fitness = 0;
for (uint i = 0; i < num_verts_source; ++i)
{
float smallest_score = HUGE_VALF;
for (uint j = 0; j < num_verts_target; ++j)
{
float3 X = vertices_source[i];
float3 Y = vertices_target[j];
float3 Yf = make_float3(-warp.T.x + ( warp.R[0].x * X.x + warp.R[0].y *X.y + warp.R[0].z * X.z),
-warp.T.y + ( warp.R[1].x * X.x + warp.R[1].y *X.y + warp.R[1].z * X.z),
-warp.T.z + ( warp.R[2].x * X.x + warp.R[2].y *X.y + warp.R[2].z * X.z) );
float this_score = dist2(Yf.x-Y.x, Yf.y-Y.y, Yf.z-Y.z);
if (this_score < smallest_score)
{
smallest_score = this_score;
}
}
if (smallest_score < error_tolerance_dist)
{
fitness+=smallest_score;
inlier_count++;
}
}
score[thread] = ((float(inlier_count)/float(num_verts_source)) < reject_model_if_below_this) ? 1e10 : fitness/float(inlier_count);
}
__global__ void computeFitnessInverse(
float * score,
const RT* candidate_solutions,
const float* vertices_source,
const uint num_verts_source,
const float* vertices_target,
const uint num_verts_target,
const float error_tolerance_dist,
const float reject_model_if_below_this)
{
uint thread = blockIdx.x *blockDim.x + threadIdx.x;
RT warp = candidate_solutions[thread];
uint inlier_count = 0;
float fitness = 0;
for (uint i = 0; i < num_verts_source; ++i)
{
float smallest_score = HUGE_VALF;
for (uint j = 0; j < num_verts_target; ++j)
{
float3 X = make_float3(vertices_source[i*3+0], vertices_source[i*3+1], vertices_source[i*3+2]);
float3 Y = make_float3(vertices_target[j*3+0], vertices_target[j*3+1], vertices_target[j*3+2]);
float3 Yf = make_float3(-warp.T.x + ( warp.R[0].x * X.x + warp.R[0].y *X.y + warp.R[0].z * X.z),
-warp.T.y + ( warp.R[1].x * X.x + warp.R[1].y *X.y + warp.R[1].z * X.z),
-warp.T.z + ( warp.R[2].x * X.x + warp.R[2].y *X.y + warp.R[2].z * X.z) );
float this_score = dist2(Yf.x-Y.x, Yf.y-Y.y, Yf.z-Y.z);
if (this_score < smallest_score)
{
smallest_score = this_score;
}
}
if (smallest_score < error_tolerance_dist)
{
fitness+=smallest_score;
inlier_count++;
}
}
score[thread] = ((float(inlier_count)/float(num_verts_source)) < reject_model_if_below_this) ? 1e10 : fitness/float(inlier_count);
}
__global__ void computeFitness(
float * score,
const RT* candidate_solutions,
const float3* vertices_source,
const uint num_verts_source,
const float3* vertices_target,
const uint num_verts_target,
const float error_tolerance_dist,
const float reject_model_if_below_this)
{
uint thread = blockIdx.x *blockDim.x + threadIdx.x;
RT warp = candidate_solutions[thread];
uint inlier_count = 0;
float fitness = 0;
for (uint i = 0; i < num_verts_source; ++i)
{
float smallest_score = HUGE_VALF;
for (uint j = 0; j < num_verts_target; ++j)
{
float3 X = vertices_source[i];
float3 Y = vertices_target[j];
float3 Yf = make_float3(warp.T.x + ( warp.R[0].x * X.x + warp.R[1].x *X.y + warp.R[2].x * X.z),
warp.T.y + ( warp.R[0].y * X.x + warp.R[1].y *X.y + warp.R[2].y * X.z),
warp.T.z + ( warp.R[0].z * X.x + warp.R[1].z *X.y + warp.R[2].z * X.z) );
float this_score = dist2(Yf.x-Y.x, Yf.y-Y.y, Yf.z-Y.z);
if (this_score < smallest_score)
{
smallest_score = this_score;
}
}
if (smallest_score < error_tolerance_dist)
{
fitness+=smallest_score;
inlier_count++;
}
}
score[thread] = ((float(inlier_count)/float(num_verts_source)) < reject_model_if_below_this) ? 1e10 : fitness/float(inlier_count);
}
__global__ void computeFitness(
float * score,
const RT* candidate_solutions,
const float* vertices_source,
const uint num_verts_source,
const float* vertices_target,
const uint num_verts_target,
const float error_tolerance_dist,
const float reject_model_if_below_this)
{
uint thread = blockIdx.x *blockDim.x + threadIdx.x;
RT warp = candidate_solutions[thread];
uint inlier_count = 0;
float fitness = 0;
for (uint i = 0; i < num_verts_source; ++i)
{
float smallest_score = HUGE_VALF;
for (uint j = 0; j < num_verts_target; ++j)
{
float3 X = make_float3(vertices_source[i*3+0], vertices_source[i*3+1], vertices_source[i*3+2]);
float3 Y = make_float3(vertices_target[j*3+0], vertices_target[j*3+1], vertices_target[j*3+2]);
float3 Yf = make_float3(warp.T.x + ( warp.R[0].x * X.x + warp.R[1].x *X.y + warp.R[2].x * X.z),
warp.T.y + ( warp.R[0].y * X.x + warp.R[1].y *X.y + warp.R[2].y * X.z),
warp.T.z + ( warp.R[0].z * X.x + warp.R[1].z *X.y + warp.R[2].z * X.z) );
float this_score = dist2(Yf.x-Y.x, Yf.y-Y.y, Yf.z-Y.z);
if (this_score < smallest_score)
{
smallest_score = this_score;
}
}
if (smallest_score < error_tolerance_dist)
{
fitness+=smallest_score;
inlier_count++;
}
}
score[thread] = ((float(inlier_count)/float(num_verts_source)) < reject_model_if_below_this) ? 1e10 : fitness/float(inlier_count);
}
RANSAC::RANSAC(
const uint max_iterations,
const uint max_iterations_per_batch,
const float error_tolerance_dist,
const float inlier_ratio_to_accept_solution,
thrust::device_vector<float3> vertices_source,
thrust::device_vector<float3> vertices_target,
thrust::device_vector<float3> vertices_fitness_source,
thrust::device_vector<float3> vertices_fitness_target):
_numThreadsPerBlock (256),
_current_best_score (HUGE_VALF),
_current_best_model_pair_index (0),
_time_to_stop (false),
_iteration_counter (0),
_max_iterations (max_iterations),
_max_iterations_per_batch (max_iterations_per_batch),
_error_tolerance_dist (error_tolerance_dist),
_inlier_ratio_to_accept_solution (inlier_ratio_to_accept_solution),
_vertices_source (vertices_source),
_vertices_target (vertices_target),
_vertices_fitness_source (vertices_fitness_source),
_vertices_fitness_target (vertices_fitness_target)
{
//check that the number of threads per block doesn't exceed the batch size
if (_numThreadsPerBlock > max_iterations_per_batch)
_numThreadsPerBlock = max_iterations_per_batch;
//random number generator
std::random_device rand_dev;
std::mt19937 generator(rand_dev());
//host side containers for the indices to be used by ransac
_vert_subset_source_host.resize(_max_iterations * MODEL_SIZE);
_vert_subset_target_host.resize(_max_iterations * MODEL_SIZE);
//sample randomly, while avoiding repeated points in the model subsets
std::uniform_int_distribution<uint> distr_source( 0, vertices_source.size()-1);
for (uint i = 0; i < _max_iterations; i++)
{
std::set<uint> a_subset;
while(a_subset.size()<MODEL_SIZE)
a_subset.insert(distr_source(generator));
for (int j = 0; j < MODEL_SIZE; ++j)
_vert_subset_source_host[i*MODEL_SIZE + j] = *std::next(a_subset.begin(), j);
}
std::uniform_int_distribution<uint> distr_target( 0, vertices_target.size()-1);
//sample randomly, while avoiding repeated points in the model subsets
for (uint i = 0; i < _max_iterations; i++)
{
std::set<uint> a_subset;
while(a_subset.size()<MODEL_SIZE)
a_subset.insert(distr_target(generator));
for (int j = 0; j < MODEL_SIZE; ++j)
_vert_subset_target_host[i*MODEL_SIZE + j] = *std::next(a_subset.begin(), j);
}
_vert_subset_source = _vert_subset_source_host;
_vert_subset_target = _vert_subset_target_host;
_numBlocks = max(1,int(floorf(float(_max_iterations_per_batch)/_numThreadsPerBlock + 0.5f)));
_numThreads = _numThreadsPerBlock*_numBlocks;
_candidate_solutions.resize(_numThreads);
_solution_scores.resize(_numThreads);
}
RANSAC::~RANSAC()
{}
void RANSAC::doIteration()
{
float best_score_of_batch = 1e10f;
dim3 dimGrid(_numBlocks);
dim3 dimThreads = dim3(_numThreadsPerBlock);
computeRT<<<dimGrid,dimThreads>>>(
_iteration_counter,
thrust::raw_pointer_cast( &_vertices_source[0]),
thrust::raw_pointer_cast( &_vertices_target[0]),
thrust::raw_pointer_cast( &_vert_subset_source[0]),
thrust::raw_pointer_cast( &_vert_subset_target[0]),
thrust::raw_pointer_cast( &_candidate_solutions[0]),
_max_iterations);
cudaGetLastError();
computeFitnessInverse<<<dimGrid, dimThreads>>>(
thrust::raw_pointer_cast(&_solution_scores[0]),
thrust::raw_pointer_cast(&_candidate_solutions[0]),
thrust::raw_pointer_cast(&_vertices_source[0]),
_vertices_source.size(),
thrust::raw_pointer_cast(&_vertices_target[0]),
_vertices_target.size(),
_error_tolerance_dist,
_inlier_ratio_to_accept_solution
);
(cudaGetLastError ());
thrust::device_vector<float>::iterator iter_inv =
thrust::min_element(_solution_scores.begin(), _solution_scores.end());
(cudaGetLastError ());
int best_of_batch_inv = iter_inv - _solution_scores.begin();
float best_score_of_batch_inv = _solution_scores[best_of_batch_inv];
computeFitness<<<dimGrid, dimThreads>>>(
thrust::raw_pointer_cast(&_solution_scores[0]),
thrust::raw_pointer_cast(&_candidate_solutions[0]),
thrust::raw_pointer_cast(&_vertices_source[0]),
_vertices_source.size(),
thrust::raw_pointer_cast(&_vertices_target[0]),
_vertices_target.size(),
_error_tolerance_dist,
_inlier_ratio_to_accept_solution
);
(cudaGetLastError ());
thrust::device_vector<float>::iterator iter_fwd =
thrust::min_element(_solution_scores.begin(), _solution_scores.end());
(cudaGetLastError ());
int best_of_batch_fwd = iter_fwd - _solution_scores.begin();
float best_score_of_batch_fwd = _solution_scores[best_of_batch_fwd];
if(best_score_of_batch_fwd < 1e6 && best_score_of_batch_inv <1e6)
{
best_score_of_batch = 0.5*(best_score_of_batch_fwd + best_score_of_batch_inv);
}
if(best_score_of_batch < _current_best_score)
{
_current_best_score = best_score_of_batch;//_solution_scores[best_of_batch];
// printf("current best: %f\n",_current_best_score);
_iteration_mutex.lock();
_current_best_model_pair_index = _iteration_counter + best_of_batch_fwd;
_iteration_mutex.unlock();
_transformation_mutex.lock();
_current_best_solution = _candidate_solutions[best_of_batch_fwd];
_transformation_mutex.unlock();
}
_iteration_mutex.lock();
_iteration_counter+= _numThreads;
_iteration_mutex.unlock();
}
void RANSAC::GetCurrentSolution( RT &solution)
{
_transformation_mutex.lock();
solution = _current_best_solution;
_transformation_mutex.unlock();
}
uint3 RANSAC::GetSourceModel()
{
return make_uint3( _vert_subset_source_host[MODEL_SIZE * _current_best_model_pair_index + 0],
_vert_subset_source_host[MODEL_SIZE * _current_best_model_pair_index + 1],
_vert_subset_source_host[MODEL_SIZE * _current_best_model_pair_index + 2]);
}
uint3 RANSAC::GetTargetModel()
{
return make_uint3( _vert_subset_target_host[MODEL_SIZE * _current_best_model_pair_index + 0],
_vert_subset_target_host[MODEL_SIZE * _current_best_model_pair_index + 1],
_vert_subset_target_host[MODEL_SIZE * _current_best_model_pair_index + 2]);
}
bool RANSAC::has_finished()
{
return (_iteration_counter >= _max_iterations);
}
float RANSAC::GetCurrentScore()
{
return _current_best_score;
}
void RANSAC::start()
{
while( (_iteration_counter < _max_iterations) && !_time_to_stop)
doIteration();
}
void RANSAC::stop()
{
_iteration_mutex.lock();
_time_to_stop = true;
_iteration_mutex.unlock();
}
void RANSAC::reset()
{
_current_best_score=HUGE_VALF;
_time_to_stop=false;
_iteration_mutex.lock();
_iteration_counter= 0;
_iteration_mutex.unlock();
}