demo2.py
Go to the documentation of this file.
1 """
2 This file is part of the pyquaternion python module
3 
4 Author: Kieran Wynn
5 Website: https://github.com/KieranWynn/pyquaternion
6 Documentation: http://kieranwynn.github.io/pyquaternion/
7 
8 Version: 1.0.0
9 License: The MIT License (MIT)
10 
11 Copyright (c) 2015 Kieran Wynn
12 
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19 
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22 
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30 
31 demo.py - Demo of pyquaternion using matplotlib
32 
33 """
34 
35 import numpy as np
36 
37 from pyquaternion import Quaternion
38 
39 #import matplotlib
40 #matplotlib.use('TKAgg')
41 
42 from matplotlib import pyplot as plt
43 from matplotlib import animation
44 from mpl_toolkits.mplot3d import Axes3D
45 
47  q1 = Quaternion.random()
48  q2 = Quaternion.random()
49  while True:
50  for q in Quaternion.intermediates(q1, q2, 20, include_endpoints=True):
51  yield q
52  #q1, q2 = q2, q1
53  q1 = q2
54  q2 = Quaternion.random()
55 
56 quaternion_generator = generate_quaternion()
57 
58 # Set up figure & 3D axis for animation
59 fig = plt.figure()
60 ax = fig.add_axes([0, 0, 1, 1], projection='3d')
61 ax.set_xlabel('X')
62 ax.set_ylabel('Y')
63 ax.set_zlabel('Z')
64 #ax.axis('off')
65 
66 # use a different color for each axis
67 colors = ['r', 'g', 'b']
68 
69 # set up lines and points
70 lines = sum([ax.plot([], [], [], c=c)
71  for c in colors], [])
72 
73 startpoints = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
74 endpoints = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
75 
76 # prepare the axes limits
77 ax.set_xlim((-8, 8))
78 ax.set_ylim((-8, 8))
79 ax.set_zlim((-8, 8))
80 
81 # set point-of-view: specified by (altitude degrees, azimuth degrees)
82 ax.view_init(30, 0)
83 
84 
85 # initialization function: plot the background of each frame
86 def init():
87  for line in lines:
88  line.set_data([], [])
89  line.set_3d_properties([])
90 
91  return lines
92 
93 # animation function. This will be called sequentially with the frame number
94 def animate(i):
95  # we'll step two time-steps per frame. This leads to nice results.
96  #i = (2 * i) % x_t.shape[1]
97 
98  q = next(quaternion_generator)
99  #print("q:", q)
100 
101  for line, start, end in zip(lines, startpoints, endpoints):
102  #end *= 5
103  start = q.rotate(start)
104  end = q.rotate(end)
105 
106  line.set_data([start[0], end[0]], [start[1], end[1]])
107  line.set_3d_properties([start[2], end[2]])
108 
109  #pt.set_data(x[-1:], y[-1:])
110  #pt.set_3d_properties(z[-1:])
111 
112  #ax.view_init(30, 0.6 * i)
113  fig.canvas.draw()
114  return lines
115 
116 # instantiate the animator.
117 anim = animation.FuncAnimation(fig, animate, init_func=init,
118  frames=500, interval=30, blit=False)
119 
120 # Save as mp4. This requires mplayer or ffmpeg to be installed
121 #anim.save('lorentz_attractor.mp4', fps=15, extra_args=['-vcodec', 'libx264'])
122 
123 plt.show()
def generate_quaternion()
Definition: demo2.py:46
def animate(i)
Definition: demo2.py:94
def init()
Definition: demo2.py:86


pyquaternion
Author(s): achille
autogenerated on Sun Mar 15 2020 03:13:33