-
Notifications
You must be signed in to change notification settings - Fork 0
/
WangTileSet.m
executable file
·162 lines (128 loc) · 4.1 KB
/
WangTileSet.m
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
%
%
% function [resultTiles, tileError] = ...
% WangTileSet(sampleTexture, tileColors, tileSize, borderSize)
%
% Inputs:
%
% sampleTexture :
% the sample texture to build tiles from
%
% tileColors :
% [numVerticalColors numHorizontalColors numTilesPerColor]
%
% tileSize :
% [height width] of each tile
%
% borderSize :
% see SingleWangTile
%
% Return:
%
% resultTiles :
% the result set of tiles packed into one big image
% the packing is determined by WangTileIndex
%
% tileError :
% the quilting errors of resultTiles
%
% Li-Yi Wei
% 8/21/2003
%
function [resultTiles, tileError] = ...
WangTileSet(sampleTexture, tileColors, tileSize, borderSize)
% constants
tileMargin = 2;
% check arguments
if(nargin ~= 4)
error('Wrong number of input arguments');
end
if((ndims(sampleTexture) < 2) | (ndims(sampleTexture) > 3))
error('Wrong sample texture dimension');
end
if(prod(size(tileColors)) ~= 3)
error('Wrong tileColors specification');
end
if(prod(size(tileSize)) ~= 2)
error('Wrong tileSize specification');
end
if(prod(size(borderSize)) ~= 2)
error('Wrong borderSize specification');
end
[sampleHeight sampleWidth sampleDepth] = size(sampleTexture);
if(any([sampleHeight sampleWidth] <= (tileSize + 2*[tileMargin tileMargin])))
error('sample texture smaller than tile size');
end
numVColors = tileColors(1);
numHColors = tileColors(2);
numTilesPerColor = tileColors(3);
if(numTilesPerColor ~= 1)
warning('can only handle 1 tile per color combination');
end
% choose sample tile locations and sizes
vTileSamples = zeros([numVColors 4]); % for east and west
hTileSamples = zeros([numHColors 4]); % for north and south
tileHeight = tileSize(1); tileWidth = tileSize(2);
for k = 1:size(vTileSamples, 1)
vTileSamples(k, 1:2) = ...
rand([1 2]) .* ...
[sampleHeight-tileHeight sampleWidth-tileWidth-tileMargin];
vTileSamples(k, 1:2) = ceil(vTileSamples(k, 1:2));
vTileSamples(k, 3) = tileHeight;
vTileSamples(k, 4) = sampleWidth-vTileSamples(k, 2)+1;
if(vTileSamples(k, 4) > 2*tileWidth)
vTileSamples(k, 4) = 2*tileWidth;
end
vTileSamples(k, 3:4) = ...
vTileSamples(k, 3:4) + vTileSamples(k, 1:2) - 1;
end
for k = 1:size(hTileSamples, 1)
hTileSamples(k, 1:2) = ...
rand([1 2]) .* ...
[sampleHeight-tileHeight-tileMargin sampleWidth-tileWidth];
hTileSamples(k, 1:2) = ceil(hTileSamples(k, 1:2));
hTileSamples(k, 3) = sampleHeight-hTileSamples(k, 1)+1;
hTileSamples(k, 4) = tileWidth;
if(hTileSamples(k, 3) > 2*tileHeight)
hTileSamples(k, 3) = 2*tileHeight;
end
hTileSamples(k, 3:4) = ...
hTileSamples(k, 3:4) + hTileSamples(k, 1:2) - 1;
end
% initialize the results
resultTiles = ...
zeros([numHColors*numHColors numVColors*numVColors sampleDepth]);
tileError = 0;
% generate individual wang tiles and pack them into the result
for sEdge = 1:numHColors
sLoc = hTileSamples(sEdge, :);
sLoc(3) = floor((sLoc(1)+sLoc(3))/2);
sTile = sampleTexture(sLoc(1):sLoc(3), sLoc(2):sLoc(4), :);
for eEdge = 1:numVColors
eLoc = vTileSamples(eEdge, :);
eLoc(4) = floor((eLoc(2)+eLoc(4))/2);
eTile = sampleTexture(eLoc(1):eLoc(3), eLoc(2):eLoc(4), :);
for nEdge = 1:numHColors
nLoc = hTileSamples(nEdge, :);
nLoc(1) = floor((nLoc(1)+nLoc(3))/2)+1;
nTile = sampleTexture(nLoc(1):nLoc(3), nLoc(2):nLoc(4), :);
for wEdge = 1:numVColors
wLoc = vTileSamples(wEdge, :);
wLoc(2) = floor((wLoc(2)+wLoc(4))/2)+1;
wTile = sampleTexture(wLoc(1):wLoc(3), wLoc(2):wLoc(4), :);
% get all source tiles, find the Wang Tile
[oneTile, sePath, enPath, nwPath, wsPath, pathCost] = ...
SingleWangTile(sTile, eTile, nTile, wTile, borderSize);
indexLoc = ...
[WangTileIndex(nEdge, sEdge) WangTileIndex(wEdge, eEdge)];
indexLoc = (indexLoc-1) .* tileSize + 1;
resultTiles(indexLoc(1):indexLoc(1)+tileSize(1)-1,...
indexLoc(2):indexLoc(2)+tileSize(2)-1, :) = ...
oneTile;
tileError = tileError + pathCost;
end
end
end
end
% done
return;