-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit_together.cpp
175 lines (145 loc) · 5.03 KB
/
fit_together.cpp
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
#include "fit_together.h"
#include "MyRect.h"
#include "WidgetContext.h"
#include "FunctionTimer.h"
#include <vector>
#include <unordered_set>
#include <stdio.h>
#include "latuile_test_json_output.h"
using namespace std ;
/* in this example: +-----> direction = EAST_WEST
|
|
V
normal_direction = NORTH_SOUTH
<-----------------A----------------> <------------B----------->
+------------+
| |
| |
| | +-----------------+
| | | |
| | | |
+------------+ | |
| +---------+ | |
+---------+ | | +-----------------+
| | | | +----------+ |
| | | | | | +--------+
| |-----| | | | | |
| | | | | r2 |---| |
| | | | | | | |
| | | | +--------+ | | +--------+
+---------+ | | | r1 | +----------+ |
| |--| | +--------+
+---------+ +--------+ | |
| |
| |
+--------+
*/
void test_fit_together()
{
TestFunctionTimer ft("test_fit_together");
vector<MyRect> A = {
{84,237,80,224},
{237,439,0,272},
{237,418,272,560},
{56,237,224,512},
{237,467,560,656},
{0,237,512,656},
{467,690,224,656},
{690,836,528,656}
};
vector<MyRect> B = {
{1212,1449,272,496},
{1038,1212,336,496},
{975,1212,96,336},
{836,1038,336,592},
{1038,1205,496,656}
};
const MyPoint expected_translation = {-146,-64};
MyPoint translation ;
fit_together(A, B, EAST_WEST, translation) ;
bool bOK = translation == expected_translation;
printf("%s\n", bOK ? "OK" : "KO");
(bOK ? nbOK : nbKO)++;
}
void fit_together(vector<WidgetContext*> (&composite_partition)[2],
Direction direction,
vector<MyRect> &B,
MyPoint& translation_B,
int &diameter)
{
FunctionTimer ft("fit_together");
vector<MyRect> A2[2] ;
for (int LEG=0; LEG < 2; LEG++)
{
for (WidgetContext* widget_ctx : composite_partition[LEG])
{
for (WidgetContext &widg : widget_ctx->widgets)
{
if (widg.type == WidgetType::RECTANGLE)
{
A2[LEG].push_back(widg.r) ;
}
}
}
}
fit_together(A2[0], A2[1], direction, translation_B) ;
B = A2[1] ;
for (MyRect& r : A2[1])
translate(r, translation_B) ;
diameter = rectangle_diameter(enveloppe(compute_frame(A2[0]), compute_frame(A2[1]))) ;
}
// Works on expanded rectangles.
void fit_together(vector<MyRect> A,
vector<MyRect> B,
Direction direction,
MyPoint& translation_B)
{
Direction normal_direction = transpose(direction) ;
vector<MyPoint> candidate_translations ;
for (const MyRect& r1 : A)
{
for (const MyRect& r2 : B)
{
MyPoint target, source ;
// compute the translation to bring r2 above (resp. before) if direction==EAST_WEST (resp. NORTH_SOUTH) r1:
value(target, normal_direction) = min(r1, normal_direction) ;
value(target, direction) = min(r1, direction) ;
value(source, direction) = min(r2, direction) ;
value(source, normal_direction) = max(r2, normal_direction) ;
candidate_translations.push_back(target - source) ;
// compute the translation to bring r2 below (resp. after) if direction==EAST_WEST (resp. NORTH_SOUTH) r1:
value(target, normal_direction) = max(r1, normal_direction) ;
value(target, direction) = min(r1, direction) ;
value(source, direction) = min(r2, direction) ;
value(source, normal_direction) = min(r2, normal_direction) ;
candidate_translations.push_back(target - source) ;
}
}
MyPoint best_translation = {0,0} ;
MyPoint best_dimension = dimensions(enveloppe(compute_frame(A), compute_frame(B))) ;
for (MyPoint& translation : candidate_translations)
{
// apply the translation to all elements of B
vector<MyRect> Bt = B ;
for (MyRect& r2 : Bt)
translate(r2, translation) ;
bool no_overlap = true ;
for (const MyRect& r1 : A)
{
for (const MyRect& r2 : Bt)
{
if (intersect_strict(r1, r2))
no_overlap = false ;
}
}
MyRect env = enveloppe(compute_frame(A), compute_frame(Bt)) ;
MyPoint dim = dimensions(env) ;
if (no_overlap && dim.x <= best_dimension.x && dim.y <= best_dimension.y)
{
best_dimension = dim ;
best_translation = translation ;
}
}
translation_B = best_translation ;
}