SceneGraph.h 16 KB

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