Merge "Updated patch coverage script." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / internal / gltf2-asset.cpp
1 /*
2 * Copyright (c) 2020 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 #include "dali-scene-loader/internal/gltf2-asset.h"
18 #include "dali/public-api/math/matrix.h"
19 #include <algorithm>
20 #include <map>
21
22 #define ENUM_STRING_MAPPING(t, x) { #x, t::x }
23
24 using namespace Dali;
25
26 namespace gltf2
27 {
28 namespace
29 {
30
31 constexpr uint32_t ACCESSOR_TYPE_ELEMENT_COUNT[]{
32   1,
33   2,
34   3,
35   4,
36   4,
37   9,
38   16,
39   static_cast<uint32_t>(-1)
40 };
41
42 const std::map<std::string_view, AccessorType::Type> ACCESSOR_TYPES{
43   ENUM_STRING_MAPPING(AccessorType, SCALAR),
44   ENUM_STRING_MAPPING(AccessorType, VEC2),
45   ENUM_STRING_MAPPING(AccessorType, VEC3),
46   ENUM_STRING_MAPPING(AccessorType, VEC4),
47   ENUM_STRING_MAPPING(AccessorType, MAT2),
48   ENUM_STRING_MAPPING(AccessorType, MAT3),
49   ENUM_STRING_MAPPING(AccessorType, MAT4),
50 };
51
52 const std::map<std::string_view, AlphaMode::Type> ALPHA_MODE_TYPES{
53   ENUM_STRING_MAPPING(AlphaMode::Type, OPAQUE),
54   ENUM_STRING_MAPPING(AlphaMode::Type, MASK),
55   ENUM_STRING_MAPPING(AlphaMode::Type, BLEND),
56 };
57
58 const std::map<std::string_view, Attribute::Type> ATTRIBUTE_TYPES{
59   ENUM_STRING_MAPPING(Attribute::Type, POSITION),
60   ENUM_STRING_MAPPING(Attribute::Type, NORMAL),
61   ENUM_STRING_MAPPING(Attribute::Type, TANGENT),
62   ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_0),
63   ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_1),
64   ENUM_STRING_MAPPING(Attribute::Type, COLOR_0),
65   ENUM_STRING_MAPPING(Attribute::Type, JOINTS_0),
66   ENUM_STRING_MAPPING(Attribute::Type, WEIGHTS_0),
67 };
68
69 const std::map<std::string_view, Animation::Sampler::Interpolation::Type> ANIMATION_SAMPLER_INTERPOLATION{
70   ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, STEP),
71   ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, LINEAR),
72   ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, CUBICSPLINE),
73 };
74
75 const std::map<std::string_view, Animation::Channel::Target::Type> ANIMATION_CHANNEL_TARGET_PATH_TYPES{
76   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, TRANSLATION),
77   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, ROTATION),
78   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, SCALE),
79   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, WEIGHTS),
80 };
81
82 }
83
84 bool Component::IsUnsigned(Type t)
85 {
86   return t == UNSIGNED_BYTE || t == UNSIGNED_SHORT || t == UNSIGNED_INT;
87 }
88
89 uint32_t Component::Size(Type t)
90 {
91   switch (t)
92   {
93   case BYTE:
94   case UNSIGNED_BYTE:
95     return 1;
96   case SHORT:
97   case UNSIGNED_SHORT:
98     return 2;
99   case UNSIGNED_INT:
100   case FLOAT:
101     return 4;
102   default:
103     return -1;
104   }
105 }
106
107 uint32_t AccessorType::ElementCount(Type t)
108 {
109   return ACCESSOR_TYPE_ELEMENT_COUNT[t];
110 }
111
112 AccessorType::Type AccessorType::FromString(const char* s, size_t len)
113 {
114   auto iFind = ACCESSOR_TYPES.find(std::string_view(s, len));
115   if (iFind != ACCESSOR_TYPES.end())
116   {
117     return iFind->second;
118   }
119   return AccessorType::INVALID;
120 }
121
122 AlphaMode::Type AlphaMode::FromString(const char* s, size_t len)
123 {
124   auto iFind = ALPHA_MODE_TYPES.find(std::string_view(s, len));
125   if (iFind != ALPHA_MODE_TYPES.end())
126   {
127     return iFind->second;
128   }
129   return AlphaMode::INVALID;
130 }
131
132 Attribute::Type Attribute::FromString(const char* s, size_t len)
133 {
134   auto iFind = ATTRIBUTE_TYPES.find(std::string_view(s, len));
135   if (iFind != ATTRIBUTE_TYPES.end())
136   {
137     return iFind->second;
138   }
139   return Attribute::INVALID;
140 }
141
142 Animation::Sampler::Interpolation::Type Animation::Sampler::Interpolation::FromString(const char* s, size_t len)
143 {
144   auto iFind = ANIMATION_SAMPLER_INTERPOLATION.find(std::string_view(s, len));
145   if (iFind != ANIMATION_SAMPLER_INTERPOLATION.end())
146   {
147     return iFind->second;
148   }
149   return Animation::Sampler::Interpolation::Type::INVALID;
150 }
151
152 uint32_t ComponentTypedBufferViewClient::GetBytesPerComponent() const
153 {
154   return Component::Size(mComponentType);
155 }
156
157 Animation::Channel::Target::Type Animation::Channel::Target::FromString(const char* s, size_t len)
158 {
159   std::string target(s, len);
160   std::transform(target.begin(), target.end(), target.begin(), ::toupper);
161
162   auto iFind = ANIMATION_CHANNEL_TARGET_PATH_TYPES.find(std::string_view(target.c_str(), len));
163   if (iFind != ANIMATION_CHANNEL_TARGET_PATH_TYPES.end())
164   {
165     return iFind->second;
166   }
167   return Animation::Channel::Target::INVALID;
168 };
169
170 void Node::SetMatrix(const Matrix& m)
171 {
172   m.GetTransformComponents(mTranslation, mRotation, mScale);
173 }
174
175 Quaternion ReadQuaternion(const json_value_s & j)
176 {
177         return Quaternion(ReadDaliVector<Vector4>(j));
178 }
179
180 }