def test_make_candidates_data(): r1 = R(5, 5, 10, 30) cloud = RectangleCloud([r1]) ## Make a spot facing down to infinity, i.e. without intersection occ = cloud.get_occupied_rect() spot = R(occ.x - INF, occ.y - INF, occ.w + 2 * INF, INF) intsec = occ.get_intersection(spot) assert not intsec rect = R(0, 0, 10, 10) data = cloud.make_candidates_data(spot, rect) cand = R(5, -5, 10, 10) assert data == [(cand, intsec, spot)] ## test intersecting spot r2 = R(15, 20, 10, 10) cloud._rects.append(r2) cloud._invalidate() occ = cloud.get_occupied_rect() spot = R(r1.x + r1.w, r2.y + r2.h, occ.x + occ.w - (r2.x + r2.w) + INF, \ occ.y + occ.h - (r2.y + r2.h) + INF) intsec = occ.get_intersection(spot) assert intsec data = cloud.make_candidates_data(spot, rect) cand = R(spot.x, spot.y, rect.w, rect.h) assert data == [(cand, intsec, spot)]
def test_rate_candidates(): # r1, r2, r3 = R(0, 10, 10, 10), R(10, 0, 10, 30), R(20, 10, 10, 10) # cloud = RectangleCloud([r1, r2, r3], steps=12) # occ = cloud.get_occupied_rect() tobefit = R(0, 0, 20, 20) # spots = cloud.get_spots_for_rectangle(tobefit) # candidates_data = set() # for sp in spots: # data = cloud.make_candidates_data(sp, tobefit, occ) # candidates_data.update(data) # candidates_data = list(candidates_data) # rated = cloud.rate_candidates(candidates_data, occ) # expected_candidates = [ # R(r2.x + r2.w, r3.y + r3.h, tobefit.w, tobefit.h), # R(r2.x - tobefit.w, r1.y + r1.h, tobefit.w, tobefit.h), # R(r2.x - tobefit.w, r1.y - tobefit.h, tobefit.w, tobefit.h), # R(r2.x + r2.w, r3.y - tobefit.h, tobefit.w, tobefit.h) # ] # assert (sorted([n[1] for n in sorted(rated, key=lambda t:t[0])][-4:], key=tuple) # == sorted(expected_candidates, key=tuple)) ## Try again with different coordinates. r1, r2, r3 = R(0, 15, 15, 10), R(15, 0, 10, 30), R(25, 10, 5, 10) cloud = RectangleCloud([r1, r2, r3]) occ = cloud.get_occupied_rect() spots = cloud.get_spots_for_rectangle(tobefit) print("\n\n>>>>>>>>>>>>>>>>>>>>candidates_data>>>>>>>>>>>>>>>>>>>") print("spots", spots) candidates_data = set() for sp in spots: data = cloud.make_candidates_data(sp, tobefit) print("sp", sp) print("data", data) candidates_data.update(data) candidates_data = list(candidates_data) print(candidates_data) print("<<<<<<<<<<<<<<<<<<<<<candidates_data<<<<<<<<<<<<<<<<<<<") rated = cloud.rate_candidates(candidates_data) for i, (ratio, c) in enumerate(rated): print(i, ratio, c) print(c.debuginfo) print() print([n for n in sorted(rated, key=lambda t:t[0])]) expected_candidates = [ R(r2.x + r2.w, r3.y + r3.h, tobefit.w, tobefit.h), R(r2.x - tobefit.w, r1.y + r1.h, tobefit.w, tobefit.h), R(r2.x - tobefit.w, r1.y - tobefit.h, tobefit.w, tobefit.h), R(r2.x + r2.w, r3.y - tobefit.h, tobefit.w, tobefit.h) ] print("\n", "spots", spots) print("expected_candidates", expected_candidates, len(expected_candidates)) print("actual candidates", rated, len(rated)) print() assert (sorted([n[1] for n in sorted(rated, key=lambda t:t[0])][-4:], key=tuple) == sorted(expected_candidates, key=tuple))