shape.py 8.5 KB

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