-
-
Notifications
You must be signed in to change notification settings - Fork 968
Expand file tree
/
Copy pathshape.py
More file actions
813 lines (743 loc) · 28.8 KB
/
shape.py
File metadata and controls
813 lines (743 loc) · 28.8 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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import copy
import math
from PyQt6 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",
"kie_linking",
]
# 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
CUBOID_FRONT_LEFT_EDGE_CENTER = 8
CUBOID_FRONT_RIGHT_EDGE_CENTER = 9
CUBOID_FRONT_TOP_EDGE_CENTER = 10
CUBOID_FRONT_BOTTOM_EDGE_CENTER = 11
CUBOID_BACK_LEFT_EDGE_CENTER = 12
CUBOID_BACK_RIGHT_EDGE_CENTER = 13
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=None,
kie_linking=None,
):
if attributes is None:
attributes = {}
if kie_linking is None:
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.hovered = 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):
if self.shape_type == "cuboid" and len(self.points) >= 8:
self.sync_cuboid_depth_vector()
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 self.shape_type == "cuboid" and "cuboid3d" not in self.other_data:
self.sync_cuboid_depth_vector()
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",
"quadrilateral",
"point",
"line",
"circle",
"linestrip",
"cuboid",
]
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 self.shape_type == "cuboid":
return len(self.points) >= 8
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)
elif self.shape_type == "cuboid":
if len(self.points) < 8:
self.points.append(point)
elif self.shape_type == "quadrilateral":
if self.points and point == self.points[0]:
self.close()
else:
self.points.append(point)
if len(self.points) == 4:
self.close()
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"""
if self.shape_type in ["polygon", "linestrip"]:
return True
if self.shape_type == "quadrilateral":
return len(self.points) < 4
return False
def get_cuboid_depth_vector(self):
cuboid_data = self.other_data.get("cuboid3d", {})
if isinstance(cuboid_data, dict):
depth_vector = cuboid_data.get("depth_vector", None)
if (
isinstance(depth_vector, (list, tuple))
and len(depth_vector) == 2
):
return [float(depth_vector[0]), float(depth_vector[1])]
if self.shape_type == "cuboid" and len(self.points) >= 5:
return [
float(self.points[4].x() - self.points[0].x()),
float(self.points[4].y() - self.points[0].y()),
]
return [0.0, 0.0]
def set_cuboid_depth_vector(
self,
depth_vector,
mode="from_rectangle",
source="manual",
):
self.other_data["cuboid3d"] = {
"version": 1,
"mode": mode,
"vertices2d_order": [
"front_tl",
"front_tr",
"front_br",
"front_bl",
"back_tl",
"back_tr",
"back_br",
"back_bl",
],
"depth_vector": [float(depth_vector[0]), float(depth_vector[1])],
"source": source,
}
def sync_cuboid_depth_vector(self):
if self.shape_type != "cuboid" or len(self.points) < 5:
return
depth_vector = [
float(self.points[4].x() - self.points[0].x()),
float(self.points[4].y() - self.points[0].y()),
]
cuboid_data = self.other_data.get("cuboid3d", {})
mode = (
cuboid_data.get("mode", "from_rectangle")
if isinstance(cuboid_data, dict)
else "from_rectangle"
)
source = (
cuboid_data.get("source", "manual")
if isinstance(cuboid_data, dict)
else "manual"
)
self.set_cuboid_depth_vector(
depth_vector=depth_vector,
mode=mode,
source=source,
)
def get_cuboid_front_center(self):
if self.shape_type != "cuboid" or len(self.points) < 4:
return QtCore.QPointF()
return QtCore.QPointF(
(self.points[0].x() + self.points[2].x()) / 2.0,
(self.points[0].y() + self.points[2].y()) / 2.0,
)
def get_cuboid_back_center(self):
if self.shape_type != "cuboid" or len(self.points) < 8:
return QtCore.QPointF()
return QtCore.QPointF(
(self.points[4].x() + self.points[6].x()) / 2.0,
(self.points[4].y() + self.points[6].y()) / 2.0,
)
@staticmethod
def get_mid_point(p1, p2):
return QtCore.QPointF(
(p1.x() + p2.x()) / 2.0,
(p1.y() + p2.y()) / 2.0,
)
def get_cuboid_visible_rear_edge_indices(self):
if self.shape_type != "cuboid" or len(self.points) < 8:
return []
depth_vector = self.get_cuboid_depth_vector()
if depth_vector[0] >= 0:
return [5, 6]
return [4, 7]
def get_cuboid_visible_rear_center_index(self):
if self.shape_type != "cuboid" or len(self.points) < 8:
return None
depth_vector = self.get_cuboid_depth_vector()
if depth_vector[0] >= 0:
return self.CUBOID_BACK_RIGHT_EDGE_CENTER
return self.CUBOID_BACK_LEFT_EDGE_CENTER
def get_cuboid_control_point(self, index):
if self.shape_type != "cuboid" or len(self.points) < 8:
return None
if index < len(self.points):
return self.points[index]
mapping = {
self.CUBOID_FRONT_LEFT_EDGE_CENTER: self.get_mid_point(
self.points[0], self.points[3]
),
self.CUBOID_FRONT_RIGHT_EDGE_CENTER: self.get_mid_point(
self.points[1], self.points[2]
),
self.CUBOID_FRONT_TOP_EDGE_CENTER: self.get_mid_point(
self.points[0], self.points[1]
),
self.CUBOID_FRONT_BOTTOM_EDGE_CENTER: self.get_mid_point(
self.points[3], self.points[2]
),
self.CUBOID_BACK_LEFT_EDGE_CENTER: self.get_mid_point(
self.points[4], self.points[7]
),
self.CUBOID_BACK_RIGHT_EDGE_CENTER: self.get_mid_point(
self.points[5], self.points[6]
),
}
return mapping.get(index)
def get_cuboid_visible_control_indices(self):
if self.shape_type != "cuboid" or len(self.points) < 8:
return []
rear_center_index = self.get_cuboid_visible_rear_center_index()
return (
[0, 1, 2, 3]
+ self.get_cuboid_visible_rear_edge_indices()
+ [
self.CUBOID_FRONT_LEFT_EDGE_CENTER,
self.CUBOID_FRONT_RIGHT_EDGE_CENTER,
self.CUBOID_FRONT_TOP_EDGE_CENTER,
self.CUBOID_FRONT_BOTTOM_EDGE_CENTER,
rear_center_index,
]
)
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))))
if self.difficult and self.shape_type != "point":
pen.setStyle(QtCore.Qt.PenStyle.DashLine)
painter.setPen(pen)
line_path = QtGui.QPainterPath()
vrtx_path = QtGui.QPainterPath()
if self.shape_type == "rectangle":
if len(self.points) not in [1, 2, 4]:
logger.error(
f"Invalid points count for rectangle: "
f"expected 1, 2 or 4, got {len(self.points)}"
)
return
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":
if len(self.points) not in [1, 2, 4]:
logger.error(
f"Invalid points count for rotation: "
f"expected 1, 2 or 4, got {len(self.points)}"
)
return
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 == "quadrilateral":
if len(self.points) not in [1, 2, 3, 4]:
logger.error(
f"Invalid points count for quadrilateral: "
f"expected 1-4, got {len(self.points)}"
)
return
if len(self.points) >= 2:
line_path.moveTo(self.points[0])
for i, p in enumerate(self.points):
line_path.lineTo(p)
if not self.selected:
if i == 0:
self.draw_vertex(vrtx_path, i)
else:
if i != 0:
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 == "cuboid":
if len(self.points) in [1, 2]:
return
if len(self.points) != 8:
logger.error(
f"Invalid points count for cuboid: expected 8, got {len(self.points)}"
)
return
front = [0, 1, 2, 3]
back = [4, 5, 6, 7]
links = [(0, 4), (1, 5), (2, 6), (3, 7)]
line_path.moveTo(self.points[front[0]])
for i in front[1:]:
line_path.lineTo(self.points[i])
line_path.lineTo(self.points[front[0]])
link_path = QtGui.QPainterPath()
for i, j in links:
link_path.moveTo(self.points[i])
link_path.lineTo(self.points[j])
back_path = QtGui.QPainterPath()
back_path.moveTo(self.points[back[0]])
for i in back[1:]:
back_path.lineTo(self.points[i])
back_path.lineTo(self.points[back[0]])
painter.drawPath(link_path)
back_pen = QtGui.QPen(pen)
back_pen.setStyle(QtCore.Qt.PenStyle.DashLine)
painter.setPen(back_pen)
painter.drawPath(back_path)
painter.setPen(pen)
orient_pen = QtGui.QPen(QtGui.QColor(255, 165, 0, 220))
orient_pen.setWidth(
max(1, int(round(self.line_width / self.scale)))
)
painter.setPen(orient_pen)
painter.drawLine(self.points[0], self.points[1])
painter.setPen(pen)
if self.selected or self.hovered:
for i in self.get_cuboid_visible_control_indices():
self.draw_vertex(vrtx_path, i)
elif self.shape_type == "circle":
if len(self.points) not in [1, 2]:
logger.error(
f"Invalid points count for circle: "
f"expected 1 or 2, got {len(self.points)}"
)
return
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)
self.draw_vertex(vrtx_path, i)
elif self.shape_type == "point":
if len(self.points) != 1:
logger.error(
f"Invalid points count for point: "
f"expected 1, got {len(self.points)}"
)
return
self.draw_vertex(vrtx_path, 0, True)
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)
# Quadrilateral selected: first vertex as outline-only (transparent, no fill)
if (
self.shape_type == "quadrilateral"
and len(self.points) >= 1
and self.selected
):
d = self.point_size / self.scale
p0 = self.points[0]
outline_color = (
self.select_line_color
if self.selected
else self.line_color
)
pen = QtGui.QPen(outline_color)
pen.setWidth(max(1, int(round(self.line_width / self.scale))))
painter.setPen(pen)
painter.setBrush(QtCore.Qt.BrushStyle.NoBrush)
painter.drawEllipse(
QtCore.QPointF(p0.x(), p0.y()), d / 2.0, d / 2.0
)
if self.fill:
color = (
self.select_fill_color
if self.selected
else self.fill_color
)
painter.fillPath(line_path, color)
if (
self.shape_type == "quadrilateral"
and len(self.points) == 4
and self.selected
and (self.is_closed() or self.label is not None)
):
self._draw_quadrilateral_order(painter)
def _draw_quadrilateral_order(self, painter):
"""Draw a single arrow on the first edge (0→1) to indicate vertex order."""
if len(self.points) != 4:
return
color = QtGui.QColor(0, 0, 0)
pen = QtGui.QPen(color)
pen.setWidth(max(1, int(round(self.line_width / self.scale))))
painter.setPen(pen)
painter.setBrush(QtCore.Qt.BrushStyle.NoBrush)
half_angle_rad = math.radians(40)
tip_len = max(4, 10 / self.scale)
arm_len = max(5, 12 / self.scale)
# Only first edge: arrow from point 0 toward point 1
from_pt = self.points[0]
to_pt = self.points[1]
dx = to_pt.x() - from_pt.x()
dy = to_pt.y() - from_pt.y()
dist = math.sqrt(dx * dx + dy * dy)
if dist < 1e-6:
return
ux, uy = dx / dist, dy / dist
angle_rad = math.atan2(dy, dx)
mx = (from_pt.x() + to_pt.x()) / 2
my = (from_pt.y() + to_pt.y()) / 2
tip_x = mx + ux * tip_len
tip_y = my + uy * tip_len
back1_x = tip_x + arm_len * math.cos(
angle_rad + math.pi - half_angle_rad
)
back1_y = tip_y + arm_len * math.sin(
angle_rad + math.pi - half_angle_rad
)
back2_x = tip_x + arm_len * math.cos(
angle_rad + math.pi + half_angle_rad
)
back2_y = tip_y + arm_len * math.sin(
angle_rad + math.pi + half_angle_rad
)
painter.drawLine(
QtCore.QPointF(tip_x, tip_y),
QtCore.QPointF(back1_x, back1_y),
)
painter.drawLine(
QtCore.QPointF(tip_x, tip_y),
QtCore.QPointF(back2_x, back2_y),
)
def draw_vertex(self, path, i, show_difficult=False):
"""Draw a vertex"""
d = self.point_size / self.scale
shape = self.point_type
if self.shape_type == "cuboid":
point = self.get_cuboid_control_point(i)
else:
point = self.points[i]
if point is None:
return
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 in (self.P_SQUARE, self.P_ROUND):
if self.difficult and show_difficult:
scale_factor = 1.5
triangle_path = QtGui.QPainterPath()
triangle_path.moveTo(
point.x(), point.y() - d * scale_factor / 2
)
triangle_path.lineTo(
point.x() - d * scale_factor / 2,
point.y() + d * scale_factor / 2,
)
triangle_path.lineTo(
point.x() + d * scale_factor / 2,
point.y() + d * scale_factor / 2,
)
triangle_path.closeSubpath()
path.addPath(triangle_path)
if shape == self.P_ROUND:
path.addPath(triangle_path)
else:
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
indices = range(len(self.points))
if self.shape_type == "cuboid":
indices = range(min(4, len(self.points)))
for i in indices:
p = self.points[i]
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"""
if self.shape_type == "cuboid":
return self.bounding_rect().contains(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 not self.points:
return QtGui.QPainterPath()
if self.shape_type == "rectangle":
path = QtGui.QPainterPath(self.points[0])
for p in self.points[1:]:
path.lineTo(p)
elif self.shape_type == "cuboid":
path = QtGui.QPainterPath(self.points[0])
if len(self.points) >= 4:
for p in self.points[1:4]:
path.lineTo(p)
path.closeSubpath()
if len(self.points) >= 8:
back_path = QtGui.QPainterPath(self.points[4])
for p in self.points[5:8]:
back_path.lineTo(p)
back_path.closeSubpath()
path.addPath(back_path)
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