def shell_damage(shell, tank):
    '''return health damage'''
    pessimistic_tank = copy(tank)
    pessimistic_tank.width *= 1.1
    pessimistic_tank.height *= 1.1
    front, right, back, left = utils.get_borders(pessimistic_tank)
    # front, right, back, left = utils.get_borders(tank)

    next_shell_x = shell.x + shell.speedX * 1000.
    next_shell_y = shell.y + shell.speedY * 1000.

    borders_with_intersections = [(b,
            geometry.intervals_intersection(
                    b[0], b[1], b[2], b[3], shell.x, shell.y, next_shell_x, next_shell_y)) for
            b in front, right, back, left]

    borders_with_intersections = filter(lambda bi: bi[1] is not None, borders_with_intersections)
    if not borders_with_intersections:
        return 0.
    border, intersection_point = min(borders_with_intersections,
            key=lambda b: math.hypot(b[1][0] - shell.x, b[1][1] - shell.y))

    angle = geometry.get_angle(border[0] - border[2], border[1] - border[3], shell.speedX, shell.speedY)
    if angle > math.pi / 2:
        angle = math.pi - angle
    angle = math.pi / 2. - angle

    r1 = math.hypot(border[0] - intersection_point[0], border[1] - intersection_point[1])
    r2 = math.hypot(border[2] - intersection_point[0], border[3] - intersection_point[1])
    r = r1 + r2
    dist_factor = (r - 2. * min(r1, r2)) / r

    return coeff_by_angle(shell, geometry.rad_to_degree(angle)) * coeff_by_dist_factor(dist_factor)
Esempio n. 2
0
def cross_boundaries(tank, world):
    tank_borders = utils.get_borders(tank)

    for world_border in utils.get_world_borders():
        for tank_border in tank_borders:
            x1, y1, x2, y2 = tank_border
            if geometry.are_intervals_intersect(x1, y1, x2, y2, *world_border):
                return True

    def can_cross(unit):
        unit_size = math.hypot(unit.width / 2., unit.height / 2.)
        tank_size = math.hypot(tank.width / 2., tank.height / 2.)
        return unit_size + tank_size > math.hypot(unit.x - tank.x, unit.y - tank.y)

    # for unit in world.obstacles:
    # for unit in world.obstacles + utils.other_tanks(world, tank):
    for unit in filter(lambda u: can_cross(u), world.obstacles + utils.other_tanks(world, tank)):
        for border in utils.get_borders(unit):
            for tank_border in tank_borders:
                x1, y1, x2, y2 = tank_border
                if geometry.are_intervals_intersect(x1, y1, x2, y2, *border):
                    return True

    return False
Esempio n. 3
0
    def __call__(self, image, segmentation, instance_segmentation=None):

        if self.train_trainsforms:
            
            if self.use_resize:
                image = self.resize(transforms.transforms.ToPILImage()(image))
                segmentation = scipy.misc.imresize(segmentation, (286, 286), 'nearest')
                if instance_segmentation is not None:
                    instance_segmentation = scipy.misc.imresize(instance_segmentation, 
                                                                (286, 286), 'nearest')
            else:
                image = transforms.transforms.ToPILImage()(image)          

            # mirroring
            if np.random.random() < 0.5:
                image = image.transpose(Image.FLIP_LEFT_RIGHT)
                segmentation = np.flip(segmentation, 1)
                if instance_segmentation is not None:
                    instance_segmentation = np.flip(instance_segmentation, 1)
            
            if self.use_resize:
                # random crop
                x_offset = np.random.randint(0, 21)
                y_offset = np.random.randint(0, 21)

                image = image.crop((x_offset, y_offset, x_offset + 256, y_offset + 256))
                segmentation = segmentation[x_offset: x_offset + 256, y_offset: y_offset + 256]
                
                if instance_segmentation is not None:
                    instance_segmentation = instance_segmentation[x_offset: x_offset + 256,
                                                                  y_offset: y_offset + 256]

        else:
            
            if self.use_resize:
                image = self.resize(transforms.transforms.ToPILImage()(image))
                segmentation = scipy.misc.imresize(segmentation, (256, 256), 'nearest')
                if instance_segmentation is not None:
                    instance_segmentation = scipy.misc.imresize(instance_segmentation, 
                                                                (286, 286), 'nearest')
            else:
                image = transforms.transforms.ToPILImage()(image)

        image_tensor = self.to_tensor(image)
        image_tensor = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(image_tensor)
        if self.use_rgb:
            segmentation_tensor = self.colors[segmentation]
            segmentation_tensor = self.to_tensor(segmentation_tensor)
            segmentation_tensor = transforms.Normalize((0.5, 0.5, 0.5),
                                                       (0.5, 0.5, 0.5))(segmentation_tensor)
        else:
            segmentation_tensor = (np.arange(self.num_classes) == segmentation[..., None]).astype(int)
            segmentation_tensor = np.rollaxis(segmentation_tensor, -1, 0)
        segmentation_tensor = torch.FloatTensor(segmentation_tensor)
        
        if instance_segmentation is not None:
            borders = get_borders(instance_segmentation)
            borders_tensor = torch.FloatTensor(borders).view(1, borders.shape[0], borders.shape[1])
            instance_segmentation_tensor = torch.LongTensor(instance_segmentation.copy()).view(1, 
                                                                                             borders.shape[0], 
                                                                                             borders.shape[1])
            return image_tensor, segmentation_tensor, instance_segmentation_tensor, borders_tensor
            
        return image_tensor, segmentation_tensor