From 0caceb00d40911c42896c2f66462797b7eaa899d Mon Sep 17 00:00:00 2001 From: Jannis Maier Date: Mon, 19 Aug 2024 10:40:26 +0200 Subject: [PATCH] Fix beamline reloading without rays bug --- Intern/rayx-ui/src/Application.cpp | 23 +++++++++++++---------- Intern/rayx-ui/src/Application.h | 2 +- Intern/rayx-ui/src/Scene.cpp | 2 +- Intern/rayx-ui/src/Scene.h | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Intern/rayx-ui/src/Application.cpp b/Intern/rayx-ui/src/Application.cpp index 617e5d1a..f744226c 100644 --- a/Intern/rayx-ui/src/Application.cpp +++ b/Intern/rayx-ui/src/Application.cpp @@ -145,16 +145,22 @@ void Application::run() { switch (m_State) { case State::LoadingBeamline: if (beamlineFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready) { - // reset rays + // Clear existing rays and reset scene m_rays.clear(); m_UIParams.rayInfo.raysLoaded = false; m_Scene = std::make_unique(m_Device); + + // Update elements and sources for UI m_UIParams.beamlineInfo.elements = m_Beamline->m_DesignElements; m_UIParams.beamlineInfo.sources = m_Beamline->m_DesignSources; + + // Store source positions in UI parameters m_UIParams.beamlineInfo.rSourcePositions.clear(); for (auto& source : m_UIParams.beamlineInfo.sources) { m_UIParams.beamlineInfo.rSourcePositions.push_back(source.getWorldPosition()); } + + // Set default selection in UI based on available sources or elements if (m_UIParams.beamlineInfo.sources.size() > 0) { m_UIParams.beamlineInfo.selectedType = SelectedType::LightSource; m_UIParams.beamlineInfo.selectedIndex = 0; @@ -164,6 +170,8 @@ void Application::run() { } else { m_UIParams.beamlineInfo.selectedType = SelectedType::None; } + + // Prepare for ray loading or element preparation size_t numElements = m_Beamline->m_DesignElements.size(); m_sortedRays.resize(numElements); if (m_UIParams.h5Ready) { @@ -215,14 +223,9 @@ void Application::run() { } RAYX_VERB << "Loaded H5 file: " << m_RMLPath.string().substr(0, m_RMLPath.string().size() - 4) + ".h5"; - - if (!buildRayCacheFuture.valid() || - (buildRayCacheFuture.valid() && buildRayCacheFuture.wait_for(std::chrono::seconds(0)) != std::future_status::ready)) { - buildRayCacheFuture = std::async(std::launch::async, &Scene::buildRayCache, m_Scene.get(), - std::ref(m_UIParams.rayInfo), std::ref(m_rays)); - } else { - RAYX_VERB << "Skipping buildRayCache, async task already running."; - } + // We do not need to check the future state here as the loop will not come back here until user interacts with UI again + buildRayCacheFuture = + std::async(std::launch::async, &Scene::buildRayCache, m_Scene.get(), std::ref(m_UIParams.rayInfo), std::ref(m_rays)); m_State = State::BuildingRays; } } @@ -245,7 +248,7 @@ void Application::run() { if (!getRObjInputsFuture.valid() || (getRObjInputsFuture.valid() && getRObjInputsFuture.wait_for(std::chrono::seconds(0)) != std::future_status::ready)) { getRObjInputsFuture = std::async(std::launch::async, &Scene::getRObjectInputs, m_Scene.get(), - std::ref(m_UIParams.beamlineInfo.elements), std::ref(m_sortedRays), m_buildTextureNeeded); + std::ref(m_UIParams.beamlineInfo.elements), m_sortedRays, m_buildTextureNeeded); } else { RAYX_VERB << "Skipping PrepareElements, async task already running."; } diff --git a/Intern/rayx-ui/src/Application.h b/Intern/rayx-ui/src/Application.h index 26608e7a..1d0335a7 100644 --- a/Intern/rayx-ui/src/Application.h +++ b/Intern/rayx-ui/src/Application.h @@ -75,7 +75,7 @@ class Application { std::filesystem::path m_RMLPath; ///< Path to the RML file std::unique_ptr m_Beamline; ///< Beamline RAYX::BundleHistory m_rays; ///< All rays - std::vector> m_sortedRays; // rays sorted by element + std::vector> m_sortedRays; ///< Rays sorted by element bool m_buildElementsNeeded = true; bool m_buildTextureNeeded = true; diff --git a/Intern/rayx-ui/src/Scene.cpp b/Intern/rayx-ui/src/Scene.cpp index 4edd46b7..82558f38 100644 --- a/Intern/rayx-ui/src/Scene.cpp +++ b/Intern/rayx-ui/src/Scene.cpp @@ -88,7 +88,7 @@ void Scene::buildRaysRObject(const RAYX::Beamline& beamline, UIRayInfo& rayInfo, } std::vector Scene::getRObjectInputs(const std::vector elements, - const std::vector> sortedRays, bool buildTexture) { + const std::vector>& sortedRays, bool buildTexture) { // RAYX_PROFILE_FUNCTION_STDOUT(); std::vector rObjectsInput; diff --git a/Intern/rayx-ui/src/Scene.h b/Intern/rayx-ui/src/Scene.h index 4c64c314..a832dce8 100644 --- a/Intern/rayx-ui/src/Scene.h +++ b/Intern/rayx-ui/src/Scene.h @@ -28,7 +28,7 @@ class Scene { std::shared_ptr descriptorPool); std::vector getRObjectInputs(const std::vector elements, - const std::vector> sortedRays, bool buildTexture); + const std::vector>& sortedRays, bool buildTexture); void buildRObjectsFromInput(std::vector&& inputs, std::shared_ptr setLayout, std::shared_ptr descriptorPool, bool buildTexture);