SceneGraph.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #pragma once
  2. /*
  3. * Copyright (c) Contributors to the Open 3D Engine Project.
  4. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #include <stdint.h>
  10. #include <AzCore/std/hash.h>
  11. #include <AzCore/std/containers/vector.h>
  12. #include <AzCore/std/containers/unordered_map.h>
  13. #include <AzCore/std/smart_ptr/shared_ptr.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <AzCore/RTTI/TypeInfo.h>
  16. #include <AzCore/RTTI/ReflectContext.h>
  17. #include <SceneAPI/SceneCore/SceneCoreConfiguration.h>
  18. #include <SceneAPI/SceneCore/Containers/Views/View.h>
  19. #include <SceneAPI/SceneCore/Containers/Views/ConvertIterator.h>
  20. namespace AZ
  21. {
  22. namespace SceneAPI
  23. {
  24. namespace DataTypes
  25. {
  26. class IGraphObject;
  27. }
  28. namespace SceneBuilder
  29. {
  30. struct QueueNode;
  31. struct ImportContext;
  32. }
  33. namespace Containers
  34. {
  35. // The SceneGraph allows for hierarchical storage of arbitrary data in a tree like fashion.
  36. // The internal storage is based on child-sibling representation: https://en.wikipedia.org/wiki/Left-child_right-sibling_binary_tree
  37. //
  38. // The SceneGraph uses a naming convention where the name of a node is concatenated with it's parent and separate by a dot ('.')
  39. // such that child node B of parent node A will have the name "A.B". Note that by default the scene graph has a nameless root node.
  40. //
  41. // There are 2 approaches to manipulating the SceneGraph. The first is direct manipulation by using NodeIndex. This supports
  42. // navigating the graph, manipulating the graph hierarchy and manipulating the stored values. This approach is the most
  43. // flexible and allows for the most control, but requires a lot of code support. This approach is best use while constructing
  44. // the graph for a scene.
  45. // The second option is by combining views. This allows for navigating the graph and manipulating stored values, but not
  46. // for manipulating the graph hierarchy. Views use iterators and can therefore be used in most STL(-like) algorithms as well
  47. // as ranged based loops, but have restrictions in what they can do. This approach is best used while inspecting or exporting
  48. // the graph for a scene.
  49. class SceneGraph
  50. {
  51. public:
  52. // Index for a node.
  53. // Instead of using just a plain int, this is it's own type to reduce the
  54. // risk of invalid indices being passed.
  55. class NodeIndex
  56. {
  57. friend class SceneGraph;
  58. friend struct SceneBuilder::QueueNode;
  59. friend struct SceneBuilder::ImportContext;
  60. public:
  61. // Type needs to be able to store an index in a NodeHeader (currently 21-bits).
  62. using IndexType = uint32_t;
  63. NodeIndex() = default;
  64. NodeIndex(const NodeIndex& rhs) = default;
  65. NodeIndex& operator=(const NodeIndex& rhs) = default;
  66. // Returns whether or not the node index is valid.
  67. // Note that this function reports explicit invalid nodes, such as the invalid node that's returned when a name can't be found, and
  68. // whether it's valid before any changes are made. If changes to the SceneGraph are made it will not be able to detect that a
  69. // previously valid index has become invalid.
  70. inline bool IsValid() const;
  71. inline bool operator==(NodeIndex rhs) const;
  72. inline bool operator!=(NodeIndex rhs) const;
  73. inline IndexType AsNumber() const;
  74. inline s32 Distance(NodeIndex rhs) const;
  75. private:
  76. static const IndexType INVALID_INDEX = static_cast<IndexType>(-1);
  77. inline explicit NodeIndex(IndexType value);
  78. IndexType m_value = NodeIndex::INVALID_INDEX;
  79. };
  80. // NodeHeader contains the relationship a node has with its surrounding nodes and additional information about a node.
  81. // Note that this is always passed by value, so direct access to the member variables doesn't risk unwanted changes.
  82. struct NodeHeader
  83. {
  84. // Number of bits used for storing an index into the stored data. Currently 21 bits, which will support about 2 million nodes.
  85. static const uint32_t INDEX_BIT_COUNT = 21;
  86. static const uint64_t INVALID_INDEX = (1 << INDEX_BIT_COUNT) - 1; // Largest possible value for the index bit count.
  87. // End point nodes are nodes that are not allowed to have children.
  88. uint64_t m_isEndPoint : 1;
  89. uint64_t m_parentIndex: INDEX_BIT_COUNT;
  90. uint64_t m_siblingIndex: INDEX_BIT_COUNT;
  91. uint64_t m_childIndex: INDEX_BIT_COUNT;
  92. inline bool HasParent() const;
  93. inline bool HasSibling() const;
  94. inline bool HasChild() const;
  95. inline bool IsEndPoint() const;
  96. inline NodeIndex GetParentIndex() const;
  97. inline NodeIndex GetSiblingIndex() const;
  98. inline NodeIndex GetChildIndex() const;
  99. inline NodeHeader();
  100. NodeHeader(const NodeHeader& rhs) = default;
  101. NodeHeader& operator=(const NodeHeader& rhs) = default;
  102. };
  103. class Name
  104. {
  105. friend class SceneGraph;
  106. public:
  107. inline Name();
  108. Name(const Name& rhs) = default;
  109. inline Name(Name&& rhs);
  110. inline Name(AZStd::string&& pathName, size_t nameOffset);
  111. Name& operator=(const Name& rhs) = default;
  112. inline Name& operator=(Name&& rhs);
  113. inline bool operator==(const Name& rhs) const;
  114. inline bool operator!=(const Name& rhs) const;
  115. // Returns the full unique path for the SceneGraph node.
  116. inline const char* GetPath() const;
  117. // Returns the name for the SceneGraph Node.
  118. inline const char* GetName() const;
  119. inline size_t GetPathLength() const;
  120. inline size_t GetNameLength() const;
  121. private:
  122. AZStd::string m_path;
  123. size_t m_nameOffset;
  124. };
  125. inline static AZStd::shared_ptr<const DataTypes::IGraphObject> ConstDataConverter(const AZStd::shared_ptr<DataTypes::IGraphObject>& value);
  126. using StringHasher = AZStd::hash<AZStd::string>;
  127. using StringHash = size_t;
  128. using NameLookup = AZStd::unordered_multimap<StringHash, uint32_t>;
  129. using HierarchyStorageType = NodeHeader;
  130. using HierarchyStorage = AZStd::vector<HierarchyStorageType>;
  131. using HierarchyStorageConstIterator = HierarchyStorage::const_iterator;
  132. using HierarchyStorageConstData = Views::View<HierarchyStorageConstIterator>;
  133. using NameStorageType = Name;
  134. using NameStorage = AZStd::vector<NameStorageType>;
  135. using NameStorageConstData = Views::View<NameStorage::const_iterator>;
  136. using ContentStorageType = AZStd::shared_ptr<DataTypes::IGraphObject>;
  137. using ContentStorage = AZStd::vector<ContentStorageType>;
  138. using ContentStorageData = Views::View<ContentStorage::const_iterator>;
  139. using ContentStorageConstDataIteratorWrapper = Views::ConvertIterator<ContentStorage::const_iterator,
  140. decltype(ConstDataConverter(nullptr))>;
  141. using ContentStorageConstData = Views::View<ContentStorageConstDataIteratorWrapper>;
  142. SCENE_CORE_API SceneGraph();
  143. inline NodeIndex GetRoot() const;
  144. SCENE_CORE_API NodeIndex Find(const char* path) const;
  145. SCENE_CORE_API NodeIndex Find(NodeIndex root, const char* name) const;
  146. inline NodeIndex Find(const Name& name);
  147. inline NodeIndex Find(const AZStd::string& path) const;
  148. inline NodeIndex Find(NodeIndex root, const AZStd::string& name) const;
  149. inline bool HasNodeContent(NodeIndex node) const;
  150. inline bool HasNodeSibling(NodeIndex node) const;
  151. inline bool HasNodeChild(NodeIndex node) const;
  152. inline bool HasNodeParent(NodeIndex node) const;
  153. inline bool IsNodeEndPoint(NodeIndex node) const;
  154. SCENE_CORE_API const Name& GetNodeName(NodeIndex node) const;
  155. inline AZStd::shared_ptr<DataTypes::IGraphObject> GetNodeContent(NodeIndex node);
  156. inline AZStd::shared_ptr<const DataTypes::IGraphObject> GetNodeContent(NodeIndex node) const;
  157. inline NodeIndex GetNodeParent(NodeIndex node) const;
  158. inline NodeIndex GetNodeParent(NodeHeader node) const;
  159. inline NodeIndex GetNodeSibling(NodeIndex node) const;
  160. inline NodeIndex GetNodeSibling(NodeHeader node) const;
  161. inline NodeIndex GetNodeChild(NodeIndex node) const;
  162. inline NodeIndex GetNodeChild(NodeHeader node) const;
  163. inline size_t GetNodeCount() const;
  164. // Used when switching from index based navigation to iterator based.
  165. inline HierarchyStorageConstData::iterator ConvertToHierarchyIterator(NodeIndex node) const;
  166. inline NameStorageConstData::iterator ConvertToNameIterator(NodeIndex node) const;
  167. inline ContentStorageData::iterator ConvertToStorageIterator(NodeIndex node);
  168. inline ContentStorageConstData::iterator ConvertToStorageIterator(NodeIndex node) const;
  169. // Used when switching from iterator based navigation to index based.
  170. // Note that any changes made to the SceneGraph using the node index will invalidate
  171. // the original iterator.
  172. inline NodeIndex ConvertToNodeIndex(HierarchyStorageConstData::iterator iterator) const;
  173. inline NodeIndex ConvertToNodeIndex(NameStorageConstData::iterator iterator) const;
  174. inline NodeIndex ConvertToNodeIndex(ContentStorageData::iterator iterator) const;
  175. inline NodeIndex ConvertToNodeIndex(ContentStorageConstData::iterator iterator) const;
  176. // Adds a child node to the given parent. If the parent already had a child, AddChild will search the sibling
  177. // chain for an available spot.
  178. SCENE_CORE_API NodeIndex AddChild(NodeIndex parent, const char* name);
  179. SCENE_CORE_API NodeIndex AddChild(NodeIndex parent, const char* name, const AZStd::shared_ptr<DataTypes::IGraphObject>& content);
  180. SCENE_CORE_API NodeIndex AddChild(NodeIndex parent, const char* name, AZStd::shared_ptr<DataTypes::IGraphObject>&& content);
  181. // Adds a sibling to given sibling. If the given sibling already has a sibling, the sibling chain is searched
  182. // for an available spot. If the parent node is known AddChild can be used to achieve the same effect,
  183. // however if index of the (last) added node is available this function can be used to reduce or skip
  184. // the search within the sibling chain. This function can therefore be used as an optimization for AddChild,
  185. // when more than 1 child is being added.
  186. SCENE_CORE_API NodeIndex AddSibling(NodeIndex sibling, const char* name);
  187. SCENE_CORE_API NodeIndex AddSibling(NodeIndex sibling, const char* name, const AZStd::shared_ptr<DataTypes::IGraphObject>& content);
  188. SCENE_CORE_API NodeIndex AddSibling(NodeIndex sibling, const char* name, AZStd::shared_ptr<DataTypes::IGraphObject>&& content);
  189. SCENE_CORE_API bool SetContent(NodeIndex node, const AZStd::shared_ptr<DataTypes::IGraphObject>& content);
  190. SCENE_CORE_API bool SetContent(NodeIndex node, AZStd::shared_ptr<DataTypes::IGraphObject>&& content);
  191. // Marks a function to no longer accept child nodes.
  192. SCENE_CORE_API bool MakeEndPoint(NodeIndex node);
  193. inline HierarchyStorageConstData GetHierarchyStorage() const;
  194. inline NameStorageConstData GetNameStorage() const;
  195. inline ContentStorageData GetContentStorage();
  196. inline ContentStorageConstData GetContentStorage() const;
  197. // Clears all data stored inside and reads the default root node.
  198. SCENE_CORE_API void Clear();
  199. // Checks if the given name can be used as a valid name for a node. This only checks the name validity, not if it's
  200. // already in use. Use Find(...) to check if a name is already in use.
  201. SCENE_CORE_API static bool IsValidName(const char* name);
  202. // Checks if the given name can be used as a valid name for a node. This only checks the name validity, not if it's
  203. // already in use. Use Find(...) to check if a name is already in use.
  204. inline static bool IsValidName(const AZStd::string& name);
  205. SCENE_CORE_API static char GetNodeSeperationCharacter();
  206. static void Reflect(AZ::ReflectContext* context);
  207. private:
  208. // Adds a child node to the given parent. AppendChild assumes that checks have already be done to guarantee the given parent
  209. // doesn't already have a child.
  210. NodeIndex::IndexType AppendChild(NodeIndex::IndexType parent, const char* name, AZStd::shared_ptr<DataTypes::IGraphObject>&& content);
  211. // Add a sibling after the given sibling. AppendSibling assumes that the correct insertion point was found before calling
  212. // and the given sibling is the last in line with no siblings following.
  213. NodeIndex::IndexType AppendSibling(NodeIndex::IndexType sibling, const char* name, AZStd::shared_ptr<DataTypes::IGraphObject>&& content);
  214. // Appends a new node to the graph and configures its heritage according to the given parent. Connections to the new node
  215. // as identified by the returned index are assumed to be setup by either the calling function such as AppendChild or
  216. // AppendSibling.
  217. NodeIndex::IndexType AppendNode(NodeIndex::IndexType parentIndex, const char* name, AZStd::shared_ptr<DataTypes::IGraphObject>&& content);
  218. NameLookup::const_iterator FindNameLookupIterator(const char* name) const;
  219. NameLookup::const_iterator FindNameLookupIterator(StringHash hash, const char* name) const;
  220. AZStd::string CombineName(const char* path, const char* name) const;
  221. void AddDefaultRoot();
  222. static const char s_nodeSeperationCharacter = '.';
  223. NameLookup m_nameLookup;
  224. HierarchyStorage m_hierarchy;
  225. NameStorage m_names;
  226. ContentStorage m_content;
  227. };
  228. } // Containers
  229. } // SceneAPI
  230. AZ_TYPE_INFO_SPECIALIZE(AZ::SceneAPI::Containers::SceneGraph, "{CAC6556D-D5FE-4D0E-BCCD-8940357C1D35}");
  231. AZ_TYPE_INFO_SPECIALIZE(AZ::SceneAPI::Containers::SceneGraph::NodeHeader, "{888C32BB-FEE3-4FA1-ADA4-09A58B03562A}");
  232. AZ_TYPE_INFO_SPECIALIZE(AZ::SceneAPI::Containers::SceneGraph::NodeIndex, "{4AD18037-E629-480D-8165-997A137327FD}");
  233. AZ_TYPE_INFO_SPECIALIZE(AZ::SceneAPI::Containers::SceneGraph::Name, "{4077AC3C-B301-4F5A-BEA7-54D6511AEC2E}");
  234. } // AZ
  235. #include <SceneAPI/SceneCore/Containers/SceneGraph.inl>