Skip to content

Commit

Permalink
Updating UI for Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Atraxus committed Feb 17, 2025
1 parent dc9c28d commit cd59b11
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 288 deletions.
31 changes: 7 additions & 24 deletions Intern/rayx-ui/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void Application::run() {

if (m_UIParams.rmlReady) {
m_RMLPath = m_UIParams.rmlPath.string();
m_UIParams.beamlineInfo = {};
beamlineFuture = std::async(std::launch::async, &Application::loadBeamline, this, m_RMLPath);
m_State = State::LoadingBeamline;
m_UIParams.rmlReady = false;
Expand All @@ -151,28 +152,10 @@ void Application::run() {
m_Scene = std::make_unique<Scene>(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;
} else if (m_UIParams.beamlineInfo.elements.size() > 0) {
m_UIParams.beamlineInfo.selectedType = SelectedType::OpticalElement;
m_UIParams.beamlineInfo.selectedIndex = 0;
} else {
m_UIParams.beamlineInfo.selectedType = SelectedType::None;
}
m_UIParams.beamlineInfo.beamline = m_Beamline.get();

// Prepare for ray loading or element preparation
size_t numElements = m_Beamline->m_DesignElements.size();
size_t numElements = m_Beamline->numElements();
m_sortedRays.resize(numElements);
if (m_UIParams.h5Ready) {
raysFuture = std::async(std::launch::async, &Application::loadRays, this, m_RMLPath, numElements);
Expand All @@ -191,7 +174,7 @@ void Application::run() {

case State::Simulating:
if (simulationFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
raysFuture = std::async(std::launch::async, &Application::loadRays, this, m_RMLPath, m_Beamline->m_DesignElements.size());
raysFuture = std::async(std::launch::async, &Application::loadRays, this, m_RMLPath, m_Beamline->numElements());
m_State = State::LoadingRays;
}
break;
Expand All @@ -211,7 +194,7 @@ void Application::run() {
} else {
for (auto ray : m_rays) {
size_t id = static_cast<size_t>(ray.back().m_lastElement);
if (id > m_Beamline->m_DesignElements.size()) {
if (id > m_Beamline->numElements()) {
m_UIParams.showH5NotExistPopup = true;
break;
}
Expand Down Expand Up @@ -247,8 +230,8 @@ void Application::run() {
// only start async task if one is not already running
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), m_sortedRays, m_buildTextureNeeded);
getRObjInputsFuture = std::async(std::launch::async, &Scene::getRObjectInputs, m_Scene.get(), std::ref(*m_Beamline),
m_sortedRays, m_buildTextureNeeded);
} else {
RAYX_VERB << "Skipping PrepareElements, async task already running.";
}
Expand Down
16 changes: 7 additions & 9 deletions Intern/rayx-ui/src/RayProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <unordered_set>

#include "Application.h"
#include "Beamline/Beamline.h"
#include "Colors.h"
#include "Debug/Instrumentor.h"

Expand Down Expand Up @@ -61,8 +62,6 @@ size_t getMaxEvents(const RAYX::BundleHistory& bundleHist) {
* Depending on the event type associated with the ray, the function produces visual lines that represent
* ray segments, colored based on the event type.
*/
// Define the type of the filter function

std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beamline& beamline, RayFilterFunction filterFunction,
uint32_t amountOfRays) {
RAYX_PROFILE_FUNCTION_STDOUT();
Expand All @@ -74,10 +73,9 @@ std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beaml
size_t maxRayIndex = rayCache.size();

// compile all elements
std::vector<RAYX::Element> compiledElements;
for (const auto& element : beamline.m_DesignElements) {
compiledElements.push_back(element.compile());
}
auto compiledElements = beamline.compileElements();
std::vector<glm::dvec4> sourceWorldPositions;
RAYX::Group::accumulateLightSourcesWorldPositions(beamline, glm::dvec4(0, 0, 0, 1), glm::dmat4(1), sourceWorldPositions);

for (size_t i : rayIndices) {
if (i >= maxRayIndex) {
Expand All @@ -86,13 +84,13 @@ std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beaml
}
auto& rayHist = rayCache[i];

if (beamline.m_DesignSources.size() <= rayHist[0].m_sourceID) {
if (beamline.numSources() <= rayHist[0].m_sourceID) {
RAYX_EXIT << "Trying to access out-of-bounds index with source ID: " << rayHist[0].m_sourceID;
}
glm::vec4 rayLastPos = glm::vec4(beamline.m_DesignSources[static_cast<size_t>(rayHist[0].m_sourceID)].getWorldPosition());
glm::vec4 rayLastPos = sourceWorldPositions[static_cast<size_t>(rayHist[0].m_sourceID)];

for (const RAYX::Ray& event : rayHist) {
if (event.m_lastElement >= beamline.m_DesignElements.size()) {
if (event.m_lastElement >= beamline.numElements()) {
RAYX_EXIT << "Trying to access out-of-bounds index with element ID: " << event.m_lastElement;
}
glm::vec4 worldPos = compiledElements[static_cast<size_t>(event.m_lastElement)].m_outTrans * glm::vec4(event.m_position, 1.0f);
Expand Down
1 change: 1 addition & 0 deletions Intern/rayx-ui/src/RayProcessing.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <functional>
#include <vector>

#include "Beamline/Beamline.h"
#include "RenderObject.h"
#define MAX_RAYS 1000

Expand Down
15 changes: 8 additions & 7 deletions Intern/rayx-ui/src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,30 @@ void Scene::buildRaysRObject(const RAYX::Beamline& beamline, UIRayInfo& rayInfo,
}
}

std::vector<Scene::RenderObjectInput> Scene::getRObjectInputs(const std::vector<RAYX::DesignElement> elements,
const std::vector<std::vector<RAYX::Ray>>& sortedRays, bool buildTexture) {
std::vector<Scene::RenderObjectInput> Scene::getRObjectInputs(const RAYX::Beamline& beamline, const std::vector<std::vector<RAYX::Ray>>& sortedRays,
bool buildTexture) {
// RAYX_PROFILE_FUNCTION_STDOUT();
auto elements = beamline.getElements();
auto compiled = beamline.compileElements();

std::vector<RenderObjectInput> rObjectsInput;
if (buildTexture) m_textureInputCache.clear();
for (uint32_t i = 0; i < elements.size(); i++) {
auto compiled = elements[i].compile();
std::vector<TextureVertex> vertices;
std::vector<uint32_t> indices;

try {
triangulateObject(compiled, vertices, indices);
triangulateObject(compiled[i], vertices, indices);
} catch (const std::exception& ex) {
RAYX_WARN << ex.what() << ". Object \"" << elements[i].getName()
RAYX_WARN << ex.what() << ". Object \"" << elements[i]->getName()
<< "\" can't be rendered due to triangulation issues. Make sure it is defined correctly in the RML file.";
continue; // Input is not generated --> Object won't be built/rendered
}

glm::dmat4& modelMatrix = compiled.m_outTrans;
glm::dmat4& modelMatrix = compiled[i].m_outTrans;

if (buildTexture) {
auto [width, height] = getRectangularDimensions(compiled.m_cutout);
auto [width, height] = getRectangularDimensions(compiled[i].m_cutout);

std::vector<std::vector<uint32_t>> footprint =
makeFootprint(sortedRays[i], -width / 2, width / 2, -height / 2, height / 2, (uint32_t)(width * 10), (uint32_t)(height * 10));
Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-ui/src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Scene {
void buildRaysRObject(const RAYX::Beamline& beamline, UIRayInfo& rayInfo, std::shared_ptr<DescriptorSetLayout> setLayout,
std::shared_ptr<DescriptorPool> descriptorPool);

std::vector<Scene::RenderObjectInput> getRObjectInputs(const std::vector<RAYX::DesignElement> elements,
const std::vector<std::vector<RAYX::Ray>>& sortedRays, bool buildTexture);
std::vector<Scene::RenderObjectInput> getRObjectInputs(const RAYX::Beamline& beamline, const std::vector<std::vector<RAYX::Ray>>& sortedRays,
bool buildTexture);

void buildRObjectsFromInput(std::vector<RenderObjectInput>&& inputs, std::shared_ptr<DescriptorSetLayout> setLayout,
std::shared_ptr<DescriptorPool> descriptorPool, bool buildTexture);
Expand Down
10 changes: 5 additions & 5 deletions Intern/rayx-ui/src/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ void Simulator::runSimulation() {
if (notEnoughEvents) {
RAYX_LOG << "Not enough events (" << m_maxEvents << ")! Consider increasing m_maxEvents.";
}


// Export Rays to external data.
std::string path = m_RMLPath.string();
Expand All @@ -46,10 +45,11 @@ void Simulator::runSimulation() {
Format fmt = formatFromString(defaultFormatString());

std::vector<std::string> names;
names.reserve(m_Beamline.m_DesignElements.size());
auto elements = m_Beamline.getElements();
names.reserve(elements.size());

for (const auto& designElement : m_Beamline.m_DesignElements) {
names.push_back(designElement.getName());
for (const auto designElement : elements) {
names.push_back(designElement->getName());
}

path += ".h5";
Expand All @@ -69,7 +69,7 @@ void Simulator::setSimulationParameters(const std::filesystem::path& RMLPath, co
}

m_RMLPath = RMLPath;
m_Beamline = std::move(beamline);
m_Beamline = std::move(*static_cast<RAYX::Beamline*>(beamline.clone().get()));
m_max_batch_size = simulationInfo.maxBatchSize;
m_seq = simulationInfo.sequential ? RAYX::Sequential::Yes : RAYX::Sequential::No;
m_maxEvents = simulationInfo.maxEvents;
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/Triangulation/TraceTriangulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ std::vector<std::vector<RAYX::Ray>> createRayGrid(size_t size, double width, dou
* cutout. Using CPU-based ray tracing, it computes the intersections between rays and the optical element's surface within the cutout. The ray
* intersections are then grouped into triangles based on the grid, and a RenderObject representing these triangles is returned.
*/
void traceTriangulation(const RAYX::Element compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices) {
void traceTriangulation(const RAYX::OpticalElement compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices) {
using DeviceType = RAYX::DeviceConfig::DeviceType;
auto tracer = RAYX::Tracer(RAYX::DeviceConfig(DeviceType::Cpu).enableBestDevice());

Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/Triangulation/TraceTriangulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* RenderObject renderObj = traceTriangulation(element, device);
* @endcode
*/
void traceTriangulation(const RAYX::Element compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices);
void traceTriangulation(const RAYX::OpticalElement compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices);

// ------ Helper functions ------

Expand Down
4 changes: 2 additions & 2 deletions Intern/rayx-ui/src/Triangulation/Triangulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ PolygonSimple calculateOutlineFromCutout(const RAYX::Cutout& cutout, std::vector
return indices;
}

void planarTriangulation(const RAYX::Element compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices) {
void planarTriangulation(const RAYX::OpticalElement compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices) {
// The slit behaviour needs special attention, since it is basically three cutouts (the slit, the beamstop and the opening)
PolygonComplex poly;
if (compiled.m_behaviour.m_type == RAYX::BTYPE_SLIT) {
Expand All @@ -440,7 +440,7 @@ bool isPlanar(const RAYX::QuadricSurface& q) {
/**
* This function takes optical elements and categorizes them for efficient triangulation.
*/
void triangulateObject(const RAYX::Element compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices) {
void triangulateObject(const RAYX::OpticalElement compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices) {
// RAYX_PROFILE_FUNCTION_STDOUT();
switch (static_cast<int>(compiled.m_surface.m_type)) {
case RAYX::STYPE_PLANE_XZ: {
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/Triangulation/Triangulate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
* @param elements A vector of optical elements to be triangulated.
* @return A vector of RenderObject, which are the triangulated version of the input elements.
*/
void triangulateObject(const RAYX::Element compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices);
void triangulateObject(const RAYX::OpticalElement compiled, std::vector<TextureVertex>& vertices, std::vector<uint32_t>& indices);
38 changes: 20 additions & 18 deletions Intern/rayx-ui/src/UserInterface/BeamlineDesignHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@
#include <string>
#include <unordered_set>

#include "Beamline/Beamline.h"
#include "Beamline/StringConversion.h"
#include "Data/Strings.cpp"
#include "Debug/Instrumentor.h"

void BeamlineDesignHandler::showBeamlineDesignWindow(UIBeamlineInfo& uiBeamlineInfo) {
// RAYX_PROFILE_FUNCTION_STDOUT();
void BeamlineDesignHandler::showBeamlineDesignWindow(UIBeamlineInfo& uiInfo) {
// If no node is selected, do nothing
if (!uiInfo.selectedNode) {
ImGui::Text("No node selected");
return;
}

if (uiBeamlineInfo.selectedType == SelectedType::LightSource) { // source
if (uiBeamlineInfo.selectedIndex >= 0 && uiBeamlineInfo.selectedIndex < static_cast<int>(uiBeamlineInfo.sources.size())) {
auto sourceParameters = uiBeamlineInfo.sources[uiBeamlineInfo.selectedIndex].m_elementParameters;
showParameters(sourceParameters, uiBeamlineInfo.elementsChanged, uiBeamlineInfo.selectedType);
} else {
// Handle out-of-bounds access for sources
RAYX_EXIT << "Error: selectedIndex is out of bounds for sources.";
if (uiInfo.selectedNode->isSource()) {
const auto srcPtr = dynamic_cast<RAYX::DesignSource*>(uiInfo.selectedNode);
if (srcPtr) {
// showParameters can now edit srcPtr->m_elementParameters
showParameters(srcPtr->m_elementParameters, uiInfo.elementsChanged, SelectedType::LightSource);
}
} else if (uiBeamlineInfo.selectedType == SelectedType::OpticalElement) { // element
if (uiBeamlineInfo.selectedIndex >= 0 && uiBeamlineInfo.selectedIndex < static_cast<int>(uiBeamlineInfo.elements.size())) {
auto elementParameters = uiBeamlineInfo.elements[uiBeamlineInfo.selectedIndex].m_elementParameters;
showParameters(elementParameters, uiBeamlineInfo.elementsChanged, uiBeamlineInfo.selectedType);
} else {
// Handle out-of-bounds access for elements
RAYX_EXIT << "Error: selectedIndex is out of bounds for elements.";
} else if (uiInfo.selectedNode->isElement()) {
const auto elemPtr = dynamic_cast<RAYX::DesignElement*>(uiInfo.selectedNode);
if (elemPtr) {
showParameters(elemPtr->m_elementParameters, uiInfo.elementsChanged, SelectedType::OpticalElement);
}
} else if (uiBeamlineInfo.selectedType == SelectedType::Group) { // group
// Handle group if needed
} else if (uiInfo.selectedNode->isGroup()) {
ImGui::Text("Group editing is to be implemented still...");
} else {
throw std::runtime_error("Tree element of unknown type encountered!");
}
}

Expand Down
Loading

0 comments on commit cd59b11

Please sign in to comment.