shape.py 6.0 KB

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