-
Notifications
You must be signed in to change notification settings - Fork 3
/
delaunay.cpp
194 lines (159 loc) · 5.17 KB
/
delaunay.cpp
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
#include "delaunay.h"
void Delaunay::drawDelaunay(const Mat& src,Scalar delaunayColor){
bool draw;
Mat dst=src.clone();
if(dst.type()==CV_8UC1){
cvtColor(dst,dst,CV_GRAY2RGB);
}
for(size_t i = 0; i < triangulation.size(); ++i)
{
triangle tri = triangulation[i];
draw=true;
Point2f pts[3];
pts[0]=tri.vtx[0].pt;
pts[1]=tri.vtx[1].pt;
pts[2]=tri.vtx[2].pt;
for(int i=0;i<3;i++){
if((pts[i].x>src.cols-1)||(pts[i].y>src.rows-1)||(pts[i].x<0)||(pts[i].y<0))
draw=false;
}
if (draw){
cv::line(dst, pts[0], pts[1], delaunayColor, 1);
cv::line(dst, pts[1], pts[2], delaunayColor, 1);
cv::line(dst, pts[2], pts[0], delaunayColor, 1);
}
}
showImage(dst,"Delaunay",imagescale);
}
bool Delaunay::iswithinTri(const Point2f &pt, int tri_id)
{
triangle tri=triangulation[tri_id];
vector<Point2f> contour;
convert2Contour(tri,contour);
return pointPolygonTest(contour,pt,false)>0;
}
int Delaunay::findTri(const Point2f &pt)
{
for(size_t i=0;i<triangulation.size();++i){
if(iswithinTri(pt,i))
return i;
}
return -1;
}
double Delaunay::interpolateAttr(const Point2f &pt, int tri_id){
MatrixXd A(3,3);
Vector3d yv_mapped(-1,-1,-1);
VectorXd coeff;
triangle tri=triangulation[tri_id];
//create matrix
for (size_t i=0;i<3;++i){
A(i,0)=tri.vtx[i].pt.x;
A(i,1)=tri.vtx[i].pt.y;
A(i,2)=tri.vtx[i].attr;
}
//solve for linear least squares fit
ColPivHouseholderQR<MatrixXd> qr_decomp(A);
auto rank_A=qr_decomp.rank();
MatrixXd B(A.rows(),yv_mapped.cols()+A.cols());
B<<A,yv_mapped;
qr_decomp.compute(B);
auto rank_B=qr_decomp.rank();
double result;
if(rank_A==rank_B && rank_A==A.cols()){
coeff=A.householderQr().solve(yv_mapped);
result=(-1.0-coeff[0]*pt.x-coeff[1]*pt.y)/coeff[2];
}
else if(A(0,2)==A(1,2) && A(1,2)==A(2,2))
result=A(0,2);
else
exitwithErrors("Error occured while predicting the disparity!");
return result;
}
double Delaunay::calTriArea(const Point2f pt1, const Point2f pt2, const Point2f pt3)
{
// vector< vector<Point2f>> contour;
vector<Point2f> contour;
contour.push_back(pt1);
contour.push_back(pt2);
contour.push_back(pt3);
// contour.push_back(tri);
return contourArea(contour);
}
double Delaunay::calTriArea(const Delaunay::vertex *v)
{
Point2f p1,p2,p3;
p1=v[0].pt;
p2=v[1].pt;
p3=v[2].pt;
return calTriArea(p1,p2,p3);
}
void Delaunay::convert2Contour(const Delaunay::triangle &tri, vector<Point2f> &contour)
{
contour.clear();
contour.push_back(tri.vtx[0].pt);
contour.push_back(tri.vtx[1].pt);
contour.push_back(tri.vtx[2].pt);
}
void Delaunay::generateDelaunay(const vector<Point2f> &pts,const vector<double>& attribute){
this->insert(pts);
getTriangulation(attribute);
std::sort(triangulation.begin(),triangulation.end(),compTri);//descend
}
void Delaunay::generateDelaunay(const vector<KeyPoint> &kpts,const vector<double>& attribute){
vector<Point2f> pts;
KeyPoint2Point2f(kpts,pts);
generateDelaunay(pts,attribute);
}
void Delaunay::generateDelaunay(const vector<Match> &matches){
vector<double> attribute;
for(int i=0;i<matches.size();++i){
double attri=matches[i].p1.x-matches[i].p2.x;
attribute.push_back(attri);
}
vector<Point2f> l,r;
getPtsFromMatches(matches,l,r);
generateDelaunay(l,attribute);
}
void Delaunay::getTriangulation(const vector<double> &attribute){
vector<double> attri_backup=attribute;
if(attribute.size()!=0)
assert(attribute.size()==(this->vtx.size()-4));
else
attri_backup=vector<double>(this->vtx.size()-4,0);
triangulation.clear();
int total = (int)(this->qedges.size()*4);
vector<bool> edgemask(total, false);
// int idx=0;
for(int i = 4; i < total; i += 2 ){
if( edgemask[i] )
continue;
Point2f pt[3];
int pt_id[3];
int edge = i;
pt_id[0]=edgeOrg(edge, &pt[0]);
if (pt_id[0]<4) continue;
edgeOrg(edge, &pt[0]);
edgemask[edge] = true;
edge = getEdge(edge, NEXT_AROUND_LEFT);
pt_id[1]=edgeOrg(edge, &pt[1]);
if(pt_id[1]<4) continue;
edgeOrg(edge, &pt[1]);
edgemask[edge] = true;
edge = getEdge(edge, NEXT_AROUND_LEFT);
pt_id[2]=edgeOrg(edge, &pt[2]);
if(pt_id[2]<4) continue;
edgeOrg(edge, &pt[2]);
edgemask[edge] = true;
triangle tri;
// tri.id=idx++;
for(int k=0;k<3;++k){
vertex v;
v.id=k;
v.pt=pt[k];
v.attr=attri_backup[pt_id[k]-4];
tri.vtx[k]=v;
}
tri.area=calTriArea(tri.vtx);
triangulation.push_back(tri);
}
}