def solve(self, rects_to_place, start_index, scorer, priority_pick=None, presorter_key=None): len_rects_to_place = len(rects_to_place) rects_to_place_prio = rects_to_place[start_index:start_index + priority_pick] start_index_rest = start_index + priority_pick if presorter_key is not None: rects_to_place_prio.sort(key=presorter_key) maxrect = MaxRect(self.width, self.height) placer = Placer(maxrect.free_rects, rects_to_place_prio, scorer) while placer.rects_to_place: next_placement = placer.get_best() if next_placement is None: raise NoFit( "Could place priority rects with {} left-overs".format( len(placer.rects_to_place))) new_placed_rect = next_placement[TO_PLACE_RECT] new_placed_rect = rectangle.set_position( new_placed_rect, next_placement[FREE_RECT][COOR][0][0], next_placement[FREE_RECT][COOR][1][0]) self._place_rect(new_placed_rect) new_free_rects, removed_free_rects = maxrect.cut_off( new_placed_rect) placer.remove(removed_free_rects, next_placement[TO_PLACE]) placer.insert(new_free_rects) for index in range(start_index_rest, len_rects_to_place): rect_to_place = rects_to_place[index] next_placement = placer.get_best_for(rect_to_place) if next_placement is None: print("Partly solved with packing density {:.0%}".format( self.packing_density())) return index # resolve rest; do not prepend here or it will be slow new_placed_rect = next_placement[TO_PLACE_RECT] new_placed_rect = rectangle.set_position( new_placed_rect, next_placement[FREE_RECT][COOR][0][0], next_placement[FREE_RECT][COOR][1][0]) self._place_rect(new_placed_rect) new_free_rects, removed_free_rects = maxrect.cut_off( new_placed_rect) placer.remove(removed_free_rects, None) placer.insert(new_free_rects) print("All solved with packing density {:.0%}".format( self.packing_density())) return index
def solve(self, rects_to_place, start_index, scorer, priority_pick=None, presorter_key=None): len_rects_to_place = len(rects_to_place) rects_to_place_prio = rects_to_place[start_index : start_index + priority_pick] start_index_rest = start_index + priority_pick if presorter_key is not None: rects_to_place_prio.sort(key=presorter_key) maxrect = MaxRect(self.width, self.height) placer = Placer(maxrect.free_rects, rects_to_place_prio, scorer) while placer.rects_to_place: next_placement = placer.get_best() if next_placement is None: raise NoFit("Could place priority rects with {} left-overs".format(len(placer.rects_to_place))) new_placed_rect = next_placement[TO_PLACE_RECT] new_placed_rect = rectangle.set_position( new_placed_rect, next_placement[FREE_RECT][COOR][0][0], next_placement[FREE_RECT][COOR][1][0] ) self._place_rect(new_placed_rect) new_free_rects, removed_free_rects = maxrect.cut_off(new_placed_rect) placer.remove(removed_free_rects, next_placement[TO_PLACE]) placer.insert(new_free_rects) for index in range(start_index_rest, len_rects_to_place): rect_to_place = rects_to_place[index] next_placement = placer.get_best_for(rect_to_place) if next_placement is None: print("Partly solved with packing density {:.0%}".format(self.packing_density())) return index # resolve rest; do not prepend here or it will be slow new_placed_rect = next_placement[TO_PLACE_RECT] new_placed_rect = rectangle.set_position( new_placed_rect, next_placement[FREE_RECT][COOR][0][0], next_placement[FREE_RECT][COOR][1][0] ) self._place_rect(new_placed_rect) new_free_rects, removed_free_rects = maxrect.cut_off(new_placed_rect) placer.remove(removed_free_rects, None) placer.insert(new_free_rects) print("All solved with packing density {:.0%}".format(self.packing_density())) return index
from maxrect_pack import rect from maxrect_pack.maxrect import MaxRect from maxrect_pack.rect import COOR from maxrect_pack.rect_chosers import simple_smallest_x_fit from maxrect_pack.recthelper import random_rects WIDTH = 20 HEIGHT = 20 if __name__ == '__main__': m = MaxRect(WIDTH, HEIGHT) rects_cut = random_rects(WIDTH, HEIGHT, 4, 4, 30) for r in rects_cut: container_rect = simple_smallest_x_fit(r, m.free_rects) r = rect.set_position(r, container_rect[COOR][0][0], container_rect[COOR][1][0]) m.cut_off(r) m.plot()
from maxrect_pack import rect from maxrect_pack.maxrect import MaxRect from maxrect_pack.rect import COOR from maxrect_pack.rect_chosers import simple_smallest_x_fit from maxrect_pack.recthelper import random_rects WIDTH = 20 HEIGHT = 20 if __name__ == "__main__": m = MaxRect(WIDTH, HEIGHT) rects_cut = random_rects(WIDTH, HEIGHT, 4, 4, 30) for r in rects_cut: container_rect = simple_smallest_x_fit(r, m.free_rects) r = rect.set_position(r, container_rect[COOR][0][0], container_rect[COOR][1][0]) m.cut_off(r) m.plot()