SceneGraphSelector.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <SceneAPI/SceneCore/Containers/Utilities/SceneGraphUtilities.h>
  9. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  10. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  11. #include <SceneAPI/SceneCore/Utilities/SceneGraphSelector.h>
  12. #include <AzCore/std/containers/set.h>
  13. #include <AzCore/Math/Transform.h>
  14. #include <SceneAPI/SceneCore/DataTypes/ManifestBase/ISceneNodeSelectionList.h>
  15. #include <SceneAPI/SceneCore/DataTypes/GraphData/ICustomPropertyData.h>
  16. #include <SceneAPI/SceneCore/DataTypes/GraphData/IMeshData.h>
  17. #include <SceneAPI/SceneCore/DataTypes/Groups/ISceneNodeGroup.h>
  18. namespace AZ
  19. {
  20. namespace SceneAPI
  21. {
  22. namespace Utilities
  23. {
  24. bool SceneGraphSelector::IsTreeViewType(const Containers::SceneGraph& graph, Containers::SceneGraph::NodeIndex& index)
  25. {
  26. if (index == graph.GetRoot())
  27. {
  28. return false;
  29. }
  30. return !graph.IsNodeEndPoint(index);
  31. }
  32. bool SceneGraphSelector::IsMesh(const Containers::SceneGraph& graph, Containers::SceneGraph::NodeIndex& index)
  33. {
  34. AZStd::shared_ptr<const DataTypes::IGraphObject> object = graph.GetNodeContent(index);
  35. return SceneGraphSelector::IsMeshObject(object);
  36. }
  37. bool SceneGraphSelector::IsMeshObject(const AZStd::shared_ptr<const DataTypes::IGraphObject>& object)
  38. {
  39. return object && object->RTTI_IsTypeOf(DataTypes::IMeshData::TYPEINFO_Uuid());
  40. }
  41. Containers::SceneGraph::NodeIndex SceneGraphSelector::RemapToOptimizedMesh(
  42. const Containers::SceneGraph& graph, const Containers::SceneGraph::NodeIndex& unoptimizedMeshNodeIndex)
  43. {
  44. return RemapNodeIndex(graph, unoptimizedMeshNodeIndex, SceneAPI::Utilities::OptimizedMeshPropertyMapKey);
  45. }
  46. Containers::SceneGraph::NodeIndex SceneGraphSelector::RemapToOriginalUnoptimizedMesh(
  47. const Containers::SceneGraph& graph, const Containers::SceneGraph::NodeIndex& optimizedMeshNodeIndex)
  48. {
  49. return RemapNodeIndex(graph, optimizedMeshNodeIndex, SceneAPI::Utilities::OriginalUnoptimizedMeshPropertyMapKey);
  50. }
  51. Containers::SceneGraph::NodeIndex SceneGraphSelector::RemapNodeIndex(
  52. const Containers::SceneGraph& graph,
  53. const Containers::SceneGraph::NodeIndex& index,
  54. const AZStd::string_view customPropertyKey)
  55. {
  56. // Search the immediate children for an ICustomPropertyData node to lookup the optimized mesh index
  57. const Containers::SceneGraph::NodeIndex customPropertyIndex =
  58. GetImmediateChildOfType(graph, index, azrtti_typeid<AZ::SceneAPI::DataTypes::ICustomPropertyData>());
  59. if (customPropertyIndex.IsValid())
  60. {
  61. const DataTypes::ICustomPropertyData* customPropertyDataNode =
  62. azrtti_cast<const DataTypes::ICustomPropertyData*>(graph.GetNodeContent(customPropertyIndex).get());
  63. // Now look up the optimized index
  64. const auto& propertyMap = customPropertyDataNode->GetPropertyMap();
  65. const auto iter = propertyMap.find(customPropertyKey);
  66. if (iter != propertyMap.end())
  67. {
  68. const AZStd::any& remappedNodeIndex = iter->second;
  69. if (!remappedNodeIndex.empty() && remappedNodeIndex.is<Containers::SceneGraph::NodeIndex>())
  70. {
  71. return AZStd::any_cast<Containers::SceneGraph::NodeIndex>(remappedNodeIndex);
  72. }
  73. }
  74. }
  75. // Return the original index if there is no optimized mesh node
  76. return index;
  77. }
  78. AZStd::string SceneGraphSelector::GenerateOptimizedMeshNodeName(
  79. const Containers::SceneGraph& graph,
  80. const Containers::SceneGraph::NodeIndex& unoptimizedMeshNodeIndex,
  81. const DataTypes::ISceneNodeGroup& sceneNodeGroup)
  82. {
  83. const auto& nodeName = graph.GetNodeName(unoptimizedMeshNodeIndex);
  84. return AZStd::string::format(
  85. "%s_%s%s", nodeName.GetName(), sceneNodeGroup.GetName().c_str(), SceneAPI::Utilities::OptimizedMeshSuffix.data());
  86. }
  87. AZStd::vector<AZStd::string> SceneGraphSelector::GenerateTargetNodes(
  88. const Containers::SceneGraph& graph,
  89. const DataTypes::ISceneNodeSelectionList& list,
  90. NodeFilterFunction nodeFilter,
  91. NodeRemapFunction nodeRemap)
  92. {
  93. AZStd::vector<AZStd::string> targetNodes;
  94. AZStd::unordered_set<AZStd::string> selectedNodesSet;
  95. AZStd::unordered_set<AZStd::string> unselectedNodesSet;
  96. CopySelectionToSet(selectedNodesSet, unselectedNodesSet, list);
  97. CorrectRootNode(graph, selectedNodesSet, unselectedNodesSet);
  98. const auto nodeIterator = graph.ConvertToHierarchyIterator(graph.GetRoot());
  99. const auto view = Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(graph, nodeIterator, graph.GetContentStorage().cbegin(), true);
  100. if (view.begin() == view.end())
  101. {
  102. return targetNodes;
  103. }
  104. for (auto it = ++view.begin(); it != view.end(); ++it)
  105. {
  106. Containers::SceneGraph::NodeIndex index = graph.ConvertToNodeIndex(it.GetHierarchyIterator());
  107. AZStd::string currentNodeName = graph.GetNodeName(index).GetPath();
  108. if (unselectedNodesSet.contains(currentNodeName))
  109. {
  110. continue;
  111. }
  112. if (selectedNodesSet.contains(currentNodeName))
  113. {
  114. if (nodeFilter(graph, index))
  115. {
  116. Containers::SceneGraph::NodeIndex remappedIndex = nodeRemap(graph, index);
  117. if (remappedIndex == index)
  118. {
  119. targetNodes.emplace_back(AZStd::move(currentNodeName));
  120. }
  121. else
  122. {
  123. const Containers::SceneGraph::Name& nodeName = graph.GetNodeName(remappedIndex);
  124. targetNodes.emplace_back(nodeName.GetPath(), nodeName.GetPathLength());
  125. }
  126. }
  127. continue;
  128. }
  129. Containers::SceneGraph::NodeIndex parentIndex = graph.GetNodeParent(index);
  130. if (parentIndex.IsValid())
  131. {
  132. AZStd::string parentNodeName = graph.GetNodeName(parentIndex).GetPath();
  133. if (unselectedNodesSet.contains(parentNodeName))
  134. {
  135. unselectedNodesSet.insert(currentNodeName);
  136. }
  137. else if (selectedNodesSet.contains(parentNodeName))
  138. {
  139. selectedNodesSet.insert(currentNodeName);
  140. if (nodeFilter(graph, index))
  141. {
  142. Containers::SceneGraph::NodeIndex remappedIndex = nodeRemap(graph, index);
  143. if (remappedIndex == index)
  144. {
  145. targetNodes.emplace_back(AZStd::move(currentNodeName));
  146. }
  147. else
  148. {
  149. const Containers::SceneGraph::Name& nodeName = graph.GetNodeName(remappedIndex);
  150. targetNodes.emplace_back(nodeName.GetPath(), nodeName.GetPathLength());
  151. }
  152. }
  153. }
  154. else
  155. {
  156. AZ_Assert(false, "SceneGraphSelector does a breadth first iteration so parent should be processed, "
  157. "but '%s' wasn't found in either selected or unselected list.", currentNodeName.c_str());
  158. }
  159. }
  160. }
  161. return targetNodes;
  162. }
  163. void SceneGraphSelector::SelectAll(const Containers::SceneGraph& graph, DataTypes::ISceneNodeSelectionList& list)
  164. {
  165. list.ClearSelectedNodes();
  166. list.ClearUnselectedNodes();
  167. auto hierarchyStorage = graph.GetHierarchyStorage();
  168. auto nameStorage = graph.GetNameStorage();
  169. auto range = Containers::Views::MakePairView(hierarchyStorage, nameStorage);
  170. for (auto it : range)
  171. {
  172. if (!it.first.IsEndPoint())
  173. {
  174. list.AddSelectedNode(it.second.GetPath());
  175. }
  176. }
  177. }
  178. void SceneGraphSelector::UnselectAll(const Containers::SceneGraph& graph, DataTypes::ISceneNodeSelectionList& list)
  179. {
  180. list.ClearSelectedNodes();
  181. list.ClearUnselectedNodes();
  182. auto hierarchyStorage = graph.GetHierarchyStorage();
  183. auto nameStorage = graph.GetNameStorage();
  184. auto range = Containers::Views::MakePairView(hierarchyStorage, nameStorage);
  185. for (auto it : range)
  186. {
  187. if (!it.first.IsEndPoint())
  188. {
  189. list.RemoveSelectedNode(it.second.GetPath());
  190. }
  191. }
  192. }
  193. void SceneGraphSelector::UpdateNodeSelection(const Containers::SceneGraph& graph, DataTypes::ISceneNodeSelectionList& list)
  194. {
  195. AZStd::unordered_set<AZStd::string> selectedNodesSet;
  196. AZStd::unordered_set<AZStd::string> unselectedNodesSet;
  197. CopySelectionToSet(selectedNodesSet, unselectedNodesSet, list);
  198. CorrectRootNode(graph, selectedNodesSet, unselectedNodesSet);
  199. list.ClearSelectedNodes();
  200. list.ClearUnselectedNodes();
  201. auto nameStorage = graph.GetNameStorage();
  202. auto contentStorage = graph.GetContentStorage();
  203. auto keyValueView = Containers::Views::MakePairView(nameStorage, contentStorage);
  204. auto nodeIterator = graph.ConvertToHierarchyIterator(graph.GetRoot());
  205. auto view = Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(graph, nodeIterator, keyValueView.cbegin(), true);
  206. if (view.begin() == view.end())
  207. {
  208. return;
  209. }
  210. for (auto it = ++view.begin(); it != view.end(); ++it)
  211. {
  212. Containers::SceneGraph::NodeIndex index = graph.ConvertToNodeIndex(it.GetHierarchyIterator());
  213. AZStd::string currentNodeName = it->first.GetPath();
  214. // Check if item is already registered and if so add it to the appropriate list.
  215. if (unselectedNodesSet.contains(currentNodeName))
  216. {
  217. list.RemoveSelectedNode(AZStd::move(currentNodeName));
  218. continue;
  219. }
  220. if (selectedNodesSet.contains(currentNodeName))
  221. {
  222. list.AddSelectedNode(AZStd::move(currentNodeName));
  223. continue;
  224. }
  225. // If not already registered, look at the parent and determine what container it's in
  226. // and add the unregistered node to that list.
  227. Containers::SceneGraph::NodeIndex parentIndex = graph.GetNodeParent(index);
  228. if (parentIndex.IsValid())
  229. {
  230. AZStd::string parentNodeName = graph.GetNodeName(parentIndex).GetPath();
  231. if (unselectedNodesSet.contains(parentNodeName))
  232. {
  233. unselectedNodesSet.insert(currentNodeName);
  234. list.RemoveSelectedNode(AZStd::move(currentNodeName));
  235. }
  236. else
  237. {
  238. selectedNodesSet.insert(currentNodeName);
  239. list.AddSelectedNode(AZStd::move(currentNodeName));
  240. }
  241. }
  242. }
  243. }
  244. void SceneGraphSelector::UpdateTargetNodes(const Containers::SceneGraph& graph, DataTypes::ISceneNodeSelectionList& list, const AZStd::unordered_set<AZStd::string>& targetNodes,
  245. NodeFilterFunction nodeFilter)
  246. {
  247. list.ClearSelectedNodes();
  248. list.ClearUnselectedNodes();
  249. auto nameStorage = graph.GetNameStorage();
  250. auto contentStorage = graph.GetContentStorage();
  251. auto view = Containers::Views::MakePairView(nameStorage, contentStorage);
  252. if (view.begin() == view.end())
  253. {
  254. return;
  255. }
  256. for (auto it = ++view.begin(); it != view.end(); ++it)
  257. {
  258. Containers::SceneGraph::NodeIndex index = graph.ConvertToNodeIndex(it.GetSecondIterator());
  259. AZStd::string currentNodeName = it->first.GetPath();
  260. if (nodeFilter(graph, index))
  261. {
  262. if (targetNodes.contains(currentNodeName))
  263. {
  264. list.AddSelectedNode(currentNodeName);
  265. }
  266. else
  267. {
  268. list.RemoveSelectedNode(currentNodeName);
  269. }
  270. }
  271. }
  272. }
  273. void SceneGraphSelector::CopySelectionToSet(
  274. AZStd::unordered_set<AZStd::string>& selected, AZStd::unordered_set<AZStd::string>& unselected,
  275. const DataTypes::ISceneNodeSelectionList& list)
  276. {
  277. list.EnumerateSelectedNodes([&selected](const AZStd::string& name)
  278. {
  279. selected.insert(name);
  280. return true;
  281. });
  282. list.EnumerateUnselectedNodes([&unselected](const AZStd::string& name)
  283. {
  284. unselected.insert(name);
  285. return true;
  286. });
  287. }
  288. void SceneGraphSelector::CorrectRootNode(const Containers::SceneGraph& graph,
  289. AZStd::unordered_set<AZStd::string>& selected, AZStd::unordered_set<AZStd::string>& unselected)
  290. {
  291. // If both of the unselected and selected node lists are empty or don't exist, deselect all the nodes
  292. // in the graph by deselecting the root node.
  293. //
  294. // If only the unselected node list is empty or doesn't exist, deselect the root node (which will deselect
  295. // all the nodes in the graph) by default and then reselect nodes based on the selected node list.
  296. //
  297. // Otherwise select the root node (which will select all the nodes in the graph) by default and then
  298. // remove selected nodes based on the deselected list.
  299. bool selectRootNode = !unselected.empty();
  300. AZStd::string rootNodeName = graph.GetNodeName(graph.GetRoot()).GetPath();
  301. auto& nodeSetToAdd = selectRootNode ? selected : unselected;
  302. auto& nodeSetToRemove = selectRootNode ? unselected : selected;
  303. if (nodeSetToAdd.find(rootNodeName) == nodeSetToAdd.end())
  304. {
  305. nodeSetToAdd.insert(rootNodeName);
  306. }
  307. auto root = nodeSetToRemove.find(rootNodeName);
  308. if (root != nodeSetToRemove.end())
  309. {
  310. nodeSetToRemove.erase(root);
  311. }
  312. }
  313. }
  314. }
  315. } // namespace AZ