shape.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from qtpy import QtGui
  2. from labelme.lib import distance
  3. from labelme.lib import distancetoline
  4. from labelme import logger
  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):
  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. def close(self):
  44. if len(self.points) <= 2:
  45. logger.error('Polygon should be created with points >2')
  46. return
  47. self._closed = True
  48. def addPoint(self, point):
  49. if self.points and point == self.points[0]:
  50. self.close()
  51. else:
  52. self.points.append(point)
  53. def popPoint(self):
  54. if self.points:
  55. return self.points.pop()
  56. return None
  57. def insertPoint(self, i, point):
  58. self.points.insert(i, point)
  59. def isClosed(self):
  60. return self._closed
  61. def setOpen(self):
  62. self._closed = False
  63. def paint(self, painter):
  64. if self.points:
  65. color = self.select_line_color \
  66. if self.selected else self.line_color
  67. pen = QtGui.QPen(color)
  68. # Try using integer sizes for smoother drawing(?)
  69. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  70. painter.setPen(pen)
  71. line_path = QtGui.QPainterPath()
  72. vrtx_path = QtGui.QPainterPath()
  73. line_path.moveTo(self.points[0])
  74. # Uncommenting the following line will draw 2 paths
  75. # for the 1st vertex, and make it non-filled, which
  76. # may be desirable.
  77. # self.drawVertex(vrtx_path, 0)
  78. for i, p in enumerate(self.points):
  79. line_path.lineTo(p)
  80. self.drawVertex(vrtx_path, i)
  81. if self.isClosed():
  82. line_path.lineTo(self.points[0])
  83. painter.drawPath(line_path)
  84. painter.drawPath(vrtx_path)
  85. painter.fillPath(vrtx_path, self.vertex_fill_color)
  86. if self.fill:
  87. color = self.select_fill_color \
  88. if self.selected else self.fill_color
  89. painter.fillPath(line_path, color)
  90. def drawVertex(self, path, i):
  91. d = self.point_size / self.scale
  92. shape = self.point_type
  93. point = self.points[i]
  94. if i == self._highlightIndex:
  95. size, shape = self._highlightSettings[self._highlightMode]
  96. d *= size
  97. if self._highlightIndex is not None:
  98. self.vertex_fill_color = self.hvertex_fill_color
  99. else:
  100. self.vertex_fill_color = Shape.vertex_fill_color
  101. if shape == self.P_SQUARE:
  102. path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
  103. elif shape == self.P_ROUND:
  104. path.addEllipse(point, d / 2.0, d / 2.0)
  105. else:
  106. assert False, "unsupported vertex shape"
  107. def nearestVertex(self, point, epsilon):
  108. min_distance = float('inf')
  109. min_i = None
  110. for i, p in enumerate(self.points):
  111. dist = distance(p - point)
  112. if dist <= epsilon and dist < min_distance:
  113. min_distance = dist
  114. min_i = i
  115. return min_i
  116. def nearestEdge(self, point, epsilon):
  117. min_distance = float('inf')
  118. post_i = None
  119. for i in range(len(self.points)):
  120. line = [self.points[i - 1], self.points[i]]
  121. dist = distancetoline(point, line)
  122. if dist <= epsilon and dist < min_distance:
  123. min_distance = dist
  124. post_i = i
  125. return post_i
  126. def containsPoint(self, point):
  127. return self.makePath().contains(point)
  128. def makePath(self):
  129. path = QtGui.QPainterPath(self.points[0])
  130. for p in self.points[1:]:
  131. path.lineTo(p)
  132. return path
  133. def boundingRect(self):
  134. return self.makePath().boundingRect()
  135. def moveBy(self, offset):
  136. self.points = [p + offset for p in self.points]
  137. def moveVertexBy(self, i, offset):
  138. self.points[i] = self.points[i] + offset
  139. def highlightVertex(self, i, action):
  140. self._highlightIndex = i
  141. self._highlightMode = action
  142. def highlightClear(self):
  143. self._highlightIndex = None
  144. def copy(self):
  145. shape = Shape(self.label)
  146. shape.points = [p for p in self.points]
  147. shape.fill = self.fill
  148. shape.selected = self.selected
  149. shape._closed = self._closed
  150. if self.line_color != Shape.line_color:
  151. shape.line_color = self.line_color
  152. if self.fill_color != Shape.fill_color:
  153. shape.fill_color = self.fill_color
  154. return shape
  155. def __len__(self):
  156. return len(self.points)
  157. def __getitem__(self, key):
  158. return self.points[key]
  159. def __setitem__(self, key, value):
  160. self.points[key] = value