-
Notifications
You must be signed in to change notification settings - Fork 508
/
shape.py
431 lines (376 loc) · 14.3 KB
/
shape.py
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
import copy
import math
from PyQt5 import QtCore, QtGui
from . import utils
from ..labeling.logger import logger
# TODO(unknown):
# - [opt] Store paths instead of creating new ones at each paint.
DEFAULT_LINE_COLOR = QtGui.QColor(0, 255, 0, 128) # bf hovering
DEFAULT_FILL_COLOR = QtGui.QColor(100, 100, 100, 100) # hovering
DEFAULT_SELECT_LINE_COLOR = QtGui.QColor(255, 255, 255) # selected
DEFAULT_SELECT_FILL_COLOR = QtGui.QColor(0, 255, 0, 155) # selected
DEFAULT_VERTEX_FILL_COLOR = QtGui.QColor(0, 255, 0, 255) # hovering
DEFAULT_HVERTEX_FILL_COLOR = QtGui.QColor(255, 255, 255, 255) # hovering
class Shape:
"""Shape data type"""
# Render handles as squares
P_SQUARE = 0
# Render handles as circles
P_ROUND = 1
# Flag for the handles we would move if dragging
MOVE_VERTEX = 0
# Flag for all other handles on the current shape
NEAR_VERTEX = 1
KEYS = [
"label",
"score",
"points",
"group_id",
"difficult",
"shape_type",
"flags",
"description",
"attributes",
]
# The following class variables influence the drawing of all shape objects.
line_color = DEFAULT_LINE_COLOR
fill_color = DEFAULT_FILL_COLOR
select_line_color = DEFAULT_SELECT_LINE_COLOR
select_fill_color = DEFAULT_SELECT_FILL_COLOR
vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
point_type = P_ROUND
point_size = 4
scale = 1.5
line_width = 2.0
def __init__(
self,
label=None,
score=None,
line_color=None,
shape_type=None,
flags=None,
group_id=None,
description=None,
difficult=False,
direction=0,
attributes={},
kie_linking=[],
):
self.label = label
self.score = score
self.group_id = group_id
self.description = description
self.difficult = difficult
self.kie_linking = kie_linking
self.points = []
self.fill = False
self.selected = False
self.shape_type = shape_type
self.flags = flags
self.other_data = {}
self.attributes = attributes
self.cache_label = None
self.cache_description = None
self.visible = True
# Rotation setting
self.direction = direction
self.center = None
self.show_degrees = True
self._highlight_index = None
self._highlight_mode = self.NEAR_VERTEX
self._highlight_settings = {
self.NEAR_VERTEX: (4, self.P_ROUND),
self.MOVE_VERTEX: (1.5, self.P_SQUARE),
}
self._vertex_fill_color = None
self._closed = False
if line_color is not None:
# Override the class line_color attribute
# with an object attribute. Currently this
# is used for drawing the pending line a different color.
self.line_color = line_color
self.shape_type = shape_type
def to_dict(self):
dictData = {
"label": self.label,
"score": self.score,
"points": [(p.x(), p.y()) for p in self.points],
"group_id": self.group_id,
"description": self.description,
"difficult": self.difficult,
"shape_type": self.shape_type,
"flags": self.flags,
"attributes": self.attributes,
"kie_linking": self.kie_linking,
}
if self.shape_type == "rotation":
dictData["direction"] = self.direction
dictData = {
**self.other_data,
**dictData,
}
return dictData
def load_from_dict(self, data: dict, close=True):
self.label = data["label"]
self.score = data.get("score")
self.points = [QtCore.QPointF(p[0], p[1]) for p in data["points"]]
self.group_id = data.get("group_id")
self.description = data.get("description", "")
self.difficult = data.get("difficult", False)
self.shape_type = data.get("shape_type", "polygon")
self.flags = data.get("flags", {})
self.attributes = data.get("attributes", {})
self.kie_linking = data.get("kie_linking", [])
if self.shape_type == "rotation":
self.direction = data.get("direction", 0)
self.other_data = {k: v for k, v in data.items() if k not in self.KEYS}
if close:
self.close()
return self
@property
def shape_type(self):
"""Get shape type (polygon, rectangle, rotation, point, line, ...)"""
return self._shape_type
@shape_type.setter
def shape_type(self, value):
"""Set shape type"""
if value is None:
value = "polygon"
if value not in self.get_supported_shape():
raise ValueError(f"Unexpected shape_type: {value}")
self._shape_type = value
@staticmethod
def get_supported_shape():
return [
"polygon",
"rectangle",
"rotation",
"point",
"line",
"circle",
"linestrip",
]
def close(self):
"""Close the shape"""
if self.shape_type == "rotation" and len(self.points) == 4:
cx = (self.points[0].x() + self.points[2].x()) / 2
cy = (self.points[0].y() + self.points[2].y()) / 2
self.center = QtCore.QPointF(cx, cy)
self._closed = True
def reach_max_points(self):
if len(self.points) >= 4:
return True
return False
def add_point(self, point):
"""Add a point"""
if self.shape_type == "rectangle":
if not self.reach_max_points():
self.points.append(point)
else:
if self.points and point == self.points[0]:
self.close()
else:
self.points.append(point)
def can_add_point(self):
"""Check if shape supports more points"""
return self.shape_type in ["polygon", "linestrip"]
def pop_point(self):
"""Remove and return the last point of the shape"""
if self.points:
return self.points.pop()
return None
def insert_point(self, i, point):
"""Insert a point to a specific index"""
self.points.insert(i, point)
def remove_point(self, i):
"""Remove point from a specific index"""
self.points.pop(i)
def is_closed(self):
"""Check if the shape is closed"""
return self._closed
def set_open(self):
"""Set shape to open - (_close=False)"""
self._closed = False
def get_rect_from_line(self, pt1, pt2):
"""Get rectangle from diagonal line"""
x1, y1 = pt1.x(), pt1.y()
x2, y2 = pt2.x(), pt2.y()
return QtCore.QRectF(x1, y1, x2 - x1, y2 - y1)
def paint(self, painter: QtGui.QPainter): # noqa: max-complexity: 18
"""Paint shape using QPainter"""
if self.points:
color = (
self.select_line_color if self.selected else self.line_color
)
pen = QtGui.QPen(color)
# Try using integer sizes for smoother drawing(?)
pen.setWidth(max(1, int(round(self.line_width / self.scale))))
painter.setPen(pen)
line_path = QtGui.QPainterPath()
vrtx_path = QtGui.QPainterPath()
if self.shape_type == "rectangle":
assert len(self.points) in [1, 2, 4]
if len(self.points) == 2:
rectangle = self.get_rect_from_line(*self.points)
line_path.addRect(rectangle)
if len(self.points) == 4:
line_path.moveTo(self.points[0])
for i, p in enumerate(self.points):
line_path.lineTo(p)
if self.selected:
self.draw_vertex(vrtx_path, i)
if self.is_closed() or self.label is not None:
line_path.lineTo(self.points[0])
elif self.shape_type == "rotation":
assert len(self.points) in [1, 2, 4]
if len(self.points) == 2:
rectangle = self.get_rect_from_line(*self.points)
line_path.addRect(rectangle)
if len(self.points) == 4:
line_path.moveTo(self.points[0])
for i, p in enumerate(self.points):
line_path.lineTo(p)
if self.selected:
self.draw_vertex(vrtx_path, i)
if self.is_closed() or self.label is not None:
line_path.lineTo(self.points[0])
elif self.shape_type == "circle":
assert len(self.points) in [1, 2]
if len(self.points) == 2:
rectangle = self.get_circle_rect_from_line(self.points)
line_path.addEllipse(rectangle)
if self.selected:
for i in range(len(self.points)):
self.draw_vertex(vrtx_path, i)
elif self.shape_type == "linestrip":
line_path.moveTo(self.points[0])
for i, p in enumerate(self.points):
line_path.lineTo(p)
if self.selected:
self.draw_vertex(vrtx_path, i)
elif self.shape_type == "point":
assert len(self.points) == 1
self.draw_vertex(vrtx_path, 0)
else:
line_path.moveTo(self.points[0])
# Uncommenting the following line will draw 2 paths
# for the 1st vertex, and make it non-filled, which
# may be desirable.
self.draw_vertex(vrtx_path, 0)
for i, p in enumerate(self.points):
line_path.lineTo(p)
if self.selected:
self.draw_vertex(vrtx_path, i)
if self.is_closed():
line_path.lineTo(self.points[0])
painter.drawPath(line_path)
painter.drawPath(vrtx_path)
if self._vertex_fill_color is not None:
painter.fillPath(vrtx_path, self._vertex_fill_color)
if self.fill:
color = (
self.select_fill_color
if self.selected
else self.fill_color
)
painter.fillPath(line_path, color)
def draw_vertex(self, path, i):
"""Draw a vertex"""
d = self.point_size / self.scale
shape = self.point_type
point = self.points[i]
if i == self._highlight_index:
size, shape = self._highlight_settings[self._highlight_mode]
d *= size
if self._highlight_index is not None:
self._vertex_fill_color = self.hvertex_fill_color
else:
self._vertex_fill_color = self.vertex_fill_color
if shape == self.P_SQUARE:
path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
elif shape == self.P_ROUND:
path.addEllipse(point, d / 2.0, d / 2.0)
else:
logger.error("Unsupported vertex shape")
def nearest_vertex(self, point, epsilon):
"""Find the index of the nearest vertex to a point
Only consider if the distance is smaller than epsilon
"""
min_distance = float("inf")
min_i = None
for i, p in enumerate(self.points):
dist = utils.distance(p - point)
if dist <= epsilon and dist < min_distance:
min_distance = dist
min_i = i
return min_i
def nearest_edge(self, point, epsilon):
"""Get nearest edge index"""
min_distance = float("inf")
post_i = None
for i in range(len(self.points)):
line = [self.points[i - 1], self.points[i]]
dist = utils.distance_to_line(point, line)
if dist <= epsilon and dist < min_distance:
min_distance = dist
post_i = i
return post_i
def contains_point(self, point):
"""Check if shape contains a point"""
return self.make_path().contains(point)
def get_circle_rect_from_line(self, line):
"""Computes parameters to draw with `QPainterPath::addEllipse`"""
if len(line) != 2:
return None
(c, _) = line
r = line[0] - line[1]
d = math.sqrt(math.pow(r.x(), 2) + math.pow(r.y(), 2))
rectangle = QtCore.QRectF(c.x() - d, c.y() - d, 2 * d, 2 * d)
return rectangle
def make_path(self):
"""Create a path from shape"""
if self.shape_type == "rectangle":
path = QtGui.QPainterPath(self.points[0])
for p in self.points[1:]:
path.lineTo(p)
elif self.shape_type == "circle":
path = QtGui.QPainterPath()
if len(self.points) == 2:
rectangle = self.get_circle_rect_from_line(self.points)
path.addEllipse(rectangle)
else:
path = QtGui.QPainterPath(self.points[0])
for p in self.points[1:]:
path.lineTo(p)
return path
def bounding_rect(self):
"""Return bounding rectangle of the shape"""
return self.make_path().boundingRect()
def move_by(self, offset):
"""Move all points by an offset"""
self.points = [p + offset for p in self.points]
def move_vertex_by(self, i, offset):
"""Move a specific vertex by an offset"""
self.points[i] = self.points[i] + offset
def highlight_vertex(self, i, action):
"""Highlight a vertex appropriately based on the current action
Args:
i (int): The vertex index
action (int): The action
(see Shape.NEAR_VERTEX and Shape.MOVE_VERTEX)
"""
self._highlight_index = i
self._highlight_mode = action
def highlight_clear(self):
"""Clear the highlighted point"""
self._highlight_index = None
def copy(self):
"""Copy shape"""
return copy.deepcopy(self)
def __len__(self):
return len(self.points)
def __getitem__(self, key):
return self.points[key]
def __setitem__(self, key, value):
self.points[key] = value