utils.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from numpy import array, diag, dot, cos, sin
  2. from src import RectangularArea
  3. class BoundingBoxArea(RectangularArea):
  4. def __init__(self, x, y, points):
  5. super(BoundingBoxArea, self).__init__(x, y, 0, 0)
  6. self.points = array(points).T
  7. self.update_bounds()
  8. def translate_points(self, tx, ty):
  9. self.points += [[tx], [ty]]
  10. def scale_points(self, scale, cx, cy):
  11. self.translate_points(-cx, -cy)
  12. self.points = dot(diag([scale, scale]), self.points)
  13. self.translate_points(cx, cy)
  14. def rotate_points(self, angle, cx, cy):
  15. cosa = cos(angle)
  16. sina = sin(angle)
  17. mat = array([[cosa, -sina], [sina, cosa]])
  18. self.translate_points(-cx, -cy)
  19. self.points = dot(mat, self.points)
  20. self.translate_points(cx, cy)
  21. def update_bounds(self):
  22. min_x, min_y = self.points.min(1)
  23. max_x, max_y = self.points.max(1)
  24. self.set_size(max_x - min_x, max_y - min_y)
  25. if min_x or min_y:
  26. self.translate(min_x, min_y)
  27. self.translate_points(-min_x, -min_y)