-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathOverlappingModel.cs
More file actions
203 lines (164 loc) · 5.9 KB
/
OverlappingModel.cs
File metadata and controls
203 lines (164 loc) · 5.9 KB
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
/*
The MIT License(MIT)
Copyright(c) mxgmn 2016.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
*/
using System;
using System.Collections.Generic;
using UnityEngine;
class OverlappingModel : Model
{
int N;
public byte[][] patterns;
int ground;
public List<byte> colors;
public OverlappingModel(byte[,] sample, int N, int width, int height, bool periodicInput, bool periodicOutput, int symmetry, int ground)
:base(width, height)
{
this.N = N;
periodic = periodicOutput;
int SMX = sample.GetLength(0), SMY = sample.GetLength(1);
colors = new List<byte>();
colors.Add((byte)0);
for (int y = 0; y < SMY; y++) for (int x = 0; x < SMX; x++)
{
byte color = sample[x, y];
int i = 0;
foreach (var c in colors)
{
if (c == color) break;
i++;
}
if (i == colors.Count) colors.Add(color);
}
int C = colors.Count;
long W = Stuff.Power(C, N * N);
Func<Func<int, int, byte>, byte[]> pattern = (f) =>
{
byte[] result = new byte[N * N];
for (int y = 0; y < N; y++) for (int x = 0; x < N; x++) result[x + y * N] = f(x, y);
return result;
};
Func<int, int, byte[]> patternFromSample = (x, y) => {return pattern((dx, dy) => {return sample[(x + dx) % SMX, (y + dy) % SMY];});};
Func<byte[], byte[]> rotate = (p) => {return pattern((x, y) => {return p[N - 1 - y + x * N];});};
Func<byte[], byte[]> reflect = (p) => {return pattern((x, y) => {return p[N - 1 - x + y * N];});};
Func<byte[], long> index = p =>
{
long result = 0, power = 1;
for (int i = 0; i < p.Length; i++)
{
result += p[p.Length - 1 - i] * power;
power *= C;
}
return result;
};
Func<long, byte[]> patternFromIndex = ind =>
{
long residue = ind, power = W;
byte[] result = new byte[N * N];
for (int i = 0; i < result.Length; i++)
{
power /= C;
int count = 0;
while (residue >= power)
{
residue -= power;
count++;
}
result[i] = (byte)count;
}
return result;
};
Dictionary<long, int> weights = new Dictionary<long, int>();
List<long> ordering = new List<long>();
for (int y = 0; y < (periodicInput ? SMY : SMY - N + 1); y++) for (int x = 0; x < (periodicInput ? SMX : SMX - N + 1); x++)
{
byte[][] ps = new byte[8][];
ps[0] = patternFromSample(x, y);
ps[1] = reflect(ps[0]);
ps[2] = rotate(ps[0]);
ps[3] = reflect(ps[2]);
ps[4] = rotate(ps[2]);
ps[5] = reflect(ps[4]);
ps[6] = rotate(ps[4]);
ps[7] = reflect(ps[6]);
for (int k = 0; k < symmetry; k++)
{
long ind = index(ps[k]);
if (weights.ContainsKey(ind)) weights[ind]++;
else {
weights.Add(ind, 1);
ordering.Add(ind);
}
}
}
T = weights.Count;
this.ground = (ground + T) % T;
patterns = new byte[T][];
base.weights = new double[T];
int counter = 0;
foreach (long w in ordering)
{
patterns[counter] = patternFromIndex(w);
base.weights[counter] = weights[w];
counter++;
}
Func<byte[], byte[], int, int, bool> agrees = (p1, p2, dx, dy) =>
{
int xmin = dx < 0 ? 0 : dx, xmax = dx < 0 ? dx + N : N, ymin = dy < 0 ? 0 : dy, ymax = dy < 0 ? dy + N : N;
for (int y = ymin; y < ymax; y++) for (int x = xmin; x < xmax; x++) if (p1[x + N * y] != p2[x - dx + N * (y - dy)]) return false;
return true;
};
propagator = new int[4][][];
for (int d = 0; d < 4; d++)
{
propagator[d] = new int[T][];
for (int t = 0; t < T; t++)
{
List<int> list = new List<int>();
for (int t2 = 0; t2 < T; t2++) if (agrees(patterns[t], patterns[t2], DX[d], DY[d])) list.Add(t2);
propagator[d][t] = new int[list.Count];
for (int c = 0; c < list.Count; c++) propagator[d][t][c] = list[c];
}
}
}
protected override bool OnBoundary(int x, int y)
{
return !periodic && (x + N > FMX || y + N > FMY || x < 0 || y < 0);
}
public byte Sample(int x, int y){
bool found = false;
byte res = (byte)99;
for (int t = 0; t < T; t++) if (wave[x + y * FMX][t]){
if (found) {return (byte)99;}
found = true;
res = patterns[t][0];
}
return res;
}
protected override void Clear()
{
base.Clear();
//here we could actually set ground as all 4 sides possibly, think i need to query which ngram is made from the ground tile index, instead of just using that index into the ngrams
if (ground != 0)
{
for (int x = 0; x < FMX; x++)
{
//top
//for (int t = 0; t < T; t++) if (t != ground) Ban(x + (FMY - 1) * FMX, t);
//bottom
for (int t = 0; t < T; t++) if (t != ground) Ban(x, t);
}
/* for (int y = 0; y < FMY; y++)
{
//right
for (int t = 0; t < T; t++) if (t != ground) Ban((y * FMX) + (FMX - 1), t);
//left
for (int t = 0; t < T; t++) if (t != ground) Ban(y * FMX, t);
}*/
Propagate();
}
}
}