-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangulateSimplePolygon.cpp
More file actions
272 lines (232 loc) · 8.75 KB
/
TriangulateSimplePolygon.cpp
File metadata and controls
272 lines (232 loc) · 8.75 KB
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
//
// Created by ale on 19-3-8.
//
#include <vector>
#include <list>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//调试用
//#define DEBUG_TRIANGULATE_SIMPLE_POLYGON
/*输入顶点集坐标默认按普通坐标系处理:
* y
* ^
* |
* o ——> x
* 如果是opencv顶点坐标,则需要自己先行处理好
* (比如按顺时针方向顺序输入轮廓顶点集)
*/
//三个基础函数
//A:i-1, B:i, C:i+1
static bool isConcave(const Point& A, const Point& B, const Point& C)
{
//if(v1.cross(v2)>0, 逆时针旋转
//if(v1.cross(v2)<0, 顺时针旋转
//AB->BC逆时针则为凸点,否则为凹点
return ((B - A).cross(C - B) < 0);//顺时针则定为凹点
//return ((C - B).cross(B - A) < 0);//opencv 坐标系
}
//逆时针三角形ABC与凹点,若凹点在三角形里面,则凹点在AB,BC和CA左边
static bool isInside(const Point& A, const Point& B, const Point& C, const Point& concave)
{
//if(v1.cross(v2)>0, 逆时针旋转
if( (B-A).cross(concave-A)>0 && (C-B).cross(concave-B)>0 && (A-C).cross(concave-C)>0)
return true;
return false;
}
static bool isIntersected(const Point& a, const Point& b, const Point& c, const Point& d)
{
if(max(a.x, b.x) < min(c.x, d.x))
return false;
if(max(a.y, b.y) < min(c.y, d.y))
return false;
if(max(c.x, d.x) < min(a.x, b.x))
return false;
if(max(c.y, d.y) < min(a.y, b.y))
return false;
//平行不算相交
if((a-c).cross(d-c)*(d-c).cross(b-c)<=0)
return false;
if((d-a).cross(b-a)*(b-a).cross(c-a)<=0)
return false;
return true;
}
//A:i-1, B:i, C:i+1; B is an ear tip if all concaves are outside triangle ABC
static bool isEarTip(const Point& A, const Point& B, const Point& C, const vector<bool> & concave, const vector<Point>& vertices, const list<int>& indices)
{
for(auto iter = indices.cbegin(); iter != indices.cend(); ++iter)
{
if(!concave[*iter])
continue;
auto iter_pre = std::prev(iter);
auto iter_next = std::next(iter);
if(iter_pre == indices.cend())
iter_pre = std::prev(iter_pre);
if(iter_next == indices.cend())
iter_next = std::next(iter_next);
//若凹点不在三角形内部(可以在三角形△ABC边上)
if(isInside(A, B, C, vertices[*iter]))
return false;
//若凹点不在AC上(不与AC共线,则说明在AB/BC上),且其相邻点组成的线段交于AC线段,则三角形ABC内部存在非mask点
if( isIntersected(A, C, vertices[*iter_pre], vertices[*iter]) ||
isIntersected(A, C, vertices[*iter], vertices[*iter_next]))
return false;
}
return true;
}
//初始化
static void updateLists(const vector<Point>& vertices, list<int>& indices, vector<bool>& concave, vector<bool>& eartips)
{
//状态清零
/*
for(int i=0; i<concave.size(); ++i)
{
concave[i] = false;
eartips[i] = false;
}*/
//寻找凹点
for(list<int>::iterator iter = indices.begin(); iter!=indices.end(); ++iter)
{
list<int>::iterator iter_pre = std::prev(iter);
list<int>::iterator iter_next = std::next(iter);
if(iter_pre == indices.end())
iter_pre = std::prev(iter_pre);
if(iter_next == indices.end())
iter_next = std::next(iter_next);
Point A = vertices[*iter_pre];
Point B = vertices[*iter];
Point C = vertices[*iter_next];
//if(isConcave( A, B, C))
// concave[*iter] = true;
concave[*iter] = isConcave(A, B, C);
}
//更新EarTip点
for(list<int>::iterator iter = indices.begin(); iter!=indices.end(); ++iter)
{
list<int>::iterator iter_pre = std::prev(iter);
list<int>::iterator iter_next = std::next(iter);
if(iter_pre == indices.end())
iter_pre = std::prev(iter_pre);
if(iter_next == indices.end())
iter_next = std::next(iter_next);
Point A = vertices[*iter_pre];
Point B = vertices[*iter];
Point C = vertices[*iter_next];
//if(!concave[*iter] && isEarTip(A, B, C, concave, vertices))
// eartips[*iter] = true;
if(!concave[*iter])
eartips[*iter] = isEarTip(A, B, C, concave, vertices, indices);
}
}
//更新相应节点的信息
static void updateConcave(const vector<Point>& vertices, const list<int>& indices, const list<int>::const_iterator& iter, vector<bool>& concave)
{
list<int>::const_iterator iter_pre = std::prev(iter);
list<int>::const_iterator iter_next = std::next(iter);
if(iter_pre == indices.end())
iter_pre = std::prev(iter_pre);
if(iter_next == indices.end())
iter_next = std::next(iter_next);
Point A = vertices[*iter_pre];
Point B = vertices[*iter];
Point C = vertices[*iter_next];
concave[*iter] = isConcave( A, B, C);
}
static void updateEarTip(const vector<Point>& vertices, const list<int>& indices, const list<int>::const_iterator& iter, const vector<bool>& concave, vector<bool>& eartip)
{
list<int>::const_iterator iter_pre = std::prev(iter);
list<int>::const_iterator iter_next = std::next(iter);
if(iter_pre == indices.end())
iter_pre = std::prev(iter_pre);
if(iter_next == indices.end())
iter_next = std::next(iter_next);
Point A = vertices[*iter_pre];
Point B = vertices[*iter];
Point C = vertices[*iter_next];
if(!concave[*iter])
eartip[*iter] = isEarTip(A, B, C, concave, vertices, indices);
}
//实际调用函数
int triangulateSimplePolygon(const vector<Point>& vertices, vector<Point>& triangles)
{
//vector<Point> triangles;
triangles.clear();
list<int> indices(vertices.size());
list<int>::iterator iter = indices.begin();
for(int i=0; i<vertices.size(); ++i)
{
*iter = i;
iter++;
}
//记录凹点与耳点
vector<bool> concave(vertices.size(), false);
vector<bool> eartips(vertices.size(), false);
//初始化,O(n**2)
updateLists(vertices, indices, concave, eartips);
while(indices.size() >=3)
{
#ifdef DEBUG_TRIANGULATE_SIMPLE_POLYGON
cout<<"indices.size(): "<<indices.size()<<endl;
cout<<"concave indices: ";
for(int i=0; i<concave.size(); ++i)
{
if(concave[i])
cout<<i<<"\t";
}
cout<<endl;
cout<<"eartips indices: ";
for(int i=0; i<eartips.size(); ++i)
{
if(eartips[i])
cout<<i<<"\t";
}
cout<<endl;
cout<<"triangles.size(): "<<triangles.size()<<endl;
imshow("debug", Mat(800, 800, CV_8UC1, Scalar(0)));
waitKey();
#endif
for(int i=0; i<eartips.size(); ++i)
{
if (eartips[i])
{
list<int>::iterator iter = find(indices.begin(), indices.end(), i);
list<int>::iterator iter_pre = std::prev(iter);
list<int>::iterator iter_next = std::next(iter);
if (iter_pre == indices.end())
iter_pre = std::prev(iter_pre);
if (iter_next == indices.end())
iter_next = std::next(iter_next);
triangles.push_back(vertices[*iter_pre]);
triangles.push_back(vertices[*iter]);
triangles.push_back(vertices[*iter_next]);
eartips[*iter] = false;
list<int>::iterator temp_next = std::next(iter);
list<int>::iterator temp_pre = std::prev(iter);
indices.erase(iter);
if(temp_next == indices.end())
temp_next = std::next(temp_next);
if(temp_pre == indices.end())
temp_pre = std::prev(temp_pre);
//必须先将所有Concave找出来,才能进行Eartip的判定
//只有被clipped节点的相邻节点的状态才会发生改变,所以不需要对所有剩余节点进行状态更新
updateConcave(vertices, indices, temp_pre, concave);
updateConcave(vertices, indices, temp_next, concave);
updateEarTip(vertices, indices, temp_pre, concave, eartips);
updateEarTip(vertices, indices, temp_next, concave, eartips);
#ifdef DEBUG_TRIANGULATE_SIMPLE_POLYGON
if(iter == indices.end())
cout<<"error in find eartips i in indices"<<endl;
cout<<"iter: "<<*iter<<" i: "<<i;
cout<<" iter_pre: "<<*iter_pre<<
" iter_next: "<<*iter_next<<endl
<<" temp_pre, temp_next: "<<*temp_pre<<", "<<*temp_next<<endl<<endl;
#endif
break;
}
}
}
indices.clear();
concave.clear();
eartips.clear();
return 0;
}