comparison pytouhou/utils/interpolator.pyx @ 131:fab7ad2f0d8b

Use Cython, improve performances!
author Thibaut Girka <thib@sitedethib.com>
date Sun, 11 Sep 2011 02:02:59 +0200
parents pytouhou/utils/interpolator.py@174324a4da51
children d778db08190f
comparison
equal deleted inserted replaced
130:11ab06f4c4c6 131:fab7ad2f0d8b
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15
16 class Interpolator(object):
17 __slots__ = ('values', 'start_values', 'end_values', 'start_frame', 'end_frame', '_frame', '_formula')
18 def __init__(self, values=(), start_frame=0, end_values=(), end_frame=0, formula=None):
19 self.values = tuple(values)
20 self.start_values = tuple(values)
21 self.end_values = tuple(end_values)
22 self.start_frame = start_frame
23 self.end_frame = end_frame
24 self._frame = 0
25 self._formula = formula or (lambda x: x)
26
27
28 def __nonzero__(self):
29 return self._frame < self.end_frame
30
31
32 def set_interpolation_start(self, frame, values):
33 self.start_values = tuple(values)
34 self.start_frame = frame
35
36
37 def set_interpolation_end(self, frame, values):
38 self.end_values = tuple(values)
39 self.end_frame = frame
40
41
42 def set_interpolation_end_frame(self, end_frame):
43 self.end_frame = end_frame
44
45
46 def set_interpolation_end_values(self, values):
47 self.end_values = tuple(values)
48
49
50 def update(self, frame):
51 self._frame = frame
52 if frame >= self.end_frame - 1: #XXX: skip the last interpolation step
53 # This bug is replicated from the original game
54 self.values = self.end_values
55 self.start_values = self.end_values
56 self.start_frame = frame
57 else:
58 coeff = self._formula(float(frame - self.start_frame) / float(self.end_frame - self.start_frame))
59 self.values = [start_value + coeff * (end_value - start_value)
60 for (start_value, end_value) in zip(self.start_values, self.end_values)]
61