-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcube.py
More file actions
230 lines (172 loc) · 7.51 KB
/
Copy pathcube.py
File metadata and controls
230 lines (172 loc) · 7.51 KB
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
import itertools
from manim.constants import DEGREES, PI
from manim.utils.color import *
from manim.mobject.types.vectorized_mobject import VGroup
from manim import override_animate
import numpy as np
from cubie import Cubie
from cube_utils import parse_move
DEFAULT_CUBE_COLORS = ["#ffffff", "#b71234", "#009b48", "#ffd500", "#ff5800", "#0046ad"]
class RubiksCube(VGroup):
# Each coordinate starts at 0 and goes to (Dimensions - 1)
# Colors are in the order Up, Right, Front, Down, Left, Back
def __init__(self, dim=3, colors=None, cubie_size=1.0, rotate_nicely=True):
if not (dim >= 2):
raise Exception("Dimension must be >= 2")
super(RubiksCube, self).__init__()
if colors is None:
colors = DEFAULT_CUBE_COLORS
self.dimensions = dim
self.colors = colors
self.cubie_size = cubie_size
self.cubies = np.ndarray((dim, dim, dim), dtype=Cubie)
self.generate_cubies()
# Center around the origin
self.shift(-self.cubie_size * (self.dim - 1) / 2)
# Rotate so that under the default camera, F is really the front etc.
self.rotate(axis=np.array([0, 0, 1]), angle=PI / 2)
self.rotate(axis=np.array([1, 0, 0]), angle=-PI / 2)
if rotate_nicely:
self.rotate(-20 * DEGREES, axis=np.array([0, 1, 0]))
self.rotate(20 * DEGREES, axis=np.array([1, 0, 0]))
def generate_cubies(self):
for x in range(self.dimensions):
for y in range(self.dimensions):
for z in range(self.dimensions):
cubie = Cubie(
x, y, z, self.dimensions, self.colors, self.cubie_size
)
cubie.shift(np.array((x, y, z), dtype=float) * self.cubie_size)
self.add(cubie)
self.cubies[x, y, z] = cubie
def set_state(self, positions):
colors = {
"U": self.colors[0],
"R": self.colors[1],
"F": self.colors[2],
"D": self.colors[3],
"L": self.colors[4],
"B": self.colors[5],
}
positions = list(positions)
# TODO: Try/except in case a color was not given
# try:
for cubie in np.rot90(self.get_face("U", False), 2).flatten():
cubie.get_face("U").set_fill(colors[positions.pop(0)], 1)
for cubie in np.rot90(np.flip(self.get_face("R", False), (0, 1)), -1).flatten():
cubie.get_face("R").set_fill(colors[positions.pop(0)], 1)
for cubie in np.rot90(np.flip(self.get_face("F", False), 0)).flatten():
cubie.get_face("F").set_fill(colors[positions.pop(0)], 1)
for cubie in np.rot90(np.flip(self.get_face("D", False), 0), 2).flatten():
cubie.get_face("D").set_fill(colors[positions.pop(0)], 1)
for cubie in np.rot90(np.flip(self.get_face("L", False), 0)).flatten():
cubie.get_face("L").set_fill(colors[positions.pop(0)], 1)
for cubie in np.rot90(np.flip(self.get_face("B", False), (0, 1)), -1).flatten():
cubie.get_face("B").set_fill(colors[positions.pop(0)], 1)
# except:
# return
def solve_by_kociemba(self, state):
return sv.solve(state).replace("3", "'").replace("1", "").split()
def get_face_slice(self, face):
"""
Return a NumPy slice object specifying which part of the array corresponds
to which face. NumPy sli indexing a ndarray,
e.g. a[:, 2] == a[np.s_[:, 2]]
"""
face_slices = {
"F": np.s_[0, :, :],
"B": np.s_[self.dimensions - 1, :, :],
"U": np.s_[:, :, self.dimensions - 1],
"D": np.s_[:, :, 0],
"L": np.s_[:, self.dimensions - 1, :],
"R": np.s_[:, 0, :],
}
if face in face_slices:
return face_slices[face]
else:
raise ValueError("Invalid face identifier " + face)
def get_face(self, face, flatten=True):
face = self.cubies[self.get_face_slice(face)]
if flatten:
return face.flatten()
else:
return face
def do_move(self, move):
face, n_turns = parse_move(move)
# Actually do the spatial rotation
axis = self.get_face(face, flatten=False)[1, 1].get_center() - self.get_center()
VGroup(*self.get_face(face)).rotate(
-(PI / 2) * n_turns,
axis,
)
self.update_indices_after_move(move)
# For chaining
return self
def update_indices_after_move(self, move):
face, n_turns = parse_move(move)
# We need to make sure that moves that are supposed to be clockwise really are
n_turns_indices = n_turns if (face in {"L", "F", "D"}) else -n_turns
# Get to a non-negative value
n_turns_indices = (n_turns_indices + 4) % 4
face_slice = self.get_face_slice(face)
face_cubies = self.cubies[face_slice]
# Change the indices of the cubies to what we expect after the move
face_cubies = np.rot90(face_cubies, k=n_turns_indices)
self.cubies[face_slice] = face_cubies
@override_animate(do_move)
def _do_move_animation(self, move, anim_args=None):
if anim_args is None:
anim_args = {}
anim = CubeMove(self, move, **anim_args)
return anim
def hash(self) -> int:
"""
Returns a deterministic int representation of the cube's configuration (state).
"""
h = 0
for i, j, k in itertools.product(range(3), range(3), range(3)):
h = hash((self.cubies[i][j][k].hash_id, h))
return h
def set_stroke_width(self, stroke_width: float):
for cubie in self.cubies.reshape(-1):
for face in cubie.submobjects:
face.stroke_width = stroke_width
return self
import numpy as np
from manim.animation.animation import Animation
from manim.constants import PI
from manim.mobject.types.vectorized_mobject import VGroup
from cube import RubiksCube
class CubeMove(Animation):
def __init__(self, mobject: RubiksCube, move, target_position=None, **kwargs):
# This only makes sense when called on a RubiksCube
assert isinstance(mobject, RubiksCube)
self.target_position = target_position if target_position is not None else mobject.get_center()
face, n_turns = parse_move(move)
# Compute the axis of rotation by taking the vector from the cube's center
# to the middle cubie of the rotated face
# TODO: this might accumulate numerical errors, but it seems ok for tens of moves
self.axis = (
mobject.get_face(face, flatten=False)[1, 1].get_center()
- mobject.get_center()
)
self.move = move
self.face = face
self.n_turns = n_turns
super().__init__(mobject, **kwargs)
def create_starting_mobject(self):
starting_mobject = self.mobject.copy()
return starting_mobject
def interpolate_mobject(self, alpha):
self.mobject.become(self.starting_mobject)
self.mobject.move_to(
self.rate_func(alpha) * self.target_position
+ (1 - self.rate_func(alpha)) * self.starting_mobject.get_center()
)
VGroup(*self.mobject.get_face(self.face)).rotate(
-self.rate_func(alpha) * (PI / 2) * self.n_turns,
self.axis,
)
def finish(self):
super().finish()
self.mobject.update_indices_after_move(self.move)