-
Notifications
You must be signed in to change notification settings - Fork 0
/
WangTilesProcessor.cpp
executable file
·568 lines (475 loc) · 18.3 KB
/
WangTilesProcessor.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* WangTilesProcessor.cpp
*
* Li-Yi Wei
* 8/8/2004
*
*/
#include <iostream>
using namespace std;
#include "WangTilesProcessor.hpp"
int WangTilesProcessor::BoxSanitize(const TilePack & tiles,
const Pyramid & input,
Pyramid & output,
const int do_corner)
{
// initialize output size
output = input;
// sanitize each level, from high resolution to low resolution
for(int level = 0; level < input.NumLevels(); level++)
{
Array3D<Pixel> & level_now = output[level];
if(level > 0)
{
const Array3D<Pixel> & level_up = output[level-1];
// perform initial box filtering + down-sampling from previous level
for(int row = 0; row < level_now.Size(0); row++)
for(int col = 0; col < level_now.Size(1); col++)
for(int cha = 0; cha < level_now.Size(2); cha++)
{
Pixel temp = 0;
for(int i = 2*row; i <= 2*row+1; i++)
for(int j = 2*col; j <= 2*col+1; j++)
{
const int row_up = i%level_up.Size(0);
const int col_up = j%level_up.Size(1);
temp += level_up[row_up][col_up][cha];
}
level_now[row][col][cha] = temp/4.0;
}
}
if(!SanitizeEdges(tiles, level_now, level_now)) return 0;
if(do_corner && !SanitizeCorners(tiles, level_now, level_now)) return 0;
}
// done
return 1;
}
void WangTilesProcessor::EdgeIndices(const int edge_orientation,
const int tile_height,
const int tile_width,
int & row_start, int & row_end,
int & col_start, int & col_end)
{
switch(edge_orientation%4)
{
case 2: // bottom
row_start = row_end = tile_height-1;
col_start = 0; col_end = tile_width-1;
break;
case 1: // right
row_start = 0; row_end = tile_height-1;
col_start = col_end = tile_width-1;
break;
case 0: // top
row_start = row_end = 0;
col_start = 0; col_end = tile_width-1;
break;
case 3: // left
row_start = 0; row_end = tile_height-1;
col_start = col_end = 0;
break;
default: // impossible
row_start = col_start = 1;
row_end = col_end = 0;
break;
}
}
void WangTilesProcessor::CornerIndices(const int edge_orientation,
const int tile_height,
const int tile_width,
int & row, int & col)
{
switch(edge_orientation%4)
{
case 2: // bottom
row = tile_height-1; col = 0;
break;
case 1: // right
row = tile_height-1; col = tile_width-1;
break;
case 0: // top
row = 0; col = tile_width-1;
break;
case 3: // left
row = 0; col = 0;
break;
default: // impossible
row = 0; col = 0;
break;
}
}
int WangTilesProcessor::CountEdgeColors(const TilePack & tiles,
int & e0_colors,
int & e1_colors,
int & e2_colors,
int & e3_colors)
{
e0_colors = 0; e1_colors = 0; e2_colors = 0; e3_colors = 0;
for(unsigned int i = 0; i < tiles.size(); i++)
for(unsigned int j = 0; j < tiles[i].size(); j++)
{
const WangTiles::Tile & tile = tiles[i][j];
if((tile.e0() < 0) || (tile.e1() < 0) || (tile.e2() < 0) || (tile.e3() < 0))
{
// no negative tile edges
return 0;
}
else
{
if(e0_colors < (tile.e0()+1)) e0_colors = tile.e0()+1;
if(e1_colors < (tile.e1()+1)) e1_colors = tile.e1()+1;
if(e2_colors < (tile.e2()+1)) e2_colors = tile.e2()+1;
if(e3_colors < (tile.e3()+1)) e3_colors = tile.e3()+1;
}
}
return 1;
}
int WangTilesProcessor::SanitizeSmall(const TilePack & tiles,
const Array3D<Pixel> & input,
Array3D<Pixel> & output)
{
if((tiles.size() <= 0) ||
(input.Size(0)%tiles.size()) || (input.Size(1)%tiles[0].size()))
{
// small image, just make it a constant average of input
output = input;
for(int cha = 0; cha < input.Size(2); cha++)
{
Pixel sum = 0;
{
for(int row = 0; row < input.Size(0); row++)
for(int col = 0; col < input.Size(1); col++)
{
sum += input[row][col][cha];
}
sum /= (input.Size(0)*input.Size(1));
}
{
for(int row = 0; row < output.Size(0); row++)
for(int col = 0; col < output.Size(1); col++)
{
output[row][col][cha] = sum;
}
}
}
return 1;
}
else
{
return 0;
}
}
int WangTilesProcessor::SanitizeEdges(const TilePack & tiles,
const Array3D<Pixel> & input,
Array3D<Pixel> & output)
{
// error checking
if(SanitizeSmall(tiles, input, output))
{
return 1;
}
const int tile_height = input.Size(0)/tiles.size();
const int tile_width = input.Size(1)/tiles[0].size();
const int tile_depth = input.Size(2);
// count the number of edge colors
int e_colors[4] = {0, 0, 0, 0};
{
if(!CountEdgeColors(tiles,
e_colors[0],
e_colors[1],
e_colors[2],
e_colors[3]))
{
return 0;
}
// check consistency
if((e_colors[0] != e_colors[2]) || (e_colors[1] != e_colors[3]))
{
return 0;
}
}
// initialize
vector< vector<MeanTile> > mean_tiles(4);
{
Array3D<Pixel> image(tile_height, tile_width, tile_depth);
for(int row = 0; row < image.Size(0); row++)
for(int col = 0; col < image.Size(1); col++)
for(int cha = 0; cha < image.Size(2); cha++)
{
image[row][col][cha] = 0;
}
for(unsigned int i = 0; i < mean_tiles.size(); i++)
{
mean_tiles[i] = vector< MeanTile >(e_colors[i]);
for(unsigned int j = 0; j < mean_tiles[i].size(); j++)
{
mean_tiles[i][j].image = image;
mean_tiles[i][j].count = 0;
}
}
}
// compute mean/average tiles per edge color
{
for(unsigned int i = 0; i < tiles.size(); i++)
for(unsigned int j = 0; j < tiles[i].size(); j++)
{
const WangTiles::Tile & tile = tiles[i][j];
const int input_row_start = i*tile_height;
const int input_col_start = j*tile_width;
const int tile_edges[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
for(int k = 0; k < 4; k++)
{
MeanTile & output_tile = mean_tiles[k][tile_edges[k]];
for(int row = 0; row < tile_height; row++)
for(int col = 0; col < tile_width; col++)
for(int cha = 0; cha < tile_depth; cha++)
{
output_tile.image[row][col][cha] += input[row+input_row_start][col+input_col_start][cha];
}
output_tile.count++;
}
}
}
{
for(unsigned int i = 0; i < mean_tiles.size(); i++)
for(unsigned int j = 0; j < mean_tiles[i].size(); j++)
{
MeanTile & tile = mean_tiles[i][j];
if(tile.count > 0)
{
Array3D<Pixel> & image = tile.image;
for(int row = 0; row < image.Size(0); row++)
for(int col = 0; col < image.Size(1); col++)
for(int cha = 0; cha < image.Size(2); cha++)
{
image[row][col][cha] /= tile.count;
}
tile.count = 1;
}
}
}
// fix the output tile boundaries
output = input;
Array3D<Pixel> boundary(output);
Array3D<Pixel> boundary_count(output);
{
// initialization
for(int i = 0; i < boundary.Size(0); i++)
for(int j = 0; j < boundary.Size(1); j++)
for(int k = 0; k < boundary.Size(2); k++)
{
boundary[i][j][k] = 0;
boundary_count[i][j][k] = 0;
}
}
{
// compute the correct tile boundary
for(unsigned int i = 0; i < tiles.size(); i++)
for(unsigned int j = 0; j < tiles[i].size(); j++)
{
const WangTiles::Tile & tile = tiles[i][j];
const int output_row_offset = i*tile_height;
const int output_col_offset = j*tile_width;
const int tile_edges[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
for(int k = 0; k < 4; k++)
{
const Array3D<Pixel> & mean_tile = mean_tiles[k][tile_edges[k]].image;
int row_start, row_end, col_start, col_end;
EdgeIndices(k, tile_height, tile_width,
row_start, row_end, col_start, col_end);
for(int row = row_start; row <= row_end; row++)
for(int col = col_start; col <= col_end; col++)
for(int cha = 0; cha < tile_depth; cha++)
{
boundary[row+output_row_offset][col+output_col_offset][cha] += mean_tile[row][col][cha];
boundary_count[row+output_row_offset][col+output_col_offset][cha] += 1.0;
}
}
}
}
{
// normalization and assign to the output
for(int i = 0; i < boundary.Size(0); i++)
for(int j = 0; j < boundary.Size(1); j++)
for(int k = 0; k < boundary.Size(2); k++)
{
if(boundary_count[i][j][k] > 0)
{
output[i][j][k] = boundary[i][j][k]/boundary_count[i][j][k];
}
}
}
// done
return 1;
}
int WangTilesProcessor::SanitizeCorners(const TilePack & tiles,
const Array3D<Pixel> & input,
Array3D<Pixel> & output)
{
if(SanitizeSmall(tiles, input, output))
{
return 1;
}
const int tile_height = input.Size(0)/tiles.size();
const int tile_width = input.Size(1)/tiles[0].size();
const int tile_depth = input.Size(2);
// initialize
// note that for each row we have all possible corner combinations
MeanTile mean_corner_tile;
{
Array3D<Pixel> image(tile_height, tile_width, tile_depth);
for(int row = 0; row < image.Size(0); row++)
for(int col = 0; col < image.Size(1); col++)
for(int cha = 0; cha < image.Size(2); cha++)
{
image[row][col][cha] = 0;
}
mean_corner_tile.image = image;
mean_corner_tile.count = 0;
}
// compute mean/average tiles for corner
// note that we need to consider all tiles to get correct results!
{
for(unsigned int i = 0; i < tiles.size(); i++)
for(unsigned int j = 0; j < tiles[i].size(); j++)
{
const WangTiles::Tile & tile = tiles[i][j];
const int input_row_start = i*tile_height;
const int input_col_start = j*tile_width;
for(int row = 0; row < tile_height; row++)
for(int col = 0; col < tile_width; col++)
for(int cha = 0; cha < tile_depth; cha++)
{
mean_corner_tile.image[row][col][cha] += input[row+input_row_start][col+input_col_start][cha];
}
mean_corner_tile.count++;
}
}
// normalization
{
if(mean_corner_tile.count > 0)
{
Array3D<Pixel> & image = mean_corner_tile.image;
for(int row = 0; row < image.Size(0); row++)
for(int col = 0; col < image.Size(1); col++)
for(int cha = 0; cha < image.Size(2); cha++)
{
image[row][col][cha] /= mean_corner_tile.count;
}
mean_corner_tile.count = 1;
}
}
// assign to the output
{
for(unsigned int i = 0; i < tiles.size(); i++)
for(unsigned int j = 0; j < tiles[i].size(); j++)
{
const WangTiles::Tile & tile = tiles[i][j];
const int tile_edges[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
const int output_row_start = i*tile_height;
const int output_col_start = j*tile_width;
for(int k = 0; k < 4; k++)
{
int row, col;
CornerIndices(k, tile_height, tile_width, row, col);
// only touch the corners
for(int cha = 0; cha < tile_depth; cha++)
{
output[row+output_row_start][col+output_col_start][cha] = mean_corner_tile.image[row][col][cha];
}
}
}
}
// done
return 1;
}
int WangTilesProcessor::TileMismatch(const TilePack & tiles,
const Pyramid & input)
{
int result = -1;
for(int lev = 0; lev < input.NumLevels(); lev++)
{
if(TileMismatch(tiles, input[lev]))
{
result = lev;
break;
}
}
return result;
}
int WangTilesProcessor::TileMismatch(const TilePack & tiles,
const Array3D<Pixel> & input)
{
int mismatch = 0;
// count the number of edge colors
int e_colors[4] = {0, 0, 0, 0};
if(!CountEdgeColors(tiles,
e_colors[0],
e_colors[1],
e_colors[2],
e_colors[3]))
{
return 0;
}
// compute tile size
const int tile_height = (tiles.size() > 0) ? input.Size(0)/tiles.size() : 0;
const int tile_width = (tiles.size() > 0 && tiles[0].size() > 0) ? input.Size(1)/tiles[0].size() : 0;
const int tile_depth = input.Size(2);
// make sure all edges with
// (1) the same S/E/N/W orientations and
// (2) same edge id
// have identical images
for(int e_index = 0; e_index < 4; e_index++)
for(int e_color = 0; e_color < e_colors[e_index]; e_color++)
{
// find a tile with e_color in e_index
int t_row = -1; int t_col = -1;
{
for(unsigned int i = 0; i < tiles.size(); i++)
for(unsigned int j = 0; j < tiles[i].size(); j++)
{
const WangTiles::Tile & tile = tiles[i][j];
const int my_e_colors[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
if(my_e_colors[e_index] == e_color)
{
// found the tile
t_row = i; t_col = j;
break;
}
}
}
if((t_row >= 0) && (t_col >= 0))
{
const WangTiles::Tile & sample_tile = tiles[t_row][t_col];
const int row_offset1 = t_row*tile_height;
const int col_offset1 = t_col*tile_width;
for(unsigned int i = 0; i < tiles.size(); i++)
for(unsigned int j = 0; j < tiles[i].size(); j++)
{
const WangTiles::Tile & tile = tiles[i][j];
const int my_e_colors[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
if(my_e_colors[e_index] == e_color)
{
// try to see if they match
const int row_offset2 = i*tile_height;
const int col_offset2 = j*tile_width;
int row_start, row_end, col_start, col_end;
EdgeIndices(e_index, tile_height, tile_width,
row_start, row_end,
col_start, col_end);
for(int row = row_start; row <= row_end; row++)
for(int col = col_start; col <= col_end; col++)
for(int cha = 0; cha < tile_depth; cha++)
{
if(input[row+row_offset1][col+col_offset1][cha] != input[row+row_offset2][col+col_offset2][cha])
{
mismatch = 1;
}
}
}
}
}
}
// done
return mismatch;
}