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
use std::fmt;
use std::ops::*;
// use flowscad::*;
// use crate::bitlib::swap_mask_shift_u64;
use crate::bitcube3::BitCube3;
use crate::bitcube4::BitCube4;
// -----------------------------------------------------------------
// 4x4x4 truncated octahedron space with a 3x3x3 space in between
// -----------------------------------------------------------------
// Position at (x,y,z) = x + 4*y + 16*z
// Rotations will happen from the center of the cube
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct BitTroc4 {
pub c4: BitCube4,
pub c3: BitCube3,
}
impl BitTroc4 {
pub fn rotate_x(self) -> Self {
Self {
c4: self.c4.rotate_x(),
c3: self.c3.rotate_x(),
}
}
pub fn rotate_y(self) -> Self {
Self {
c4: self.c4.rotate_y(),
c3: self.c3.rotate_y(),
}
}
pub fn rotate_z(self) -> Self {
Self {
c4: self.c4.rotate_z(),
c3: self.c3.rotate_z(),
}
}
pub fn rotate_d(self) -> Self {
Self {
c4: self.c4.rotate_d(),
c3: self.c3.rotate_d(),
}
}
pub fn shift_x(self, shift: i8) -> Self {
Self {
c4: self.c4.shift_x(shift),
c3: self.c3.shift_x(shift),
}
}
pub fn shift_y(self, shift: i8) -> Self {
Self {
c4: self.c4.shift_y(shift),
c3: self.c3.shift_y(shift),
}
}
pub fn shift_z(self, shift: i8) -> Self {
Self {
c4: self.c4.shift_z(shift),
c3: self.c3.shift_z(shift),
}
}
// /// Given a piece in the 4-cube, shift it towards the origin so that it touches the x, y, and z
// /// planes
// pub fn shift_to_origin(self) -> Self {
// let mut shape = self.0;
// let z_shift = (shape.trailing_zeros() / 16) * 16;
// shape = shape.unbounded_shr(z_shift);
// let xy_proj = shape | shape.unbounded_shr(32);
// let xy_proj = xy_proj | xy_proj.unbounded_shr(16);
// let y_shift = (xy_proj.trailing_zeros() / 4) * 4;
// shape = shape.unbounded_shr(y_shift);
// let x_shift = xy_proj | xy_proj.unbounded_shr(8);
// let x_shift = x_shift | x_shift.unbounded_shr(4);
// shape = shape.unbounded_shr(x_shift.trailing_zeros());
// Self(shape)
// }
pub fn overlap(self, other: Self) -> bool {
self.c4.overlap(other.c4) && self.c3.overlap(other.c3)
}
}
impl BitOr for BitTroc4 {
type Output = Self;
fn bitor(self, other: Self) -> Self::Output {
Self {
c4: self.c4 | other.c4,
c3: self.c3 | other.c3,
}
}
}
impl BitAnd for BitTroc4 {
type Output = Self;
fn bitand(self, other: Self) -> Self::Output {
Self {
c4: self.c4 & other.c4,
c3: self.c3 & other.c3,
}
}
}
// impl fmt::Debug for BitTroc4 {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// write!(f, "{}\n{}", self.c4, self.c3)
// }
// }
impl fmt::Display for BitTroc4 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:}\n{:}", self.c4, self.c3)
}
}
#[cfg(test)]
mod test {
use super::*;
// const FULL: BitCube4 = BitCube4(0xffff_ffff_ffff_ffff_u64);
const FULL: BitTroc4 = BitTroc4 {
c4: BitCube4(0xffff_ffff_ffff_ffff_u64),
c3: BitCube3(0o777_777_777),
};
const ORDER: BitTroc4 = BitTroc4 {
c4: BitCube4(0xfedc_ba98_7654_3210_u64),
c3: BitCube3(0o76543210),
};
// const UPPER_RIGHT_2X4X2: BitCube4 = BitCube4(0xcccc_cccc_0000_0000_u64);
// const LOWER_RIGHT_2X4X2: BitCube4 = BitCube4(0x0000_0000_cccc_cccc_u64);
// const LOWER_LEFT_2X4X2: BitCube4 = BitCube4(0x0000_0000_3333_3333_u64);
// const CENTER_X: BitCube4 = BitCube4(0x0000_0ff0_0ff0_0000_u64);
// const CENTER_Y: BitCube4 = BitCube4(0x0000_6666_6666_0000_u64);
// const CENTER_Z: BitCube4 = BitCube4(0x0660_0660_0660_0660_u64);
// const CENTER_ALL: BitCube4 = BitCube4(CENTER_X.0 | CENTER_Y.0 | CENTER_Z.0);
// const SUBCUBE_0: BitCube4 = BitCube4(0x0033_0033_u64);
// const SUBCUBE_1: BitCube4 = BitCube4(0x00cc_00cc_u64);
// const SUBCUBE_2: BitCube4 = BitCube4(0x3300_3300_u64);
// const SUBCUBE_3: BitCube4 = BitCube4(0xcc00_cc00_u64);
// const SUBCUBE_4: BitCube4 = BitCube4(0x0033_0033_0000_0000_u64);
// const SUBCUBE_5: BitCube4 = BitCube4(0x00cc_00cc_0000_0000_u64);
// const SUBCUBE_6: BitCube4 = BitCube4(0x3300_3300_0000_0000_u64);
// const SUBCUBE_7: BitCube4 = BitCube4(0xcc00_cc00_0000_0000_u64);
#[test]
fn test_debug() {
assert_eq!(format!("{:?}", ORDER),
"BitTroc4 { c4: BitCube4(0xfedcba9876543210), c3: BitCube3(0o076543210)\n010 101 000\n100 001 111\n000 110 011 }"
);
}
#[test]
fn test_display() {
assert_eq!(format!("{:}", ORDER),
"1100 1110 1101 1111\n0100 0110 0101 0111\n1000 1010 1001 1011\n0000 0010 0001 0011\n010 101 000\n100 001 111\n000 110 011"
);
}
#[test]
fn test_bitor() {
assert_eq!(FULL | FULL, FULL);
// assert_eq!(CENTER_X | CENTER_Y | CENTER_Z, CENTER_ALL);
}
#[test]
fn test_shift_x() {
assert_eq!(
FULL.shift_x(1),
BitTroc4 {
c4: BitCube4(0xeeee_eeee_eeee_eeee_u64),
c3: BitCube3(0o666_666_666)
}
);
}
/*
#[test]
fn test_shift_y() {
assert_eq!(FULL.shift_y(1),
BitCube4(0xfff0_fff0_fff0_fff0_u64)
);
}
#[test]
fn test_shift_z() {
assert_eq!(FULL.shift_z(1),
BitCube4(0xffff_ffff_ffff_0000_u64)
);
}
#[test]
fn test_shift_to_origin() {
assert_eq!(FULL.shift_to_origin(), FULL);
assert_eq!((CENTER_X | CENTER_Y).shift_to_origin(), BitCube4(0x0000_0000_6ff6_6ff6));
}
#[test]
fn test_rotate_x() {
assert_eq!(FULL.rotate_x(), FULL);
assert_eq!(CENTER_X.rotate_x(), CENTER_X);
assert_eq!(CENTER_Y.rotate_x(), CENTER_Z);
assert_eq!(CENTER_Z.rotate_x(), CENTER_Y);
assert_eq!(BitCube4(0xf).rotate_x(), BitCube4(0xf000));
assert_eq!(BitCube4(0xf000).rotate_x(), BitCube4(0xf000000000000000));
}
#[test]
fn test_rotate_y() {
assert_eq!(FULL.rotate_y(), FULL);
assert_eq!(UPPER_RIGHT_2X4X2.rotate_y(), LOWER_RIGHT_2X4X2);
assert_eq!(LOWER_RIGHT_2X4X2.rotate_y(), LOWER_LEFT_2X4X2);
assert_eq!(CENTER_X.rotate_y(), CENTER_Z);
assert_eq!(CENTER_Y.rotate_y(), CENTER_Y);
assert_eq!(CENTER_Z.rotate_y(), CENTER_X);
assert_eq!(BitCube4(0xf).rotate_y(), BitCube4(0x0001000100010001));
}
#[test]
fn test_rotate_z() {
assert_eq!(FULL.rotate_z(), FULL);
assert_eq!(CENTER_X.rotate_z(), CENTER_Y);
assert_eq!(CENTER_Y.rotate_z(), CENTER_X);
assert_eq!(CENTER_Z.rotate_z(), CENTER_Z);
assert_eq!(BitCube4(0xf).rotate_z(), BitCube4(0x8888));
}
#[test]
fn test_rotate_d() {
assert_eq!(FULL.rotate_d(), FULL);
assert_eq!(SUBCUBE_0.rotate_d(), SUBCUBE_0);
assert_eq!(SUBCUBE_1.rotate_d(), SUBCUBE_2);
assert_eq!(SUBCUBE_2.rotate_d(), SUBCUBE_4);
assert_eq!(SUBCUBE_4.rotate_d(), SUBCUBE_1);
assert_eq!(SUBCUBE_3.rotate_d(), SUBCUBE_6);
assert_eq!(SUBCUBE_6.rotate_d(), SUBCUBE_5);
assert_eq!(SUBCUBE_5.rotate_d(), SUBCUBE_3);
assert_eq!(SUBCUBE_7.rotate_d(), SUBCUBE_7);
assert_eq!(CENTER_X.rotate_d(), CENTER_Y);
assert_eq!(CENTER_Y.rotate_d(), CENTER_Z);
assert_eq!(CENTER_Z.rotate_d(), CENTER_X);
assert_eq!(BitCube4(0x1011f).rotate_d(), BitCube4(0x0000000100011113));
}
#[test]
fn test_overlap() {
assert!(CENTER_X.overlap(CENTER_Y));
}
#[test]
fn test_into_bitperm3() {
assert_eq!(BitCube4::from(BitCube3(0o777777777)), BitCube4(0x77707770777));
assert_eq!(BitCube4::from(BitCube3(0o700000000)), BitCube4(0x70000000000));
assert_eq!(BitCube4::from(BitCube3(0o76543210)), BitCube4(0x7605430210));
}
*/
}