forked from manhofer/Line3Dpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.cc
515 lines (437 loc) · 16.2 KB
/
view.cc
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
#include "view.h"
namespace L3DPP
{
//------------------------------------------------------------------------------
View::View(const unsigned int id, L3DPP::DataArray<float4>* lines,
const Eigen::Matrix3d& K, const Eigen::Matrix3d& R,
const Eigen::Vector3d& t,
const unsigned int width, const unsigned int height,
const float median_depth,
L3DPP::DataArray<float>* superpixels) :
id_(id), lines_(lines), K_(K), R_(R), t_(t),
width_(width), height_(height), initial_median_depth_(fmax(fabs(median_depth),L3D_EPS)),
superpixels_(superpixels)
{
// init
diagonal_ = sqrtf(float(width_*width_+height_*height_));
min_line_length_ = diagonal_*L3D_DEF_MIN_LINE_LENGTH_FACTOR;
collin_t_ = 0.0f;
// camera
pp_ = Eigen::Vector3d(K_(0,2),K_(1,2),1.0);
Kinv_ = K_.inverse();
Rt_ = R_.transpose();
RtKinv_ = Rt_*Kinv_;
C_ = Rt_ * (-1.0 * t_);
k_ = 0.0f;
median_depth_ = 0.0f;
median_sigma_ = 0.0f;
#ifdef L3DPP_CUDA
C_f3_ = make_float3(C_.x(),C_.y(),C_.z());
// RtKinv -> data array
RtKinv_DA_ = new L3DPP::DataArray<float>(3,3);
for(size_t r=0; r<3; ++r)
for(size_t c=0; c<3; ++c)
RtKinv_DA_->dataCPU(c,r)[0] = RtKinv_(r,c);
#endif //L3DPP_CUDA
}
//------------------------------------------------------------------------------
View::~View()
{
if(lines_ != NULL)
delete lines_;
if(superpixels_ != NULL)
delete superpixels_;
#ifdef L3DPP_CUDA
if(RtKinv_DA_ != NULL)
delete RtKinv_DA_;
#endif //L3DPP_CUDA
}
//------------------------------------------------------------------------------
void View::drawLineImage(cv::Mat& img)
{
img = cv::Mat::zeros(height_,width_,CV_8UC3);
for(size_t i=0; i<lines_->width(); ++i)
{
float4 coords = lines_->dataCPU(i,0)[0];
cv::Point p1(coords.x,coords.y);
cv::Point p2(coords.z,coords.w);
cv::line(img,p1,p2,cv::Scalar(255,255,255),3);
}
}
//------------------------------------------------------------------------------
void View::drawSingleLine(const unsigned int id, cv::Mat& img,
const cv::Scalar& color)
{
if(id < lines_->width())
{
float4 coords = lines_->dataCPU(id,0)[0];
cv::Point p1(coords.x,coords.y);
cv::Point p2(coords.z,coords.w);
cv::line(img,p1,p2,color,3);
}
}
//------------------------------------------------------------------------------
void View::drawEpipolarLine(const Eigen::Vector3d& epi, cv::Mat& img)
{
// intersect with image borders
Eigen::Vector3d p1(0,0,1);
Eigen::Vector3d p2(img.cols,0,1);
Eigen::Vector3d p3(img.cols,img.rows,1);
Eigen::Vector3d p4(0,img.rows,1);
Eigen::Vector3d borders[4];
borders[0] = p1.cross(p2);
borders[1] = p2.cross(p3);
borders[2] = p3.cross(p4);
borders[3] = p4.cross(p1);
std::vector<Eigen::Vector3d> intersections;
for(size_t i=0; i<4; ++i)
{
Eigen::Vector3d I = borders[i].cross(epi);
if(fabs(I.z()) > L3D_EPS)
{
I /= I.z();
I(2) = 1.0;
// check position
if(I.x() > -1.0 && I.x() < img.cols+1 &&
I.y() > -1.0 && I.y() < img.rows+1)
{
intersections.push_back(I);
}
}
}
if(intersections.size() < 2)
return;
// find intersections that are farthest apart
double max_dist = 0.0f;
Eigen::Vector3d e_p1(0,0,0);
Eigen::Vector3d e_p2(0,0,0);
for(size_t i=0; i<intersections.size()-1; ++i)
{
Eigen::Vector3d _p = intersections[i];
for(size_t j = i+1; j<intersections.size(); ++j)
{
Eigen::Vector3d _q = intersections[j];
double len = (_p-_q).norm();
if(len > max_dist)
{
max_dist = len;
e_p1 = _p;
e_p2 = _q;
}
}
}
cv::Point pt1(e_p1.x(),e_p1.y());
cv::Point pt2(e_p2.x(),e_p2.y());
cv::line(img,pt1,pt2,cv::Scalar(0,255,255),3);
}
//------------------------------------------------------------------------------
void View::findCollinearSegments(const float dist_t, bool useGPU)
{
if(fabs(dist_t-collin_t_) < L3D_EPS)
{
// already computed
return;
}
if(dist_t > L3D_EPS)
{
#ifndef L3DPP_CUDA
useGPU = false;
#endif //L3DPP_CUDA
collin_t_ = dist_t;
if(useGPU)
findCollinGPU();
else
findCollinCPU();
}
}
//------------------------------------------------------------------------------
void View::findCollinGPU()
{
// reset
collin_ = std::vector<std::list<unsigned int> >(lines_->width());
#ifdef L3DPP_CUDA
// upload
lines_->upload();
// buffer
L3DPP::DataArray<char>* buffer = new L3DPP::DataArray<char>(lines_->width(),
lines_->width(),true);
// GPU function
L3DPP::find_collinear_segments_GPU(buffer,lines_,collin_t_);
buffer->download();
buffer->removeFromGPU();
// process
#ifdef L3DPP_OPENMP
#pragma omp parallel for
#endif //L3DPP_OPENMP
for(size_t i=0; i<collin_.size(); ++i)
{
for(size_t c=0; c<buffer->width(); ++c)
{
char data = buffer->dataCPU(c,i)[0];
if(data == 1)
collin_[i].push_back(c);
}
}
// cleanup
delete buffer;
lines_->removeFromGPU();
#endif //L3DPP_CUDA
}
//------------------------------------------------------------------------------
void View::findCollinCPU()
{
// reset
collin_ = std::vector<std::list<unsigned int> >(lines_->width());
#ifdef L3DPP_OPENMP
#pragma omp parallel for
#endif //L3DPP_OPENMP
for(size_t r=0; r<collin_.size(); ++r)
{
Eigen::Vector3d p[2];
float4 l1 = lines_->dataCPU(r,0)[0];
p[0] = Eigen::Vector3d(l1.x,l1.y,1.0f);
p[1] = Eigen::Vector3d(l1.z,l1.w,1.0f);
Eigen::Vector3d line1 = p[0].cross(p[1]);
for(size_t c=0; c<lines_->width(); ++c)
{
if(r == c)
continue;
// line data
float4 l2 = lines_->dataCPU(c,0)[0];
Eigen::Vector3d q[2];
q[0] = Eigen::Vector3d(l2.x,l2.y,1.0f);
q[1] = Eigen::Vector3d(l2.z,l2.w,1.0f);
Eigen::Vector3d line2 = q[0].cross(q[1]);
// check location (overlap)
if(pointOnSegment(p[0],p[1],q[0]) ||
pointOnSegment(p[0],p[1],q[1]) ||
pointOnSegment(q[0],q[1],p[0]) ||
pointOnSegment(q[0],q[1],p[1]))
{
// overlap -> not collinear
continue;
}
// compute distances
float d1 = fmax(distance_point2line_2D(line1,q[0]),
distance_point2line_2D(line1,q[1]));
float d2 = fmax(distance_point2line_2D(line2,p[0]),
distance_point2line_2D(line2,p[1]));
if(fmax(d1,d2) < collin_t_)
{
collin_[r].push_back(c);
}
}
}
}
//------------------------------------------------------------------------------
float View::distance_point2line_2D(const Eigen::Vector3d& line, const Eigen::Vector3d& p)
{
return fabs((line.x()*p.x()+line.y()*p.y()+line.z())/sqrtf(line.x()*line.x()+line.y()*line.y()));
}
//------------------------------------------------------------------------------
float View::smallerAngle(const Eigen::Vector2d& v1, const Eigen::Vector2d& v2)
{
float angle = acos(fmax(fmin(v1.dot(v2),1.0f),-1.0f));
if(angle > L3D_PI_1_2)
angle = M_PI-angle;
return angle;
}
//------------------------------------------------------------------------------
std::list<unsigned int> View::collinearSegments(const unsigned int segID)
{
if(collin_.size() == lines_->width() && segID < lines_->width())
return collin_[segID];
else
return std::list<unsigned int>();
}
//------------------------------------------------------------------------------
bool View::pointOnSegment(const Eigen::Vector3d& p1, const Eigen::Vector3d& p2,
const Eigen::Vector3d& x)
{
Eigen::Vector2d v1(p1.x()-x.x(),p1.y()-x.y());
Eigen::Vector2d v2(p2.x()-x.x(),p2.y()-x.y());
return (v1.dot(v2) < L3D_EPS);
}
//------------------------------------------------------------------------------
void View::computeSpatialRegularizer(const float r)
{
k_ = getSpecificSpatialReg(r);
}
//------------------------------------------------------------------------------
float View::getSpecificSpatialReg(const float r)
{
Eigen::Vector3d pp_shifted = pp_+Eigen::Vector3d(r,0.0,0.0);
Eigen::Vector3d ray_pp = getNormalizedRay(pp_);
Eigen::Vector3d ray_pp_shifted = getNormalizedRay(pp_shifted);
double alpha = acos(fmin(fmax(double(ray_pp.dot(ray_pp_shifted)),-1.0),1.0));
return sin(alpha);
}
//------------------------------------------------------------------------------
Eigen::Vector3d View::getNormalizedRay(const Eigen::Vector3d& p)
{
Eigen::Vector3d ray = RtKinv_*p;
return ray.normalized();
}
//------------------------------------------------------------------------------
Eigen::Vector3d View::getNormalizedRay(const Eigen::Vector2d& p)
{
return getNormalizedRay(Eigen::Vector3d(p.x(),p.y(),1.0));
}
//------------------------------------------------------------------------------
Eigen::Vector3d View::getNormalizedLinePointRay(const unsigned int lID,
const bool pt1)
{
Eigen::Vector3d ray(0,0,0);
if(lID < lines_->width())
{
Eigen::Vector3d p;
if(pt1)
{
// ray through P1
p = Eigen::Vector3d(lines_->dataCPU(lID,0)[0].x,
lines_->dataCPU(lID,0)[0].y,1.0);
}
else
{
// ray through P2
p = Eigen::Vector3d(lines_->dataCPU(lID,0)[0].z,
lines_->dataCPU(lID,0)[0].w,1.0);
}
return getNormalizedRay(p);
}
return ray;
}
//------------------------------------------------------------------------------
L3DPP::Segment3D View::unprojectSegment(const unsigned int segID, const float depth1,
const float depth2)
{
L3DPP::Segment3D seg3D;
if(segID < lines_->width())
{
Eigen::Vector3d p1(lines_->dataCPU(segID,0)[0].x,
lines_->dataCPU(segID,0)[0].y,1.0);
Eigen::Vector3d p2(lines_->dataCPU(segID,0)[0].z,
lines_->dataCPU(segID,0)[0].w,1.0);
seg3D = L3DPP::Segment3D(C_ + getNormalizedRay(p1)*depth1,
C_ + getNormalizedRay(p2)*depth2);
}
return seg3D;
}
//------------------------------------------------------------------------------
Eigen::Vector2d View::project(const Eigen::Vector3d& P)
{
Eigen::Vector3d q = (R_*P + t_);
// projection to unit focal plane
double xn = (1.0 * q[0] + 0.0 * q[2]) / q[2];
double yn = (1.0 * q[1] + 0.0 * q[2]) / q[2];
// projection function
q[0] = xn;
q[1] = yn;
q[2] = 1;
q = K_*q;
Eigen::Vector2d res;
res(0) = q(0)/q(2);
res(1) = q(1)/q(2);
return res;
}
//------------------------------------------------------------------------------
Eigen::Vector3d View::projectWithCheck(const Eigen::Vector3d& P)
{
Eigen::Vector3d q = (R_*P + t_);
// projection to unit focal plane
double xn = (1.0 * q[0] + 0.0 * q[2]) / q[2];
double yn = (1.0 * q[1] + 0.0 * q[2]) / q[2];
// projection function
q[0] = xn;
q[1] = yn;
q[2] = 1;
q = K_*q;
Eigen::Vector3d res(0,0,-1);
if(fabs(q(2)) > L3D_EPS)
{
res(0) = q(0)/q(2);
res(1) = q(1)/q(2);
res(2) = 1;
}
return res;
}
//------------------------------------------------------------------------------
bool View::projectedLongEnough(const L3DPP::Segment3D& seg3D)
{
Eigen::Vector2d p1 = project(seg3D.P1());
Eigen::Vector2d p2 = project(seg3D.P2());
return ((p1-p2).norm() > min_line_length_);
}
//------------------------------------------------------------------------------
Eigen::Vector4f View::getLineSegment2D(const unsigned int id)
{
Eigen::Vector4f coords(0,0,0,0);
if(id < lines_->width())
{
float4 c = lines_->dataCPU(id,0)[0];
coords(0) = c.x;
coords(1) = c.y;
coords(2) = c.z;
coords(3) = c.w;
}
return coords;
}
//------------------------------------------------------------------------------
float View::regularizerFrom3Dpoint(const Eigen::Vector3d& P)
{
return (P-C_).norm()*k_;
}
//------------------------------------------------------------------------------
Eigen::Vector3d View::getOpticalAxis()
{
return getNormalizedRay(pp_);
}
//------------------------------------------------------------------------------
double View::opticalAxesAngle(L3DPP::View* v)
{
Eigen::Vector3d r1 = getNormalizedRay(pp_);
Eigen::Vector3d r2 = v->getOpticalAxis();
return acos(fmin(fmax(double(r1.dot(r2)),-1.0),1.0));
}
//------------------------------------------------------------------------------
double View::segmentQualityAngle(const L3DPP::Segment3D& seg3D,
const unsigned int segID)
{
if(segID < lines_->width())
{
Eigen::Vector2d p1(lines_->dataCPU(segID,0)[0].x,
lines_->dataCPU(segID,0)[0].y);
Eigen::Vector2d p2(lines_->dataCPU(segID,0)[0].z,
lines_->dataCPU(segID,0)[0].w);
Eigen::Vector2d p = 0.5*(p1+p2);
Eigen::Vector3d r1 = getNormalizedRay(p);
Eigen::Vector3d r2 = seg3D.dir();
return acos(fmin(fmax(double(r1.dot(r2)),-1.0),1.0));
}
return 0.0;
}
//------------------------------------------------------------------------------
float View::distanceVisualNeighborScore(L3DPP::View* v)
{
// bring tgt camera center to src coordinate frame
Eigen::Vector3d Ctgt_t = R_*v->C()+t_;
// define two planes trough the camera center
Eigen::Vector3d n1(1,0,0);
Eigen::Vector3d n2(0,1,0);
// compute distances to the planes
float dist1 = fabs(n1.dot(Ctgt_t));
float dist2 = fabs(n2.dot(Ctgt_t));
return dist1+dist2;
}
//------------------------------------------------------------------------------
float View::baseLine(L3DPP::View* v)
{
return (C_ - v->C()).norm();
}
//------------------------------------------------------------------------------
void View::translate(const Eigen::Vector3d& t)
{
C_ += t;
t_ = -R_ * C_;
}
}