Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/kinematic.py
blob: 12249d31f851f76753bdb2bcb07f42305a488087 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import math

import model
import theme

class Frame:
    def __init__(self, keyframe=None):
        if keyframe:
            self.assign(keyframe)
        else:
            self.joints = None
            self.parts = None
            self.middle = None
            self.hsize = None

    def assign(self, keyframe):
        self.joints = keyframe.joints.copy()
        self.parts = keyframe.parts.copy()
        self.middle = keyframe.middle
        self.hsize = keyframe.sticks['HEAD'][1]

def makeframes():
    frames = {}

    for i in model.keys:
        if not i.empty():
            frames[i.x] = Frame(i)

    if not frames:
        return {}

    fsecs = frames.keys()
    fsecs.sort()

    # set border frames
    frames[0] = frames[fsecs[0]]
    frames[theme.KEYFRAMEWIDTH] = frames[fsecs[-1]]

    # now fill in frames between
    fsecs = frames.keys()
    fsecs.sort()

    # frame interval
    fint = int(theme.KEYFRAMEWIDTH/float(theme.TOTALFRAMES)) 

    for i in range(len(fsecs)):
        if i == len(fsecs)-1:
            continue # nothing after end

        startsecs = fsecs[i]
        endsecs = fsecs[i+1]
        start_frame = frames[startsecs]
        end_frame = frames[endsecs]
        numframes = int((endsecs-startsecs)/float(fint))-1

        for j in range(numframes-1): # MAYBE SHOULD BE numframes
            frame = frames[startsecs + ((j+1)*fint)] = Frame()

            frame.joints = _intjoints(start_frame.joints, end_frame.joints,
                    j+1, numframes)
            frame.parts = _intparts(start_frame.parts, end_frame.parts,
                    j+1, numframes)
            frame.middle = _intmiddle(start_frame.middle, end_frame.middle,
                    j+1, numframes)
            frame.hsize = _inthsize(start_frame.hsize, end_frame.hsize,
                    j+1, numframes)

    return frames

def getparentjoint(jname, joints, middle):
    if jname in ['rightshoulder','leftshoulder','groin','neck']:
        return middle

    parentjoints = {'rightelbow':'rightshoulder',
                    'righthand':'rightelbow',
                    'leftelbow':'leftshoulder',
                    'lefthand':'leftelbow',
                    'righthip':'groin',
                    'rightknee':'righthip',
                    'rightheel':'rightknee',
                    'righttoe':'rightheel',
                    'lefthip':'groin',
                    'leftknee':'lefthip',
                    'leftheel':'leftknee',
                    'lefttoe':'leftheel',
                    'head':'neck'}

    return joints[parentjoints[jname]]

def getparentsticks(stickname):
    if stickname in ['RIGHT SHOULDER','LEFT SHOULDER','NECK','TORSO']:
        return []
    if stickname in ['HEAD']:
        return ['NECK']
    if stickname == 'UPPER RIGHT ARM':
        return ['RIGHT SHOULDER']
    if stickname == 'LOWER RIGHT ARM':
        return ['UPPER RIGHT ARM','RIGHT SHOULDER']
    if stickname == 'UPPER LEFT ARM':
        return ['LEFT SHOULDER']
    if stickname == 'LOWER LEFT ARM':
        return ['UPPER LEFT ARM','LEFT SHOULDER']
    if stickname == 'RIGHT HIP':
        return ['TORSO']
    if stickname == 'UPPER RIGHT LEG':
        return ['RIGHT HIP','TORSO']
    if stickname == 'LOWER RIGHT LEG':
        return ['UPPER RIGHT LEG','RIGHT HIP','TORSO']
    if stickname == 'RIGHT FOOT':
        return ['LOWER RIGHT LEG','UPPER RIGHT LEG','RIGHT HIP','TORSO']
    if stickname == 'LEFT HIP':
        return ['TORSO']
    if stickname == 'UPPER LEFT LEG':
        return ['LEFT HIP','TORSO']
    if stickname == 'LOWER LEFT LEG':
        return ['UPPER LEFT LEG','LEFT HIP','TORSO']
    if stickname == 'LEFT FOOT':
        return ['LOWER LEFT LEG','UPPER LEFT LEG','LEFT HIP','TORSO']

def getpoints(x,y,angle,len):
    nx = int(round(x + (len * math.cos(math.radians(angle)))))
    ny = int(round(y - (len * math.sin(math.radians(angle)))))
    return (nx,ny)

def _interpolate(x,x0,y0,x1,y1):
    if x1-x0 == 0:
        return y0
    m = float(y1-y0)/float(x1-x0)
    y = y0 + ((x-x0)*m)
    return y

def _intjoints(sjoints, ejoints, count, numpoints):
    # numpoints: number of points between start and end
    # count: point were getting now
    ijoints = {}
    for jname in sjoints:
        (x0,y0) = sjoints[jname]
        (x1,y1) = ejoints[jname]
        #print 'x0:%s,y0:%s' % (x0,y0)
        #print 'x1:%s,y1:%s' % (x1,y1)
        x = x0 + (count * ((x1-x0)/float(numpoints)))
        y = _interpolate(x,x0,y0,x1,y1)
        ijoints[jname] = (int(x),int(y))
    return ijoints

def _intparts(sparts,eparts,count,numpoints):
    iparts = {}
    for pname in sparts:
        x0 = sparts[pname]
        x1 = eparts[pname]
        if x0 == x1:
            iparts[pname] = x0
            continue
        x = x0 + (count * ((x1-x0)/float(numpoints)))
        iparts[pname] = int(x)
    return iparts

def _intmiddle(smiddle,emiddle,count,numpoints):
    (x0,y0) = smiddle
    (x1,y1) = emiddle
    x = x0 + (count * ((x1-x0)/float(numpoints)))
    y = _interpolate(x,x0,y0,x1,y1)
    return (int(x),int(y))

def _inthsize(shsize,ehsize,count,numpoints):
    x0 = shsize
    x1 = ehsize
    if x0 == x1:
        return x0
    x = x0 + (count * ((x1-x0)/float(numpoints)))
    return int(x)


"""
    def makeframes(self):
        endsecs = KEYFRAMEWIDTH
        fint = int(endsecs/float(TOTALFRAMES)) # frame interval
        self.frames = {}
        self.fparts = {}
        self.fmiddles = {}
        self.fhsize = {}
        for i in range(len(self.keyframes)):
            secs = self.keyframes[i]
            if model.keys[i].joints:
                self.frames[secs] = model.keys[i].joints.copy()
                self.fparts[secs] = model.keys[i].parts.copy()
                self.fmiddles[secs] = model.keys[i].middle
                #print '%s:KFMIDDLE:%s = (%s,%s)' % (i,secs,self.fmiddles[secs][0],self.fmiddles[secs][1])
                self.fhsize[secs] = model.keys[i].sticks['HEAD'][1]
        fsecs = self.frames.keys()
        fsecs.sort()
        if not fsecs:
            return
        # ADD frame at 0
        self.frames[0] = self.frames[fsecs[0]].copy()
        self.fparts[0] = self.fparts[fsecs[0]].copy()
        self.fmiddles[0] = self.fmiddles[fsecs[0]]
        self.fhsize[0] = self.fhsize[fsecs[0]]
        # ADD frame at end
        self.frames[endsecs] = self.frames[fsecs[-1]].copy()
        self.fparts[endsecs] = self.fparts[fsecs[-1]].copy()
        self.fmiddles[endsecs] = self.fmiddles[fsecs[-1]]
        self.fhsize[endsecs] = self.fhsize[fsecs[-1]]
        # now fill in frames between
        fsecs = self.frames.keys()
        fsecs.sort()
        for i in range(len(fsecs)):
            if i == len(fsecs)-1:
               continue # nothing after end
            startsecs = fsecs[i]
            endsecs = fsecs[i+1]
            numframes = int((endsecs-startsecs)/float(fint))-1
            #print 'NUMFRAMES(%s):%s' % (i,numframes)
            for j in range(numframes-1): # MAYBE SHOULD BE numframes
                secs = startsecs + ((j+1)*fint)
                self.frames[secs] = self.intjoints(self.frames[startsecs],self.frames[endsecs],
                                                   j+1,numframes)
                self.fparts[secs] = self.intparts(self.fparts[startsecs],self.fparts[endsecs],
                                                  j+1,numframes)
                self.fmiddles[secs] = self.intmiddle(self.fmiddles[startsecs],self.fmiddles[endsecs],
                                                     j+1,numframes)
                self.fhsize[secs] = self.inthsize(self.fhsize[startsecs],self.fhsize[endsecs],
                                                  j+1,numframes)
                #print '%s,%s(%s secs):(%s,%s) START(%s,%s) - END(%s,%s) startsecs:%s endsecs:%s numframes:%s' % (i,j,secs,self.fmiddles[secs][0],self.fmiddles[secs][1],self.fmiddles[startsecs][0],self.fmiddles[startsecs][1],self.fmiddles[endsecs][0],self.fmiddles[endsecs][1],startsecs,endsecs,numframes)
        #print self.frames.keys()

"""