diff --git a/include/openspace/scene/asset.h b/include/openspace/scene/asset.h index c27a142f98..25eb53860f 100644 --- a/include/openspace/scene/asset.h +++ b/include/openspace/scene/asset.h @@ -129,15 +129,14 @@ public: void request(std::shared_ptr child); void unrequest(Asset* child); - const std::vector>& requestedAssets() const; - std::vector> requestingAssets() const; - const std::vector>& requiredAssets() const; - std::vector> requiringAssets() const; + std::vector requestedAssets() const; + std::vector requestingAssets() const; + std::vector requiredAssets() const; + std::vector requiringAssets() const; - std::vector> requiredSubTreeAssets() const; - std::vector> subTreeAssets() const; - std::vector> childAssets() const; - std::vector> parentAssets() const; + std::vector requiredSubTreeAssets() const; + std::vector subTreeAssets() const; + std::vector childAssets() const; bool isRequired() const; bool isRequested() const; diff --git a/modules/imgui/src/guiassetcomponent.cpp b/modules/imgui/src/guiassetcomponent.cpp index 3b190f40b7..0ccf5fa399 100644 --- a/modules/imgui/src/guiassetcomponent.cpp +++ b/modules/imgui/src/guiassetcomponent.cpp @@ -78,7 +78,7 @@ void GuiAssetComponent::render() { std::string rootPath; - for (const std::shared_ptr& a : assetManager.rootAsset().childAssets()) { + for (Asset* a : assetManager.rootAsset().childAssets()) { renderTree(*a, rootPath); } @@ -103,8 +103,8 @@ void GuiAssetComponent::renderTree(const Asset& asset, const std::string& relati assetText += " (" + std::to_string(prog) + "%)"; } - const std::vector>& requested = asset.requestedAssets(); - const std::vector>& required = asset.requiredAssets(); + std::vector requested = asset.requestedAssets(); + std::vector required = asset.requiredAssets(); const std::vector& resourceSyncs = asset.ownSynchronizations(); @@ -113,12 +113,12 @@ void GuiAssetComponent::renderTree(const Asset& asset, const std::string& relati ImGui::Text("%s", assetText.c_str()); } else if (ImGui::TreeNode(assetPath.c_str(), "%s", assetText.c_str())) { - for (const std::shared_ptr& child : required) { + for (const Asset* child : required) { renderTree(*child, assetDirectory); } if (!requested.empty() && ImGui::TreeNode("Requested assets")) { - for (const std::shared_ptr& child : requested) { + for (const Asset* child : requested) { renderTree(*child, assetDirectory); } ImGui::TreePop(); diff --git a/modules/sync/tasks/syncassettask.cpp b/modules/sync/tasks/syncassettask.cpp index 2822b09651..5ea8a551b1 100644 --- a/modules/sync/tasks/syncassettask.cpp +++ b/modules/sync/tasks/syncassettask.cpp @@ -112,12 +112,11 @@ void SyncAssetTask::perform(const Task::ProgressCallback& progressCallback) { loader.add(_asset); loader.rootAsset().startSynchronizations(); - std::vector> allAssets = - loader.rootAsset().subTreeAssets(); + std::vector allAssets = loader.rootAsset().subTreeAssets(); while (true) { bool inProgress = false; - for (const std::shared_ptr& asset : allAssets) { + for (const Asset* asset : allAssets) { Asset::State state = asset->state(); if (state == Asset::State::Unloaded || state == Asset::State::Loaded || diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 42c7a7e5e8..f1a4a7f55d 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -751,11 +751,10 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { _loadingScreen->setPhase(LoadingScreen::Phase::Synchronization); _loadingScreen->postMessage("Synchronizing assets"); - std::vector> allAssets = - _assetManager->rootAsset().subTreeAssets(); + std::vector allAssets = _assetManager->rootAsset().subTreeAssets(); std::unordered_set resourceSyncs; - for (const std::shared_ptr& a : allAssets) { + for (const Asset* a : allAssets) { std::vector syncs = a->ownSynchronizations(); for (ResourceSynchronization* s : syncs) { diff --git a/src/scene/asset.cpp b/src/scene/asset.cpp index ef19dea0ba..9470b97183 100644 --- a/src/scene/asset.cpp +++ b/src/scene/asset.cpp @@ -38,11 +38,11 @@ namespace openspace { namespace { constexpr const char* _loggerCat = "Asset"; - float syncProgress(const std::vector>& assets) { + float syncProgress(const std::vector& assets) { size_t nTotalBytes = 0; size_t nSyncedBytes = 0; - for (const std::shared_ptr& a : assets) { + for (const Asset* a : assets) { const std::vector& s = a->ownSynchronizations(); for (ResourceSynchronization* sync : s) { @@ -209,12 +209,12 @@ void Asset::syncStateChanged(ResourceSynchronization* sync, } bool Asset::isSyncResolveReady() { - std::vector> requiredAssets = this->requiredAssets(); + std::vector requiredAssets = this->requiredAssets(); const auto unsynchronizedAsset = std::find_if( requiredAssets.cbegin(), requiredAssets.cend(), - [](const std::shared_ptr& a) { return !a->isSynchronized(); } + [](Asset* a) { return !a->isSynchronized(); } ); if (unsynchronizedAsset != requiredAssets.cend()) { @@ -244,31 +244,31 @@ std::vector Asset::ownSynchronizations() const { return res; } -std::vector> Asset::subTreeAssets() const { - std::unordered_set> assets({ shared_from_this() }); - for (const std::shared_ptr& c : childAssets()) { - if (c.get() == this) { +std::vector Asset::subTreeAssets() const { + std::unordered_set assets({ this }); + for (Asset* c : childAssets()) { + if (c == this) { throw ghoul::RuntimeError(fmt::format( "Detected cycle in asset inclusion for {} at {}", _assetName, _assetPath )); } - const std::vector>& subTree = c->subTreeAssets(); + std::vector subTree = c->subTreeAssets(); std::copy(subTree.begin(), subTree.end(), std::inserter(assets, assets.end())); } - std::vector> assetVector(assets.begin(), assets.end()); + std::vector assetVector(assets.begin(), assets.end()); return assetVector; } -std::vector> Asset::requiredSubTreeAssets() const { - std::unordered_set> assets({ shared_from_this() }); +std::vector Asset::requiredSubTreeAssets() const { + std::unordered_set assets({ this }); for (const std::shared_ptr& dep : _requiredAssets) { - const std::vector>& subTree = - dep->requiredSubTreeAssets(); + std::vector subTree = dep->requiredSubTreeAssets(); + std::copy(subTree.begin(), subTree.end(), std::inserter(assets, assets.end())); } - std::vector> assetVector(assets.begin(), assets.end()); + std::vector assetVector(assets.begin(), assets.end()); return assetVector; } @@ -372,7 +372,7 @@ bool Asset::startSynchronizations() { LWARNING(fmt::format("Cannot start synchronizations of unloaded asset {}", id())); return false; } - for (const std::shared_ptr& child : requestedAssets()) { + for (Asset* child : requestedAssets()) { child->startSynchronizations(); } @@ -386,7 +386,7 @@ bool Asset::startSynchronizations() { bool childFailed = false; // Start synchronization of all children first - for (const std::shared_ptr& child : requiredAssets()) { + for (Asset* child : requiredAssets()) { if (!child->startSynchronizations()) { childFailed = true; } @@ -406,11 +406,11 @@ bool Asset::startSynchronizations() { } bool Asset::cancelAllSynchronizations() { - const std::vector>& children = childAssets(); + const std::vector& children = childAssets(); bool cancelledAnySync = std::any_of( children.cbegin(), children.cend(), - [](const std::shared_ptr& child) { + [](Asset* child) { return child->cancelAllSynchronizations(); } ); @@ -433,11 +433,11 @@ bool Asset::cancelUnwantedSynchronizations() { return false; } - const std::vector>& children = childAssets(); + const std::vector& children = childAssets(); bool cancelledAnySync = std::any_of( children.begin(), children.end(), - [](const std::shared_ptr& child) { + [](Asset* child) { return child->cancelUnwantedSynchronizations(); } ); @@ -461,12 +461,12 @@ bool Asset::restartAllSynchronizations() { } float Asset::requiredSynchronizationProgress() const { - const std::vector>& assets = requiredSubTreeAssets(); + std::vector assets = requiredSubTreeAssets(); return syncProgress(assets); } float Asset::requestedSynchronizationProgress() { - const std::vector>& assets = subTreeAssets(); + std::vector assets = subTreeAssets(); return syncProgress(assets); } @@ -488,11 +488,11 @@ void Asset::unload() { setState(State::Unloaded); loader()->unloadAsset(this); - for (const std::shared_ptr& child : requiredAssets()) { - unrequire(child.get()); + for (Asset* child : requiredAssets()) { + unrequire(child); } - for (const std::shared_ptr& child : requestedAssets()) { - unrequest(child.get()); + for (Asset* child : requestedAssets()) { + unrequest(child); } } @@ -666,7 +666,7 @@ void Asset::deinitialize() { } // 2 and 1. Deinitialize unwanted requirements and requests - for (const std::shared_ptr& dependency : childAssets()) { + for (Asset* dependency : childAssets()) { dependency->deinitializeIfUnwanted(); } } @@ -836,53 +836,57 @@ bool Asset::requests(Asset* asset) const { return it != _requiredAssets.cend(); } -const std::vector>& Asset::requiredAssets() const { - return _requiredAssets; +std::vector Asset::requiredAssets() const { + std::vector res; + res.reserve(_requiredAssets.size()); + for (const std::shared_ptr& a : _requiredAssets) { + res.push_back(a.get()); + } + return res; } -std::vector> Asset::requiringAssets() const { - std::vector> assets; - assets.reserve(_requiringAssets.size()); +std::vector Asset::requiringAssets() const { + std::vector res; + res.reserve(_requiringAssets.size()); for (const std::weak_ptr& a : _requiringAssets) { if (std::shared_ptr shared = a.lock(); shared) { - assets.push_back(shared); + res.push_back(shared.get()); } } - return assets; + return res; } -const std::vector>& Asset::requestedAssets() const { - return _requestedAssets; +std::vector Asset::requestedAssets() const { + std::vector res; + res.reserve(_requestedAssets.size()); + for (const std::shared_ptr& a : _requestedAssets) { + res.push_back(a.get()); + } + return res; } -std::vector> Asset::requestingAssets() const { - std::vector> assets; - assets.reserve(_requestingAssets.size()); +std::vector Asset::requestingAssets() const { + std::vector res; + res.reserve(_requestingAssets.size()); for (const std::weak_ptr& a : _requestingAssets) { - std::shared_ptr shared = a.lock(); - if (shared) { - assets.push_back(shared); + if (std::shared_ptr shared = a.lock(); shared) { + res.push_back(shared.get()); } } - return assets; + return res; } -std::vector> Asset::childAssets() const { - std::vector> children; +std::vector Asset::childAssets() const { + std::vector children; children.reserve(_requiredAssets.size() + _requestedAssets.size()); - children.insert(children.end(), _requiredAssets.begin(), _requiredAssets.end()); - children.insert(children.end(), _requestedAssets.begin(), _requestedAssets.end()); - return children; -} -std::vector> Asset::parentAssets() const { - std::vector> parents; - std::vector> requiring = requiringAssets(); - std::vector> requesting = requestingAssets(); - parents.reserve(requiring.size() + requesting.size()); - parents.insert(parents.end(), requiring.begin(), requiring.end()); - parents.insert(parents.end(), requesting.begin(), requesting.end()); - return parents; + for (const std::shared_ptr& a : _requiredAssets) { + children.push_back(a.get()); + } + for (const std::shared_ptr& a : _requestedAssets) { + children.push_back(a.get()); + } + return children; } bool Asset::isRequired() const { @@ -894,13 +898,20 @@ bool Asset::isRequested() const { } bool Asset::shouldBeInitialized() const { - std::vector> parents = parentAssets(); - const auto initializedAsset = std::find_if( - parents.cbegin(), - parents.cend(), - [](const std::shared_ptr& a) { return a->state() == State::Initialized; } + const bool requiring = std::all_of( + _requiringAssets.begin(), _requiringAssets.end(), + [](const std::weak_ptr& asset) { + return asset.lock()->state() == State::Initialized; + } ); - return initializedAsset != parents.cend(); + const bool requesting = std::all_of( + _requestingAssets.begin(), _requestingAssets.end(), + [](const std::weak_ptr& asset) { + return asset.lock()->isInitialized(); + } + ); + + return requiring && requesting; } } // namespace openspace diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index a1540deebe..946a56bfed 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -105,10 +105,7 @@ void AssetManager::removeAll() { ZoneScoped _pendingStateChangeCommands.clear(); - std::vector> allAssets = - _assetLoader.rootAsset().requestedAssets(); - - for (const std::shared_ptr& a : allAssets) { + for (const Asset* a : _assetLoader.rootAsset().requestedAssets()) { _pendingStateChangeCommands[a->assetFilePath()] = false; } } diff --git a/src/scene/scenelicensewriter.cpp b/src/scene/scenelicensewriter.cpp index 90319fe9d6..b954008889 100644 --- a/src/scene/scenelicensewriter.cpp +++ b/src/scene/scenelicensewriter.cpp @@ -48,10 +48,10 @@ std::string SceneLicenseWriter::generateJson() const { std::stringstream json; json << "["; - std::vector> assets = + std::vector assets = global::openSpaceEngine.assetManager().rootAsset().subTreeAssets(); - for (const std::shared_ptr& asset : assets) { + for (const Asset* asset : assets) { std::optional meta = asset->metaInformation(); if (!meta.has_value()) {