Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Atraxus committed Feb 14, 2025
1 parent 1efb357 commit baf74f7
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 44 deletions.
53 changes: 25 additions & 28 deletions Intern/rayx-core/src/Beamline/Beamline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,23 @@ Group Group::clone() const {
copy.addChild(BeamlineNode(std::make_shared<Group>(ptr->clone())));
}
},
child);
child->data);
}
return copy;
}

template <typename Callback>
void Group::traverse(Callback&& callback) const {
for (const auto& child : m_children) {
callback(child);
if (auto* groupPtr = std::get_if<std::unique_ptr<Group>>(&child)) {
if (*groupPtr) (*groupPtr)->traverse(std::forward<Callback>(callback));
callback(*child);
if (const auto* groupPtr = std::get_if<Group>(&(child->data))) {
if (groupPtr) groupPtr->traverse(std::forward<Callback>(callback));
}
}
}

// A Group is always a Group.
NodeType Group::getNodeType() const { return NodeType::Group; }

// Add a child node.
void Group::addChild(BeamlineNode&& child) { m_children.push_back(std::move(child)); }
void Group::addChild(BeamlineNode&& child) { m_children.push_back(std::make_unique<BeamlineNode>(std::move(child))); }

MaterialTables Group::calcMinimalMaterialTables() const {
auto elements = getElements();
Expand All @@ -82,14 +79,14 @@ void Group::accumulateLightSourcesWorldPositions(const Group& group, const glm::
glm::dmat4 currentOri = parentOri * group.getOrientation();

for (const auto& child : group) {
if (std::holds_alternative<std::unique_ptr<DesignSource>>(child)) {
auto& srcPtr = std::get<std::unique_ptr<DesignSource>>(child);
if (std::holds_alternative<DesignSource>(child->data)) {
const auto* srcPtr = &std::get<DesignSource>(child->data);
if (srcPtr) {
glm::dvec4 worldPos = currentOri * srcPtr->getPosition() + currentPos;
positions.push_back(worldPos);
}
} else if (std::holds_alternative<std::unique_ptr<Group>>(child)) {
auto& childGroupPtr = std::get<std::unique_ptr<Group>>(child);
} else if (std::holds_alternative<Group>(child->data)) {
const auto* childGroupPtr = &std::get<Group>(child->data);
if (childGroupPtr) {
accumulateLightSourcesWorldPositions(*childGroupPtr, currentPos, currentOri, positions);
}
Expand All @@ -105,14 +102,14 @@ std::vector<OpticalElement> Group::compileElements() const {
glm::dmat4 thisGroupOri = parentOri * grp.getOrientation();

for (const auto& child : grp.m_children) { // For each child...
if (std::holds_alternative<std::unique_ptr<DesignElement>>(child)) {
const auto& dePtr = std::get<std::unique_ptr<DesignElement>>(child);
if (std::holds_alternative<DesignElement>(child->data)) {
const auto* dePtr = &std::get<DesignElement>(child->data);
if (dePtr) {
// Compile an OpticalElement from the DesignElement
elements.push_back(dePtr->compile(thisGroupPos, thisGroupOri));
}
} else if (std::holds_alternative<std::unique_ptr<Group>>(child)) {
const auto& groupPtr = std::get<std::unique_ptr<Group>>(child);
} else if (std::holds_alternative<Group>(child->data)) {
const auto* groupPtr = &std::get<Group>(child->data);
if (groupPtr) {
// Recurse into the child group
self(self, *groupPtr, thisGroupPos, thisGroupOri);
Expand All @@ -137,8 +134,8 @@ std::vector<Ray> Group::compileSources(int thread_count) const {
glm::dmat4 currentOrientation = parentOrientation * group.getOrientation();

for (const auto& child : group.m_children) { // For each child...
if (std::holds_alternative<std::unique_ptr<DesignSource>>(child)) {
const auto& srcPtr = std::get<std::unique_ptr<DesignSource>>(child);
if (std::holds_alternative<DesignSource>(child->data)) {
const auto* srcPtr = &std::get<DesignSource>(child->data);
if (srcPtr) {
// Compile the rays for this source
auto sourceRays = srcPtr->compile(thread_count, currentPosition, currentOrientation);
Expand All @@ -147,8 +144,8 @@ std::vector<Ray> Group::compileSources(int thread_count) const {
}
rays.insert(rays.end(), sourceRays.begin(), sourceRays.end());
}
} else if (std::holds_alternative<std::unique_ptr<Group>>(child)) {
const auto& childGroupPtr = std::get<std::unique_ptr<Group>>(child);
} else if (std::holds_alternative<Group>(child->data)) {
const auto* childGroupPtr = &std::get<Group>(child->data);
if (childGroupPtr) {
self(*childGroupPtr, currentPosition, currentOrientation, self);
}
Expand All @@ -163,8 +160,8 @@ std::vector<Ray> Group::compileSources(int thread_count) const {
std::vector<DesignElement*> Group::getElements() const {
std::vector<DesignElement*> elements;
for (const auto& node : m_children) {
if (std::holds_alternative<std::unique_ptr<DesignElement>>(node)) {
elements.push_back(std::get<std::unique_ptr<DesignElement>>(node).get());
if (std::holds_alternative<DesignElement>(node->data)) {
elements.push_back(&std::get<DesignElement>(node->data));
}
}
return elements;
Expand All @@ -173,8 +170,8 @@ std::vector<DesignElement*> Group::getElements() const {
std::vector<DesignSource*> Group::getSources() const {
std::vector<DesignSource*> sources;
for (const auto& node : m_children) {
if (std::holds_alternative<std::unique_ptr<DesignSource>>(node)) {
sources.push_back(std::get<std::unique_ptr<DesignSource>>(node).get());
if (std::holds_alternative<DesignSource>(node->data)) {
sources.push_back(&std::get<DesignSource>(node->data));
}
}
return sources;
Expand All @@ -183,8 +180,8 @@ std::vector<DesignSource*> Group::getSources() const {
std::vector<Group*> Group::getGroups() const {
std::vector<Group*> groups;
for (const auto& node : m_children) {
if (std::holds_alternative<std::unique_ptr<Group>>(node)) {
groups.push_back(std::get<std::unique_ptr<Group>>(node).get());
if (std::holds_alternative<Group>(node->data)) {
groups.push_back(&std::get<Group>(node->data));
}
}
return groups;
Expand All @@ -193,7 +190,7 @@ std::vector<Group*> Group::getGroups() const {
size_t Group::numElements() const {
size_t count = 0;
traverse([&count](const BeamlineNode& node) {
if (std::holds_alternative<std::unique_ptr<DesignElement>>(node)) {
if (std::holds_alternative<DesignElement>(node.data)) {
++count;
}
});
Expand All @@ -203,7 +200,7 @@ size_t Group::numElements() const {
size_t Group::numSources() const {
size_t count = 0;
traverse([&count](const BeamlineNode& node) {
if (std::holds_alternative<std::unique_ptr<DesignSource>>(node)) {
if (std::holds_alternative<DesignSource>(node.data)) {
++count;
}
});
Expand Down
26 changes: 16 additions & 10 deletions Intern/rayx-core/src/Beamline/Beamline.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

namespace RAYX {

class Group;
using BeamlineNode = std::variant<std::unique_ptr<DesignElement>, std::unique_ptr<DesignSource>, std::unique_ptr<Group>>;
struct BeamlineNode;
enum class NodeType { OpticalElement, LightSource, Group };

class RAYX_API Group {
Expand Down Expand Up @@ -42,8 +41,7 @@ class RAYX_API Group {
auto cbegin() const { return m_children.cbegin(); }
auto cend() const { return m_children.cend(); }

NodeType getNodeType() const;
std::vector<BeamlineNode>& getChildren() { return m_children; }
const std::vector<std::unique_ptr<BeamlineNode>>& getChildren() const { return m_children; }

// Add a child (by move).
void addChild(BeamlineNode&& child);
Expand Down Expand Up @@ -71,24 +69,32 @@ class RAYX_API Group {
private:
glm::dvec4 m_position = glm::dvec4(0, 0, 0, 1);
glm::dmat4 m_orientation = glm::dmat4(1);
std::vector<BeamlineNode> m_children;
std::vector<std::unique_ptr<BeamlineNode>> m_children;
};

using Beamline = Group; // Conceptually, a Beamline is a Group
using NodeData = std::variant<Group, DesignElement, DesignSource>;

struct BeamlineNode {
BeamlineNode(NodeData&& other) : data(std::move(other)), parent(nullptr) {}
NodeData data; // The variant storing the node-specific fields
BeamlineNode* parent = nullptr; // or a parent index if you want
// ? Store node type
};

// Utility function to determine node type.
inline NodeType getNodeType(const BeamlineNode& node) {
return std::visit(
[](auto&& element) -> NodeType {
using T = std::decay_t<decltype(element)>;
if constexpr (std::is_same_v<T, std::unique_ptr<DesignElement>>) {
if constexpr (std::is_same_v<T, DesignElement>) {
return NodeType::OpticalElement;
} else if constexpr (std::is_same_v<T, std::unique_ptr<DesignSource>>) {
} else if constexpr (std::is_same_v<T, DesignSource>) {
return NodeType::LightSource;
} else if constexpr (std::is_same_v<T, std::unique_ptr<Group>>) {
} else if constexpr (std::is_same_v<T, Group>) {
return NodeType::Group;
}
},
node);
node.data);
}

} // namespace RAYX
6 changes: 3 additions & 3 deletions Intern/rayx-core/src/Data/Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ void addBeamlineObjectFromXML(rapidxml::xml_node<>* node, Group& group, std::fil

// TODO: could likely be made nicer
if (isSource) {
group.addChild(std::move(ds));
group.addChild(BeamlineNode(std::move(*ds)));
} else {
parseElement(parser, de.get());
group.addChild(std::move(de));
group.addChild(BeamlineNode(std::move(*de)));
}
}

Expand All @@ -138,7 +138,7 @@ void handleObjectCollection(rapidxml::xml_node<>* collection, Group& group, cons
handleObjectCollection(object, *nestedGroup, filename);

// Add the group to the beamline.
group.addChild(std::move(nestedGroup));
group.addChild(BeamlineNode(std::move(*nestedGroup)));
} else if (strcmp(object->name(), "param") != 0) {
RAYX_EXIT << "received weird object->name(): " << object->name();
}
Expand Down
4 changes: 3 additions & 1 deletion Intern/rayx-core/tests/setupTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,16 @@ std::vector<RAYX::Ray> rayUiCompat(std::string filename, Sequential seq = Sequen

std::vector<RAYX::Ray> out;

auto compiled = beamline.compileElements();

for (auto ray_hist : hist) {
auto opt_ray = lastSequentialHit(ray_hist, beamline.numElements());

if (opt_ray) {
auto orig_r = *opt_ray;
auto r = orig_r;
int elem = (int)r.m_lastElement;
double btype = beamline.compileElements()[elem].m_behaviour.m_type; // m_element.m_behaviour.m_type;
double btype = compiled[elem].m_behaviour.m_type; // m_element.m_behaviour.m_type;
// these types of behaviours indicate that Ray-UI uses a DesignPlane::XY for this.
// Thus, (as rayx uses an XZ plane) to allow comparison with Ray-UI we need to swap the y and z coordinates here.
if (btype == BTYPE_IMAGE_PLANE || btype == BTYPE_SLIT) {
Expand Down
2 changes: 1 addition & 1 deletion Intern/rayx-ui/src/UserInterface/BeamlineOutliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void BeamlineOutliner::renderImGuiTreeFromGroup(RAYX::Group* group, RAYX::Beamli
if (!group) return;
int ctr = 0;
for (auto& child : *group) {
RAYX::NodeType nodeType = RAYX::getNodeType(child);
RAYX::NodeType nodeType = child->data.getNodeType();
std::string label;

// Use the node's name if possible.
Expand Down
5 changes: 4 additions & 1 deletion compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ echo

mode=Debug
enable_cuda=0
target=all

for var in "$@"
do
Expand All @@ -23,6 +24,8 @@ do
echo "--release 'Build in Release mode. Otherwise build in Debug mode'"
echo "--cuda 'Build with Cuda for tracing on GPU. Otherwise build without Cuda'"
exit
elif [[ "$var" == "--test" ]]; then
target=rayx-core-tst
else
echo "Error: Unknown option '$var'"
exit 1
Expand Down Expand Up @@ -60,4 +63,4 @@ fi

# compiling
echo Compiling ...
cmake --build $build --config $mode --target all -j $(nproc) --
cmake --build $build --config $mode --target $target -j $(nproc) --

0 comments on commit baf74f7

Please sign in to comment.