Normand Briere
2018-07-01 655810d1c4e710e7c85772b8dde96772dbcf274b
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
package mocap.player;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.vecmath.Point3d;
 
import mocap.figure.AnimData;
import mocap.figure.Bone;
 
/**
 * Plays a mocap animation, the "motor" that drives every skeleton.
 * Needs the skeleton and the animation data (usually mocap data).
 * The method update(fps) must be called periodically to make animation run.
 * 
 * @author Michael Kipp
 */
public class MocapPlayer extends AnimPlayer
{
 
    private static final float DEFAULT_FPS = 90;
    private boolean _isPlaying = false;
    private Bone _root;
    private Bone[] _bones; // skeleton
    private double _time = 0;
    private float _fps;  // playback fps
    private int _frame = -1;  // current frame
    private AnimData _animData;
    private Point3d _offset;
    private boolean _pinAtRoot = false;
 
    public MocapPlayer(Bone skeleton, AnimData dat, Point3d offset)
    {
        _fps = DEFAULT_FPS;
        _animData = dat;
        _offset = offset;
        _root = skeleton;
        initSkeleton(skeleton);
    }
 
    private void initSkeleton(Bone skeleton)
    {
        List<Bone> ls = new ArrayList<Bone>();
        skeleton.collectBones(ls);
        _bones = new Bone[ls.size()];
        _bones = (Bone[]) ls.toArray(_bones);
    }
 
    private int mapTimeToFrame(double time)
    {
        return (int) Math.max(time * _fps, 0);
    }
 
    public Bone[] getBones()
    {
        return _bones;
    }
 
    /**
     * When set, figure's root will not move at all, the figure is "pinned"
     * at its root joint (usually the hip).
     * 
     * @param val whether to pin or not
     */
    public void setPinAtRoot(boolean val) {
        _pinAtRoot = val;
    }
 
    public void gotoTime(double sec)
    {
        int f = mapTimeToFrame(sec);
        gotoFrame(f < _animData.getNumFrames() ? f : _animData.getNumFrames() - 1);
    }
 
    private void gotoFrame(int frame)
    {
        if (_animData != null) {
            for (int i = 0; i < _bones.length; i++) {
                Bone bone = _bones[i];
 
                if (bone != _root || !_pinAtRoot) {
                    bone.setPose(frame, _animData.getBoneData(bone.getIndex()), _offset);
                }
            }
            _frame = frame;
            for (PlayerFrameListener li : _listeners) {
                li.frameUpdate(frame);
            }
        }
    }
 
    public int getNumFrames()
    {
        return _animData.getNumFrames();
    }
 
    public int getCurrentFrame()
    {
        return _frame;
    }
 
    /**
     * Intended playback speed of the motion capture fragment.
     * @param fps
     */
    public void setPlaybackFps(float fps)
    {
        _fps = fps;
        _time = _frame / _fps;
    }
 
    public float getPlaybackFps()
    {
        return _fps;
    }
 
    public void setIsPlaying(boolean val)
    {
        if (val && _animData != null) {
            _isPlaying = true;
        } else {
            _isPlaying = false;
        }
    }
 
    public boolean isPlaying()
    {
        return _isPlaying;
    }
 
    public void reset()
    {
        _isPlaying = false;
        _time = 0;
        _frame = -1;
        gotoFrame(0);
    }
 
    public void frameForward()
    {
        _frame++;
        if (_frame >= _animData.getNumFrames()) {
            _frame = 0;
        }
        _time = _frame / _fps;
        gotoFrame(_frame);
    }
 
    public void frameBackward()
    {
        _frame--;
        if (_frame < 0) {
            _frame = _animData.getNumFrames() - 1;
        }
        _time = _frame / _fps;
        gotoFrame(_frame);
    }
 
    /**
     * Converts the system's fps to the intended playback fps und changes
     * animation frames accordingly. Usually system speed is higher (and
     * is also an upper limit to the playback speed).
     * 
     * @param fps The fps speed of the system
     */
    @Override
    public void update(float fps)
    {
        if (_isPlaying) {
            _time += 1 / fps;  // time in seconds since setIsPlaying
            int frame = (int) (_time * _fps); // frame in animation
            if (frame >= _animData.getNumFrames()) {
                _time = 0;
                _frame = -1;
            } else if (frame > _frame) {
                gotoFrame(frame);
                _frame = frame;
            }
        }
//        return _frame;
    }
}