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