shape.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #
  2. # Copyright (C) 2011 Michael Pitidis, Hussein Abdulwahid.
  3. #
  4. # This file is part of Labelme.
  5. #
  6. # Labelme is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Labelme is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Labelme. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. try:
  20. from PyQt5 import QtGui
  21. except ImportError:
  22. from PyQt4 import QtGui
  23. from .lib import distance
  24. # TODO(unknown):
  25. # - [opt] Store paths instead of creating new ones at each paint.
  26. DEFAULT_LINE_COLOR = QtGui.QColor(0, 255, 0, 128)
  27. DEFAULT_FILL_COLOR = QtGui.QColor(255, 0, 0, 128)
  28. DEFAULT_SELECT_LINE_COLOR = QtGui.QColor(255, 255, 255)
  29. DEFAULT_SELECT_FILL_COLOR = QtGui.QColor(0, 128, 255, 155)
  30. DEFAULT_VERTEX_FILL_COLOR = QtGui.QColor(0, 255, 0, 255)
  31. DEFAULT_HVERTEX_FILL_COLOR = QtGui.QColor(255, 0, 0)
  32. class Shape(object):
  33. P_SQUARE, P_ROUND = 0, 1
  34. MOVE_VERTEX, NEAR_VERTEX = 0, 1
  35. # The following class variables influence the drawing of all shape objects.
  36. line_color = DEFAULT_LINE_COLOR
  37. fill_color = DEFAULT_FILL_COLOR
  38. select_line_color = DEFAULT_SELECT_LINE_COLOR
  39. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  40. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  41. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  42. point_type = P_ROUND
  43. point_size = 8
  44. scale = 1.0
  45. def __init__(self, label=None, line_color=None):
  46. self.label = label
  47. self.points = []
  48. self.fill = False
  49. self.selected = False
  50. self._highlightIndex = None
  51. self._highlightMode = self.NEAR_VERTEX
  52. self._highlightSettings = {
  53. self.NEAR_VERTEX: (4, self.P_ROUND),
  54. self.MOVE_VERTEX: (1.5, self.P_SQUARE),
  55. }
  56. self._closed = False
  57. if line_color is not None:
  58. # Override the class line_color attribute
  59. # with an object attribute. Currently this
  60. # is used for drawing the pending line a different color.
  61. self.line_color = line_color
  62. def close(self):
  63. assert len(self.points) > 2, 'Polygon should be created with points >2'
  64. self._closed = True
  65. def addPoint(self, point):
  66. if self.points and point == self.points[0]:
  67. self.close()
  68. else:
  69. self.points.append(point)
  70. def popPoint(self):
  71. if self.points:
  72. return self.points.pop()
  73. return None
  74. def isClosed(self):
  75. return self._closed
  76. def setOpen(self):
  77. self._closed = False
  78. def paint(self, painter):
  79. if self.points:
  80. color = self.select_line_color \
  81. if self.selected else self.line_color
  82. pen = QtGui.QPen(color)
  83. # Try using integer sizes for smoother drawing(?)
  84. pen.setWidth(max(1, int(round(2.0 / self.scale))))
  85. painter.setPen(pen)
  86. line_path = QtGui.QPainterPath()
  87. vrtx_path = QtGui.QPainterPath()
  88. line_path.moveTo(self.points[0])
  89. # Uncommenting the following line will draw 2 paths
  90. # for the 1st vertex, and make it non-filled, which
  91. # may be desirable.
  92. # self.drawVertex(vrtx_path, 0)
  93. for i, p in enumerate(self.points):
  94. line_path.lineTo(p)
  95. self.drawVertex(vrtx_path, i)
  96. if self.isClosed():
  97. line_path.lineTo(self.points[0])
  98. painter.drawPath(line_path)
  99. painter.drawPath(vrtx_path)
  100. painter.fillPath(vrtx_path, self.vertex_fill_color)
  101. if self.fill:
  102. color = self.select_fill_color \
  103. if self.selected else self.fill_color
  104. painter.fillPath(line_path, color)
  105. def drawVertex(self, path, i):
  106. d = self.point_size / self.scale
  107. shape = self.point_type
  108. point = self.points[i]
  109. if i == self._highlightIndex:
  110. size, shape = self._highlightSettings[self._highlightMode]
  111. d *= size
  112. if self._highlightIndex is not None:
  113. self.vertex_fill_color = self.hvertex_fill_color
  114. else:
  115. self.vertex_fill_color = Shape.vertex_fill_color
  116. if shape == self.P_SQUARE:
  117. path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
  118. elif shape == self.P_ROUND:
  119. path.addEllipse(point, d / 2.0, d / 2.0)
  120. else:
  121. assert False, "unsupported vertex shape"
  122. def nearestVertex(self, point, epsilon):
  123. min_distance = float('inf')
  124. min_i = None
  125. for i, p in enumerate(self.points):
  126. dist = distance(p - point)
  127. if dist <= epsilon and dist < min_distance:
  128. min_distance = dist
  129. min_i = i
  130. return min_i
  131. def containsPoint(self, point):
  132. return self.makePath().contains(point)
  133. def makePath(self):
  134. path = QtGui.QPainterPath(self.points[0])
  135. for p in self.points[1:]:
  136. path.lineTo(p)
  137. return path
  138. def boundingRect(self):
  139. return self.makePath().boundingRect()
  140. def moveBy(self, offset):
  141. self.points = [p + offset for p in self.points]
  142. def moveVertexBy(self, i, offset):
  143. self.points[i] = self.points[i] + offset
  144. def highlightVertex(self, i, action):
  145. self._highlightIndex = i
  146. self._highlightMode = action
  147. def highlightClear(self):
  148. self._highlightIndex = None
  149. def copy(self):
  150. shape = Shape(self.label)
  151. shape.points = [p for p in self.points]
  152. shape.fill = self.fill
  153. shape.selected = self.selected
  154. shape._closed = self._closed
  155. if self.line_color != Shape.line_color:
  156. shape.line_color = self.line_color
  157. if self.fill_color != Shape.fill_color:
  158. shape.fill_color = self.fill_color
  159. return shape
  160. def __len__(self):
  161. return len(self.points)
  162. def __getitem__(self, key):
  163. return self.points[key]
  164. def __setitem__(self, key, value):
  165. self.points[key] = value