-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedgeitem.cpp
More file actions
359 lines (305 loc) · 9.09 KB
/
edgeitem.cpp
File metadata and controls
359 lines (305 loc) · 9.09 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
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
#include "edgeitem.h"
#include "labelimage.h"
#include "endpoint.h"
#include <QGraphicsItemAnimation>
#include <QTimeLine>
#include <QtDebug>
#include "action.h"
EdgeItem::EdgeItem(LabelImage* labelImage, const std::list<cv::Point>& points)
{
std::vector<QPointF> temp;
for (auto point : points) {
QPointF curr(point.x, point.y);
temp.push_back(curr);
}
init(labelImage, temp, 0, temp.size()-1);
}
EdgeItem::EdgeItem(LabelImage *labelImage, const std::vector<QPointF>& points, int start, int end)
{
init(labelImage, points, start, end);
}
EdgeItem::~EdgeItem()
{
removeFromScene();
}
void EdgeItem::removeFromScene()
{
if (pHead){
if (scene())
scene()->removeItem(pHead);
}
if (pTail){
if (scene())
scene()->removeItem(pTail);
}
if (scene())
scene()->removeItem(this);
}
void EdgeItem::init(LabelImage *labelImage, const std::vector<QPointF>& points, int start, int end)
{
image = labelImage;
QPointF tl(points[start].x(), points[start].y());
QPointF br(points[start].x(), points[start].y());
QPointF prev;
for (int i = start; i <= end; i++) {
QPointF curr(points[i]);
// ignore duplicate pixels at same location
if (!prev.isNull() && prev == curr) continue;
if (curr.x() < tl.x()) tl.setX(curr.x());
if (curr.x() > br.x()) br.setX(curr.x());
if (curr.y() < tl.y()) tl.setY(curr.y());
if (curr.y() > br.y()) br.setY(curr.y());
qpoints.push_back(curr);
prev = curr;
}
bbx.setTopLeft(tl);
bbx.setBottomRight(br);
// spoints: points in local coordinate (origin at bounding rectangle center)
for (auto point : qpoints)
spoints.push_back(point - (br + tl)/2);
selected = false;
colorDefault = Qt::green;
colorSelected = Qt::cyan;
colorHover = Qt::red;
colorBlink = Qt::yellow;
color = colorDefault;
borderWidth = 0.1;
initBorderWidth = borderWidth;
edgeWidth = 1.2;
splitLineLength = 6;
splitLineWidth = 0.3;
// padding to the bouding rectangle, need to cover border and split line
padding = std::max((edgeWidth-1)/2 + borderWidth, splitLineLength/2 - 0.5);
splitIndex = -0.25;
colorSplitA = Qt::cyan;
colorSplitB = Qt::cyan;
colorSplitLine = Qt::yellow;
showSplit = false;
pHead = NULL;
pTail = NULL;
setZValue(0);
// setCacheMode(DeviceCoordinateCache);
}
void EdgeItem::createEndPoints()
{
if (!pHead)
pHead = new EndPoint(this, image, 0);
scene()->addItem(pHead);
if (!pTail)
pTail = new EndPoint(this, image, qpoints.size()-1);
scene()->addItem(pTail);
}
void EdgeItem::createEndPoints(int headIndex, int tailIndex)
{
if (!pHead)
pHead = new EndPoint(this, image, headIndex);
scene()->addItem(pHead);
if (!pTail)
pTail = new EndPoint(this, image, tailIndex);
scene()->addItem(pTail);
}
EndPoint* EdgeItem::head() const
{
return pHead;
}
EndPoint* EdgeItem::tail() const
{
return pTail;
}
bool EdgeItem::pointVisible(int pointIndex) const
{
bool afterHead = pHead ? pointIndex >= (int)(pHead->indexOnEdge()) : pointIndex >= 0;
bool beforeTail = pTail ? pointIndex <= (int)(pTail->indexOnEdge()) : pointIndex < (int)points().size();
return afterHead && beforeTail;
}
std::vector<QPointF> EdgeItem::points() const
{
return qpoints;
}
QPointF EdgeItem::center() const
{
// center of bounding rectangle in scene coordinate
return QPointF(image->boundingRect().left() + (bbx.left()+bbx.right())/2,
image->boundingRect().top() + (bbx.top()+bbx.bottom())/2);
}
QRectF EdgeItem::boundingRect() const
{
// bounding rectangle in local coordinate
return QRectF(-bbx.width()/2-padding, -bbx.height()/2-padding,
bbx.width()+1+padding*2, bbx.height()+1+padding*2);
}
QPainterPath EdgeItem::shape() const
{
// rectangles as pixels
float dist = (edgeWidth-1)/2;
QPainterPath path;
path.setFillRule(Qt::WindingFill);
for (unsigned int i = 0; i < points().size(); i++)
if (pointVisible(i))
path.addRect(spoints[i].x()-dist, spoints[i].y()-dist, dist*2+1, dist*2+1);
return path;
}
QPainterPath EdgeItem::shapeSplitPointA() const
{
float dist = (edgeWidth-1)/2;
QPainterPath path;
int r = round(splitIndex);
int i = splitIndex > r ? r : r-1;
if (pointVisible(i))
path.addRect(spoints[i].x()-dist, spoints[i].y()-dist, dist*2+1, dist*2+1);
return path;
}
QPainterPath EdgeItem::shapeSplitPointB() const
{
float dist = (edgeWidth-1)/2;
QPainterPath path;
int r = round(splitIndex);
int i = splitIndex > r ? r+1 : r;
if (pointVisible(i))
path.addRect(spoints[i].x()-dist, spoints[i].y()-dist, dist*2+1, dist*2+1);
return path;
}
QPainterPath EdgeItem::shapeSplitLine() const
{
// draw a line between two pixels
float half = splitLineLength/2;
QPainterPath path;
int r = round(splitIndex);
int a = splitIndex > r ? r : r-1;
int b = splitIndex > r ? r+1 : r;
if (pointVisible(a) && pointVisible(b)){
QPointF pointA = spoints[a] + QPointF(0.5, 0.5);
QPointF pointB = spoints[b] + QPointF(0.5, 0.5);
QPointF mid = (pointA + pointB) / 2;
QPointF d = (pointB - pointA) / QLineF(pointA, pointB).length();
QLineF n = QLineF(QPointF(0,0), d).normalVector();
path.moveTo(mid - n.p2()*half);
path.lineTo(mid + n.p2()*half);
}
return path;
}
void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setPen(QPen(color, borderWidth));
painter->setBrush(QBrush(color));
painter->drawPath(shape());
if (showSplit) {
painter->setPen(QPen(colorSplitA, borderWidth));
painter->setBrush(QBrush(colorSplitA));
painter->drawPath(shapeSplitPointA());
painter->setPen(QPen(colorSplitB, borderWidth));
painter->setBrush(QBrush(colorSplitB));
painter->drawPath(shapeSplitPointB());
painter->setPen(QPen(colorSplitLine, splitLineWidth, Qt::DashLine));
painter->setBrush(QBrush(colorSplitLine));
painter->drawPath(shapeSplitLine());
}
}
double EdgeItem::convertSplitIndex(const QPointF& pos, const int pointIndex)
{
QPointF forward, backward;
double len_f, len_b;
len_f = std::numeric_limits<double>::infinity();
len_b = std::numeric_limits<double>::infinity();
if (pointIndex+1 < (int)points().size()) {
forward = points()[pointIndex+1] + QPointF(0.5, 0.5);
len_f = QLineF(pos, forward).length();
}
if (pointIndex > 0) {
backward = points()[pointIndex-1] + QPointF(0.5, 0.5);
len_b = QLineF(pos, backward).length();
}
if (len_b <= len_f) {
return pointIndex - 0.25;
} else {
return pointIndex + 0.25;
}
}
void EdgeItem::hoverEnter(const QPointF& pos, const int pointIndex)
{
if (!selected) {
showSplit = true;
splitIndex = convertSplitIndex(pos, pointIndex);
color = colorHover;
}
setZValue(1);
if(pHead) pHead->setVisible(true);
if(pTail) pTail->setVisible(true);
update(boundingRect());
}
void EdgeItem::hoverLeave()
{
if (!selected) {
showSplit = false;
color = colorDefault;
}
setZValue(0);
if(pHead) pHead->setVisible(false);
if(pTail) pTail->setVisible(false);
update(boundingRect());
}
void EdgeItem::setShowSplit(bool show)
{
showSplit = show;
}
bool EdgeItem::showingSplit() const
{
return showSplit;
}
std::vector<EdgeItem*> EdgeItem::split()
{
std::vector<EdgeItem*> edges;
int r = round(splitIndex);
int a = splitIndex > r ? r : r-1;
int b = splitIndex > r ? r+1 : r;
if (!pointVisible(a) || !pointVisible(b)) return edges;
EdgeItem* edge1 = new EdgeItem(image, qpoints, 0, a);
EdgeItem* edge2 = new EdgeItem(image, qpoints, b, qpoints.size()-1) ;
edges.push_back(edge1);
edges.push_back(edge2);
return edges;
}
void EdgeItem::blink()
{
QTimeLine *timer = new QTimeLine(500);
timer->setFrameRange(0, 100);
QObject::connect(timer, SIGNAL(frameChanged(int)), this, SLOT(setBlinkParameters(int)));
timer->start();
}
void EdgeItem::setBlinkParameters(int animationProgress)
{
double scaleFactor = 1 + 4*(100-animationProgress)/100.0;
if (animationProgress < 99) {
color = colorBlink;
borderWidth = initBorderWidth * scaleFactor;
setZValue(1);
if(pHead) pHead->setVisible(true);
if(pTail) pTail->setVisible(true);
} else {
color = selected ? colorSelected : colorDefault;
borderWidth = initBorderWidth;
setZValue(0);
if(pHead) pHead->setVisible(false);
if(pTail) pTail->setVisible(false);
}
update(boundingRect());
}
void EdgeItem::select()
{
selected = true;
showSplit = false;
color = colorSelected;
update(boundingRect());
}
void EdgeItem::unselect()
{
selected = false;
color = colorDefault;
update(boundingRect());
}
bool EdgeItem::isSelected() const
{
return selected;
}