1 #ifndef DALI_INTERNAL_TRANSFORM_MANAGER_H
2 #define DALI_INTERNAL_TRANSFORM_MANAGER_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/internal/update/manager/free-list.h>
23 #include <dali/public-api/common/constants.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/math/matrix.h>
26 #include <dali/public-api/math/quaternion.h>
27 #include <dali/public-api/math/vector3.h>
36 * Struct to store the animatable part of a component (Scale, orientation and position)
38 struct TransformComponentAnimatable
40 TransformComponentAnimatable()
41 : mScale(Vector3::ONE),
42 mOrientation(Quaternion::IDENTITY),
43 mPosition(Vector3::ZERO)
48 Quaternion mOrientation;
53 * Struct to store the non-animatable part of a component (AnchorPoint and ParentOrigin)
55 struct TransformComponentStatic
57 TransformComponentStatic()
58 : mAnchorPoint(AnchorPoint::DEFAULT),
59 mParentOrigin(ParentOrigin::DEFAULT),
60 mPositionUsesAnchorPoint(true)
65 Vector3 mParentOrigin;
66 bool mPositionUsesAnchorPoint;
71 DONT_INHERIT_TRANSFORM = 0,
74 INHERIT_ORIENTATION = 4,
75 INHERIT_ALL = INHERIT_POSITION | INHERIT_SCALE | INHERIT_ORIENTATION,
78 enum TransformManagerProperty
80 TRANSFORM_PROPERTY_POSITION = 0,
81 TRANSFORM_PROPERTY_SCALE,
82 TRANSFORM_PROPERTY_ANCHOR_POINT,
83 TRANSFORM_PROPERTY_PARENT_ORIGIN,
84 TRANSFORM_PROPERTY_SIZE,
85 TRANSFORM_PROPERTY_WORLD_POSITION,
86 TRANSFORM_PROPERTY_WORLD_SCALE,
87 TRANSFORM_PROPERTY_WORLD_ORIENTATION,
88 TRANSFORM_PROPERTY_WORLD_MATRIX,
89 TRANSFORM_PROPERTY_COUNT,
92 using TransformId = uint32_t; // 4,294,967,295 transforms supported
93 static const TransformId INVALID_TRANSFORM_ID = -1;
95 class TransformManager;
97 struct TransformManagerData
99 TransformManager* Manager() const
104 TransformId Id() const
109 TransformManager* mManager{nullptr};
110 TransformId mId{INVALID_TRANSFORM_ID};
113 } // namespace SceneGraph
114 } // namespace Internal
116 // Allow TransformComponentAnimatable to be treated as a POD type
118 struct TypeTraits<Dali::Internal::SceneGraph::TransformComponentAnimatable> : public Dali::BasicTypes<Dali::Internal::SceneGraph::TransformComponentAnimatable>
122 IS_TRIVIAL_TYPE = true
131 * Transform manager computes the local to world transformations
132 * of all the nodes in the scene. All the transformation data is stored contiguously
133 * in memory which minimizes cache misses during updates
135 class TransformManager
139 * Default constructor
149 * Add a new transform component to the manager
150 * @return A TransformId used to access the component
152 TransformId CreateTransform();
155 * Removes an existing transform component
156 * @param[in] id Id of the transform to remove
158 void RemoveTransform(TransformId id);
161 * Sets the parent transform of an existing component
162 * @param[in] id Id of the transform
163 * @param[in] parentId Id of the new parent
165 void SetParent(TransformId id, TransformId parentId);
168 * Gets the world transform matrix of an exisiting transform component
169 * @param[in] id Id of the transform component
170 * @return The local to world transformation matrix of the component
172 const Matrix& GetWorldMatrix(TransformId id) const;
175 * Gets the world transform matrix of an exisiting transform component
176 * @param[in] id Id of the transform component
177 * @return The local to world transformation matrix of the component
179 Matrix& GetWorldMatrix(TransformId id);
182 * Checks if the local transform was updated in the last Update
183 * @param[in] id Id of the transform
184 * @return true if local matrix changed in the last update, false otherwise
186 bool IsLocalMatrixDirty(TransformId id) const
188 return mLocalMatrixDirty[mIds[id]];
192 * Sets position inheritance mode.
193 * @param[in] id Id of the transform
194 * @param[in] inherit True if position is inherited from parent, false otherwise
196 void SetInheritPosition(TransformId id, bool inherit);
199 * Sets scale inheritance mode.
200 * @param[in] id Id of the transform
201 * @param[in] inherit True if scale is inherited from parent, false otherwise
203 void SetInheritScale(TransformId id, bool inherit);
206 * Sets orientation inheritance mode.
207 * @param[in] id Id of the transform
208 * @param[in] inherit True if orientation is inherited from parent, false otherwise
210 void SetInheritOrientation(TransformId id, bool inherit);
213 * Recomputes all world transform matrices
214 * @return true if any component has been changed in this frame, false otherwise
219 * Resets all the animatable properties to its base value
221 void ResetToBaseValue();
224 * Get the value of a Vector3 property
225 * @param[in] id Id of the transform component
226 * @param[in] property The property
228 Vector3& GetVector3PropertyValue(TransformId id, TransformManagerProperty property);
231 * Get the value of a Vector3 property
232 * @param[in] id Id of the transform component
233 * @param[in] property The property
235 const Vector3& GetVector3PropertyValue(TransformId id, TransformManagerProperty property) const;
238 * Get the value of a component of Vector3 property
239 * @param[in] id Id of the transform component
240 * @param[in] property The property
241 * param[in] component The component (0,1,2)
243 const float& GetVector3PropertyComponentValue(TransformId id, TransformManagerProperty property, uint32_t component) const;
246 * Set the value of a Vector3 property
247 * @param[in] id Id of the transform component
248 * @param[in] property The property
249 * @param[in] value The new value
251 void SetVector3PropertyValue(TransformId id, TransformManagerProperty property, const Vector3& value);
254 * Set the value of a component of a Vector3 property
255 * @param[in] id Id of the transform component
256 * @param[in] property The property
257 * @param[in] value The new value
258 * param[in] component The component (0,1,2)
260 void SetVector3PropertyComponentValue(TransformId id, TransformManagerProperty property, float value, uint32_t component);
263 * Bakes the value of a 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 BakeVector3PropertyValue(TransformId id, TransformManagerProperty property, const Vector3& value);
271 * Bakes the value of a Vector3 property relative to the current value
272 * @param[in] id Id of the transform component
273 * @param[in] property The property
274 * @param[in] value The new value
276 void BakeRelativeVector3PropertyValue(TransformId id, TransformManagerProperty property, const Vector3& value);
279 * Bakes the value of a Vector3 property by multiplying the current value and the new value
280 * @param[in] id Id of the transform component
281 * @param[in] property The property
282 * @param[in] value The new value
284 void BakeMultiplyVector3PropertyValue(TransformId id, TransformManagerProperty property, const Vector3& value);
287 * Bakes the value of a 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 BakeVector3PropertyComponentValue(TransformId id, TransformManagerProperty property, float value, uint32_t component);
295 * Bakes the value of the x component of Vector3 property
296 * @param[in] id Id of the transform component
297 * @param[in] property The property
298 * @param[in] value The new value
300 void BakeXVector3PropertyValue(TransformId id, TransformManagerProperty property, float value);
303 * Bakes the value of the y component of Vector3 property
304 * @param[in] id Id of the transform component
305 * @param[in] property The property
306 * @param[in] value The new value
308 void BakeYVector3PropertyValue(TransformId id, TransformManagerProperty property, float value);
311 * Bakes the value of the z component of Vector3 property
312 * @param[in] id Id of the transform component
313 * @param[in] property The property
314 * @param[in] value The new value
316 void BakeZVector3PropertyValue(TransformId id, TransformManagerProperty property, float value);
319 * Get the value of a quaternion property
320 * @param[in] id Id of the transform component
322 Quaternion& GetQuaternionPropertyValue(TransformId id);
325 * Get the value of a quaternion property
326 * @param[in] id Id of the transform component
328 const Quaternion& GetQuaternionPropertyValue(TransformId id) const;
331 * Set the value of a quaternion property
332 * @param[in] id Id of the transform component
333 * @param[in] q The new value
335 void SetQuaternionPropertyValue(TransformId id, const Quaternion& q);
338 * Bake the value of a quaternion property
339 * @param[in] id Id of the transform component
340 * @param[in] q The new value
342 void BakeQuaternionPropertyValue(TransformId id, const Quaternion& q);
345 * Bake the value of a quaternion property relative to its current value
346 * @param[in] id Id of the transform component
347 * @param[in] q The new value
349 void BakeRelativeQuaternionPropertyValue(TransformId id, const Quaternion& q);
352 * Get the bounding sphere, in world coordinates, of a given component
353 * @param[in] id Id of the transform component
354 * @return The world space bounding sphere of the component
356 const Vector4& GetBoundingSphere(TransformId id) const;
359 * Get the world matrix and size of a given component
360 * @param[in] id Id of the transform component
361 * @param[out] The world matrix of the component
362 * @param[out] size size of the component
364 void GetWorldMatrixAndSize(TransformId id, Matrix& worldMatrix, Vector3& size) const;
367 * @brief Sets the boolean which states whether the position should use the anchor-point on the given transform component.
368 * @param[in] id Id of the transform component
369 * @param[in] value True if the position should use the anchor-point
371 void SetPositionUsesAnchorPoint(TransformId id, bool value);
374 //Helper struct to order components
377 bool operator<(const SOrderItem& item) const
379 return level < item.level;
387 * Swaps two components in the vectors
388 * @param[in] i Index of a component
389 * @param[in] j Index of a component
391 void SwapComponents(uint32_t i, uint32_t j);
394 * Reorders components in hierarchical order so update can iterate sequentially
395 * updating the world transforms
397 void ReorderComponents();
399 uint32_t mComponentCount; ///< Total number of components
400 FreeList mIds; ///< FreeList of Ids
401 Vector<TransformComponentAnimatable> mTxComponentAnimatable; ///< Animatable part of the components
402 Vector<TransformComponentStatic> mTxComponentStatic; ///< Static part of the components
403 Vector<uint32_t> mInheritanceMode; ///< Inheritance mode of the components
404 Vector<TransformId> mComponentId; ///< Ids of the components
405 Vector<Vector3> mSize; ///< Size of the components
406 Vector<TransformId> mParent; ///< Parent of the components
407 Vector<Matrix> mWorld; ///< Local to world transform of the components
408 Vector<Matrix> mLocal; ///< Local to parent space transform of the components
409 Vector<Vector4> mBoundingSpheres; ///< Bounding spheres. xyz is the center and w is the radius
410 Vector<TransformComponentAnimatable> mTxComponentAnimatableBaseValue; ///< Base values for the animatable part of the components
411 Vector<Vector3> mSizeBase; ///< Base value for the size of the components
412 Vector<bool> mComponentDirty; ///< 1u if some of the parts of the component has changed in this frame, 0 otherwise
413 Vector<bool> mLocalMatrixDirty; ///< 1u if the local matrix has been updated in this frame, 0 otherwise
414 Vector<SOrderItem> mOrderedComponents; ///< Used to reorder components when hierarchy changes
415 bool mReorder; ///< Flag to determine if the components have to reordered in the next Update
418 } //namespace SceneGraph
420 } //namespace Internal
424 #endif // DALI_INTERNAL_TRANSFORM_MANAGER_H