2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/public-api/loader/bvh-loader.h>
26 #include <string_view>
36 static constexpr std::string_view TOKEN_OFFSET = "OFFSET";
37 static constexpr std::string_view TOKEN_CHANNELS = "CHANNELS";
38 static constexpr std::string_view TOKEN_XPOSITION = "Xposition";
39 static constexpr std::string_view TOKEN_YPOSITION = "Yposition";
40 static constexpr std::string_view TOKEN_ZPOSITION = "Zposition";
41 static constexpr std::string_view TOKEN_XROTATION = "Xrotation";
42 static constexpr std::string_view TOKEN_YROTATION = "Yrotation";
43 static constexpr std::string_view TOKEN_ZROTATION = "Zrotation";
44 static constexpr std::string_view TOKEN_JOINT = "JOINT";
45 static constexpr std::string_view TOKEN_END_SITE = "End Site";
46 static constexpr std::string_view TOKEN_FRAMES = "Frames";
47 static constexpr std::string_view TOKEN_FRAME_TIME = "Frame Time";
48 static constexpr std::string_view TOKEN_HIERARCHY = "HIERARCHY";
49 static constexpr std::string_view TOKEN_ROOT = "ROOT";
50 static constexpr std::string_view TOKEN_MOTION = "MOTION";
51 static constexpr std::string_view PROPERTY_NAME_POSITION = "position";
52 static constexpr std::string_view PROPERTY_NAME_ORIENTATION = "orientation";
53 static constexpr std::string_view TOKEN_CLOSING_BRACE = "}";
67 std::vector<float> values;
74 std::vector<Vector3> translations;
75 std::vector<Quaternion> rotations;
76 std::vector<Channel> channels;
77 std::vector<std::shared_ptr<Joint>> children;
80 void trim(std::string& s)
82 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
83 return !std::isspace(ch);
85 s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
86 return !std::isspace(ch);
91 void ParseHierarchy(std::ifstream& file, std::shared_ptr<Joint>& joint)
94 while(std::getline(file, line))
97 std::istringstream stream(line);
99 std::getline(stream, token, ' ');
101 if(token == TOKEN_OFFSET.data())
103 stream >> joint->offset.x >> joint->offset.y >> joint->offset.z;
105 else if(token == TOKEN_CHANNELS.data())
107 uint32_t channelCount = 0;
108 std::getline(stream, token, ' ');
109 channelCount = static_cast<uint32_t>(std::atoi(token.c_str()));
110 for(uint32_t i = 0; i < channelCount; ++i)
112 std::getline(stream, token, ' ');
114 if(token == TOKEN_XPOSITION.data())
116 joint->channels.push_back(Channel::XPOSITION);
118 else if(token == TOKEN_YPOSITION.data())
120 joint->channels.push_back(Channel::YPOSITION);
122 else if(token == TOKEN_ZPOSITION.data())
124 joint->channels.push_back(Channel::ZPOSITION);
126 else if(token == TOKEN_XROTATION.data())
128 joint->channels.push_back(Channel::XROTATION);
130 else if(token == TOKEN_YROTATION.data())
132 joint->channels.push_back(Channel::YROTATION);
134 else if(token == TOKEN_ZROTATION.data())
136 joint->channels.push_back(Channel::ZROTATION);
140 else if(token == TOKEN_JOINT.data())
142 std::shared_ptr<Joint> child(new Joint);
143 joint->children.push_back(child);
144 std::getline(stream, token, ' ');
146 ParseHierarchy(file, child);
148 else if(line == TOKEN_END_SITE.data())
150 while(std::getline(file, line))
153 if(line == TOKEN_CLOSING_BRACE.data())
159 else if(token == TOKEN_CLOSING_BRACE.data())
166 void MakeList(std::shared_ptr<Joint>& joint, std::vector<std::shared_ptr<Joint>>& jointList)
168 jointList.push_back(joint);
169 for(uint32_t i = 0; i < joint->children.size(); ++i)
171 MakeList(joint->children[i], jointList);
175 void ParseMotion(std::ifstream& file, std::shared_ptr<Joint>& hierarchy, uint32_t& frameCount, float& frameTime)
177 std::vector<std::shared_ptr<Joint>> jointList;
178 MakeList(hierarchy, jointList);
180 bool frameCountLoaded = false;
181 bool frameTimeLoaded = false;
183 while((!frameCountLoaded || !frameTimeLoaded) && std::getline(file, line))
186 std::istringstream stream(line);
188 std::getline(stream, token, ':');
190 if(token == TOKEN_FRAMES.data())
192 stream >> frameCount;
193 frameCountLoaded = true;
195 else if(token == TOKEN_FRAME_TIME.data())
198 frameTimeLoaded = true;
202 while(getline(file, line))
205 std::istringstream stream(line);
206 for(auto&& joint : jointList)
209 Quaternion rotation[3];
210 for(uint32_t i = 0; i < joint->channels.size(); ++i)
212 if(joint->channels[i] == Channel::XPOSITION)
214 stream >> translation.x;
216 else if(joint->channels[i] == Channel::YPOSITION)
218 stream >> translation.y;
220 else if(joint->channels[i] == Channel::ZPOSITION)
222 stream >> translation.z;
224 else if(joint->channels[i] == Channel::XROTATION)
228 rotation[0] = Quaternion(Radian(Degree(radian)), Vector3::XAXIS);
230 else if(joint->channels[i] == Channel::YROTATION)
234 rotation[1] = Quaternion(Radian(Degree(radian)), Vector3::YAXIS);
236 else if(joint->channels[i] == Channel::ZROTATION)
240 rotation[2] = Quaternion(Radian(Degree(radian)), Vector3::ZAXIS);
243 joint->translations.push_back(translation);
244 joint->rotations.push_back(rotation[2] * rotation[0] * rotation[1]);
249 bool ParseBvh(const std::string& path, uint32_t& frameCount, float& frameTime, std::shared_ptr<Joint>& rootJoint)
251 std::ifstream file(path.data());
258 while(getline(file, line))
261 std::istringstream stream(line);
263 std::getline(stream, token, ' ');
264 if(token == TOKEN_HIERARCHY.data())
267 while(getline(file, line))
270 std::istringstream stream(line);
272 std::getline(stream, token, ' ');
273 if(token == TOKEN_ROOT.data())
275 std::getline(stream, token, ' ');
276 rootJoint->name = token;
277 ParseHierarchy(file, rootJoint);
282 if(token == TOKEN_MOTION.data())
284 ParseMotion(file, rootJoint, frameCount, frameTime);
290 AnimationDefinition GenerateAnimation(const std::string& animationName, std::shared_ptr<Joint>& hierarchy, uint32_t frameCount, float frameTime, const Vector3& scale)
292 AnimationDefinition animationDefinition;
294 animationDefinition.mName = animationName;
295 animationDefinition.mDuration = frameTime * (frameCount - 1);
296 float keyFrameInterval = (frameCount > 1u) ? 1.0f / static_cast<float>(frameCount - 1u) : Dali::Math::MACHINE_EPSILON_10;
298 std::vector<std::shared_ptr<Joint>> jointList;
299 MakeList(hierarchy, jointList);
301 if(!jointList.empty())
303 animationDefinition.mProperties.resize(jointList.size() * 2u); // translation and rotation
304 for(uint32_t i = 0; i < jointList.size(); ++i)
306 AnimatedProperty& translationProperty = animationDefinition.mProperties[i * 2u];
307 translationProperty.mTimePeriod = Dali::TimePeriod(animationDefinition.mDuration);
308 translationProperty.mNodeName = jointList[i]->name;
309 translationProperty.mPropertyName = PROPERTY_NAME_POSITION.data();
311 AnimatedProperty& rotationProperty = animationDefinition.mProperties[i * 2u + 1];
312 rotationProperty.mTimePeriod = Dali::TimePeriod(animationDefinition.mDuration);
313 rotationProperty.mNodeName = jointList[i]->name;
314 rotationProperty.mPropertyName = PROPERTY_NAME_ORIENTATION.data();
316 translationProperty.mKeyFrames = Dali::KeyFrames::New();
317 rotationProperty.mKeyFrames = Dali::KeyFrames::New();
318 for(uint32_t j = 0; j < frameCount; ++j)
320 translationProperty.mKeyFrames.Add(static_cast<float>(j) * keyFrameInterval, (jointList[i]->translations[j] / scale));
321 rotationProperty.mKeyFrames.Add(static_cast<float>(j) * keyFrameInterval, jointList[i]->rotations[j]);
326 return animationDefinition;
330 AnimationDefinition LoadBvh(const std::string& path, const std::string& animationName, const Vector3& scale)
332 uint32_t frameCount = 0;
333 float frameTime = 0.0f;
334 std::shared_ptr<Joint> rootJoint(new Joint);
335 if(!ParseBvh(path, frameCount, frameTime, rootJoint))
337 AnimationDefinition animationDefinition;
338 return animationDefinition;
341 return GenerateAnimation(animationName, rootJoint, frameCount, frameTime, scale);
343 } // namespace Loader
344 } // namespace Scene3D