def test_max_width_height_diag(self): for i in range(1, 101): sizes = [(j, i + 1 - j) for j in range(1, i + 1)] pos = rpack.pack(sizes, max_width=i) w, _ = rpack.bbox_size(sizes, pos) self.assertEqual(w, i) pos = rpack.pack(sizes, max_height=i) _, h = rpack.bbox_size(sizes, pos) self.assertEqual(h, i)
def main(args): random.seed(0) t0 = time.time() while time.time() - t0 < args.duration: func = random.choices([randrec, randneg, randtype], [0.8, 0.1, 0.1]) sizes = func[0]() try: pos = rpack.pack(sizes) except Exception: pass else: if pos: rpack.packing_density(sizes, pos) rpack.bbox_size(sizes, pos) rpack.overlapping(sizes, pos)
def test_max_width_height_both3(self): sizes = [(3, 4), (3, 4), (4, 3), (3, 4), (3, 4)] max_height = max_width = 9 pos = rpack.pack(sizes, max_width=max_width, max_height=max_height) self.assertLessEqual(max(*rpack.bbox_size(sizes, pos)), max_width) index = rpack._core.overlapping(sizes, pos) self.assertFalse(index)
def test_max_width_height_both2(self): sizes = [(2736, 3648), (2736, 3648), (3648, 2736), (2736, 3648), (2736, 3648)] max_height = max_width = 14130 pos = rpack.pack(sizes, max_width=max_width, max_height=max_height) self.assertLessEqual(max(*rpack.bbox_size(sizes, pos)), max_width) index = rpack._core.overlapping(sizes, pos) self.assertFalse(index)
def test_max_width_height_both4(self): sizes = [(3, 4), (3, 4), (4, 3), (3, 4), (3, 4)] max_width, max_height = (16, 4) pos = rpack.pack(sizes, max_width=max_width, max_height=max_height) w, h = rpack.bbox_size(sizes, pos) self.assertLessEqual(w, max_width) self.assertLessEqual(h, max_height) index = rpack._core.overlapping(sizes, pos) self.assertFalse(index)
def plot_packing_golden_ratio(data): candidates = list() for n in range(40, 101, 10): for m in range(100, 1001, 100): for rec, pos, _ in data[n, m]: w, h = rpack.bbox_size(rec, pos) rho = rpack.packing_density(rec, pos) candidates.append((round(abs(w / h - 1.61803398875), 2), -rho, rec, pos)) candidates.sort() _, _, rec, pos = candidates[0] p = PlotPacking(rec, pos, title=f', {len(rec)} rectangles.') while p.feed(): pass p.save(os.path.join(args.output_dir, f'packing_phi'))
def candidates(): """Find bbox with golden ratio proportions""" cands = list() try: i = 0 while True: i += 1 random.seed(i) sizes = [(random.randint(50, 1000), random.randint(50, 1000)) for _ in range(random.randint(30, 40))] pos = rpack.pack(sizes) rho = rpack.packing_density(sizes, pos) w, h = rpack.bbox_size(sizes, pos) if abs(w/h - 1.61803398875) < 0.01: print('Found candidate:', rho, 'seed', i) cands.append((rho, sizes, pos, i)) except KeyboardInterrupt: pass cands.sort(reverse=True) return cands[0:10]
def test_max_width_height_long_running(self): random.seed(123) try: while True: n = random.randint(1, 20) m = 20 sizes = [(random.randint(1, m), random.randint(1, m)) for _ in range(n)] max_width = random.randint(1, m*n + 10) max_height = random.randint(1, m*n + 10) try: pos = rpack.pack(sizes, max_width=max_width, max_height=max_height) except rpack.PackingImpossibleError: continue else: w, h = rpack.bbox_size(sizes, pos) assert w <= max_width assert h <= max_height assert not rpack.overlapping(sizes, pos) except KeyboardInterrupt: print("Stopped.")
def plot_animations(data): candidates = list() for n in range(40, 101, 10): for m in range(100, 1001, 100): for rec, pos, _ in data[n, m]: w, h = rpack.bbox_size(rec, pos) rho = rpack.packing_density(rec, pos) candidates.append((round(abs(w / h - 0.61803398875), 2), -rho, rec, pos)) candidates.sort() _, _, sizes, pos = candidates[0] # Sort the rectangles so the animation will plot them from left to # right. sizes = [s for s, _ in sorted(zip(sizes, pos), key=lambda x: x[1])] pos.sort() p = PlotPacking(sizes, pos, gridlines=True, trim=True) p.animation(os.path.join(args.output_dir, f'example_grid'), 60, 20) p = PlotPacking(sizes, pos, gridlines=True, trim=True) while p.feed(): pass p.save(os.path.join(args.output_dir, f'example_grid'))
def test_max_width_height_both(self): sizes = [(j, j) for j in range(1, 101)] pos = rpack.pack(sizes, max_width=611, max_height=611) w, h = rpack.bbox_size(sizes, pos) self.assertLessEqual(w, 611) self.assertLessEqual(h, 611)