-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotation.js
235 lines (210 loc) · 7.34 KB
/
rotation.js
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
/* global Vector3, create3dBoolArray */
// piece 1 can't rotate -> solutions won't have rotational symmetry
//piece 1 should have maximum possible rotations compared to other pieces
// there's width, depth, and height axis, or w,d,h (new axes); correspond to x, y, z
let FACES = 6;
let ROTATIONS = 4;
class PieceRotation{
constructor(){
this.face = 0;
this.rotation = 0;
}
//STATIC METHODS
static getAllRotatedPieces(originalPiece){ //returns 2d array containing all rotated piece arrays
let rotatedPieces = [];
//iterate through all rotations
for(let f =0; f<FACES; f++){
for(let r =0; r < ROTATIONS; r++){
let piece = PieceRotation.getRotatedPiece(originalPiece, {face: f, rotation: r});
if(!this.checkPieceMatch(piece, rotatedPieces)){ //if piece isn't a repeat
rotatedPieces.push(piece);
}
}
}
return rotatedPieces;
}
static checkPieceMatch(piece, arrayOfPieces){ //returns true if there's a match with piece
if(arrayOfPieces.length === 0){
return false;
}
//get dimensions of piece
let pzm = piece.length;
let pym = piece[0].length;
let pxm = piece[0][0].length;
for(let rp of arrayOfPieces){ //iterate through other pieces
let zm = rp.length;
let ym = rp[0].length;
let xm = rp[0][0].length;
//check if dimensions match
if(pzm === zm && pym === ym && pxm === xm){
let piecesMatch = true;
for(let z = 0; z < pzm; z++){ //iterate through piece
for(let y = 0; y < pym; y++){
for(let x = 0; x < pxm; x++){
if(piece[z][y][x] !== rp[z][y][x]){ //if piece units don't match
piecesMatch = false;
}
}
}
} //piece iteration loop end
if(piecesMatch){
return true;
}
} //pieces don't match, check next piece in for loop
} //none of pieces match
return false;
}
static getRotatedPiece(originalPiece, pieceRotation){ // parameters: 3d bool array, pieceRotation
let axisMatchup = AxisMatchup.getNewAxisMatchup(pieceRotation.face, pieceRotation.rotation);
let dimensions = new Vector3(originalPiece[0][0].length, originalPiece[0].length, originalPiece.length);
//setup new piece array
let newPieceDimensions = axisMatchup.getNewDimensions(dimensions);
let newPiece = create3dBoolArray(newPieceDimensions);
//iterating through original piece to set newPiece values
let zm = dimensions.z;
let ym = dimensions.y;
let xm = dimensions.x;
for(let z = 0; z < zm; z++){
for(let y = 0; y < ym; y++){
for(let x = 0; x < xm; x++){
if(originalPiece[z][y][x] === true){
let newCoor = axisMatchup.getNewCoordinates(new Vector3(x, y, z), dimensions);
//console.log(newCoor);
newPiece[newCoor.z][newCoor.y][newCoor.x] = true;
}
}
}
}
//returns 3d array
return newPiece;
}
}
// AXIS STUFF
let Axis = {
w: 1, //must start at 1; axis uses negative values and -0 = 0
d: 2,
h: 3,
}
class AxisMatchup { //for rotating pieceArrays
constructor(){
//this.w is the new w, which is set to one of the three original axes
this.w = Axis.w; // new axis = some old axis
this.d = Axis.d;
this.h = Axis.h;
}
//METHODS
getNewCoordinates(originalCoor, dimensions){ //original coordinates, xyz; dimensions of piece box
let vw = this.getNewCoordinateValue(originalCoor, this.w, dimensions); //value for the w axis
let vd = this.getNewCoordinateValue(originalCoor, this.d, dimensions);
let vh = this.getNewCoordinateValue(originalCoor, this.h, dimensions);
//returns new coordinates based on axis matchup
return new Vector3(vw, vd, vh);
}
getNewDimensions(oldDimensions){
let vw = this.getNewDimensionValue(this.w, oldDimensions); //value for the w axis
let vd = this.getNewDimensionValue(this.d, oldDimensions);
let vh = this.getNewDimensionValue(this.h, oldDimensions);
//returns new coordinates based on axis matchup
return new Vector3(vw, vd, vh);
}
//private
getNewCoordinateValue(coordinates, axisType, dimensions){ //gets a coordinate value from -coordinates- based on axisType
if(Axis.w === Math.abs(axisType)){
if(axisType<0){
return (-coordinates.x + dimensions.x-1);
}
return coordinates.x;
} else if(Axis.d === Math.abs(axisType)){
if(axisType<0){
return (-coordinates.y + dimensions.y-1);
}
return coordinates.y;
}else if(Axis.h === Math.abs(axisType)){
if(axisType<0){
return (-coordinates.z + dimensions.z-1)
}
return coordinates.z;
}
console.log("ERROR: Invalid axisType inputted: " + axisType);
}
getNewDimensionValue(axisType, oldDimensions){ //gets a coordinate value from -coordinates- based on axisType
if(Axis.w === Math.abs(axisType)){
return oldDimensions.x;
} else if(Axis.d === Math.abs(axisType)){
return oldDimensions.y;
}else if(Axis.h === Math.abs(axisType)){
return oldDimensions.z;
}
console.log("ERROR: Invalid axisType inputted: " + axisType);
}
//STATIC METHODS
static getNewAxisMatchup(targetFace, targetRotation){
let axisMatchup = new AxisMatchup();
axisMatchup = AxisMatchup.getAxisMatchupFace(axisMatchup, targetFace);
axisMatchup = AxisMatchup.getAxisMatchupRotation(axisMatchup, targetRotation);
return axisMatchup;
}
//private static methods
static getAxisMatchupFace(axisMatchup, targetFace){ //6 total faces
let newAxisMatchup = new AxisMatchup();
switch(targetFace) {
case 0:
//use original axis
return axisMatchup;
break;
case 1:
newAxisMatchup.w = axisMatchup.d;
newAxisMatchup.d = -axisMatchup.w;
newAxisMatchup.h = axisMatchup.h;
break;
case 2:
newAxisMatchup.w = -axisMatchup.w;
newAxisMatchup.d = -axisMatchup.d;
newAxisMatchup.h = axisMatchup.h;
break;
case 3:
newAxisMatchup.w = -axisMatchup.d;
newAxisMatchup.d = axisMatchup.w;
newAxisMatchup.h = axisMatchup.h;
break;
case 4:
newAxisMatchup.w = axisMatchup.w;
newAxisMatchup.d = -axisMatchup.h;
newAxisMatchup.h = axisMatchup.d;
break;
case 5:
newAxisMatchup.w = axisMatchup.w;
newAxisMatchup.d = axisMatchup.h;
newAxisMatchup.h = -axisMatchup.d;
break;
default:
console.log("Invalid face rotation entered: " + targetFace);
}
return newAxisMatchup;
}
static getAxisMatchupRotation(axisMatchup, targetRotation){ //4 ways to rotate a face
let newAxisMatchup = new AxisMatchup();
newAxisMatchup.d = axisMatchup.d;
switch(targetRotation) {
case 0:
//use original axis
return axisMatchup;
break;
case 1:
newAxisMatchup.w = axisMatchup.h;
newAxisMatchup.h = -axisMatchup.w;
break;
case 2:
newAxisMatchup.w = -axisMatchup.w;
newAxisMatchup.h = -axisMatchup.h;
break;
case 3:
newAxisMatchup.w = -axisMatchup.h;
newAxisMatchup.h = axisMatchup.w;
break;
default:
console.log("Invalid sub-rotation entered: " + targetRotation);
}
return newAxisMatchup;
}
}