Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / runtime / FUiEffects_RuntimeModelSurface.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  * @file        FUiEffects_RuntimeModelSurface.cpp
19  * @brief       This file contains an implementation of ModelSurface class methods
20  *
21  */
22
23 #include <memory>
24 #include "../FUiEffects_EffectErrorMessages.h"
25 #include <FBaseSysLog.h>
26 #include <FBaseErrors.h>
27 #include <FUiEffects_PePointSurface.h>
28 #include <FUiEffects_PeSpringSurface.h>
29 #include <FUiEffects_PeRodSurface.h>
30 #include "FUiEffects_RuntimeModelSurface.h"
31 #include "FUiEffects_RuntimeGraphicalSurface.h"
32 #include "FUiEffects_RuntimeModel.h"
33 #include "FUiEffects_RuntimePropertyCast.h"
34
35 using namespace Tizen::Ui::Effects::_PhysicsEngine;
36 using namespace Tizen::Ui::Effects::_Utils;
37 using namespace std;
38
39 namespace Tizen { namespace Ui { namespace Effects { namespace _Runtime
40 {
41
42 void
43 ElementsSurfaceContainerDeleter::operator()(TypeElemSurfaceCollection* pElemSurfaceCollection)
44 {
45         TypeElemSurfaceCollection::iterator it;
46         for (it = pElemSurfaceCollection->begin(); it != pElemSurfaceCollection->end(); ++it)
47         {
48                 delete *it;
49         }
50         delete pElemSurfaceCollection;
51 }
52
53 ModelSurface::ModelSurface(long objId,
54                                                    unsigned short iterationCount_,
55                                                    unsigned short modelSpeed_,
56                                                    float envResistance_,
57                                                    const Vec3f &gravityAccelerationIdentity_,
58                                                    float gravityAccelerationValue_,
59                                                    float groundLevel_)
60                                                                 : ElementSurface(objId)
61                                                                 , __pElemSurfaceCollection(null)
62                                                                 , __pGraphicalSurface(null)
63                                                                 , __iterationCount(iterationCount_)
64                                                                 , __modelSpeed(modelSpeed_)
65                                                                 , __envResistance(envResistance_)
66                                                                 , __gravityAccelerationIdentity(gravityAccelerationIdentity_)
67                                                                 , __gravityAccelerationValue(gravityAccelerationValue_)
68                                                                 , __gravityAccelerationVector(gravityAccelerationValue_ * gravityAccelerationIdentity_)
69                                                                 , __groundLevel(groundLevel_)
70                                                                 , __pPointsCollection(null)
71                                                                 , __pSpringsCollection(null)
72                                                                 , __pRodsCollection(null)
73 {
74         unique_ptr<TypeElemSurfaceCollection, ElementsSurfaceContainerDeleter> pElemSurfaceCollection(new (std::nothrow) TypeElemSurfaceCollection, ElementsSurfaceContainerDeleter());
75         SysTryReturnVoidResult(NID_UI_EFFECT, pElemSurfaceCollection.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
76
77         unique_ptr<TypePointSurfaceCollection> pPointsCollection(new (std::nothrow) TypePointSurfaceCollection);
78         SysTryReturnVoidResult(NID_UI_EFFECT, pPointsCollection.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
79         unique_ptr<TypeSpringSurfaceCollection> pSpringsCollection(new (std::nothrow) TypeSpringSurfaceCollection);
80         SysTryReturnVoidResult(NID_UI_EFFECT, pSpringsCollection.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
81         unique_ptr<TypeRodSurfaceCollection> pRodsCollection(new (std::nothrow) TypeRodSurfaceCollection);
82         SysTryReturnVoidResult(NID_UI_EFFECT, pRodsCollection.get() != null, E_OUT_OF_MEMORY, _UiEffectError::OUT_OF_MEMORY);
83
84         __pElemSurfaceCollection = std::move(pElemSurfaceCollection);
85         __pPointsCollection = std::move(pPointsCollection);
86         __pSpringsCollection = std::move(pSpringsCollection);
87         __pRodsCollection = std::move(pRodsCollection);
88 }
89
90 ModelSurface::~ModelSurface(void)
91 {
92
93 }
94
95 void
96 ModelSurface::Calculate(float timeStep)
97 {
98         TypeElemSurfaceCollection::iterator it;
99         for (int j = 0; j < __modelSpeed; ++j)
100         {
101                 for (int i = 0; i < __iterationCount; ++i)
102                 {
103                         for (it = __pElemSurfaceCollection->begin(); it != __pElemSurfaceCollection->end(); ++it )
104                         {
105                                 (*it)->Calculate(timeStep / __iterationCount);
106                         }
107                 }
108         }
109         return;
110 }
111
112 PropertyCast
113 ModelSurface::GetProperty(ElementProperty propName)const
114 {
115         PropertyCast propCast = {PropertyCast::NO_PROPERTY, {0}};
116         switch (propName)
117         {
118         case N_NUMBER_ITERATIONS:
119                 propCast.type = PropertyCast::NUMBER;
120                 propCast.value.numberValue = __iterationCount;
121                 break;
122         case N_MODEL_SPEED:
123                 propCast.type = PropertyCast::NUMBER;
124                 propCast.value.numberValue = __modelSpeed;
125                 break;
126         case N_ENV_RESISTANCE:
127                 propCast.type = PropertyCast::NUMBER;
128                 propCast.value.numberValue = __envResistance;
129                 break;
130         case N_GRAVITY_ACCEL_VALUE:
131                 propCast.type = PropertyCast::NUMBER;
132                 propCast.value.numberValue = __gravityAccelerationValue;
133                 break;
134         case V_GRAVITY_ACCELERATION:
135                 propCast.type = PropertyCast::VEC3;
136                 propCast.value.vec3Value = &__gravityAccelerationVector;
137                 break;
138         default:
139                 propCast.type = PropertyCast::NO_PROPERTY;
140                 break;
141         }
142         return propCast;
143 }
144
145 bool
146 ModelSurface::SetProperty(ElementProperty propName, bool propValue)
147 {
148         return false;
149 }
150
151 bool
152 ModelSurface::SetProperty(ElementProperty propName, LUA_NUMBER propValue)
153 {
154         switch (propName)
155         {
156         case N_NUMBER_ITERATIONS:
157                 __iterationCount = propValue;
158                 break;
159         case N_MODEL_SPEED:
160                 __modelSpeed = propValue;
161                 break;
162         case N_ENV_RESISTANCE:
163                 __envResistance = propValue;
164                 break;
165         case N_GRAVITY_ACCEL_VALUE:
166                 __gravityAccelerationValue = propValue;
167                 __gravityAccelerationVector = __gravityAccelerationValue * __gravityAccelerationIdentity;
168                 break;
169         default:
170                 return false;
171                 break;
172         }
173         return true;
174 }
175
176 bool
177 ModelSurface::SetProperty(ElementProperty propName, const Vec3f &propValue)
178 {
179         switch (propName)
180         {
181         case V_GRAVITY_ACCELERATION:
182                 __gravityAccelerationIdentity = propValue.getNormalized();
183                 __gravityAccelerationVector = __gravityAccelerationValue * __gravityAccelerationIdentity;
184                 break;
185         default:
186                 return false;
187                 break;
188         }
189
190         return true;
191 }
192
193 bool
194 ModelSurface::SetPropertyGroup(GroupElements group, ElementProperty property, LUA_NUMBER value)
195 {
196         switch (group)
197         {
198         case ALL_POINTS:
199                 return SetPropertyGroup<TypePointSurfaceCollection,
200                                                                 TypePointSurfaceCollection::iterator>(__pPointsCollection.get(), property, value);
201                 break;
202         case ALL_SPRINGS:
203                 return SetPropertyGroup<TypeSpringSurfaceCollection,
204                                                                 TypeSpringSurfaceCollection::iterator>(__pSpringsCollection.get(), property, value);
205                 break;
206         case ALL_RODS:
207                 return SetPropertyGroup<TypeRodSurfaceCollection,
208                                                                 TypeRodSurfaceCollection::iterator>(__pRodsCollection.get(), property, value);
209                 break;
210         default:
211                 return false;
212                 break;
213         }
214 }
215
216 template<typename T, typename TIter> bool
217 ModelSurface::SetPropertyGroup(T* pDataPointer, ElementProperty property, LUA_NUMBER value)
218 {
219         TIter it;
220         TIter itBegin = pDataPointer->begin();
221         TIter itEnd = pDataPointer->end();
222
223         if (pDataPointer->empty())
224         {
225                 return false;
226         }
227
228         for (it = itBegin; it != itEnd; ++it)
229         {
230                 if (!((*it)->SetProperty(property, value)))
231                 {
232                         return false;
233                 }
234         }
235         return true;
236 }
237
238 void
239 ModelSurface::Initialize(void)
240 {
241         TypeElemSurfaceCollection::iterator it;
242         for (it = __pElemSurfaceCollection->begin(); it != __pElemSurfaceCollection->end(); ++it)
243         {
244                 (*it)->Initialize();
245         }
246
247         __pGraphicalSurface->Initialize();
248
249         return;
250 }
251
252 void
253 ModelSurface::AddGraphicalSurface(unique_ptr<GraphicalSurface> pGraphicalSurface)
254 {
255         __pGraphicalSurface = std::move(pGraphicalSurface);
256         return;
257 }
258
259 void
260 ModelSurface::AddElementSurface(ElementSurface* pElementSurface)
261 {
262         __pElemSurfaceCollection->push_back(pElementSurface);
263         return;
264 }
265
266 bool
267 ModelSurface::Construct(TypeMapElements* pMapElements, TypeMapGSurface* pMapGSurface, RenderDataSurfaceCollection* pRenderDataSurfaceCollection)
268 {
269         pMapElements->insert(std::make_pair(this->GetId(), this));
270         pMapElements->insert(std::make_pair(__pGraphicalSurface->GetId(), __pGraphicalSurface.get()));
271         pMapGSurface->insert(std::make_pair(__pGraphicalSurface->GetId(), __pGraphicalSurface.get()));
272         TypeElemSurfaceCollection::iterator it;
273         for (it = __pElemSurfaceCollection->begin(); it != __pElemSurfaceCollection->end(); ++it)
274         {
275                 pMapElements->insert(std::make_pair((*it)->GetId(), *it));
276
277                 if ((*it)->GetType() >= ESURFACE_POINT_SURFACE)
278                 {
279                         PointSurface* p = dynamic_cast<PointSurface*>(*it);
280                         SysAssertf(p != null, _UiEffectError::INTERNAL_ERROR);
281                         bool res = p->Construct(&__envResistance, &__gravityAccelerationVector, &__groundLevel);
282                         SysTryReturn(NID_UI_EFFECT, res, false, E_SYSTEM, _UiEffectError::INTERNAL_ERROR);
283
284                         __pPointsCollection->push_back(p);
285                 }
286                 else if ((*it)->GetType() == ESURFACE_SPRING_SURFACE)
287                 {
288                         SpringSurface* p = dynamic_cast<SpringSurface*>(*it);
289                         SysAssertf(p != null, _UiEffectError::INTERNAL_ERROR);
290                         __pSpringsCollection->push_back(p);
291                 }
292                 else if ((*it)->GetType() == ESURFACE_ROD_SURFACE)
293                 {
294                         RodSurface* p = dynamic_cast<RodSurface*>(*it);
295                         SysAssertf(p != null, _UiEffectError::INTERNAL_ERROR);
296                         __pRodsCollection->push_back(p);
297                 }
298         }
299
300         //constructing the collection of pointers to physical points (PointSurfaceNURBS) and
301         //constructing data collection for rendering
302         __pGraphicalSurface->Construct(__pElemSurfaceCollection.get(), pRenderDataSurfaceCollection);
303
304         return true;
305 }
306
307 void
308 ModelSurface::UpdateGraphicalSurface(void)
309 {
310         __pGraphicalSurface->Update();
311         return;
312 }
313
314 ESurface
315 ModelSurface::GetType(void)const
316 {
317         return ESURFACE_MODEL_SURFACE;
318 }
319
320 float
321 ModelSurface::GetDistanceFromGround(void) const
322 {
323         TypePointSurfaceCollection::iterator it;
324         float sum = 0.f;
325         for (it = __pPointsCollection->begin(); it != __pPointsCollection->end(); ++it)
326         {
327                 sum += (*it)->GetPosition().z - __groundLevel;
328         }
329
330         TypePointSurfaceCollection::size_type size = __pPointsCollection->size();
331
332         return (size == 0 ? sum : sum / size);
333 }
334
335 } } } } // Tizen::Ui::Effects::_Runtime