-
Notifications
You must be signed in to change notification settings - Fork 2
/
Animator.py
77 lines (57 loc) · 2.02 KB
/
Animator.py
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
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import matplotlib.animation as animator
class Animator(object):
def prepareAnimation(self, figure, interval = 20, frames=100):
"""
Prepares the 2D animator object for animation.
keyword arguments:
figure -- Matplotlibs figure object.
interval -- The interval between two frames.
frames -- The number of frames used in the animation.
"""
self._figure = figure
self._interval = interval
self._frames = frames
return self
def feedSimulationData(self, simulationData, domainSize):
"""
Gets the simulation data and domain size and saves them as class properties.
keyword arguments:
simulationData -- The simulation data array.
domainSize -- The tuple that represents the lenghts of the square domain in each dimension.
return:
self
"""
self._time, self._positions, self._orientations = simulationData
self._domainSize = domainSize
return self
def saveAnimation(self, filename, fpsVar=25, codecVar="h264"):
"""
Saves the animation.
returns
Animator
"""
animation = self._getAnimation()
FFWriter = animator.FFMpegWriter(fps=fpsVar, codec=codecVar)
animation.save(filename, writer=FFWriter)
return self
def showAnimation(self):
"""
Show the animation to the user.
returns
self
"""
self._getAnimation()
plt.show()
return self
def _getAnimation(self):
return self.animation if 'animation' in self.__dict__ else self._generateAnimation()
def _generateAnimation(self):
"""
Generate the animation.
returns
animation object
"""
self.animation = FuncAnimation(self._figure, self._animate, interval=self._interval, frames = self._frames)
return self.animation