shape.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import copy
  2. from qtpy import QtGui
  3. import labelme.utils
  4. # TODO(unknown):
  5. # - [opt] Store paths instead of creating new ones at each paint.
  6. DEFAULT_LINE_COLOR = QtGui.QColor(0, 255, 0, 128)
  7. DEFAULT_FILL_COLOR = QtGui.QColor(255, 0, 0, 128)
  8. DEFAULT_SELECT_LINE_COLOR = QtGui.QColor(255, 255, 255)
  9. DEFAULT_SELECT_FILL_COLOR = QtGui.QColor(0, 128, 255, 155)
  10. DEFAULT_VERTEX_FILL_COLOR = QtGui.QColor(0, 255, 0, 255)
  11. DEFAULT_HVERTEX_FILL_COLOR = QtGui.QColor(255, 0, 0)
  12. class Shape(object):
  13. P_SQUARE, P_ROUND = 0, 1
  14. MOVE_VERTEX, NEAR_VERTEX = 0, 1
  15. # The following class variables influence the drawing of all shape objects.
  16. line_color = DEFAULT_LINE_COLOR
  17. fill_color = DEFAULT_FILL_COLOR
  18. select_line_color = DEFAULT_SELECT_LINE_COLOR
  19. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  20. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  21. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  22. point_type = P_ROUND
  23. point_size = 8
  24. scale = 1.0
  25. def __init__(self, label=None, line_color=None):
  26. self.label = label
  27. self.points = []
  28. self.fill = False
  29. self.selected = False
  30. self._highlightIndex = None
  31. self._highlightMode = self.NEAR_VERTEX
  32. self._highlightSettings = {
  33. self.NEAR_VERTEX: (4, self.P_ROUND),
  34. self.MOVE_VERTEX: (1.5, self.P_SQUARE),
  35. }
  36. self._closed = False
  37. if line_color is not None:
  38. # Override the class line_color attribute
  39. # with an object attribute. Currently this
  40. # is used for drawing the pending line a different color.
  41. self.line_color = line_color
  42. def close(self):
  43. self._closed = True
  44. def addPoint(self, point):
  45. if self.points and point == self.points[0]:
  46. self.close()
  47. else:
  48. self.points.append(point)
  49. def popPoint(self):
  50. if self.points:
  51. return self.points.pop()
  52. return None
  53. def insertPoint(self, i, point):
  54. self.points.insert(i, point)
  55. def isClosed(self):
  56. return self._closed
  57. def setOpen(self):
  58. self._closed = False
  59. def paint(self, painter):
  60. if self.points:
  61. color = self.select_line_color \
  62. if self.selected else self.line_color
  63. pen = QtGui.QPen(color)
  64. # Try using integer sizes for smoother drawing(?)
  65. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  66. painter.setPen(pen)
  67. line_path = QtGui.QPainterPath()
  68. vrtx_path = QtGui.QPainterPath()
  69. line_path.moveTo(self.points[0])
  70. # Uncommenting the following line will draw 2 paths
  71. # for the 1st vertex, and make it non-filled, which
  72. # may be desirable.
  73. # self.drawVertex(vrtx_path, 0)
  74. for i, p in enumerate(self.points):
  75. line_path.lineTo(p)
  76. self.drawVertex(vrtx_path, i)
  77. if self.isClosed():
  78. line_path.lineTo(self.points[0])
  79. painter.drawPath(line_path)
  80. painter.drawPath(vrtx_path)
  81. painter.fillPath(vrtx_path, self.vertex_fill_color)
  82. if self.fill:
  83. color = self.select_fill_color \
  84. if self.selected else self.fill_color
  85. painter.fillPath(line_path, color)
  86. def drawVertex(self, path, i):
  87. d = self.point_size / self.scale
  88. shape = self.point_type
  89. point = self.points[i]
  90. if i == self._highlightIndex:
  91. size, shape = self._highlightSettings[self._highlightMode]
  92. d *= size
  93. if self._highlightIndex is not None:
  94. self.vertex_fill_color = self.hvertex_fill_color
  95. else:
  96. self.vertex_fill_color = Shape.vertex_fill_color
  97. if shape == self.P_SQUARE:
  98. path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
  99. elif shape == self.P_ROUND:
  100. path.addEllipse(point, d / 2.0, d / 2.0)
  101. else:
  102. assert False, "unsupported vertex shape"
  103. def nearestVertex(self, point, epsilon):
  104. min_distance = float('inf')
  105. min_i = None
  106. for i, p in enumerate(self.points):
  107. dist = labelme.utils.distance(p - point)
  108. if dist <= epsilon and dist < min_distance:
  109. min_distance = dist
  110. min_i = i
  111. return min_i
  112. def nearestEdge(self, point, epsilon):
  113. min_distance = float('inf')
  114. post_i = None
  115. for i in range(len(self.points)):
  116. line = [self.points[i - 1], self.points[i]]
  117. dist = labelme.utils.distancetoline(point, line)
  118. if dist <= epsilon and dist < min_distance:
  119. min_distance = dist
  120. post_i = i
  121. return post_i
  122. def containsPoint(self, point):
  123. return self.makePath().contains(point)
  124. def makePath(self):
  125. path = QtGui.QPainterPath(self.points[0])
  126. for p in self.points[1:]:
  127. path.lineTo(p)
  128. return path
  129. def boundingRect(self):
  130. return self.makePath().boundingRect()
  131. def moveBy(self, offset):
  132. self.points = [p + offset for p in self.points]
  133. def moveVertexBy(self, i, offset):
  134. self.points[i] = self.points[i] + offset
  135. def highlightVertex(self, i, action):
  136. self._highlightIndex = i
  137. self._highlightMode = action
  138. def highlightClear(self):
  139. self._highlightIndex = None
  140. def copy(self):
  141. shape = Shape(self.label)
  142. shape.points = [copy.deepcopy(p) for p in self.points]
  143. shape.fill = self.fill
  144. shape.selected = self.selected
  145. shape._closed = self._closed
  146. shape.line_color = copy.deepcopy(self.line_color)
  147. shape.fill_color = copy.deepcopy(self.fill_color)
  148. return shape
  149. def __len__(self):
  150. return len(self.points)
  151. def __getitem__(self, key):
  152. return self.points[key]
  153. def __setitem__(self, key, value):
  154. self.points[key] = value