-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect_test.py
executable file
·285 lines (260 loc) · 11.2 KB
/
rect_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
import math
import numpy as np
import unittest
import rectangles as r
import rect_lagrange as rl
import rect_fit_sides as rfs
from random import shuffle
from scipy.optimize import fsolve
class TestRectangles(unittest.TestCase):
def print_rects(self, R):
print('\nRectangles:')
for ri in R:
print(ri)
def get_matrix_from_finiteset(self, N, sol_set):
mat = np.zeros((2, N))
if len(sol_set) != 1:
print('Warning: more than one solution')
for sol in sol_set:
break
for i in range(N):
mat[0, i] = sol[i]
for i in range(N):
mat[1, i] = sol[N + i]
return mat
# Solve rectangulations numerically with Lagrange multipliers.
# With this method we need quite good initial values...
def test_lagrange_method_numerical(self):
w = 320
h = 180
k = 1.5
diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2]]
for diag in diagonals:
N = len(diag)
B, dim = r.do_diagonal_rectangulation(diag)
dim[0, :] *= w
dim[1, :] *= h
E = r.build_rectangulation_equations(B)
# Number of lambda values will be same as number of eqs (N+1)
initial_est = np.ones(dim.size + N + 1)
initial_est[0:dim.size] = dim.reshape(dim.size)
def dfunc(X): return rl.get_derivative_from_eqs(X, E, w, h, k)
X = fsolve(dfunc, initial_est)
self.assertAlmostEqual(0.,
rl.get_optimization_f_val(X, E, w, h, k))
# Solve rectangulations analitycally (using sympy) with Lagrange
# multipliers.
def test_lagrange_method_analitycal(self):
w = 320
h = 180
k = 1.5
# [2, 0, 4, 1, 3] can be handled, but takes a little bit
diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2]]
for diag in diagonals:
N = len(diag)
B, dim = r.do_diagonal_rectangulation(diag)
dim[0, :] *= w
dim[1, :] *= h
E = r.build_rectangulation_equations(B)
sol = rl.solve_rectangle_eqs(E, w, h, k)
print(sol)
mat_sol = self.get_matrix_from_finiteset(N, sol)
r.draw_resized_rectangles(B, mat_sol, w, h)
def test_fit_sides(self):
w = 320
h = 180
k = 1.5
diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2], [2, 0, 4, 1, 3]]
for diag in diagonals:
N = len(diag)
B, dim = r.do_diagonal_rectangulation(diag)
dim[0, :] *= w
dim[1, :] *= h
E = r.build_rectangulation_equations(B)
sol = rfs.solve_fit_rectangles(E, B, w, h, k)
mat_sol = self.get_matrix_from_finiteset(N, sol)
r.draw_resized_rectangles(B, mat_sol, w, h)
def test_scipy_minimize(self):
w = 320
h = 180
k = 1.5
c = 0.05
Bc = [np.array([[0, 1, 2],
[0, 1, 2],
[0, 1, 2]], dtype=int),
np.array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2]], dtype=int),
np.array([[0, 0, 2],
[1, 1, 2],
[1, 1, 2]], dtype=int),
np.array([[0, 1, 1, 3, 3],
[0, 1, 1, 3, 3],
[2, 2, 2, 3, 3],
[2, 2, 2, 3, 3],
[2, 2, 2, 4, 4]], dtype=int)]
Ec = [[[1., 1., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.],
[0., 0., 0., 1., -1., 0.],
[0., 0., 0., 0., 1., -1.]],
[[1., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., 1.],
[1., -1., 0., 0., 0., 0.],
[0., 1., -1., 0., 0., 0.]],
[[1., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 1., 0.],
[1., -1., 0., 0., 0., 0.],
[0., 0., 0., 1., 1., -1.]],
[[1., 1., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 1., 0., 0.],
[1., 1., -1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., -1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., -1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 1., -1., -1.]]]
sol_c = [np.array([[106.66666667, 106.66666667, 106.66666667],
[180., 180., 180.]]),
np.array([[320., 320., 320.],
[60., 60., 60.]]),
np.array([[203.63636358, 203.63636358, 116.36363642],
[90.00000004, 89.99999996, 180.]]),
np.array([[94.76075857, 94.76075848, 189.52151705,
130.47848295, 130.47848295],
[109.87696965, 109.87696965, 70.12303035,
89.99999997, 90.00000003]])]
diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2], [2, 0, 4, 1, 3]]
for i, diag in enumerate(diagonals):
B, dim = r.do_diagonal_rectangulation(diag)
self.assertTrue((B == Bc[i]).all())
dim[0, :] *= w
dim[1, :] *= h
E = r.build_rectangulation_equations(B)
self.assertTrue((E == Ec[i]).all())
sol = r.minimize_rectangulation(E, dim, w, h, k, c)
self.assertLess(np.sum(np.abs(sol_c[i] - sol)), 0.01)
r.draw_resized_rectangles(B, dim, w, h)
def test_diagonal_rectangulation_15rect(self):
w = 320
h = 180
k = 1.5
# 0.1: proportion is predominant
# 0.05: seems good ballanced
c = 0.05
B, dim = r.do_diagonal_rectangulation(
[7, 12, 6, 4, 10, 1, 13, 5, 14, 8, 9, 2, 0, 3, 11])
Bc = np.array([[0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
[1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
[1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
[1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
[4, 4, 4, 4, 4, 5, 5, 5, 8, 9, 9, 11, 11, 11, 11],
[4, 4, 4, 4, 4, 5, 5, 5, 8, 9, 9, 11, 11, 11, 11],
[6, 6, 6, 6, 6, 6, 6, 6, 8, 9, 9, 11, 11, 11, 11],
[7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 11, 11],
[7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 11, 11],
[7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 11, 11],
[7, 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 11, 11, 11, 11],
[7, 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 11, 11, 11, 11],
[7, 7, 7, 7, 7, 7, 7, 7, 12, 12, 12, 12, 12, 13, 14],
[7, 7, 7, 7, 7, 7, 7, 7, 12, 12, 12, 12, 12, 13, 14],
[7, 7, 7, 7, 7, 7, 7, 7, 12, 12, 12, 12, 12, 13, 14]],
dtype=int)
self.assertTrue((B == Bc).all())
r.draw_resized_rectangles(B, dim, 1, 1)
dim[0, :] *= w
dim[1, :] *= h
E = r.build_rectangulation_equations(B)
mat_sol = r.minimize_rectangulation(E, dim, w, h, k, c)
r.draw_resized_rectangles(B, mat_sol, w, h)
def test_diagonal_rectangulation_random(self):
w = 320
h = 180
k = 1.5
c = 0.05
rect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
shuffle(rect)
print(rect)
B, dim = r.do_diagonal_rectangulation(rect)
r.draw_resized_rectangles(B, dim, 1, 1)
dim[0, :] *= w
dim[1, :] *= h
E = r.build_rectangulation_equations(B)
mat_sol = r.minimize_rectangulation(E, dim, w, h, k, c)
r.draw_resized_rectangles(B, mat_sol, w, h)
def test_best_3rect(self):
N = 3
c = 0.05
k = 1.33
w = 320
h = 180
B, sol, seq = r.get_best_rect_for_window(N, c, k, w, h)
print(sol)
sol_c = np.array([[116.36363645, 203.63636355, 203.63636355],
[180., 89.99999993, 90.00000007]])
self.assertLess(np.sum(np.abs(sol_c - sol)), 0.01)
r.draw_resized_rectangles(B, sol, w, h)
def test_best_5rect(self):
N = 5
c = 0.05
k = 1.33
w = 320
h = 180
B, sol, seq = r.get_best_rect_for_window(N, c, k, w, h)
print(sol)
sol_c = np.array([[159.99999938, 160.00000062, 106.66666643,
106.6666668, 106.66666677],
[77.17563584, 77.17563584, 102.82436416,
102.82436416, 102.82436416]])
self.assertLess(np.sum(np.abs(sol_c - sol)), 0.01)
r.draw_resized_rectangles(B, sol, w, h)
def test_subseq_generation(self):
subseqs = []
for ss in r.get_subsequence(3, [1, 2, 3, 4]):
subseqs.append(ss)
self.assertEqual([[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]], subseqs)
def test_baxter_permutation(self):
self.assertEqual(r.is_baxter_permutation([1, 2, 3, 4]), True)
self.assertEqual(r.is_baxter_permutation([3, 1, 4, 2]), False)
self.assertEqual(r.is_baxter_permutation([2, 4, 1, 3]), False)
def test_count_number_diagonal_rects(self):
num_rects_for_N = [1, 2, 6, 22, 92, 422, 2074]
for N in range(1, 8):
self.assertEqual(num_rects_for_N[N - 1],
r.count_number_diagonal_rects(N))
def test_subseq_match_pattern(self):
seq = [3, 7, 5, 4, 2, 1, 6]
pattern = [3, 1, 4, 2]
gaps = [False, True, False, True]
self.assertEqual(r.subseq_matches_pattern(seq, [3, 5, 4, 1],
pattern, gaps), False)
gaps = [True, True, True, True]
self.assertEqual(r.subseq_matches_pattern([4, 5, 3, 1, 2],
[4, 5, 1, 2],
[3, 4, 1, 2], gaps), True)
gaps = [True, False, True, False]
self.assertEqual(r.subseq_matches_pattern([4, 5, 3, 1, 2],
[4, 5, 1, 2],
[3, 4, 1, 2], gaps), True)
gaps = [True, True, False, True]
self.assertEqual(r.subseq_matches_pattern([4, 5, 3, 1, 2],
[4, 5, 1, 2],
[3, 4, 1, 2], gaps), False)
# Draws best rectangulations while width/height ratio increases.
def test_best_rect_for_w_h_ratio(self):
N = 5
c = 0.05
num_pt = 15
# For instance 320x180
num_pix = 57600
aspect_lb = 0.3
aspect_ub = 4.
# Usual camera x/y ratio
k = 1.33
for aspect in np.linspace(aspect_lb, aspect_ub, num_pt):
w = math.sqrt(aspect*num_pix)
h = w/aspect
B, sol, seq = r.get_best_rect_for_window(N, c, k, w, h)
print(seq)
title = 'w/h ratio = {:.2f}'.format(aspect)
r.draw_resized_rectangles(B, sol, w, h, title=title)
if __name__ == '__main__':
unittest.main()