import numpy

class scaler(object):
    def __init__(self, box, bitmap_size):
        self.box = box
        self.bitmap_size = bitmap_size
        
        self.scale = min(self.bitmap_size[0] / (self.box[1]-self.box[0]),
                         self.bitmap_size[1] / (self.box[3]-self.box[2]))
        self.box_center = (0.5* box[0] + 0.5*box[1], 0.5*box[2]  + 0.5*box[3])
        self.box_top_left = (self.box_center[0] - 0.5*bitmap_size[0] / self.scale,
                             self.box_center[1] + 0.5*bitmap_size[1] / self.scale)
        
    def map_shape(self, shape):
        return gds_shape(numpy.asarray((shape.points * [1, -1] - [self.box_top_left[0], -self.box_top_left[1]]) * self.scale  , dtype = numpy.integer))
    
    def map_shape_to_list(self, shape):
        return list(numpy.asarray(numpy.reshape((shape.points * [1, -1] - [self.box_top_left[0], -self.box_top_left[1]]) * self.scale , numpy.prod(numpy.size(shape.__points__))), dtype=numpy.integer))

    def map_coordinate(self, coordinate):
        return ((coordinate[0]-self.box_top_left[0]) * self.scale,
                (-coordinate[1]+self.box_top_left[1]) * self.scale)
        
    def map_length(self, length):
        return length * self.scale
