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))