[Adaptation Layer] Added rive-tizen adaptation layer class.
[platform/core/uifw/rive-tizen.git] / submodule / src / bones / weight.cpp
1 #include "bones/weight.hpp"
2 #include "container_component.hpp"
3 #include "shapes/path_vertex.hpp"
4
5 using namespace rive;
6
7 StatusCode Weight::onAddedDirty(CoreContext* context)
8 {
9         StatusCode code = Super::onAddedDirty(context);
10         if (code != StatusCode::Ok)
11         {
12                 return code;
13         }
14         if (!parent()->is<PathVertex>())
15         {
16                 return StatusCode::MissingObject;
17         }
18
19         parent()->as<PathVertex>()->weight(this);
20
21         return StatusCode::Ok;
22 }
23
24 static int encodedWeightValue(unsigned int index, unsigned int data)
25 {
26         return (data >> (index * 8)) & 0xFF;
27 }
28
29 void Weight::deform(float x,
30                     float y,
31                     unsigned int indices,
32                     unsigned int weights,
33                     const Mat2D& world,
34                     const float* boneTransforms,
35                     Vec2D& result)
36 {
37         float xx = 0, xy = 0, yx = 0, yy = 0, tx = 0, ty = 0;
38         float rx = world[0] * x + world[2] * y + world[4];
39         float ry = world[1] * x + world[3] * y + world[5];
40         for (int i = 0; i < 4; i++)
41         {
42                 int weight = encodedWeightValue(i, weights);
43                 if (weight == 0)
44                 {
45                         continue;
46                 }
47
48                 float normalizedWeight = weight / 255.0f;
49                 int index = encodedWeightValue(i, indices);
50                 int startBoneTransformIndex = index * 6;
51                 xx += boneTransforms[startBoneTransformIndex++] * normalizedWeight;
52                 xy += boneTransforms[startBoneTransformIndex++] * normalizedWeight;
53                 yx += boneTransforms[startBoneTransformIndex++] * normalizedWeight;
54                 yy += boneTransforms[startBoneTransformIndex++] * normalizedWeight;
55                 tx += boneTransforms[startBoneTransformIndex++] * normalizedWeight;
56                 ty += boneTransforms[startBoneTransformIndex++] * normalizedWeight;
57         }
58         result[0] = xx * rx + yx * ry + tx;
59         result[1] = xy * rx + yy * ry + ty;
60 }