Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / physics-engine / FUiEffects_PeRodSurface.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FUiEffects_PeRodSurface.cpp
20  * @brief       This file contains an implementation of RodSurface class methods
21  *
22  */
23
24 #include <FUiEffects_PeRodSurface.h>
25
26 using namespace Tizen::Ui::Effects::_Runtime;
27 using namespace Tizen::Ui::Effects::_Utils;
28
29 namespace Tizen { namespace Ui { namespace Effects { namespace _PhysicsEngine
30 {
31
32 RodSurface::RodSurface(
33         long                    objId,
34         float                   stiffness,
35         PointSurface*   pPoint1,
36         PointSurface*   pPoint2,
37         PointSurface*   pPoint3
38 )
39         : ElementSurface(objId),
40         __stiffnessInit(stiffness)
41 {
42         this->_pPoint1 = pPoint1;
43         this->_pPoint2 = pPoint2;
44         this->_pPoint3 = pPoint3;
45
46         // init when created
47         InitRod();
48 }
49
50 RodSurface::~RodSurface(void)
51 {
52         return;
53 }
54
55 ESurface
56 RodSurface::GetType(void) const
57 {
58         return ESURFACE_ROD_SURFACE;
59 }
60
61 void
62 RodSurface::Initialize(void)
63 {
64         InitRod();
65         return;
66 }
67
68 void
69 RodSurface::Calculate(float timeStep)
70 {
71         UseUnbendModel(timeStep);
72         return;
73 }
74
75 PropertyCast
76 RodSurface::GetProperty(ElementProperty property) const
77 {
78         PropertyCast result;
79         switch (property)
80         {
81         case N_STIFFNESS:
82                 result.type = PropertyCast::NUMBER;
83                 result.value.numberValue = __stiffness;
84                 break;
85         default:
86                 result = ElementSurface::GetProperty(property);
87                 break;
88         }
89         return result;
90 }
91
92 bool
93 RodSurface::SetProperty(ElementProperty property, bool boolValue)
94 {
95         return ElementSurface::SetProperty(property, boolValue);
96 }
97
98 bool
99 RodSurface::SetProperty(ElementProperty property, LUA_NUMBER numericValue)
100 {
101         bool result = true;     // for successful set
102         switch (property)
103         {
104         // coefficient of rigidity
105         case N_STIFFNESS:
106                 __stiffness = numericValue;
107                 break;
108         default:
109                 result = ElementSurface::SetProperty(property, numericValue);
110                 break;
111         }
112         return result;
113 }
114
115 bool
116 RodSurface::SetProperty(ElementProperty property, const Vec3f& vectorValue)
117 {
118         return ElementSurface::SetProperty(property, vectorValue);
119 }
120
121 void
122 RodSurface::InitRod(void)
123 {
124         this->_deformationAngle = 0.f;
125         this->__stiffness = __stiffnessInit;
126         return;
127 }
128
129 // old rod model, currently not used
130 void
131 RodSurface::UseStretchModel(float timeStep)
132 {
133         Vec3f
134                 direction31 = _pPoint3->GetPosition() - _pPoint1->GetPosition(),
135                 direction21 = _pPoint2->GetPosition() - _pPoint1->GetPosition(),
136                 rodForce;
137
138         direction31.normalize();
139         direction21.normalize();
140
141         // |[AxB]| = |A||B|sin(alpha) (angle between A and B)
142         // |A| = |B| = 1, so fDeformArc is equal to angle sine
143         _deformationAngle = direction21.getCrossed(direction31).length();
144         float rodForceValue = __stiffness * _deformationAngle;
145
146         rodForce = direction31 * rodForceValue;
147
148         _pPoint3->AddForce( rodForce);
149         _pPoint1->AddForce(-rodForce);
150         return;
151 }
152
153 // new rod model, now in use
154 void
155 RodSurface::UseUnbendModel(float timeStep)
156 {
157         Vec3f
158                 direction21 = _pPoint1->GetPosition() - _pPoint2->GetPosition(),
159                 direction23 = _pPoint3->GetPosition() - _pPoint2->GetPosition();
160
161         direction21.normalize();
162         direction23.normalize();
163
164         Vec3f rodNormal = direction23.getCrossed(direction21);
165         float deformation = rodNormal.length(),
166                 forceValue = __stiffness * deformation;
167
168         Vec3f forceDirection;
169         // force for central point
170         forceDirection = direction21 + direction23;
171         _pPoint2->AddForce( forceDirection.normalize() * forceValue );
172
173         // forces for edge points
174         forceValue *= 0.5f;
175         _pPoint1->AddForce( rodNormal.getCrossed(direction21).normalize() * forceValue );
176         _pPoint3->AddForce( direction23.getCrossed(rodNormal).normalize() * forceValue );
177         return;
178 }
179
180 } } } } // Tizen::Ui::Effects::_PhysicsEngine