shape.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. # !/usr/bin/python
  14. # -*- coding: utf-8 -*-
  15. import math
  16. import sys
  17. from PyQt5.QtCore import QPointF
  18. from PyQt5.QtGui import QColor, QPen, QPainterPath, QFont
  19. from libs.utils import distance
  20. DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128)
  21. DEFAULT_FILL_COLOR = QColor(255, 0, 0, 128)
  22. DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255)
  23. DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
  24. DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
  25. DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
  26. DEFAULT_LOCK_COLOR = QColor(255, 0, 255)
  27. MIN_Y_LABEL = 10
  28. class Shape(object):
  29. P_SQUARE, P_ROUND = range(2)
  30. MOVE_VERTEX, NEAR_VERTEX = range(2)
  31. # The following class variables influence the drawing
  32. # of _all_ shape objects.
  33. line_color = DEFAULT_LINE_COLOR
  34. fill_color = DEFAULT_FILL_COLOR
  35. select_line_color = DEFAULT_SELECT_LINE_COLOR
  36. select_fill_color = DEFAULT_SELECT_FILL_COLOR
  37. vertex_fill_color = DEFAULT_VERTEX_FILL_COLOR
  38. hvertex_fill_color = DEFAULT_HVERTEX_FILL_COLOR
  39. point_type = P_ROUND
  40. point_size = 8
  41. scale = 1.0
  42. def __init__(self, label=None, line_color=None, difficult=False, key_cls="None", paintLabel=False, paintIdx=False):
  43. self.label = label
  44. self.idx = None # bbox order, only for table annotation
  45. self.points = []
  46. self.fill = False
  47. self.selected = False
  48. self.difficult = difficult
  49. self.key_cls = key_cls
  50. self.paintLabel = paintLabel
  51. self.paintIdx = paintIdx
  52. self.locked = False
  53. self.direction = 0
  54. self.center = None
  55. self.epsilon = 5 # same as canvas
  56. self._highlightIndex = None
  57. self._highlightMode = self.NEAR_VERTEX
  58. self._highlightSettings = {
  59. self.NEAR_VERTEX: (4, self.P_ROUND),
  60. self.MOVE_VERTEX: (1.5, self.P_SQUARE),
  61. }
  62. self.fontsize = 8
  63. self._closed = False
  64. if line_color is not None:
  65. # Override the class line_color attribute
  66. # with an object attribute. Currently this
  67. # is used for drawing the pending line a different color.
  68. self.line_color = line_color
  69. def rotate(self, theta):
  70. for i, p in enumerate(self.points):
  71. self.points[i] = self.rotatePoint(p, theta)
  72. self.direction -= theta
  73. self.direction = self.direction % (2 * math.pi)
  74. def rotatePoint(self, p, theta):
  75. order = p - self.center
  76. cosTheta = math.cos(theta)
  77. sinTheta = math.sin(theta)
  78. pResx = cosTheta * order.x() + sinTheta * order.y()
  79. pResy = - sinTheta * order.x() + cosTheta * order.y()
  80. pRes = QPointF(self.center.x() + pResx, self.center.y() + pResy)
  81. return pRes
  82. def close(self):
  83. self.center = QPointF((self.points[0].x() + self.points[2].x()) / 2,
  84. (self.points[0].y() + self.points[2].y()) / 2)
  85. self._closed = True
  86. def reachMaxPoints(self):
  87. if len(self.points) >= 4:
  88. return True
  89. return False
  90. def addPoint(self, point):
  91. if self.reachMaxPoints() and self.closeEnough(self.points[0], point):
  92. self.close()
  93. else:
  94. self.points.append(point)
  95. def closeEnough(self, p1, p2):
  96. return distance(p1 - p2) < self.epsilon
  97. def popPoint(self):
  98. if self.points:
  99. return self.points.pop()
  100. return None
  101. def isClosed(self):
  102. return self._closed
  103. def setOpen(self):
  104. self._closed = False
  105. def paint(self, painter):
  106. if self.points:
  107. color = self.select_line_color if self.selected else self.line_color
  108. pen = QPen(color)
  109. # Try using integer sizes for smoother drawing(?)
  110. # pen.setWidth(max(1, int(round(2.0 / self.scale))))
  111. painter.setPen(pen)
  112. line_path = QPainterPath()
  113. vrtx_path = QPainterPath()
  114. line_path.moveTo(self.points[0])
  115. # Uncommenting the following line will draw 2 paths
  116. # for the 1st vertex, and make it non-filled, which
  117. # may be desirable.
  118. # self.drawVertex(vrtx_path, 0)
  119. for i, p in enumerate(self.points):
  120. line_path.lineTo(p)
  121. self.drawVertex(vrtx_path, i)
  122. if self.isClosed():
  123. line_path.lineTo(self.points[0])
  124. painter.drawPath(line_path)
  125. painter.drawPath(vrtx_path)
  126. painter.fillPath(vrtx_path, self.vertex_fill_color)
  127. # Draw text at the top-left
  128. if self.paintLabel:
  129. min_x = sys.maxsize
  130. min_y = sys.maxsize
  131. for point in self.points:
  132. min_x = min(min_x, point.x())
  133. min_y = min(min_y, point.y())
  134. if min_x != sys.maxsize and min_y != sys.maxsize:
  135. font = QFont()
  136. font.setPointSize(self.fontsize)
  137. font.setBold(True)
  138. painter.setFont(font)
  139. if self.label is None:
  140. self.label = ""
  141. if min_y < MIN_Y_LABEL:
  142. min_y += MIN_Y_LABEL
  143. painter.drawText(min_x, min_y, self.label)
  144. # Draw number at the top-right
  145. if self.paintIdx:
  146. min_x = sys.maxsize
  147. min_y = sys.maxsize
  148. for point in self.points:
  149. min_x = min(min_x, point.x())
  150. min_y = min(min_y, point.y())
  151. if min_x != sys.maxsize and min_y != sys.maxsize:
  152. font = QFont()
  153. font.setPointSize(self.fontsize)
  154. font.setBold(True)
  155. painter.setFont(font)
  156. text = ''
  157. if self.idx != None:
  158. text = str(self.idx)
  159. if min_y < MIN_Y_LABEL:
  160. min_y += MIN_Y_LABEL
  161. painter.drawText(min_x, min_y, text)
  162. if self.fill:
  163. color = self.select_fill_color if self.selected else self.fill_color
  164. painter.fillPath(line_path, color)
  165. def drawVertex(self, path, i):
  166. d = self.point_size / self.scale
  167. shape = self.point_type
  168. point = self.points[i]
  169. if i == self._highlightIndex:
  170. size, shape = self._highlightSettings[self._highlightMode]
  171. d *= size
  172. if self._highlightIndex is not None:
  173. self.vertex_fill_color = self.hvertex_fill_color
  174. else:
  175. self.vertex_fill_color = Shape.vertex_fill_color
  176. if shape == self.P_SQUARE:
  177. path.addRect(point.x() - d / 2, point.y() - d / 2, d, d)
  178. elif shape == self.P_ROUND:
  179. path.addEllipse(point, d / 2.0, d / 2.0)
  180. else:
  181. assert False, "unsupported vertex shape"
  182. def nearestVertex(self, point, epsilon):
  183. for i, p in enumerate(self.points):
  184. if distance(p - point) <= epsilon:
  185. return i
  186. return None
  187. def containsPoint(self, point):
  188. return self.makePath().contains(point)
  189. def makePath(self):
  190. path = QPainterPath(self.points[0])
  191. for p in self.points[1:]:
  192. path.lineTo(p)
  193. return path
  194. def boundingRect(self):
  195. return self.makePath().boundingRect()
  196. def moveBy(self, offset):
  197. self.points = [p + offset for p in self.points]
  198. def moveVertexBy(self, i, offset):
  199. self.points[i] = self.points[i] + offset
  200. def highlightVertex(self, i, action):
  201. self._highlightIndex = i
  202. self._highlightMode = action
  203. def highlightClear(self):
  204. self._highlightIndex = None
  205. def copy(self):
  206. shape = Shape("%s" % self.label)
  207. shape.points = [p for p in self.points]
  208. shape.center = self.center
  209. shape.direction = self.direction
  210. shape.fill = self.fill
  211. shape.selected = self.selected
  212. shape._closed = self._closed
  213. if self.line_color != Shape.line_color:
  214. shape.line_color = self.line_color
  215. if self.fill_color != Shape.fill_color:
  216. shape.fill_color = self.fill_color
  217. shape.difficult = self.difficult
  218. shape.key_cls = self.key_cls
  219. return shape
  220. def __len__(self):
  221. return len(self.points)
  222. def __getitem__(self, key):
  223. return self.points[key]
  224. def __setitem__(self, key, value):
  225. self.points[key] = value