Ejemplo n.º 1
0
 def timed(*args, **kwargs):
     tic = time_function()
     result = method(*args, **kwargs)
     log.debug('%s executed in %.4f seconds.',
               method.__name__,
               time_function() - tic)
     return result
Ejemplo n.º 2
0
 def timed(*args, **kwargs):
     tic = time_function()
     result = method(*args, **kwargs)
     log.debug('%s executed in %.4f seconds.',
               method.__name__,
               time_function() - tic)
     return result
Ejemplo n.º 3
0
def multipack(polygons, 
              sheet_size     = None,
              iterations     = 50,
              density_escape = .985,
              buffer_dist    = 0.09,
              plot           = False,
              return_all     = False):
    '''
    Run multiple iterations of rectangle packing, by randomly permutating the insertion order

    If sheet size isn't specified, it creates a large sheet that can fit all of the polygons
    '''
    rectangles, transforms_obb  = polygons_obb(polygons)
    rectangles                 += 2.0*buffer_dist
    polygon_area                = np.array([p.area for p in polygons])


    tic             = time_function()  
    overall_density = 0
    
    if sheet_size==None:
        max_dim    = np.max(rectangles, axis=0)
        sum_dim    = np.sum(rectangles, axis=0)
        sheet_size = [sum_dim[0], max_dim[1]*2]

    for i in range(iterations):
        density, offset, inserted, sheet = pack_rectangles(rectangles, 
                                                           sheet_size = sheet_size, 
                                                           shuffle    = (i != 0))
        if density > overall_density:
            overall_density  = density
            overall_offset   = offset
            overall_inserted = inserted
            overall_sheet    = sheet
            if density > density_escape: break
            
    toc = time_function()       
    log.info('Packing finished %i iterations in %f seconds', i+1, toc-tic)
    log.info('%i/%i parts were packed successfully', np.sum(overall_inserted), len(polygons))
    log.info('Final rectangular density is %f.', overall_density)
    
    polygon_density = np.sum(polygon_area[overall_inserted])/np.product(overall_sheet)
    log.info('Final polygonal density is %f.', polygon_density)

    transforms_obb     = transforms_obb[overall_inserted]
    transforms_packed  = transforms_obb.copy()
    transforms_packed.reshape(-1,9)[:,[2,5]] += overall_offset + buffer_dist
 
    if plot: 
        transform_polygons(np.array(polygons)[overall_inserted], transforms_packed, plot=True)
        plt.show()

    rectangles -= 2.0*buffer_dist
    
    if return_all:
        return (overall_inserted, 
                transforms_packed, 
                transforms_obb, 
                overall_sheet,
                rectangles[overall_inserted])

    return overall_inserted, transforms_packed