-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiamondSearch.cpp
258 lines (191 loc) · 9.02 KB
/
DiamondSearch.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
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
/*
This file is part of the Dandere2x project.
Dandere2x is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dandere2x is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
/*
========= Copyright aka_katto 2018, All rights reserved. ============
Original Author: aka_katto
Date: 4/11/20
Purpose:
===================================================================== */
#include "DiamondSearch.h"
#include <algorithm>
//-----------------------------------------------------------------------------
// Purpose: An iterative implementation of diamond search, plus or minus some
// custom heuristics I've added myself.
//-----------------------------------------------------------------------------
Block DiamondSearch::match_block(int x, int y) {
// These variables stay the same
int x_bounds = 5; // desired_image.get_width();
int y_bounds = 5; // desired_image.get_height();
int x_origin = x;
int y_origin = y;
// These variables change per each iteration
int initial_x = x;
int initial_y = y;
// Recycled variables that are cleared after each iteration
std::vector<Block> blocks = std::vector<Block>();
std::vector<Block::Point> point_array(CHECKS_PER_STEP);
// Main loop .Note that if it hits the end of the for loop, we've gone over our max_checks, and this
// are cutting the search short in order to keep the searching time having a constant worse-case search.
// In other words, we're artificially capping the upper bounds.
for (int x = 0; x < max_checks; x++) {
// Base Cases (if we were doing this recursively, these essentially would be the base cases)
{
// If we ran out of checks, return what we're at right now
if (x == max_checks - 1) {
// double sum = AbstractBlockMatch::mse_blocks(initial_x, initial_y, x_origin, y_origin);
// return Block(initial_x, initial_y, x_origin, y_origin, sum);
}
// If step size is 0, return where we are at right now, since we've reached the end of our search
if (step_size <= 0) {
// double sum = AbstractBlockMatch::mse_blocks(initial_x, initial_y, x_origin, y_origin);
// return Block(initial_x, initial_y, x_origin, y_origin, sum);
}
}
// Reset variables unique for each iteration
blocks.clear();
bool flag_array[16] = {true};
{
point_array[0].x = x_origin + step_size;
point_array[0].y = y_origin;
point_array[1].x = x_origin + step_size / 2;
point_array[1].y = y_origin + step_size / 2;
point_array[2].x = x_origin;
point_array[2].y = y_origin + step_size;
point_array[3].x = x_origin - step_size / 2;
point_array[3].y = y_origin + step_size / 2;
point_array[4].x = x_origin - step_size;
point_array[4].y = y_origin;
point_array[5].x = x_origin + step_size / 2;
point_array[5].y = y_origin - step_size / 2;
point_array[6].x = x_origin + step_size;
point_array[6].y = y_origin;
point_array[7].x = x_origin;
point_array[7].y = y_origin;
point_array[8].x = x_origin + step_size * 2;
point_array[8].y = y_origin;
point_array[9].x = x_origin + step_size;
point_array[9].y = y_origin + step_size;
point_array[10].x = x_origin;
point_array[10].y = y_origin + step_size * 2;
point_array[11].x = x_origin - step_size;
point_array[11].y = y_origin + step_size;
point_array[12].x = x_origin - step_size * 2;
point_array[12].y = y_origin;
point_array[13].x = x_origin + step_size;
point_array[13].y = y_origin - step_size;
point_array[14].x = x_origin + step_size * 2;
point_array[14].y = y_origin;
point_array[15].x = x_origin - step_size;
point_array[15].y = y_origin - step_size;
}
// Create an array that is used to signal if a point is "valid" or not, i.e if it is legal to probe.
bool any_legal_points = flag_invalid_points(x_bounds, y_bounds, point_array.data(), CHECKS_PER_STEP, 30,
flag_array);
// Cycle through each search point (if it's legal that is) and add it to the block vector.
for (int j = 0; j < CHECKS_PER_STEP; j++) {
if (!flag_array[j]) { // checks if point is legal
// double sum = AbstractBlockMatch::mse_blocks(initial_x, initial_y, point_array[j].x, point_array[j].y);
//
// Block block = Block(initial_x, initial_y, point_array[j].x, point_array[j].y, sum);
// blocks.push_back(block);
}
}
// Get the most promising block from the list (i.e the block with the smallest 'sum')
std::vector<Block>::iterator smallest_block = min_element(blocks.begin(), blocks.end());
// Custom-Implemented Heuristics #todo
{
// /** If the smallest block we found meets the MSE requirements, stop here*/
// if (smallest_block->sum <= min_mse) {
// return *smallest_block;
// }
//
//
// /** Testing feature - if the smallest block is garishly larger than the minimum required,
// stop looking. */
// if (smallest_block->sum >= min_mse * min_mse) {
// return Block(0, 0, 0, 0, 10000);
// }
}
// Assignments for next iteration
if ((smallest_block->x_end == x_origin) && smallest_block->y_end == y_origin) {
x_origin = smallest_block->x_end;
y_origin = smallest_block->y_end;
step_size /= 2;
continue;
} else {
x_origin = smallest_block->x_end;
y_origin = smallest_block->y_end;
}
}
return Block();
}
void DiamondSearch::set_max_checks(int max_checks) {
this->max_checks = max_checks;
}
void DiamondSearch::set_step_size(int step_size) {
this->step_size = step_size;
}
//-----------------------------------------------------------------------------
// Purpose: Neatly construct all the points diamond search for probe.
//-----------------------------------------------------------------------------
std::vector<Block::Point> DiamondSearch::get_diamond_search_points(int x_origin, int y_origin, int step_size) {
//create the points to be used in diamond searching
std::vector<Block::Point> point_array(CHECKS_PER_STEP);
//construct the list of "diamond" points to be checked if legal or not
point_array[0].x = x_origin + step_size;
point_array[0].y = y_origin;
point_array[1].x = x_origin + step_size / 2;
point_array[1].y = y_origin + step_size / 2;
point_array[2].x = x_origin;
point_array[2].y = y_origin + step_size;
point_array[3].x = x_origin - step_size / 2;
point_array[3].y = y_origin + step_size / 2;
point_array[4].x = x_origin - step_size;
point_array[4].y = y_origin;
point_array[5].x = x_origin + step_size / 2;
point_array[5].y = y_origin - step_size / 2;
point_array[6].x = x_origin + step_size;
point_array[6].y = y_origin;
point_array[7].x = x_origin;
point_array[7].y = y_origin;
point_array[8].x = x_origin + step_size * 2;
point_array[8].y = y_origin;
point_array[9].x = x_origin + step_size;
point_array[9].y = y_origin + step_size;
point_array[10].x = x_origin;
point_array[10].y = y_origin + step_size * 2;
point_array[11].x = x_origin - step_size;
point_array[11].y = y_origin + step_size;
point_array[12].x = x_origin - step_size * 2;
point_array[12].y = y_origin;
point_array[13].x = x_origin + step_size;
point_array[13].y = y_origin - step_size;
point_array[14].x = x_origin + step_size * 2;
point_array[14].y = y_origin;
point_array[15].x = x_origin - step_size;
point_array[15].y = y_origin - step_size;
return point_array; // Safe: returning vector that manages its memory
}
bool DiamondSearch::flag_invalid_points(int x_bounds, int y_bounds, Block::Point *points, int size, int block_size,
bool *flagged) {
int count = 0;
for (int i = 0; i < size; i++) {
if ((points[i].x + block_size > x_bounds) || (points[i].x < 0) ||
(points[i].y + block_size > y_bounds) || (points[i].y < 0)) {
flagged[i] = true;
count++;
}
}
return count != size;
}