-import cairo
-
-
-class Box:
- def __init__(self, x, y, w, h, r, g, b):
- self.x = x
- self.y = y
- self.w = w
- self.h = h
- self.r = r
- self.g = g
- self.b = b
-
- def collision(self, scene):
- for c in scene:
- if (
- self is not c
- and max(self.x, c.x) <= min(self.x + self.w, c.x + c.w)
- and max(self.y, c.y) <= min(self.y + self.h, c.y + c.h)
- ):
- return True
- return False
-
-
-def scene2tensor(xh, yh, scene, size=64):
- width, height = size, size
- pixel_map = torch.ByteTensor(width, height, 4).fill_(255)
- data = pixel_map.numpy()
- surface = cairo.ImageSurface.create_for_data(
- data, cairo.FORMAT_ARGB32, width, height
- )
-
- ctx = cairo.Context(surface)
- ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)
-
- for b in scene:
- ctx.move_to(b.x * size, b.y * size)
- ctx.rel_line_to(b.w * size, 0)
- ctx.rel_line_to(0, b.h * size)
- ctx.rel_line_to(-b.w * size, 0)
- ctx.close_path()
- ctx.set_source_rgba(b.r, b.g, b.b, 1.0)
- ctx.fill()