[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-FacialAnimation.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 #include <dali-scene3d/public-api/loader/animation-definition.h>
19 #include <dali-scene3d/public-api/loader/facial-animation-loader.h>
20 #include <dali-test-suite-utils.h>
21
22 #include <fstream>
23
24 using namespace Dali;
25 using namespace Dali::Scene3D::Loader;
26
27 namespace
28 {
29 std::string ReadBufferFromFile(const std::string& url)
30 {
31   std::string  rawString;
32   std::fstream fileStream;
33
34   fileStream.open(url, std::ios::in | std::ios::binary);
35   if(!fileStream.is_open())
36   {
37     DALI_LOG_WARNING("stream open failed for: \"%s\", in mode: \"%d\".\n", url.c_str(), static_cast<int>(std::ios::in | std::ios::binary));
38   }
39
40   // get length of file:
41   fileStream.seekg(0, std::ios::end);
42   auto length = fileStream.tellg();
43   fileStream.seekg(0, std::ios::beg);
44
45   rawString.resize(length);
46   fileStream.read(rawString.data(), length);
47
48   fileStream.close();
49
50   return rawString;
51 }
52 } // namespace
53
54 int UtcDaliLoadFacialAnimation(void)
55 {
56   TestApplication app;
57
58   for(uint32_t tc = 0; tc < 2; ++tc)
59   {
60     tet_printf("UtcDaliLoadFacialAnimation testcase %u\n", tc);
61     AnimationDefinition animDef;
62     switch(tc)
63     {
64       case 0: // Load bvh from url
65       {
66         animDef = LoadFacialAnimation(TEST_RESOURCE_DIR "/facial-blendshape-animation.json");
67         break;
68       }
69       case 1: // Load bvh from buffer stream.
70       {
71         std::string rawString = ReadBufferFromFile(TEST_RESOURCE_DIR "/facial-blendshape-animation.json");
72         animDef               = LoadFacialAnimationFromBuffer(reinterpret_cast<uint8_t*>(rawString.data()), static_cast<int>(rawString.length()));
73         break;
74       }
75     }
76
77     std::string name = animDef.GetName();
78     DALI_TEST_EQUAL(name, "Facial_Blendshape_Animation");
79     DALI_TEST_EQUAL(animDef.GetDuration(), 14.966001f);
80     DALI_TEST_EQUAL(animDef.GetEndAction(), Animation::BAKE);
81     DALI_TEST_EQUAL(animDef.GetSpeedFactor(), 1.0f);
82     DALI_TEST_EQUAL(animDef.GetLoopCount(), 1);
83     DALI_TEST_EQUAL(animDef.GetPropertyCount(), 122);
84
85     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mNodeName, "GEO_1");
86     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mPropertyName, "uBlendShapeWeight[0]");
87     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mKeyFrames.GetType(), Property::Type::FLOAT);
88     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mTimePeriod.delaySeconds, 0.0f);
89     DALI_TEST_EQUAL(animDef.GetPropertyAt(0).mTimePeriod.durationSeconds, 14.966001f);
90
91     DALI_TEST_EQUAL(animDef.GetPropertyAt(69).mNodeName, "GEO_2");
92     DALI_TEST_EQUAL(animDef.GetPropertyAt(69).mPropertyName, "uBlendShapeWeight[1]");
93     DALI_TEST_EQUAL(animDef.GetPropertyAt(69).mKeyFrames.GetType(), Property::Type::FLOAT);
94     DALI_TEST_EQUAL(animDef.GetPropertyAt(69).mTimePeriod.delaySeconds, 0.0f);
95     DALI_TEST_EQUAL(animDef.GetPropertyAt(69).mTimePeriod.durationSeconds, 14.966001f);
96
97     DALI_TEST_EQUAL(animDef.GetPropertyAt(86).mNodeName, "GEO_3");
98     DALI_TEST_EQUAL(animDef.GetPropertyAt(86).mPropertyName, "uBlendShapeWeight[2]");
99     DALI_TEST_EQUAL(animDef.GetPropertyAt(86).mKeyFrames.GetType(), Property::Type::FLOAT);
100     DALI_TEST_EQUAL(animDef.GetPropertyAt(86).mTimePeriod.delaySeconds, 0.0f);
101     DALI_TEST_EQUAL(animDef.GetPropertyAt(86).mTimePeriod.durationSeconds, 14.966001f);
102
103     DALI_TEST_EQUAL(animDef.GetPropertyAt(100).mNodeName, "GEO_4");
104     DALI_TEST_EQUAL(animDef.GetPropertyAt(100).mPropertyName, "uBlendShapeWeight[7]");
105     DALI_TEST_EQUAL(animDef.GetPropertyAt(100).mKeyFrames.GetType(), Property::Type::FLOAT);
106     DALI_TEST_EQUAL(animDef.GetPropertyAt(100).mTimePeriod.delaySeconds, 0.0f);
107     DALI_TEST_EQUAL(animDef.GetPropertyAt(100).mTimePeriod.durationSeconds, 14.966001f);
108
109     DALI_TEST_EQUAL(animDef.GetPropertyAt(121).mNodeName, "GEO_5");
110     DALI_TEST_EQUAL(animDef.GetPropertyAt(121).mPropertyName, "uBlendShapeWeight[19]");
111     DALI_TEST_EQUAL(animDef.GetPropertyAt(121).mKeyFrames.GetType(), Property::Type::FLOAT);
112     DALI_TEST_EQUAL(animDef.GetPropertyAt(121).mTimePeriod.delaySeconds, 0.0f);
113     DALI_TEST_EQUAL(animDef.GetPropertyAt(121).mTimePeriod.durationSeconds, 14.966001f);
114
115     auto actor = Actor::New();
116     actor.SetProperty(Actor::Property::NAME, "GEO_1");
117
118     char        weightNameBuffer[32];
119     char* const pWeightName = weightNameBuffer + snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s", "uBlendShapeWeight");
120     for(int i = 0; i < 122; i++)
121     {
122       snprintf(pWeightName, sizeof(weightNameBuffer) - (pWeightName - weightNameBuffer), "[%d]", i);
123       std::string weightName{weightNameBuffer};
124       actor.RegisterProperty(weightName, 0.0f);
125     }
126
127     auto getActor = [&actor](const Dali::Scene3D::Loader::AnimatedProperty& property) {
128       return actor.FindChildByName(property.mNodeName);
129     };
130
131     auto anim = animDef.ReAnimate(getActor);
132     DALI_TEST_EQUAL(anim.GetDuration(), animDef.GetDuration());
133     DALI_TEST_EQUAL(anim.GetEndAction(), animDef.GetEndAction());
134     DALI_TEST_EQUAL(anim.GetSpeedFactor(), animDef.GetSpeedFactor());
135     DALI_TEST_EQUAL(anim.GetLoopCount(), animDef.GetLoopCount());
136   }
137
138   END_TEST;
139 }