aboutsummaryrefslogtreecommitdiffstats
path: root/dev/a4-dance/motion_clip.cc
blob: f239aec2db3c5600946a17ea435f7278cc026366 (plain) (blame)
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
#include "motion_clip.h"

#include <fstream>

#include "amc_util.h"
#include "simple_parser.h"

MotionClip::MotionClip() {
}

MotionClip::~MotionClip() {
}

int MotionClip::size() const {
    return (int)frames_.size();
}

Pose MotionClip::pose(int frame_num) const {
    return frames_[frame_num];
}

Pose& MotionClip::pose(int frame_num) {
    return frames_[frame_num];
}

void MotionClip::TrimFront(int num_frames) {
    frames_.erase(frames_.begin(), frames_.begin() + num_frames);
}

void MotionClip::TrimBack(int num_frames) {
    frames_.erase(frames_.end()-num_frames-1, frames_.end()-1);
}

void MotionClip::PrependPose(const Pose &pose) {
    frames_.insert(frames_.begin(), pose);
}

void MotionClip::AppendPose(const Pose &pose) {
    frames_.push_back(pose);
}


void MotionClip::PrependClip(const MotionClip &clip, int num_blend_frames) {
    for (int i=0; i < clip.size()-num_blend_frames; i++) {
        frames_.insert(frames_.begin(), clip[i]);
    }
    for (int i=0; i<num_blend_frames; i++) {
        int clip_index = clip.size() - num_blend_frames + i;
        int orig_index = clip_index;
        float alpha = (float)i/(float)(num_blend_frames-1);  // 0.0 to 1.0
        Pose blended = clip[clip_index].Lerp(frames_[orig_index], alpha);
        frames_[orig_index] = blended;
    }
}

void MotionClip::AppendClip(const MotionClip &clip, int num_blend_frames) {
    for (int i=0; i<num_blend_frames; i++) {
        int clip_index = i;
        int orig_index = (int)frames_.size() - num_blend_frames + i;
        float alpha = (float)i/(float)(num_blend_frames-1);  // 0.0 to 1.0
        Pose blended = frames_[orig_index].Lerp(clip[clip_index], alpha);
        frames_[orig_index] = blended;
    }
    for (int i=num_blend_frames; i < clip.size(); i++) {
        frames_.push_back(clip[i]);
    }
}


void MotionClip::MakeLoop(int num_blend_frames) {
    MotionClip tmp_clip;
    for (int i=0; i<num_blend_frames; i++) {
        tmp_clip.PrependPose(frames_.back());
        frames_.pop_back();
    }
    for (int i=0; i<tmp_clip.size(); i++) {
        float alpha = (float)i/(float)(tmp_clip.size()-1);
        frames_[i] = tmp_clip[i].Lerp(frames_[i], alpha);
    }
}

void MotionClip::CalcRelativeTranslations() {
    Point3 prev_pos = frames_[0].root_position();
    for (int i=0; i<frames_.size(); i++) {
        Point3 cur_pos = frames_[i].root_position();
        frames_[i].set_root_relative_translation(cur_pos - prev_pos);
        prev_pos = cur_pos;
    }
}


Pose MotionClip::operator[](const int frame_num) const {
    return frames_[frame_num];
}

Pose& MotionClip::operator[](const int frame_num) {
    return frames_[frame_num];
}


void MotionClip::LoadFromAMC(const std::string &filename, const Skeleton &skeleton) {
    std::ifstream ifs(filename.c_str());
    SimpleParser parser(&ifs);
    
    parser.SwallowLine();
    parser.SwallowLine();
    parser.SwallowLine();
    
    while (parser.Good()) {
        Pose frame;
        int frame_num;
        parser.ReadInt(frame_num);
        if (!parser.Good()) {
            return;
        }
        frame.set_frame_number(frame_num);
        
        while (!parser.UpcomingInt()) {
            std::string bone;
            parser.ReadToken(bone);
            if (!parser.Good()) {
                return;
            }
            if (bone == "root") {
                Point3 position;
                parser.ReadFloat(position[0]);
                parser.ReadFloat(position[1]);
                parser.ReadFloat(position[2]);
                position = amc2meter(position);
                frame.set_root_position(position);
                
                Vector3 orientation;
                parser.ReadFloat(orientation[0]);
                parser.ReadFloat(orientation[1]);
                parser.ReadFloat(orientation[2]);
                frame.set_root_orientation(orientation);                
            }
            else {
                float rx=0, ry=0, rz=0;
                
                if (skeleton.rx_dof(bone)) {
                    parser.ReadFloat(rx);
                }
                if (skeleton.ry_dof(bone)) {
                    parser.ReadFloat(ry);
                }
                if (skeleton.rz_dof(bone)) {
                    parser.ReadFloat(rz);
                }
                frame.set_joint_angles(bone, Vector3(rx, ry, rz));
            }
        }
        frames_.push_back(frame);
    }
}