SceneGraph.inl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * 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 <AzCore/std/iterator.h>
  9. #include <AzCore/Casting/numeric_cast.h>
  10. namespace AZ
  11. {
  12. namespace SceneAPI
  13. {
  14. namespace Containers
  15. {
  16. //
  17. // NodeIndex
  18. //
  19. bool SceneGraph::NodeIndex::IsValid() const
  20. {
  21. return m_value != NodeIndex::INVALID_INDEX;
  22. }
  23. bool SceneGraph::NodeIndex::operator==(NodeIndex rhs) const
  24. {
  25. return m_value == rhs.m_value;
  26. }
  27. bool SceneGraph::NodeIndex::operator!=(NodeIndex rhs) const
  28. {
  29. return m_value != rhs.m_value;
  30. }
  31. SceneGraph::NodeIndex::IndexType SceneGraph::NodeIndex::AsNumber() const
  32. {
  33. return m_value;
  34. }
  35. s32 SceneGraph::NodeIndex::Distance(NodeIndex rhs) const
  36. {
  37. return static_cast<s32>(rhs.m_value) - static_cast<s32>(m_value);
  38. }
  39. SceneGraph::NodeIndex::NodeIndex(IndexType value)
  40. : m_value(value)
  41. {
  42. }
  43. //
  44. // SceneGraph NodeHeader
  45. //
  46. SceneGraph::NodeHeader::NodeHeader()
  47. : m_isEndPoint(0)
  48. , m_parentIndex(NodeHeader::INVALID_INDEX)
  49. , m_siblingIndex(NodeHeader::INVALID_INDEX)
  50. , m_childIndex(NodeHeader::INVALID_INDEX)
  51. {
  52. }
  53. bool SceneGraph::NodeHeader::HasParent() const
  54. {
  55. return m_parentIndex != NodeHeader::INVALID_INDEX;
  56. }
  57. bool SceneGraph::NodeHeader::HasSibling() const
  58. {
  59. return m_siblingIndex != NodeHeader::INVALID_INDEX;
  60. }
  61. bool SceneGraph::NodeHeader::HasChild() const
  62. {
  63. return m_childIndex != NodeHeader::INVALID_INDEX;
  64. }
  65. bool SceneGraph::NodeHeader::IsEndPoint() const
  66. {
  67. return m_isEndPoint;
  68. }
  69. SceneGraph::NodeIndex SceneGraph::NodeHeader::GetParentIndex() const
  70. {
  71. return NodeIndex(m_parentIndex);
  72. }
  73. SceneGraph::NodeIndex SceneGraph::NodeHeader::GetSiblingIndex() const
  74. {
  75. return NodeIndex(m_siblingIndex);
  76. }
  77. SceneGraph::NodeIndex SceneGraph::NodeHeader::GetChildIndex() const
  78. {
  79. return NodeIndex(m_childIndex);
  80. }
  81. //
  82. // Name
  83. //
  84. SceneGraph::Name::Name()
  85. : m_nameOffset(0)
  86. {
  87. }
  88. SceneGraph::Name::Name(Name&& rhs)
  89. : m_path(AZStd::move(rhs.m_path))
  90. , m_nameOffset(rhs.m_nameOffset)
  91. {
  92. }
  93. SceneGraph::Name::Name(AZStd::string&& pathName, size_t nameOffset)
  94. : m_path(AZStd::move(pathName))
  95. , m_nameOffset(nameOffset)
  96. {
  97. if (m_nameOffset >= m_path.size())
  98. {
  99. m_nameOffset = m_path.size();
  100. }
  101. }
  102. SceneGraph::Name& SceneGraph::Name::operator=(Name&& rhs)
  103. {
  104. m_path = AZStd::move(rhs.m_path);
  105. m_nameOffset = rhs.m_nameOffset;
  106. return *this;
  107. }
  108. bool SceneGraph::Name::operator==(const Name& rhs) const
  109. {
  110. return m_nameOffset == rhs.m_nameOffset && m_path == rhs.m_path;
  111. }
  112. bool SceneGraph::Name::operator!=(const Name& rhs) const
  113. {
  114. return m_nameOffset != rhs.m_nameOffset || m_path != rhs.m_path;
  115. }
  116. const char* SceneGraph::Name::GetPath() const
  117. {
  118. return m_path.c_str();
  119. }
  120. const char* SceneGraph::Name::GetName() const
  121. {
  122. AZ_Assert(m_nameOffset <= m_path.length(), "Offset to name in SceneGraph path is invalid.");
  123. return m_path.c_str() + m_nameOffset;
  124. }
  125. size_t SceneGraph::Name::GetPathLength() const
  126. {
  127. return m_path.length();
  128. }
  129. size_t SceneGraph::Name::GetNameLength() const
  130. {
  131. AZ_Assert(m_nameOffset <= m_path.length(), "Offset to name in SceneGraph path is invalid.");
  132. return m_path.length() - m_nameOffset;
  133. }
  134. //
  135. // SceneGraph
  136. //
  137. AZStd::shared_ptr<const DataTypes::IGraphObject> SceneGraph::ConstDataConverter(const AZStd::shared_ptr<DataTypes::IGraphObject>& value)
  138. {
  139. return AZStd::shared_ptr<const DataTypes::IGraphObject>(value);
  140. }
  141. SceneGraph::NodeIndex SceneGraph::GetRoot() const
  142. {
  143. return NodeIndex(0);
  144. }
  145. SceneGraph::NodeIndex SceneGraph::Find(const AZStd::string& path) const
  146. {
  147. return Find(path.c_str());
  148. }
  149. SceneGraph::NodeIndex SceneGraph::Find(const Name& name)
  150. {
  151. return Find(name.GetPath());
  152. }
  153. SceneGraph::NodeIndex SceneGraph::Find(NodeIndex root, const AZStd::string& name) const
  154. {
  155. return Find(root, name.c_str());
  156. }
  157. bool SceneGraph::HasNodeContent(NodeIndex node) const
  158. {
  159. return node.m_value < m_content.size() ? m_content[node.m_value] != nullptr : false;
  160. }
  161. bool SceneGraph::HasNodeSibling(NodeIndex node) const
  162. {
  163. return node.m_value < m_hierarchy.size() ? m_hierarchy[node.m_value].HasSibling() : false;
  164. }
  165. bool SceneGraph::HasNodeChild(NodeIndex node) const
  166. {
  167. return node.m_value < m_hierarchy.size() ? m_hierarchy[node.m_value].HasChild() : false;
  168. }
  169. bool SceneGraph::HasNodeParent(NodeIndex node) const
  170. {
  171. return node.m_value < m_hierarchy.size() ? m_hierarchy[node.m_value].HasParent() : false;
  172. }
  173. bool SceneGraph::IsNodeEndPoint(NodeIndex node) const
  174. {
  175. return node.m_value < m_hierarchy.size() ? m_hierarchy[node.m_value].IsEndPoint() : true;
  176. }
  177. AZStd::shared_ptr<DataTypes::IGraphObject> SceneGraph::GetNodeContent(NodeIndex node)
  178. {
  179. return node.m_value < m_content.size() ? m_content[node.m_value] : nullptr;
  180. }
  181. AZStd::shared_ptr<const DataTypes::IGraphObject> SceneGraph::GetNodeContent(NodeIndex node) const
  182. {
  183. return node.m_value < m_content.size() ? m_content[node.m_value] : nullptr;
  184. }
  185. SceneGraph::NodeIndex SceneGraph::GetNodeParent(NodeIndex node) const
  186. {
  187. return node.m_value < m_hierarchy.size() ? GetNodeParent(m_hierarchy[node.m_value]) : NodeIndex();
  188. }
  189. SceneGraph::NodeIndex SceneGraph::GetNodeParent(NodeHeader node) const
  190. {
  191. return NodeIndex(node.m_parentIndex != NodeHeader::INVALID_INDEX ? static_cast<uint32_t>(node.m_parentIndex) : NodeIndex::INVALID_INDEX);
  192. }
  193. SceneGraph::NodeIndex SceneGraph::GetNodeSibling(NodeIndex node) const
  194. {
  195. return node.m_value < m_hierarchy.size() ? GetNodeSibling(m_hierarchy[node.m_value]) : NodeIndex();
  196. }
  197. SceneGraph::NodeIndex SceneGraph::GetNodeSibling(NodeHeader node) const
  198. {
  199. return NodeIndex(node.m_siblingIndex != NodeHeader::INVALID_INDEX ? static_cast<uint32_t>(node.m_siblingIndex) : NodeIndex::INVALID_INDEX);
  200. }
  201. SceneGraph::NodeIndex SceneGraph::GetNodeChild(NodeIndex node) const
  202. {
  203. return node.m_value < m_hierarchy.size() ? GetNodeChild(m_hierarchy[node.m_value]) : NodeIndex();
  204. }
  205. SceneGraph::NodeIndex SceneGraph::GetNodeChild(NodeHeader node) const
  206. {
  207. return NodeIndex(node.m_childIndex != NodeHeader::INVALID_INDEX ? static_cast<uint32_t>(node.m_childIndex) : NodeIndex::INVALID_INDEX);
  208. }
  209. size_t SceneGraph::GetNodeCount() const
  210. {
  211. return m_hierarchy.size();
  212. }
  213. SceneGraph::HierarchyStorageConstData::iterator SceneGraph::ConvertToHierarchyIterator(NodeIndex node) const
  214. {
  215. return node.m_value < m_hierarchy.size() ? m_hierarchy.cbegin() + node.m_value : m_hierarchy.cend();
  216. }
  217. SceneGraph::NameStorageConstData::iterator SceneGraph::ConvertToNameIterator(NodeIndex node) const
  218. {
  219. return node.m_value < m_names.size() ? m_names.cbegin() + node.m_value : m_names.cend();
  220. }
  221. SceneGraph::ContentStorageData::iterator SceneGraph::ConvertToStorageIterator(NodeIndex node)
  222. {
  223. return node.m_value < m_content.size() ? m_content.cbegin() + node.m_value : m_content.cend();
  224. }
  225. SceneGraph::ContentStorageConstData::iterator SceneGraph::ConvertToStorageIterator(NodeIndex node) const
  226. {
  227. return node.m_value < m_content.size() ?
  228. Views::MakeConvertIterator(m_content.cbegin() + node.m_value, ConstDataConverter) :
  229. Views::MakeConvertIterator(m_content.cend(), ConstDataConverter);
  230. }
  231. SceneGraph::NodeIndex SceneGraph::ConvertToNodeIndex(HierarchyStorageConstData::iterator iterator) const
  232. {
  233. return NodeIndex(iterator != m_hierarchy.cend() ? static_cast<uint32_t>(std::distance(m_hierarchy.cbegin(), iterator)) : NodeIndex::INVALID_INDEX);
  234. }
  235. SceneGraph::NodeIndex SceneGraph::ConvertToNodeIndex(NameStorageConstData::iterator iterator) const
  236. {
  237. return NodeIndex(iterator != m_names.cend() ? static_cast<uint32_t>(std::distance(m_names.cbegin(), iterator)) : NodeIndex::INVALID_INDEX);
  238. }
  239. SceneGraph::NodeIndex SceneGraph::ConvertToNodeIndex(ContentStorageData::iterator iterator) const
  240. {
  241. return NodeIndex(iterator != m_content.end() ? static_cast<uint32_t>(std::distance(m_content.begin(), iterator)) : NodeIndex::INVALID_INDEX);
  242. }
  243. SceneGraph::NodeIndex SceneGraph::ConvertToNodeIndex(ContentStorageConstData::iterator iterator) const
  244. {
  245. return NodeIndex(
  246. iterator.GetBaseIterator() != m_content.cend() ?
  247. aznumeric_caster(AZStd::distance(m_content.cbegin(), iterator.GetBaseIterator())) :
  248. NodeIndex::INVALID_INDEX);
  249. }
  250. SceneGraph::HierarchyStorageConstData SceneGraph::GetHierarchyStorage() const
  251. {
  252. return HierarchyStorageConstData(m_hierarchy.begin(), m_hierarchy.end());
  253. }
  254. SceneGraph::NameStorageConstData SceneGraph::GetNameStorage() const
  255. {
  256. return NameStorageConstData(m_names.begin(), m_names.end());
  257. }
  258. SceneGraph::ContentStorageData SceneGraph::GetContentStorage()
  259. {
  260. return ContentStorageData(m_content.begin(), m_content.end());
  261. }
  262. SceneGraph::ContentStorageConstData SceneGraph::GetContentStorage() const
  263. {
  264. return ContentStorageConstData(
  265. Views::MakeConvertIterator(m_content.cbegin(), ConstDataConverter),
  266. Views::MakeConvertIterator(m_content.cend(), ConstDataConverter));
  267. }
  268. bool SceneGraph::IsValidName(const AZStd::string& name)
  269. {
  270. return name.size() > 0 ? IsValidName(name.c_str()) : false;
  271. }
  272. } // Containers
  273. } // SceneAPI
  274. } // AZ