- 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
// Your friendly neighbour https://en.wikipedia.org/wiki/Dihedral_group
//
// This file implements the dihedral group of order 16, also called
// of degree 8. That's why its called groupD8.
import { Matrix } from './Matrix';
/*
* Transform matrix for operation n is:
* | ux | vx |
* | uy | vy |
*/
const ux = [1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, -1, -1, 0, 1];
const uy = [0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, -1, -1];
const vx = [0, -1, -1, -1, 0, 1, 1, 1, 0, 1, 1, 1, 0, -1, -1, -1];
const vy = [1, 1, 0, -1, -1, -1, 0, 1, -1, -1, 0, 1, 1, 1, 0, -1];
/**
* [Cayley Table]{@link https://en.wikipedia.org/wiki/Cayley_table}
* for the composition of each rotation in the dihederal group D8.
* @type {number[][]}
* @private
*/
const rotationCayley: number[][] = [];
/**
* Matrices for each `GD8Symmetry` rotation.
* @type {PIXI.Matrix[]}
* @private
*/
const rotationMatrices: Matrix[] = [];
/*
* Alias for {@code Math.sign}.
*/
const signum = Math.sign;
/*
* Initializes `rotationCayley` and `rotationMatrices`. It is called
* only once below.
*/
function init(): void
{
for (let i = 0; i < 16; i++)
{
const row: number[] = [];
rotationCayley.push(row);
for (let j = 0; j < 16; j++)
{
/* Multiplies rotation matrices i and j. */
const _ux = signum((ux[i] * ux[j]) + (vx[i] * uy[j]));
const _uy = signum((uy[i] * ux[j]) + (vy[i] * uy[j]));
const _vx = signum((ux[i] * vx[j]) + (vx[i] * vy[j]));
const _vy = signum((uy[i] * vx[j]) + (vy[i] * vy[j]));
/* Finds rotation matrix matching the product and pushes it. */
for (let k = 0; k < 16; k++)
{
if (ux[k] === _ux && uy[k] === _uy
&& vx[k] === _vx && vy[k] === _vy)
{
row.push(k);
break;
}
}
}
}
for (let i = 0; i < 16; i++)
{
const mat = new Matrix();
mat.set(ux[i], uy[i], vx[i], vy[i], 0, 0);
rotationMatrices.push(mat);
}
}
init();
type GD8Symmetry = number;
/**
* @memberof PIXI
* @typedef {number} GD8Symmetry
* @see PIXI.groupD8
*/
/**
* Implements the dihedral group D8, which is similar to
* group D4;
* D8 is the same but with diagonals, and it is used for texture
* rotations.
*
* The directions the U- and V- axes after rotation
* of an angle of `a: GD8Constant` are the vectors `(uX(a), uY(a))`
* and `(vX(a), vY(a))`. These aren't necessarily unit vectors.
*
* **Origin:**<br>
* This is the small part of gameofbombs.com portal system. It works.
* @see PIXI.groupD8.E
* @see PIXI.groupD8.SE
* @see PIXI.groupD8.S
* @see PIXI.groupD8.SW
* @see PIXI.groupD8.W
* @see PIXI.groupD8.NW
* @see PIXI.groupD8.N
* @see PIXI.groupD8.NE
* @author Ivan @ivanpopelyshev
* @namespace PIXI.groupD8
* @memberof PIXI
*/
export const groupD8 = {
/**
* | Rotation | Direction |
* |----------|-----------|
* | 0° | East |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
E: 0,
/**
* | Rotation | Direction |
* |----------|-----------|
* | 45°↻ | Southeast |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
SE: 1,
/**
* | Rotation | Direction |
* |----------|-----------|
* | 90°↻ | South |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
S: 2,
/**
* | Rotation | Direction |
* |----------|-----------|
* | 135°↻ | Southwest |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
SW: 3,
/**
* | Rotation | Direction |
* |----------|-----------|
* | 180° | West |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
W: 4,
/**
* | Rotation | Direction |
* |-------------|--------------|
* | -135°/225°↻ | Northwest |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
NW: 5,
/**
* | Rotation | Direction |
* |-------------|--------------|
* | -90°/270°↻ | North |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
N: 6,
/**
* | Rotation | Direction |
* |-------------|--------------|
* | -45°/315°↻ | Northeast |
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
NE: 7,
/**
* Reflection about Y-axis.
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
MIRROR_VERTICAL: 8,
/**
* Reflection about the main diagonal.
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
MAIN_DIAGONAL: 10,
/**
* Reflection about X-axis.
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
MIRROR_HORIZONTAL: 12,
/**
* Reflection about reverse diagonal.
* @memberof PIXI.groupD8
* @constant {PIXI.GD8Symmetry}
*/
REVERSE_DIAGONAL: 14,
/**
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
* @returns {PIXI.GD8Symmetry} The X-component of the U-axis
* after rotating the axes.
*/
uX: (ind: GD8Symmetry): GD8Symmetry => ux[ind],
/**
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
* @returns {PIXI.GD8Symmetry} The Y-component of the U-axis
* after rotating the axes.
*/
uY: (ind: GD8Symmetry): GD8Symmetry => uy[ind],
/**
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
* @returns {PIXI.GD8Symmetry} The X-component of the V-axis
* after rotating the axes.
*/
vX: (ind: GD8Symmetry): GD8Symmetry => vx[ind],
/**
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} ind - sprite rotation angle.
* @returns {PIXI.GD8Symmetry} The Y-component of the V-axis
* after rotating the axes.
*/
vY: (ind: GD8Symmetry): GD8Symmetry => vy[ind],
/**
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} rotation - symmetry whose opposite
* is needed. Only rotations have opposite symmetries while
* reflections don't.
* @returns {PIXI.GD8Symmetry} The opposite symmetry of `rotation`
*/
inv: (rotation: GD8Symmetry): GD8Symmetry =>
{
if (rotation & 8)// true only if between 8 & 15 (reflections)
{
return rotation & 15;// or rotation % 16
}
return (-rotation) & 7;// or (8 - rotation) % 8
},
/**
* Composes the two D8 operations.
*
* Taking `^` as reflection:
*
* | | E=0 | S=2 | W=4 | N=6 | E^=8 | S^=10 | W^=12 | N^=14 |
* |-------|-----|-----|-----|-----|------|-------|-------|-------|
* | E=0 | E | S | W | N | E^ | S^ | W^ | N^ |
* | S=2 | S | W | N | E | S^ | W^ | N^ | E^ |
* | W=4 | W | N | E | S | W^ | N^ | E^ | S^ |
* | N=6 | N | E | S | W | N^ | E^ | S^ | W^ |
* | E^=8 | E^ | N^ | W^ | S^ | E | N | W | S |
* | S^=10 | S^ | E^ | N^ | W^ | S | E | N | W |
* | W^=12 | W^ | S^ | E^ | N^ | W | S | E | N |
* | N^=14 | N^ | W^ | S^ | E^ | N | W | S | E |
*
* [This is a Cayley table]{@link https://en.wikipedia.org/wiki/Cayley_table}
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} rotationSecond - Second operation, which
* is the row in the above cayley table.
* @param {PIXI.GD8Symmetry} rotationFirst - First operation, which
* is the column in the above cayley table.
* @returns {PIXI.GD8Symmetry} Composed operation
*/
add: (rotationSecond: GD8Symmetry, rotationFirst: GD8Symmetry): GD8Symmetry => (
rotationCayley[rotationSecond][rotationFirst]
),
/**
* Reverse of `add`.
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} rotationSecond - Second operation
* @param {PIXI.GD8Symmetry} rotationFirst - First operation
* @returns {PIXI.GD8Symmetry} Result
*/
sub: (rotationSecond: GD8Symmetry, rotationFirst: GD8Symmetry): GD8Symmetry => (
rotationCayley[rotationSecond][groupD8.inv(rotationFirst)]
),
/**
* Adds 180 degrees to rotation, which is a commutative
* operation.
* @memberof PIXI.groupD8
* @param {number} rotation - The number to rotate.
* @returns {number} Rotated number
*/
rotate180: (rotation: number): number => rotation ^ 4,
/**
* Checks if the rotation angle is vertical, i.e. south
* or north. It doesn't work for reflections.
* @memberof PIXI.groupD8
* @param {PIXI.GD8Symmetry} rotation - The number to check.
* @returns {boolean} Whether or not the direction is vertical
*/
isVertical: (rotation: GD8Symmetry): boolean => (rotation & 3) === 2, // rotation % 4 === 2
/**
* Approximates the vector `V(dx,dy)` into one of the
* eight directions provided by `groupD8`.
* @memberof PIXI.groupD8
* @param {number} dx - X-component of the vector
* @param {number} dy - Y-component of the vector
* @returns {PIXI.GD8Symmetry} Approximation of the vector into
* one of the eight symmetries.
*/
byDirection: (dx: number, dy: number): GD8Symmetry =>
{
if (Math.abs(dx) * 2 <= Math.abs(dy))
{
if (dy >= 0)
{
return groupD8.S;
}
return groupD8.N;
}
else if (Math.abs(dy) * 2 <= Math.abs(dx))
{
if (dx > 0)
{
return groupD8.E;
}
return groupD8.W;
}
else if (dy > 0)
{
if (dx > 0)
{
return groupD8.SE;
}
return groupD8.SW;
}
else if (dx > 0)
{
return groupD8.NE;
}
return groupD8.NW;
},
/**
* Helps sprite to compensate texture packer rotation.
* @memberof PIXI.groupD8
* @param {PIXI.Matrix} matrix - sprite world matrix
* @param {PIXI.GD8Symmetry} rotation - The rotation factor to use.
* @param {number} tx - sprite anchoring
* @param {number} ty - sprite anchoring
*/
matrixAppendRotationInv: (matrix: Matrix, rotation: GD8Symmetry, tx = 0, ty = 0): void =>
{
// Packer used "rotation", we use "inv(rotation)"
const mat: Matrix = rotationMatrices[groupD8.inv(rotation)];
mat.tx = tx;
mat.ty = ty;
matrix.append(mat);
},
};