本文整理汇总了Python中projection.Quaternion类的典型用法代码示例。如果您正苦于以下问题:Python Quaternion类的具体用法?Python Quaternion怎么用?Python Quaternion使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Quaternion类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _mouse_motion
def _mouse_motion(self, event):
"""Handler for mouse motion"""
if self._button1 or self._button2:
dx = event.x - self._event_xy[0]
dy = event.y - self._event_xy[1]
self._event_xy = (event.x, event.y)
if self._button1:
if self._shift:
ax_LR = self._ax_LR_alt
else:
ax_LR = self._ax_LR
rot1 = Quaternion.from_v_theta(self._ax_UD,
self._step_UD * dy)
rot2 = Quaternion.from_v_theta(ax_LR,
self._step_LR * dx)
self.rotate(rot1 * rot2)
self._draw_cube()
if self._button2:
factor = 1 - 0.003 * (dx + dy)
xlim = self.get_xlim()
ylim = self.get_ylim()
self.set_xlim(factor * xlim[0], factor * xlim[1])
self.set_ylim(factor * ylim[0], factor * ylim[1])
self.figure.canvas.draw()
示例2: cube_axes
def cube_axes(N=1, **kwargs):
"""Create an N x N x N rubiks cube
kwargs are passed to the PolyView3D instance.
"""
stickerwidth = 0.9
small = 0.5 * (1. - stickerwidth)
d1 = 1 - small
d2 = 1 - 2 * small
d3 = 1.01
base_sticker = np.array([[d1, d2, d3], [d2, d1, d3],
[-d2, d1, d3], [-d1, d2, d3],
[-d1, -d2, d3], [-d2, -d1, d3],
[d2, -d1, d3], [d1, -d2, d3],
[d1, d2, d3]], dtype=float)
base_face = np.array([[1, 1, 1],
[1, -1, 1],
[-1, -1, 1],
[-1, 1, 1],
[1, 1, 1]], dtype=float)
x, y, z = np.eye(3)
rots = [Quaternion.from_v_theta(x, theta)
for theta in (np.pi / 2, -np.pi / 2)]
rots += [Quaternion.from_v_theta(y, theta)
for theta in (np.pi / 2, -np.pi / 2, np.pi, 2 * np.pi)]
cubie_width = 2. / N
translations = np.array([[-1 + (i + 0.5) * cubie_width,
-1 + (j + 0.5) * cubie_width, 0]
for i in range(N) for j in range(N)])
colors = ['blue', 'green', 'white', 'yellow', 'orange', 'red']
factor = np.array([1. / N, 1. / N, 1])
ax = PolyView3D(**kwargs)
facecolor = []
polys = []
for t in translations:
base_face_trans = factor * base_face + t
base_sticker_trans = factor * base_sticker + t
for r, c in zip(rots, colors):
polys += [r.rotate(base_face_trans),
r.rotate(base_sticker_trans)]
facecolor += ['k', c]
ax.poly3D_batch(polys, facecolor=facecolor)
ax.figure.text(0.05, 0.05,
("Drag Mouse or use arrow keys to change perspective.\n"
"Hold shift to adjust z-axis rotation"),
ha='left', va='bottom')
return ax
示例3: _key_press
def _key_press(self, event):
"""Handler for key press events"""
if event.key == 'shift':
self._shift = True
elif event.key.isdigit():
self._digit_flags[int(event.key)] = 1
elif event.key == 'right':
if self._shift:
ax_LR = self._ax_LR_alt
else:
ax_LR = self._ax_LR
self.rotate(Quaternion.from_v_theta(ax_LR,
5 * self._step_LR))
elif event.key == 'left':
if self._shift:
ax_LR = self._ax_LR_alt
else:
ax_LR = self._ax_LR
self.rotate(Quaternion.from_v_theta(ax_LR,
-5 * self._step_LR))
elif event.key == 'up':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
5 * self._step_UD))
elif event.key == 'down':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
-5 * self._step_UD))
elif event.key.upper() in 'LRUDBF':
if self._shift:
direction = -1
else:
direction = 1
if np.any(self._digit_flags[:self.cube.N]):
for d in solvesolve.arange(self.cube.N)[self._digit_flags[:self.cube.N]]:
self.rotate_face(event.key.upper(), direction, layer=d)
if direction == 1:
self._pycuber_rep((event.key.upper()))
elif direction == -1:
self._pycuber_rep((event.key.upper()+"'"))
else:
self.rotate_face(event.key.upper(), direction)
if direction == 1:
self._pycuber_rep((event.key.upper()))
elif direction == -1:
self._pycuber_rep((event.key.upper()+"'"))
self._draw_cube()
示例4: rotate_event
def rotate_event(self, direction, stepsize):
if direction == 'right':
self.rotate(Quaternion.from_v_theta(self._ax_LR,
stepsize * self._step_LR))
elif direction == 'left':
self.rotate(Quaternion.from_v_theta(self._ax_LR,
-stepsize * self._step_LR))
elif direction == 'up':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
stepsize * self._step_UD))
elif direction == 'down':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
-stepsize * self._step_UD))
self._draw_cube()
示例5: rotate_face
def rotate_face(self, f, n=1, layer=0):
"""Rotate Face"""
if layer < 0 or layer >= self.N:
raise ValueError('layer should be between 0 and N-1')
try:
f_last, n_last, layer_last = self._move_list[-1]
except:
f_last, n_last, layer_last = None, None, None
if (f == f_last) and (layer == layer_last):
ntot = (n_last + n) % 4
if abs(ntot - 4) < abs(ntot):
ntot = ntot - 4
if np.allclose(ntot, 0):
self._move_list = self._move_list[:-1]
else:
self._move_list[-1] = (f, ntot, layer)
else:
self._move_list.append((f, n, layer))
v = self.facesdict[f]
r = Quaternion.from_v_theta(v, n * np.pi / 2)
M = r.as_rotation_matrix()
proj = np.dot(self._face_centroids[:, :3], v)
cubie_width = 2. / self.N
flag = ((proj > 0.9 - (layer + 1) * cubie_width) &
(proj < 1.1 - layer * cubie_width))
for x in [self._stickers, self._sticker_centroids,
self._faces]:
x[flag] = np.dot(x[flag], M.T)
self._face_centroids[flag, :3] = np.dot(self._face_centroids[flag, :3],
M.T)
示例6: _key_press
def _key_press(self, event):
"""Handler for key press events"""
if not event.key:
return
elif event.key == 'shift':
self._shift = True
elif event.key.isdigit():
self._digit_flags[int(event.key)] = 1
elif event.key == 'right':
if self._shift:
ax_LR = self._ax_LR_alt
else:
ax_LR = self._ax_LR
self.rotate(Quaternion.from_v_theta(ax_LR,
5 * self._step_LR))
elif event.key == 'left':
if self._shift:
ax_LR = self._ax_LR_alt
else:
ax_LR = self._ax_LR
self.rotate(Quaternion.from_v_theta(ax_LR,
- 5 * self._step_LR))
elif event.key == 'up':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
5 * self._step_UD))
elif event.key == 'down':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
- 5 * self._step_UD))
elif event.key.upper() in InteractiveCube.FACES:
# if self._shift:
if event.key in InteractiveCube.FACES:
# Upper-case
direction = -1
else:
# Lower-case
direction = 1
N = self.cube.N
if np.any(self._digit_flags[:N]):
for d in np.arange(N)[self._digit_flags[:N]]:
self.rotate_face(event.key.upper(), direction, layer=d)
else:
self.rotate_face(event.key.upper(), direction)
self._draw_cube()
示例7: _key_press
def _key_press(self, event):
"""Handler for key press events"""
if event.key == 'shift':
self._ax_LR = (0, 0, 1)
elif event.key == 'right':
self.rotate(Quaternion.from_v_theta(self._ax_LR,
5 * self._step_LR))
elif event.key == 'left':
self.rotate(Quaternion.from_v_theta(self._ax_LR,
-5 * self._step_LR))
elif event.key == 'up':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
5 * self._step_UD))
elif event.key == 'down':
self.rotate(Quaternion.from_v_theta(self._ax_UD,
-5 * self._step_UD))
self._update_projection()
示例8: rotate
def rotate(self, v, theta):
new_rot = Quaternion.from_v_theta(v, theta)
self.current_rotation = self.current_rotation * new_rot
faces_proj = project_points(self._faces, self.current_rotation,
self.view)
xy = faces_proj[:, :, :2]
zorder = -faces_proj[:, :4, 2].mean(-1)
for i in range(6):
self.polys[i].set_xy(xy[i])
self.polys[i].set_zorder(zorder[i])
示例9: __init__
def __init__(self, view=(0, 0, 10), colors=None, alpha=0.8):
self.view = view
self.current_rotation = Quaternion.from_v_theta((0, 0, 1), 0)
if colors is None:
colors = ['blue', 'green', 'white', 'yellow', 'orange', 'red']
else:
assert len(colors) == 6
self.polys = [plt.Polygon(self._faces[i, :, :2],
facecolor=colors[i],
alpha=alpha)
for i in range(6)]
示例10: __init__
def __init__(self, view=(0, 0, 10), fig=None,
rect=[0, 0, 1, 1], **kwargs):
if fig is None:
fig = plt.gcf()
self.view = np.asarray(view)
self.start_rot = Quaternion.from_v_theta((1, -1, 0), -np.pi / 6)
# Define movement for up/down arrows or up/down mouse movement
self._ax_UD = (1, 0, 0)
self._step_UD = 0.01
# Define movement for left/right arrows or left/right mouse movement
self._ax_LR = (0, -1, 0)
self._step_LR = 0.01
# Internal state variable
self._button1 = False
self._button2 = False
self._event_xy = None
self._current_rot = self.start_rot
self._npts = [1]
self._xyzs = [[0, 0, 0]]
self._xys = [[0, 0]]
self._polys = []
# initialize the axes. We'll set some keywords by default
kwargs.update(dict(aspect='equal',
xlim=(-2.5, 2.5), ylim=(-2.5, 2.5),
frameon=False, xticks=[], yticks=[]))
super(PolyView3D, self).__init__(fig, rect, **kwargs)
self.xaxis.set_major_formatter(plt.NullFormatter())
self.yaxis.set_major_formatter(plt.NullFormatter())
# connect some GUI events
self.figure.canvas.mpl_connect('button_press_event',
self._mouse_press)
self.figure.canvas.mpl_connect('button_release_event',
self._mouse_release)
self.figure.canvas.mpl_connect('motion_notify_event',
self._mouse_motion)
self.figure.canvas.mpl_connect('key_press_event',
self._key_press)
self.figure.canvas.mpl_connect('key_release_event',
self._key_release)
示例11: __init__
def __init__(self, cube=None,
interactive=True,
view=(0, 0, 10),
fig=None, rect=[0, 0.16, 1, 0.84],
**kwargs):
if cube is None:
self.cube = Cube(3)
elif isinstance(cube, Cube):
self.cube = cube
else:
self.cube = Cube(cube)
self._view = view
self._start_rot = Quaternion.from_v_theta((1, -1, 0),
-np.pi / 6)
if fig is None:
fig = plt.gcf()
# disable default key press events
callbacks = fig.canvas.callbacks.callbacks
del callbacks['key_press_event']
# add some defaults, and draw axes
kwargs.update(dict(aspect=kwargs.get('aspect', 'equal'),
xlim=kwargs.get('xlim', (-2.0, 2.0)),
ylim=kwargs.get('ylim', (-2.0, 2.0)),
frameon=kwargs.get('frameon', False),
xticks=kwargs.get('xticks', []),
yticks=kwargs.get('yticks', [])))
super(InteractiveCube, self).__init__(fig, rect, **kwargs)
self.xaxis.set_major_formatter(plt.NullFormatter())
self.yaxis.set_major_formatter(plt.NullFormatter())
self._start_xlim = kwargs['xlim']
self._start_ylim = kwargs['ylim']
# Define movement for up/down arrows or up/down mouse movement
self._ax_UD = (1, 0, 0)
self._step_UD = 0.01
# Define movement for left/right arrows or left/right mouse movement
self._ax_LR = (0, -1, 0)
self._step_LR = 0.01
self._ax_LR_alt = (0, 0, 1)
# Internal state variable
self._active = False # true when mouse is over axes
self._button1 = False # true when button 1 is pressed
self._button2 = False # true when button 2 is pressed
self._event_xy = None # store xy position of mouse event
self._shift = False # shift key pressed
self._digit_flags = np.zeros(10, dtype=bool) # digits 0-9 pressed
self._current_rot = self._start_rot #current rotation state
self._face_polys = None
self._sticker_polys = None
self._draw_cube()
# connect some GUI events
self.figure.canvas.mpl_connect('button_press_event',
self._mouse_press)
self.figure.canvas.mpl_connect('button_release_event',
self._mouse_release)
self.figure.canvas.mpl_connect('motion_notify_event',
self._mouse_motion)
self.figure.canvas.mpl_connect('key_press_event',
self._key_press)
self.figure.canvas.mpl_connect('key_release_event',
self._key_release)
self._initialize_widgets()
# write some instructions
self.figure.text(0.05, 0.05,
"Mouse/arrow keys adjust view\n"
"U/D/L/R/B/F keys turn faces\n"
"(hold shift for counter-clockwise)",
size=10)