Fixed culling to take scale into consideration
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / transform-manager.h
1
2 #ifndef TRANSFORM_MANAGER_H_
3 #define TRANSFORM_MANAGER_H_
4
5 /*
6  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 // INTERNAL INCLUDES
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/math/matrix.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <dali/public-api/math/vector3.h>
27 #include <dali/internal/update/manager/free-list.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace SceneGraph
36 {
37
38 /**
39  * Struct to store the animatable part of a component (Scale, orientation and position)
40  */
41 struct TransformComponentAnimatable
42 {
43   TransformComponentAnimatable()
44   :mScale(1.0f,1.0f,1.0f),
45    mOrientation(1.0f,0.0f,0.0f,0.0f),
46    mPosition(0.0f,0.0f,0.0f)
47   {}
48
49   Vector3 mScale;
50   Quaternion mOrientation;
51   Vector3 mPosition;
52 };
53
54 /**
55  * Struct to store the non-animatable part of a component (AnchorPoint and ParentOrigin)
56  */
57 struct TransformComponentStatic
58 {
59   TransformComponentStatic()
60   :mAnchorPoint(0.5f,0.5f,0.5f),
61    mParentOrigin(0.0f,0.0f,0.5f)
62   {}
63
64   Vector3 mAnchorPoint;
65   Vector3 mParentOrigin;
66 };
67
68 enum InheritanceMode
69 {
70   DONT_INHERIT_TRANSFORM  = 0,
71   INHERIT_POSITION        = 1,
72   INHERIT_SCALE           = 2,
73   INHERIT_ORIENTATION     = 4,
74   INHERIT_ALL             = INHERIT_POSITION | INHERIT_SCALE | INHERIT_ORIENTATION,
75 };
76
77 enum TransformManagerProperty
78 {
79   TRANSFORM_PROPERTY_POSITION = 0,
80   TRANSFORM_PROPERTY_SCALE,
81   TRANSFORM_PROPERTY_ANCHOR_POINT,
82   TRANSFORM_PROPERTY_PARENT_ORIGIN,
83   TRANSFORM_PROPERTY_SIZE,
84   TRANSFORM_PROPERTY_WORLD_POSITION,
85   TRANSFORM_PROPERTY_WORLD_SCALE,
86   TRANSFORM_PROPERTY_WORLD_ORIENTATION,
87   TRANSFORM_PROPERTY_WORLD_MATRIX,
88   TRANSFORM_PROPERTY_COUNT,
89 };
90
91 typedef unsigned int TransformId;
92 static const TransformId INVALID_TRANSFORM_ID = -1;
93
94 } //SceneGraph
95 } //Internal
96
97 // Allow TransformComponentAnimatable to be treated as a POD type
98 template <> struct TypeTraits< Dali::Internal::SceneGraph::TransformComponentAnimatable >: public Dali::BasicTypes< Dali::Internal::SceneGraph::TransformComponentAnimatable > { enum { IS_TRIVIAL_TYPE = true }; };
99
100 namespace Internal
101 {
102
103 namespace SceneGraph
104 {
105
106 /**
107  * Transform manager computes the local to world transformations
108  * of all the nodes in the scene. All the transformation data is stored contiguously
109  * in memory which minimizes cache misses during updates
110  */
111 class TransformManager
112 {
113 public:
114
115   /**
116    * Default constructor
117    */
118   TransformManager();
119
120   /**
121    * Class destructor
122    */
123   ~TransformManager();
124
125   /**
126    * Add a new transform component to the manager
127    * @return A TransformId used to access the component
128    */
129   TransformId CreateTransform();
130
131   /**
132    * Removes an existing transform component
133    * @param[in] id Id of the transform to remove
134    */
135   void RemoveTransform(TransformId id);
136
137   /**
138    * Sets the parent transform of an existing component
139    * @param[in] id Id of the transform
140    * @param[in] parentId Id of the new parent
141    */
142   void SetParent( TransformId id, TransformId parentId );
143
144   /**
145    * Gets the world transform matrix of an exisiting transform component
146    * @param[in] id Id of the transform component
147    * @return The local to world transformation matrix of the component
148    */
149   const Matrix& GetWorldMatrix( TransformId id ) const;
150
151   /**
152    * Gets the world transform matrix of an exisiting transform component
153    * @param[in] id Id of the transform component
154    * @return The local to world transformation matrix of the component
155    */
156   Matrix& GetWorldMatrix( TransformId id );
157
158   /**
159    * Checks if the local transform was updated in the last Update
160    * @param[in] id Id of the transform
161    * @return true if local matrix changed in the last update, false otherwise
162    */
163   bool IsLocalMatrixDirty( TransformId id ) const
164   {
165     return mLocalMatrixDirty[mIds[id]];
166   }
167
168   /**
169    * Sets position inheritance mode.
170    * @param[in] id Id of the transform
171    * @param[in] inherit True if position is inherited from parent, false otherwise
172    */
173   void SetInheritPosition( TransformId id, bool inherit );
174
175   /**
176    * Sets scale inheritance mode.
177    * @param[in] id Id of the transform
178    * @param[in] inherit True if scale is inherited from parent, false otherwise
179    */
180   void SetInheritScale( TransformId id, bool inherit );
181
182   /**
183    * Sets orientation inheritance mode.
184    * @param[in] id Id of the transform
185    * @param[in] inherit True if orientation is inherited from parent, false otherwise
186    */
187   void SetInheritOrientation( TransformId id, bool inherit );
188
189   /**
190    * Recomputes all world transform matrices
191    */
192   void Update();
193
194   /**
195    * Resets all the animatable properties to its base value
196    */
197   void ResetToBaseValue();
198
199   /**
200    * Get the value of a Vector3 property
201    * @param[in] id Id of the transform component
202    * @param[in] property The property
203    */
204   Vector3& GetVector3PropertyValue( TransformId id, TransformManagerProperty property );
205
206   /**
207    * Get the value of a Vector3 property
208    * @param[in] id Id of the transform component
209    * @param[in] property The property
210    */
211   const Vector3& GetVector3PropertyValue( TransformId id, TransformManagerProperty property ) const;
212
213   /**
214    * Get the value of a component of Vector3 property
215    * @param[in] id Id of the transform component
216    * @param[in] property The property
217    * param[in] component The component (0,1,2)
218    */
219   const float& GetVector3PropertyComponentValue(TransformId id, TransformManagerProperty property, unsigned int component ) const;
220
221   /**
222    * Set the value of a Vector3 property
223    * @param[in] id Id of the transform component
224    * @param[in] property The property
225    * @param[in] value The new value
226    */
227   void SetVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
228
229   /**
230    * Set the value of a component of a Vector3 property
231    * @param[in] id Id of the transform component
232    * @param[in] property The property
233    * @param[in] value The new value
234    * param[in] component The component (0,1,2)
235    */
236   void SetVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, unsigned int component );
237
238   /**
239    * Bakes the value of a Vector3 property
240    * @param[in] id Id of the transform component
241    * @param[in] property The property
242    * @param[in] value The new value
243    */
244   void BakeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
245
246   /**
247    * Bakes the value of a Vector3 property relative to the current value
248    * @param[in] id Id of the transform component
249    * @param[in] property The property
250    * @param[in] value The new value
251    */
252   void BakeRelativeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
253
254   /**
255    * Bakes the value of a Vector3 property by multiplying the current value and the new value
256    * @param[in] id Id of the transform component
257    * @param[in] property The property
258    * @param[in] value The new value
259    */
260   void BakeMultiplyVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
261
262   /**
263    * Bakes the value of a component of Vector3 property
264    * @param[in] id Id of the transform component
265    * @param[in] property The property
266    * @param[in] value The new value
267    */
268   void BakeVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, unsigned int component );
269
270   /**
271    * Bakes the value of the x component of Vector3 property
272    * @param[in] id Id of the transform component
273    * @param[in] property The property
274    * @param[in] value The new value
275    */
276   void BakeXVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
277
278   /**
279    * Bakes the value of the y component of Vector3 property
280    * @param[in] id Id of the transform component
281    * @param[in] property The property
282    * @param[in] value The new value
283    */
284   void BakeYVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
285
286   /**
287    * Bakes the value of the z component of Vector3 property
288    * @param[in] id Id of the transform component
289    * @param[in] property The property
290    * @param[in] value The new value
291    */
292   void BakeZVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
293
294   /**
295    * Get the value of a quaternion property
296    * @param[in] id Id of the transform component
297    */
298   Quaternion& GetQuaternionPropertyValue( TransformId id );
299
300   /**
301    * Get the value of a quaternion property
302    * @param[in] id Id of the transform component
303    */
304   const Quaternion& GetQuaternionPropertyValue( TransformId id ) const;
305
306   /**
307    * Set the value of a quaternion property
308    * @param[in] id Id of the transform component
309    * @param[in] q The new value
310    */
311   void SetQuaternionPropertyValue( TransformId id, const Quaternion& q );
312
313   /**
314    * Bake the value of a quaternion property
315    * @param[in] id Id of the transform component
316    * @param[in] q The new value
317    */
318   void BakeQuaternionPropertyValue( TransformId id, const Quaternion& q );
319
320   /**
321    * Bake the value of a quaternion property relative to its current value
322    * @param[in] id Id of the transform component
323    * @param[in] q The new value
324    */
325   void BakeRelativeQuaternionPropertyValue( TransformId id, const Quaternion& q );
326
327   /**
328    * Get the bounding sphere, in world coordinates, of a given component
329    * @param[in] id Id of the transform component
330    * @return The world space bounding sphere of the component
331    */
332   const Vector4& GetBoundingSphere( TransformId id ) const;
333
334   /**
335    * Get the world matrix and size of a given component
336    * @param[in] id Id of the transform component
337    * @param[out] size size of the component
338    * @return The world matrix of the component
339    */
340   const Matrix& GetWorldMatrixAndSize( TransformId id, Vector3& size ) const;
341
342 private:
343
344   //Helper struct to order components
345   struct SOrderItem
346   {
347     bool operator<(const SOrderItem& item) const {return level < item.level;}
348
349     TransformId  id;
350     unsigned int level;
351   };
352
353   /**
354    * Swaps two components in the vectors
355    * @param[in] i Index of a component
356    * @param[in] j Index of a component
357    */
358   void SwapComponents( unsigned int i, unsigned int j );
359
360   /**
361    * Reorders components in hierarchical order so update can iterate sequentially
362    * updating the world transforms
363    */
364   void ReorderComponents();
365
366   unsigned int mComponentCount;                                ///< Total number of components
367   FreeList mIds;                                               ///< FreeList of Ids
368   Vector<TransformComponentAnimatable> mTxComponentAnimatable; ///< Animatable part of the components
369   Vector<TransformComponentStatic> mTxComponentStatic;         ///< Static part of the components
370   Vector<unsigned int> mInheritanceMode;                       ///< Inheritance mode of the components
371   Vector<TransformId> mComponentId;                            ///< Ids of the components
372   Vector<Vector3> mSize;                                       ///< Size of the components
373   Vector<TransformId> mParent;                                 ///< Parent of the components
374   Vector<Matrix> mWorld;                                       ///< Local to world transform of the components
375   Vector<Matrix> mLocal;                                       ///< Local to parent space transform of the components
376   Vector<Vector4> mBoundingSpheres;                            ///< Bounding spheres. xyz is the center and w is the radius
377   Vector<TransformComponentAnimatable> mTxComponentAnimatableBaseValue;  ///< Base values for the animatable part of the components
378   Vector<Vector3> mSizeBase;                                             ///< Base value for the size of the components
379   Vector<bool> mComponentDirty;    ///< 1u if some of the parts of the component has changed in this frame, 0 otherwise
380   Vector<bool> mLocalMatrixDirty;  ///< 1u if the local matrix has been updated in this frame, 0 otherwise
381   Vector<SOrderItem> mOrderedComponents;   ///< Used to reorder components when hierarchy changes
382   bool mReorder;                           ///< Flag to determine if the components have to reordered in the next Update
383 };
384
385 } //namespace SceneGraph
386
387 } //namespace Internal
388
389
390 } //namespace Dali
391
392
393 #endif /* TRANSFORM_MANAGER_H_ */