[Tizen] Add MotionData class and generate Animation by this
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / model-motion / motion-data-load-task.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/model-motion/motion-data-load-task.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-scene3d/public-api/loader/bvh-loader.h>
26 #include <dali-scene3d/public-api/loader/facial-animation-loader.h>
27
28 namespace Dali
29 {
30 namespace Scene3D
31 {
32 namespace Internal
33 {
34 MotionDataLoadTask::MotionDataLoadTask(const std::string& path, const Vector3& scale, CallbackBase* callback)
35 : AsyncTask(callback),
36   mFileUrl(path),
37   mRawBuffer(nullptr),
38   mRawBufferLength(0),
39   mScale(scale),
40   mAnimationDefinition{},
41   mLoadMethod(MotionDataLoadTask::LoadMethod::BVH_FILE)
42 {
43 }
44
45 MotionDataLoadTask::MotionDataLoadTask(const uint8_t* rawBuffer, int rawBufferLength, const Vector3& scale, CallbackBase* callback)
46 : AsyncTask(callback),
47   mFileUrl(),
48   mRawBuffer(nullptr),
49   mRawBufferLength(rawBufferLength),
50   mScale(scale),
51   mAnimationDefinition{},
52   mLoadMethod(MotionDataLoadTask::LoadMethod::BVH_BUFFER)
53 {
54   // To keep life of raw buffer, let we copy data now.
55   mRawBuffer = new uint8_t[mRawBufferLength];
56   memcpy(mRawBuffer, rawBuffer, sizeof(uint8_t) * mRawBufferLength);
57 }
58
59 MotionDataLoadTask::MotionDataLoadTask(const std::string& url, CallbackBase* callback)
60 : AsyncTask(callback),
61   mFileUrl(url),
62   mRawBuffer(nullptr),
63   mRawBufferLength(0),
64   mScale(),
65   mAnimationDefinition{},
66   mLoadMethod(MotionDataLoadTask::LoadMethod::FACIAL_FILE)
67 {
68 }
69
70 MotionDataLoadTask::MotionDataLoadTask(const uint8_t* rawBuffer, int rawBufferLength, CallbackBase* callback)
71 : AsyncTask(callback),
72   mFileUrl(),
73   mRawBuffer(nullptr),
74   mRawBufferLength(rawBufferLength),
75   mScale(),
76   mAnimationDefinition{},
77   mLoadMethod(MotionDataLoadTask::LoadMethod::FACIAL_BUFFER)
78 {
79   // To keep life of raw buffer, let we copy data now.
80   mRawBuffer = new uint8_t[mRawBufferLength];
81   memcpy(mRawBuffer, rawBuffer, sizeof(uint8_t) * mRawBufferLength);
82 }
83
84 MotionDataLoadTask::~MotionDataLoadTask()
85 {
86   if(mRawBuffer)
87   {
88     delete[] mRawBuffer;
89   }
90 }
91
92 void MotionDataLoadTask::Process()
93 {
94   switch(mLoadMethod)
95   {
96     case LoadMethod::BVH_FILE:
97     {
98       mAnimationDefinition = std::move(Loader::LoadBvh(mFileUrl, "LoadedBvhMotionData", mScale));
99       break;
100     }
101     case LoadMethod::BVH_BUFFER:
102     {
103       mAnimationDefinition = std::move(Loader::LoadBvhFromBuffer(mRawBuffer, mRawBufferLength, "LoadedBvhMotionData", mScale));
104       break;
105     }
106     case LoadMethod::FACIAL_FILE:
107     {
108       mAnimationDefinition = std::move(Loader::LoadFacialAnimation(mFileUrl));
109       break;
110     }
111     case LoadMethod::FACIAL_BUFFER:
112     {
113       mAnimationDefinition = std::move(Loader::LoadFacialAnimationFromBuffer(mRawBuffer, mRawBufferLength));
114       break;
115     }
116   }
117 }
118
119 bool MotionDataLoadTask::IsReady()
120 {
121   return true;
122 }
123
124 const Scene3D::Loader::AnimationDefinition& MotionDataLoadTask::GetAnimationDefinition() const
125 {
126   return mAnimationDefinition;
127 }
128
129 } // namespace Internal
130
131 } // namespace Scene3D
132
133 } // namespace Dali