shape.py 7.2 KB

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