annotate pytouhou/utils/interpolator.pyx @ 677:082c39d7d1c3

Use a struct for difficulty coeffs, so that we can impl Default on it.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 15 Aug 2019 21:32:54 +0200
parents e15672733c93
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
6 ## it under the terms of the GNU General Public License as published
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
7 ## by the Free Software Foundation; version 3 only.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
9 ## This program is distributed in the hope that it will be useful,
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
12 ## GNU General Public License for more details.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
14
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
15 from libc.stdlib cimport malloc, free
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 50
diff changeset
16
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
17
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
18 cdef class Interpolator:
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
19 def __init__(self, tuple values, unsigned long start_frame=0, tuple end_values=None,
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
20 unsigned long end_frame=0, formula=None):
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
21 self._length = len(values)
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
22 self._values = <double*>malloc(self._length * sizeof(double))
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
23 self.start_values = <double*>malloc(self._length * sizeof(double))
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
24 self.end_values = <double*>malloc(self._length * sizeof(double))
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
25 for i in range(self._length):
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
26 self._values[i] = values[i]
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
27 self.start_values[i] = self._values[i]
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
28 if end_values is not None:
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
29 for i in range(self._length):
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
30 self.end_values[i] = end_values[i]
122
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 62
diff changeset
31 self.start_frame = start_frame
174324a4da51 Add support for launch animations! (Warning: slow :()
Thibaut Girka <thib@sitedethib.com>
parents: 62
diff changeset
32 self.end_frame = end_frame
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
33 self._frame = 0
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
34 self._formula = formula
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
35
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
36
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
37 def __dealloc__(self):
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
38 free(self.end_values)
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
39 free(self.start_values)
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
40 free(self._values)
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
41
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
42
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
43 property values:
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
44 def __get__(self):
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
45 return tuple([self._values[i] for i in range(self._length)])
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
48 def __nonzero__(self):
60
af7914413b89 Enable interpolators to use different formulas
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
49 return self._frame < self.end_frame
57
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
50
694f25881d0f Fix move_to interpolation, add support for a few ANM and ECL instructions
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
51
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
52 cpdef set_interpolation_start(self, unsigned long frame, tuple values):
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
53 for i in range(self._length):
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
54 self.start_values[i] = values[i]
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 self.start_frame = frame
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
58 cpdef set_interpolation_end(self, unsigned long frame, tuple values):
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
59 for i in range(self._length):
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
60 self.end_values[i] = values[i]
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 self.end_frame = frame
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
64 cpdef set_interpolation_end_frame(self, unsigned long end_frame):
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 self.end_frame = end_frame
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
68 cpdef set_interpolation_end_values(self, tuple values):
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
69 for i in range(self._length):
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
70 self.end_values[i] = values[i]
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
73 cpdef update(self, unsigned long frame):
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
74 cdef double coeff
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
75
50
811cefefb5c8 Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
76 self._frame = frame
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
77 if frame + 1 >= self.end_frame: #XXX: skip the last interpolation step
62
1f591adcea04 Fix animation determination (ins_98 stuff) and some interpolation functions
Thibaut Girka <thib@sitedethib.com>
parents: 60
diff changeset
78 # This bug is replicated from the original game
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
79 for i in range(self._length):
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
80 self._values[i] = self.end_values[i]
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
81 self.start_values[i] = self.end_values[i]
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 self.start_frame = frame
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 else:
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
84 coeff = float(frame - self.start_frame) / float(self.end_frame - self.start_frame)
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
85 if self._formula is not None:
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
86 coeff = self._formula(coeff)
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 437
diff changeset
87 for i in range(self._length):
437
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
88 start_value = self.start_values[i]
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
89 end_value = self.end_values[i]
d778db08190f Make Interpolator an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 131
diff changeset
90 self._values[i] = start_value + coeff * (end_value - start_value)