/
TileRotationBuilder.cs
329 lines (294 loc) · 12.6 KB
/
TileRotationBuilder.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace DeBroglie.Rot
{
/// <summary>
/// Builds a <see cref="TileRotation"/>.
/// This class lets you specify some transformations between tiles via rotation and reflection.
/// It then infers the full set of rotations possible, and informs you if there are contradictions.
///
/// As an example of inference, if a square tile 1 transforms to tile 2 when rotated clockwise, and tile 2 transforms to itself when reflected in the x-axis,
/// then we can infer that tile 1 must transform to tile 1 when reflected in the y-axis.
/// </summary>
public class TileRotationBuilder
{
private Dictionary<Tile, SubGroup> tileToSubGroup = new Dictionary<Tile, SubGroup>();
private RotationGroup rotationGroup;
private TileRotationTreatment defaultTreatment;
public TileRotationBuilder(RotationGroup rotationGroup, TileRotationTreatment defaultTreatment = TileRotationTreatment.Unchanged)
{
this.rotationGroup = rotationGroup;
this.defaultTreatment = defaultTreatment;
}
public TileRotationBuilder(int rotationalSymmetry, bool reflectionalSymmetry, TileRotationTreatment defaultTreatment = TileRotationTreatment.Unchanged)
{
this.rotationGroup = new RotationGroup(rotationalSymmetry, reflectionalSymmetry);
this.defaultTreatment = defaultTreatment;
}
public RotationGroup RotationGroup => rotationGroup;
/// <summary>
/// Indicates that if you reflect then rotate clockwise the src tile as indicated, then you get the dest tile.
/// </summary>
public void Add(Tile src, Rotation rotation, Tile dest)
{
rotationGroup.CheckContains(rotation);
GetGroup(src, out var srcSg);
GetGroup(dest, out var destSg);
// Groups need merging
if(srcSg != destSg)
{
var srcR = srcSg.GetRotations(src)[0];
var destR = destSg.GetRotations(dest)[0];
// Arrange destRG so that it is relatively rotated
// to srcRG as specified by r.
destSg.Permute(rot => destR.Inverse() * srcR * rotation * rot);
// Attempt to copy over tiles
srcSg.Entries.AddRange(destSg.Entries);
foreach (var kv in destSg.Tiles)
{
Set(srcSg, kv.Key, kv.Value, $"record rotation from {src} to {dest} by {rotation}");
tileToSubGroup[kv.Value] = srcSg;
}
}
srcSg.Entries.Add(new Entry
{
Src = src,
Rotation = rotation,
Dest = dest,
});
Expand(srcSg);
}
private bool Set(SubGroup sg, Rotation rotation, Tile tile, string action)
{
if(sg.Tiles.TryGetValue(rotation, out var current))
{
if(current != tile)
{
throw new Exception($"Cannot {action}: conflict between {current} and {tile}");
}
return false;
}
sg.Tiles[rotation] = tile;
return true;
}
public void SetTreatment(Tile tile, TileRotationTreatment treatment)
{
GetGroup(tile, out var rg);
if(rg.Treatment != null && rg.Treatment !=treatment)
{
throw new Exception($"Cannot set {tile} treatment, inconsistent with {rg.Treatment} of {rg.TreatmentSetBy}");
}
rg.Treatment = treatment;
rg.TreatmentSetBy = tile;
}
/// <summary>
/// Declares that a tile is symetric, and therefore transforms to iteself.
/// This is a shorthand for calling Add(tile,..., tile) for specific rotations.
/// </summary>
public void AddSymmetry(Tile tile, TileSymmetry ts)
{
// I've listed the subgroups in the order found here:
// https://groupprops.subwiki.org/wiki/Subgroup_structure_of_dihedral_group:D8
switch (ts)
{
case TileSymmetry.F:
GetGroup(tile, out var _);
break;
case TileSymmetry.N:
Add(tile, new Rotation(2 * 90, false), tile);
break;
case TileSymmetry.T:
Add(tile, new Rotation(0 * 90, true), tile);
break;
case TileSymmetry.L:
Add(tile, new Rotation(1 * 90, true), tile);
break;
case TileSymmetry.E:
Add(tile, new Rotation(2 * 90, true), tile);
break;
case TileSymmetry.Q:
Add(tile, new Rotation(3 * 90, true), tile);
break;
case TileSymmetry.I:
Add(tile, new Rotation(0 * 90, true), tile);
Add(tile, new Rotation(2 * 90, false), tile);
break;
case TileSymmetry.Slash:
Add(tile, new Rotation(1 * 90, true), tile);
Add(tile, new Rotation(2 * 90, false), tile);
break;
case TileSymmetry.Cyclic:
Add(tile, new Rotation(1 * 90, false), tile);
break;
case TileSymmetry.X:
Add(tile, new Rotation(0 * 90, true), tile);
Add(tile, new Rotation(1 * 90, false), tile);
break;
}
}
/// <summary>
/// Extracts the full set of rotations
/// </summary>
/// <returns></returns>
public TileRotation Build()
{
// For a given tile (found in a given rotation group)
// Find the full set of tiles it rotates to.
IDictionary<Rotation, Tile> GetDict(Tile t, SubGroup sg)
{
var treatment = sg.Treatment ?? defaultTreatment;
if(treatment == TileRotationTreatment.Generated)
{
sg = Clone(sg);
Generate(sg);
}
var r1 = sg.GetRotations(t)[0];
var result = new Dictionary<Rotation, Tile>();
foreach(var r2 in rotationGroup)
{
if (!sg.Tiles.TryGetValue(r2, out var dest))
{
continue;
}
result[r1.Inverse() * r2] = dest;
}
return result;
}
return new TileRotation(
tileToSubGroup.ToDictionary(kv => kv.Key, kv => GetDict(kv.Key, kv.Value)),
tileToSubGroup.Where(kv=>kv.Value.Treatment.HasValue).ToDictionary(kv => kv.Key, kv => kv.Value.Treatment.Value),
defaultTreatment,
rotationGroup);
}
// Gets the rotation group containing Tile, creating it if it doesn't exist
private void GetGroup(Tile tile, out SubGroup sg)
{
if(tileToSubGroup.TryGetValue(tile, out sg))
{
return;
}
sg = new SubGroup();
sg.Tiles[new Rotation()] = tile;
tileToSubGroup[tile] = sg;
}
// Ensures that rg.Tiles is fully filled in
// according to rg.Entries.
private void Expand(SubGroup sg)
{
bool expanded;
do
{
expanded = false;
foreach (var entry in sg.Entries)
{
foreach (var kv in sg.Tiles.ToList())
{
if (kv.Value == entry.Src)
{
expanded = expanded || Set(sg, kv.Key * entry.Rotation, entry.Dest, "resolve conflicting rotations");
}
if (kv.Value == entry.Dest)
{
expanded = expanded || Set(sg, kv.Key * entry.Rotation.Inverse(), entry.Src, "resolve conflicting rotations");
}
}
}
} while (expanded);
}
private SubGroup Clone(SubGroup sg)
{
return new SubGroup
{
Entries = sg.Entries.ToList(),
Tiles = sg.Tiles.ToDictionary(x => x.Key, x => x.Value),
Treatment = sg.Treatment,
TreatmentSetBy = sg.TreatmentSetBy,
};
}
// Fills all remaining slots with RotatedTile
// Care is taken that as few distinct RotatedTiles are used as possible
// If there's two possible choices, prefernce is given to rotations over reflections.
private void Generate(SubGroup sg)
{
start:
// This doesn't use rotationGroup.Rotations as the order is well defined;
for (var refl = 0; refl < (rotationGroup.ReflectionalSymmetry ? 2 : 1); refl++)
{
for (var rot = 0; rot < 360; rot += rotationGroup.SmallestAngle)
{
var rotation = new Rotation(rot, refl > 0);
if (sg.Tiles.ContainsKey(rotation))
continue;
// Found an empty spot, figure out what to rotate from
for (var refl2 = 0; refl2 < (rotationGroup.ReflectionalSymmetry ? 2 : 1); refl2++)
{
for (var rot2 = 0; rot2 < 360; rot2 += rotationGroup.SmallestAngle)
{
var rotation2 = new Rotation(rot2, (refl2 > 0) != (refl > 0));
if (!sg.Tiles.TryGetValue(rotation2, out var srcTile))
continue;
// Don't allow RotatedTiles to nest.
if(srcTile.Value is RotatedTile rt)
{
srcTile = rt.Tile;
var rtRotation = rt.Rotation;
rotation2 = rtRotation.Inverse() * rotation2;
}
var srcToDest = rotation2.Inverse() * rotation;
Tile destTile;
if (srcToDest.ReflectX == false && srcToDest.RotateCw == 0)
{
destTile = srcTile;
}
else
{
destTile = new Tile(new RotatedTile
{
Tile = srcTile,
Rotation = srcToDest,
});
}
// Found where we want to rotate from
sg.Entries.Add(new Entry
{
Src = srcTile,
Rotation = srcToDest,
Dest = destTile,
});
Expand(sg);
goto start;
}
}
}
}
}
/// <summary>
/// Stores a set of tiles related to each other by transformations.
/// If we have two key value pairs (k1, v1) and (k2, v2) in Tiles, then
/// we can apply rortaion (k1.Inverse() * k2) to rotate v1 to v2.
/// </summary>
private class SubGroup
{
public List<Entry> Entries { get; set; } = new List<Entry>();
public Dictionary<Rotation, Tile> Tiles { get; set; } = new Dictionary<Rotation, Tile>();
public TileRotationTreatment? Treatment { get; set; }
public Tile TreatmentSetBy { get; set; }
// A tile may appear multiple times in a rotation group if it is symmetric in some way.
public List<Rotation> GetRotations(Tile tile)
{
return Tiles.Where(kv => kv.Value == tile).Select(x => x.Key).ToList();
}
public void Permute(Func<Rotation, Rotation> f)
{
Tiles = Tiles.ToDictionary(kv => f(kv.Key), kv => kv.Value);
}
}
private class Entry
{
public Tile Src { get; set; }
public Rotation Rotation { get; set; }
public Tile Dest { get; set; }
}
}
}