shape.py 9.9 KB

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