Skip to content

Commit

Permalink
Fix ray rendering in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Atraxus committed Oct 7, 2024
1 parent cf264fb commit 8ec21ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
24 changes: 16 additions & 8 deletions Intern/rayx-core/src/Writer/H5Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <highfive/H5DataSpace.hpp>
#include <highfive/H5File.hpp>
#include <limits>
#include <string>

#include "Debug/Debug.h"
Expand Down Expand Up @@ -65,27 +66,35 @@ void writeH5(const RAYX::BundleHistory& hist, const std::string& filename, const
RAYX::BundleHistory fromDoubles(const std::vector<double>& doubles, const Format& format) {
RAYX_PROFILE_FUNCTION_STDOUT();
const size_t formatSize = format.size();
const size_t numRays = doubles.size() / formatSize;
const size_t numEntries = doubles.size() / formatSize;

if (doubles.size() % formatSize != 0) {
throw std::invalid_argument("Size of doubles does not match expected size based on format");
}

RAYX::BundleHistory bundleHist;
bundleHist.reserve(numRays / 2); // Estimate: assume at least 2 events per ray on average
bundleHist.reserve(numEntries / 2); // Estimate: assume at least 2 events per ray on average

RAYX::RayHistory rayHist;
rayHist.reserve(8); // Estimate: assume 8 events per ray on average

const double* data = doubles.data();

for (size_t i = 0; i < numRays; ++i) {
size_t currentRayID = std::numeric_limits<size_t>::max(); // Initialize with an invalid Ray-ID

for (size_t i = 0; i < numEntries; ++i) {
const double* rayData = data + i * formatSize;

if (!rayHist.empty()) {
bundleHist.push_back(std::move(rayHist));
rayHist.clear();
rayHist.reserve(8);
size_t rayID = static_cast<size_t>(rayData[0]); // Extract the Ray-ID

if (rayID != currentRayID) {
if (!rayHist.empty()) {
bundleHist.push_back(std::move(rayHist));
size_t lastRayHistSize = rayHist.size();
rayHist.clear();
rayHist.reserve(lastRayHistSize);
}
currentRayID = rayID;
}

const auto ray = RAYX::Ray{
Expand All @@ -95,7 +104,6 @@ RAYX::BundleHistory fromDoubles(const std::vector<double>& doubles, const Format
.m_energy = rayData[9], // energy
.m_field =
{
// electric field
{rayData[10], rayData[11]},
{rayData[12], rayData[13]},
{rayData[14], rayData[15]},
Expand Down
19 changes: 5 additions & 14 deletions Intern/rayx-ui/src/RayProcessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beaml
amountOfRays = (uint32_t)std::min(amountOfRays, uint32_t(rayCache.size()));
std::vector<size_t> rayIndices = filterFunction(rayCache, amountOfRays);
size_t maxRayIndex = rayCache.size();
int counter = 0;

// compile all elements
std::vector<RAYX::Element> compiledElements;
for (const auto& element : beamline.m_DesignElements) {
compiledElements.push_back(element.compile());
counter++;
}

for (size_t i : rayIndices) {
Expand All @@ -93,8 +91,6 @@ std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beaml
}
glm::vec4 rayLastPos = glm::vec4(beamline.m_DesignSources[static_cast<size_t>(rayHist[0].m_sourceID)].getWorldPosition());

bool isFirstEvent = true;

for (const RAYX::Ray& event : rayHist) {
if (event.m_lastElement >= beamline.m_DesignElements.size()) {
RAYX_EXIT << "Trying to access out-of-bounds index with element ID: " << event.m_lastElement;
Expand All @@ -105,20 +101,15 @@ std::vector<Line> getRays(const RAYX::BundleHistory& rayCache, const RAYX::Beaml
: (event.m_eventType == RAYX::ETYPE_ABSORBED) ? RED
: WHITE;

if (!isFirstEvent) {
// Only execute if not the first event with > 0
ColorVertex origin = {rayLastPos, originColor};
ColorVertex point = {worldPos, pointColor};

rays.push_back(Line(origin, point));
}

rayLastPos = worldPos; // Update rayLastPos in every case for the next iteration
isFirstEvent = false; // Update the flag after the first iteration
ColorVertex origin = {rayLastPos, originColor};
ColorVertex point = {worldPos, pointColor};
rays.push_back(Line(origin, point));
rayLastPos = point.pos;
}
}
return rays;
}

void sortRaysByElement(const RAYX::BundleHistory& rays, std::vector<std::vector<RAYX::Ray>>& sortedRays, size_t numElements) {
RAYX_PROFILE_FUNCTION_STDOUT();

Expand Down

0 comments on commit 8ec21ec

Please sign in to comment.