shape.py 11 KB

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