shape.py 8.7 KB

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