-
Notifications
You must be signed in to change notification settings - Fork 1
/
AMCParser.cpp
271 lines (231 loc) · 6.96 KB
/
AMCParser.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "AMCParser.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <glm/common.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace MotionParser;
AMCParser::AMCParser()
{
}
AMCParser::~AMCParser()
{
}
void AMCParser::ReadAMC(std::string path)
{
path_ = path;
ReadFileContent();
Parse_Header();
Parse_Frames();
}
void AMCParser::SetCharacterKeyFrames(Character* character)
{
if(character == nullptr)
return;
// check mapping keys all exist in character
//std::cout << "Set Keyframes " << raw_name_dof_map_.size() << "\n";
auto character_map = character->GetBoneNameMap();
bool status = true;
for(auto it = raw_name_dof_map_.cbegin();
it != raw_name_dof_map_.cend();
++it)
{
//std::cout << it->first << std::endl;
if(it->first == "root")
continue;
auto find_it = character_map.find(it->first);
status &= (find_it != character_map.end());
}
// stop if not match
assert(status);
// If match then compute quaternion
for(auto it = raw_name_dof_map_.cbegin();
it != raw_name_dof_map_.cend();
++it)
{
//std::cout << it->first << std::endl;
if(it->first == "root")
{
SetKeyframeStates(it->first, DOF_ENUM::DOF_NONE);
continue;
}
auto target = character_map.find(it->first);
SetKeyframeStates(it->first, target->second->data->dof_type);
}
character->SetKeyframes(bone_keyframes_map_);
}
bool AMCParser::ReadFileContent()
{
std::cout << "Read AMC File Content..." << std::endl;
std::ifstream file(path_.c_str(), std::ios::in);
if(!file || !file.is_open())
{
std::cout << "File Open Failed\n" << std::endl;
return false;
}
// Access whole file content
std::stringstream buffer;
buffer << file.rdbuf();
file_content_ = buffer.str();
file.close();
std::cout << "AMC File Load Success!\n";
return true;
}
void AMCParser::Parse_Header()
{
char buf[128];
ss_ << file_content_;
while(ss_.getline(buf,128))
{
if(buf[0] == '#' || buf[0] == ':')
continue;
else
break;
}
}
void AMCParser::Parse_Frames()
{
std::stringstream line_ss;
char line_str[128];
while(ss_.getline(line_str, 128))
{
int frame_id;
std::string keyword;
line_ss.clear();
line_ss.str(line_str);
line_ss >> frame_id;
if(line_ss.fail())
{
line_ss.clear();
line_ss.str(line_str);
float angle;
std::vector<float> angles_vec;
line_ss >> keyword;
auto iter = raw_name_dof_map_.find(keyword);
bool find_key = (iter != raw_name_dof_map_.end());
while(line_ss >> angle)
{
angles_vec.push_back(angle);
}
if(find_key)
{
// add to second
iter->second.push_back(std::move(angles_vec));
}
else
{
std::vector<std::vector<float>> frames_vec;
frames_vec.push_back(std::move(angles_vec));
// create new one using emplace()
raw_name_dof_map_.emplace(keyword, std::move(frames_vec));
}
}
}
}
void AMCParser::SetKeyframeStates(const std::string& key, DOF_ENUM dof_type)
{
const auto& frames = raw_name_dof_map_[key];
if(key == "root")
{
glm::vec3 trans(0,0,0);
glm::vec3 euler_rotation(0,0,0);
//auto root_frames = raw_name_dof_map_["root"];
for(size_t i =0 ; i< frames.size(); ++i)
{
const std::vector<float>& frame_info = frames[i];
trans = glm::vec3(frame_info[0],
frame_info[1],
frame_info[2]);
euler_rotation = glm::vec3(frame_info[3],
frame_info[4],
frame_info[5]);
/*
std::cout << "root (" << i << ")"
<< trans.x << " "
<< trans.y << " "
<< trans.z << " "
<< euler_rotation.x << " "
<< euler_rotation.y << " "
<< euler_rotation.z << "\n";
*/
glm::quat rot(euler_rotation);
KeyframeState* frame_state = new KeyframeState(trans, rot);
bone_keyframes_map_[key].push_back(frame_state);
}
return;
}
for(size_t i =0 ; i< frames.size(); ++i)
{
//std::cout << key << " (" << i << ")";
SetKeyframeStateInfo(key, dof_type, i);
}
}
void AMCParser::SetKeyframeStateInfo(const std::string& key,
DOF_ENUM dof_type,
size_t idx)
{
glm::vec3 trans(0,0,0);
glm::vec3 euler_rotation(0,0,0);
const auto& frames = raw_name_dof_map_[key];
const std::vector<float>& frame_info = frames[idx];
switch(dof_type)
{
case DOF_ENUM::DOF_NONE:
{
break;
}
case DOF_ENUM::DOF_RX:
{
euler_rotation = glm::vec3(frame_info[0], 0, 0);
break;
}
case DOF_ENUM::DOF_RY:
{
euler_rotation = glm::vec3(0, frame_info[0], 0);
break;
}
case DOF_ENUM::DOF_RZ:
{
euler_rotation = glm::vec3(0, 0, frame_info[0]);
break;
}
case DOF_ENUM::DOF_RX_RY:
{
euler_rotation = glm::vec3(frame_info[0],
frame_info[1],
0);
break;
}
case DOF_ENUM::DOF_RY_RZ:
{
euler_rotation = glm::vec3(0,
frame_info[0],
frame_info[1]);
break;
}
case DOF_ENUM::DOF_RX_RZ:
{
euler_rotation = glm::vec3(frame_info[0],
0,
frame_info[1]);
break;
}
case DOF_ENUM::DOF_RX_RY_RZ:
{
euler_rotation = glm::vec3(frame_info[0],
frame_info[1],
frame_info[2]);
break;
}
}
glm::quat rot(euler_rotation);
//std::cout << key << " " << dof_type << "\n";
/*
std::cout << euler_rotation.x << " "
<< euler_rotation.y << " "
<< euler_rotation.z << "\n";
*/
KeyframeState* frame_state = new KeyframeState(trans, rot);
bone_keyframes_map_[key].push_back(frame_state);
}