-
Notifications
You must be signed in to change notification settings - Fork 1
/
FlipRotate2D.cs
305 lines (289 loc) · 10.3 KB
/
FlipRotate2D.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using StringBuilder = System.Text.StringBuilder;
using RotateFlipType = System.Drawing.RotateFlipType;
using DotNetTransformer.Extensions;
using DotNetTransformer.Math.Group;
using DotNetTransformer.Math.Permutation;
using DotNetTransformer.Math.Set;
namespace DotNetTransformer.Math.Transform {
using T = FlipRotate2D;
using P = PermutationByte;
[Serializable]
[DebuggerDisplay("{ToString()}, CycleLength = {CycleLength}")]
public struct FlipRotate2D : IFlipRotate<T, P>
{
public readonly byte Value;
private FlipRotate2D(byte value) { Value = value; }
private FlipRotate2D(int value) { Value = (byte)value; }
private FlipRotate2D(byte permutation, int vertex) {
Value = (byte)(0x65471320 >> ((vertex ^ permutation) << 2) & 7);
}
public FlipRotate2D(P permutation, int vertex) {
if((permutation._value & -6) != 0)
throw new ArgumentException(
"Parameter \"permutation\" has invalid value."
);
this = new T(permutation._value, vertex);
}
/// <summary>
/// "NO": No changes.
/// <para> 0 1 --> 0 1 </para>
/// <para> 3 2 --> 3 2 </para>
/// </summary>
public static T None { get { return new T(); } }
/// <summary>
/// "HT": 180 degree rotation.
/// <para> 0 1 --> 2 3 </para>
/// <para> 3 2 --> 1 0 </para>
/// </summary>
public static T HalfTurn { get { return new T(1); } }
/// <summary>
/// "FX": Horizontal flip. Reflection across y-axis.
/// <para> 0 1 --> 1 0 </para>
/// <para> 3 2 --> 2 3 </para>
/// </summary>
public static T FlipX { get { return new T(2); } }
/// <summary>
/// "FY": Vertical flip. Reflection across x-axis.
/// <para> 0 1 --> 3 2 </para>
/// <para> 3 2 --> 0 1 </para>
/// </summary>
public static T FlipY { get { return new T(3); } }
/// <summary>
/// "PD": Reflection across primary diagonal line.
/// <para> 0 1 --> 0 3 </para>
/// <para> 3 2 --> 1 2 </para>
/// </summary>
public static T ReflectOverPrimaryDiagonal { get { return new T(4); } }
/// <summary>
/// "SD": Reflection across secondary diagonal line.
/// <para> 0 1 --> 2 1 </para>
/// <para> 3 2 --> 3 0 </para>
/// </summary>
public static T ReflectOverSecondaryDiagonal { get { return new T(5); } }
/// <summary>
/// "RC": 90 degree clockwise rotation.
/// <para> 0 1 --> 3 0 </para>
/// <para> 3 2 --> 2 1 </para>
/// </summary>
public static T RotateClockwise { get { return new T(6); } }
/// <summary>
/// "RN": 90 degree counter clockwise rotation.
/// <para> 0 1 --> 1 2 </para>
/// <para> 3 2 --> 0 3 </para>
/// </summary>
public static T RotateCounterClockwise { get { return new T(7); } }
public static T GetFlip(int dimension) {
if((dimension & -_dimCount) != 0)
throw new ArgumentOutOfRangeException("dimension");
return new T(0, 1 << dimension);
}
public static T GetRotate(int dimFrom, int dimTo) {
if((dimFrom & -_dimCount) != 0)
throw new ArgumentOutOfRangeException("dimFrom");
if((dimTo & -_dimCount) != 0)
throw new ArgumentOutOfRangeException("dimTo");
if(dimFrom == dimTo)
throw new ArgumentException(
);
int x = dimFrom ^ dimTo;
P p = new P((byte)((x << (dimFrom << 1)) ^ (x << (dimTo << 1))));
return new T(p._value, 1 << dimTo);
}
private static IDictionary<byte, IFiniteSet<T>> _reflections;
private static IDictionary<byte, IFiniteGroup<T>> _rotations;
private static IDictionary<byte, IFiniteGroup<T>> _allValues;
public static IFiniteSet<T> GetReflections(int dimensions) {
if(dimensions == 0) return FiniteSet<T>.Empty;
return GetValues<IFiniteSet<T>>(
dimensions, ref _reflections,
dim => new ReflectionsSet<T, P>(dim, (P p, int v) => new T(p, v))
);
}
public static IFiniteGroup<T> GetRotations(int dimensions) {
if(dimensions == 0) dimensions = 1;
return GetValues<IFiniteGroup<T>>(
dimensions, ref _rotations,
dim => new RotationsGroup<T, P>(dim, (P p, int v) => new T(p, v))
);
}
public static IFiniteGroup<T> GetAllValues(int dimensions) {
if(dimensions == 0) return GetRotations(1);
return GetValues<IFiniteGroup<T>>(
dimensions, ref _allValues,
dim => new FlipRotateGroup<T, P>(dim, (P p, int v) => new T(p, v))
);
}
private static S GetValues<S>(int dimensions,
ref IDictionary<byte, S> collection,
Converter<byte, S> ctor
)
where S : IFiniteSet<T>
{
if(dimensions < 0 || dimensions > _dimCount)
throw new ArgumentOutOfRangeException(
);
byte dim = (byte)dimensions;
if(ReferenceEquals(collection, null))
collection = new SortedList<byte, S>(_dimCount + 1);
if(collection.ContainsKey(dim))
return collection[dim];
else {
S r = ctor(dim);
collection.Add(dim, r);
return r;
}
}
private const byte _dimCount = 2;
private const byte _count = 8;
private static readonly string[] _names;
static FlipRotate2D() {
_names = new string[_count] { "NO", "HT", "FX", "FY", "PD", "SD", "RC", "RN" };
}
/// <summary><return>
/// <para>true for "FX", "FY", "PD", "SD";</para>
/// <para>false for "NO", "HT", "RC", "RN".</para>
/// </return></summary>
public bool IsReflection { get { return (Value + 2 & 4) == 4; } }
/// <summary><return>
/// <para>true for "FX", "FY";</para>
/// <para>false for "NO", "HT", "PD", "SD", "RC", "RN".</para>
/// </return></summary>
public bool IsAxisReflection { get { return (Value & 6) == 2; } }
/// <summary><return>
/// <para>true for "PD", "SD";</para>
/// <para>false for "NO", "HT", "FX", "FY", "RC", "RN".</para>
/// </return></summary>
public bool IsDiagonalReflection { get { return (Value & 6) == 4; } }
/// <summary><return>
/// <para>true for "NO", "HT", "RC", "RN";</para>
/// <para>false for "FX", "FY", "PD", "SD".</para>
/// </return></summary>
public bool IsRotation { get { return (Value + 2 & 4) == 0; } }
/// <summary><return>
/// <para>true for "NO", "HT";</para>
/// <para>false for "FX", "FY", "PD", "SD", "RC", "RN".</para>
/// </return></summary>
public bool IsStraightAngleRotation { get { return Value < 2; } }
/// <summary><return>
/// <para>true for "RC", "RN";</para>
/// <para>false for "NO", "HT", "FX", "FY", "PD", "SD".</para>
/// </return></summary>
public bool IsRightAngleRotation { get { return Value > 5; } }
/// <summary><return>
/// <para>true for "PD", "SD", "RC", "RN";</para>
/// <para>false for "NO", "HT", "FX", "FY".</para>
/// </return></summary>
public bool IsSwapDimensions { get { return Value > 3; } }
public P Permutation {
get { return new P((byte)((Value >> 2) * 5)); }
}
public int Vertex {
get { return 0x6C9C >> (Value << 1) & 3; }
}
/// <summary>
/// The order of a cyclic group that can be generated by this element.
/// </summary>
public int CycleLength {
get {
return 0x44222221 >> (Value << 2) & 7;
/*//
return 1 << (0xA554 >> (Value << 1) & 3);
return 1 << ((Value + 3 - (Value >> 2)) >> 2);
//*/
}
}
public T InverseElement {
get {
return new T(Value + 2 >> 3 ^ Value);
/*//
return new T(0xC0 >> Value & 1 ^ Value);
return new T(0x67543210 >> (Value << 2) & 7);
return new T((Value >> 1) & (Value >> 2) ^ Value);
return new T(8 >> (Value >> 1) & 1 ^ Value);
return new T(0x40 >> (Value & 6) & 3 ^ Value);
//*/
}
}
public T Add(T other) {
return new T(Value >> 1 & (other.Value >> 2) ^ other.Value ^ Value);
/*//
return new T((other.Value >> 1 & Value) >> 1 ^ other.Value ^ Value);
//*/
}
public T Subtract(T other) {
return new T((other.Value ^ Value) >> 1 & (other.Value >> 2) ^ other.Value ^ Value);
/*//
return new T((other.Value >> 1 & (other.Value ^ Value)) >> 1 ^ other.Value ^ Value);
//*/
}
public T Times(int count) {
return new T((count & 1) * Value ^ ((Value >> 1 & Value & count) >> 1));
/*//
return new T((count & 1) * Value ^ ((Value + 2 >> 3) & (count >> 1)));
return new T((count & 1) * Value ^ ((0xC0 >> Value) & (count >> 1) & 1));
//*/
}
public override int GetHashCode() {
return Permutation.GetHashCode() ^ Vertex;
}
public override bool Equals(object o) { return o is T && Equals((T)o); }
public bool Equals(T o) { return Value == o.Value; }
public override string ToString() { return _names[Value]; }
public PermutationByte ToPermutationByte() {
P p = Permutation;
int v = Vertex;
const int b = 0x55;
v ^= ((b << p[0]) & 0x3 ^ v) << 2;
v ^= ((b << p[1]) & 0xF ^ v) << 4;
/*//
for(byte i = 0, l = 2; i < _dimCount; ++i, l <<= 1)
v ^= (((-1 << l) ^ -1) & (b << p[i]) ^ v) << l;
//*/
return PermutationByte.FromByteInternal((byte)v);
}
public RotateFlipType ToRotateFlipType() {
return (RotateFlipType)(0x31756420 >> (Value << 2) & 7);
/*//
return (RotateFlipType)((Value << 1 & 6) ^ (Value >> 2) ^ (Value & 4));
//*/
}
/// <exception cref="ArgumentException">
/// Invalid <paramref name="name"/>.
/// </exception>
public static T FromString(string name) {
int index = Array.IndexOf<string>(_names, name);
if(index >= 0) return new T(index);
StringBuilder sb = new StringBuilder("Acceptable values : ");
sb.Append(_names[0]);
for(int i = 1; i < _count; ++i) {
sb.Append(", ");
sb.Append(_names[i]);
}
sb.Append(".");
throw new ArgumentException(sb.ToString(), "name");
}
public static T FromInt32(int value) { return new T(value & 7); }
public static T FromRotateFlipType(RotateFlipType value) {
return new T(0x53427160 >> ((byte)value << 2) & 7);
/*//
byte v = (byte)value;
return new T((v << 2 & 4) ^ (v << 1 & 2) ^ (v >> 1));
//*/
}
public static bool operator ==(T l, T r) { return l.Equals(r); }
public static bool operator !=(T l, T r) { return !l.Equals(r); }
public static T operator +(T o) { return o; }
public static T operator -(T o) { return o.InverseElement; }
public static T operator +(T l, T r) { return l.Add(r); }
public static T operator -(T l, T r) { return l.Subtract(r); }
public static T operator *(T l, int r) { return l.Times(r); }
public static T operator *(int l, T r) { return r.Times(l); }
public static implicit operator T(P o) { return new T(o, 0); }
public static explicit operator T(int o) { return FromInt32(o); }
public static implicit operator T(RotateFlipType o) { return FromRotateFlipType(o); }
public static implicit operator RotateFlipType(T o) { return o.ToRotateFlipType(); }
}
}