2 #ifndef TRANSFORM_MANAGER_H_
3 #define TRANSFORM_MANAGER_H_
6 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
39 * Struct to store the animatable part of a component (Scale, orientation and position)
41 struct TransformComponentAnimatable
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)
50 Quaternion mOrientation;
55 * Struct to store the non-animatable part of a component (AnchorPoint and ParentOrigin)
57 struct TransformComponentStatic
59 TransformComponentStatic()
60 :mAnchorPoint(0.5f,0.5f,0.5f),
61 mParentOrigin(0.0f,0.0f,0.5f)
65 Vector3 mParentOrigin;
70 DONT_INHERIT_TRANSFORM = 0,
73 INHERIT_ORIENTATION = 4,
74 INHERIT_ALL = INHERIT_POSITION | INHERIT_SCALE | INHERIT_ORIENTATION,
77 enum TransformManagerProperty
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,
91 typedef unsigned int TransformId;
92 static const TransformId INVALID_TRANSFORM_ID = -1;
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 }; };
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
111 class TransformManager
116 * Default constructor
126 * Add a new transform component to the manager
127 * @return A TransformId used to access the component
129 TransformId CreateTransform();
132 * Removes an existing transform component
133 * @param[in] id Id of the transform to remove
135 void RemoveTransform(TransformId id);
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
142 void SetParent( TransformId id, TransformId parentId );
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
149 const Matrix& GetWorldMatrix( TransformId id ) const;
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
156 Matrix& GetWorldMatrix( TransformId id );
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
163 bool IsLocalMatrixDirty( TransformId id ) const
165 return mLocalMatrixDirty[mIds[id]];
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
173 void SetInheritPosition( TransformId id, bool inherit );
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
180 void SetInheritScale( TransformId id, bool inherit );
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
187 void SetInheritOrientation( TransformId id, bool inherit );
190 * Recomputes all world transform matrices
195 * Resets all the animatable properties to its base value
197 void ResetToBaseValue();
200 * Get the value of a Vector3 property
201 * @param[in] id Id of the transform component
202 * @param[in] property The property
204 Vector3& GetVector3PropertyValue( TransformId id, TransformManagerProperty property );
207 * Get the value of a Vector3 property
208 * @param[in] id Id of the transform component
209 * @param[in] property The property
211 const Vector3& GetVector3PropertyValue( TransformId id, TransformManagerProperty property ) const;
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)
219 const float& GetVector3PropertyComponentValue(TransformId id, TransformManagerProperty property, unsigned int component ) const;
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
227 void SetVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
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)
236 void SetVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, unsigned int component );
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
244 void BakeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
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
252 void BakeRelativeVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
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
260 void BakeMultiplyVector3PropertyValue( TransformId id, TransformManagerProperty property, const Vector3& value );
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
268 void BakeVector3PropertyComponentValue( TransformId id, TransformManagerProperty property, float value, unsigned int component );
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
276 void BakeXVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
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
284 void BakeYVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
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
292 void BakeZVector3PropertyValue( TransformId id, TransformManagerProperty property, float value );
295 * Get the value of a quaternion property
296 * @param[in] id Id of the transform component
298 Quaternion& GetQuaternionPropertyValue( TransformId id );
301 * Get the value of a quaternion property
302 * @param[in] id Id of the transform component
304 const Quaternion& GetQuaternionPropertyValue( TransformId id ) const;
307 * Set the value of a quaternion property
308 * @param[in] id Id of the transform component
309 * @param[in] q The new value
311 void SetQuaternionPropertyValue( TransformId id, const Quaternion& q );
314 * Bake the value of a quaternion property
315 * @param[in] id Id of the transform component
316 * @param[in] q The new value
318 void BakeQuaternionPropertyValue( TransformId id, const Quaternion& q );
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
325 void BakeRelativeQuaternionPropertyValue( TransformId id, const Quaternion& q );
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
332 const Vector4& GetBoundingSphere( TransformId id ) const;
335 * Get the world matrix and size of a given component
336 * @param[in] id Id of the transform component
337 * @param[out] The world matrix of the component
338 * @param[out] size size of the component
340 void GetWorldMatrixAndSize( TransformId id, Matrix& worldMatrix, Vector3& size ) const;
344 //Helper struct to order components
347 bool operator<(const SOrderItem& item) const {return level < item.level;}
354 * Swaps two components in the vectors
355 * @param[in] i Index of a component
356 * @param[in] j Index of a component
358 void SwapComponents( unsigned int i, unsigned int j );
361 * Reorders components in hierarchical order so update can iterate sequentially
362 * updating the world transforms
364 void ReorderComponents();
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
385 } //namespace SceneGraph
387 } //namespace Internal
393 #endif /* TRANSFORM_MANAGER_H_ */