-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "Group.h" | ||
#include <stdexcept> | ||
|
||
#include "DesignSource.h" | ||
#include "DesignElement.h" | ||
|
||
namespace RAYX { | ||
|
||
// Implementation of Group's getNodeType | ||
NodeType Group::getNodeType() const { | ||
// A Group is inherently of NodeType::Group | ||
return NodeType::Group; | ||
} | ||
|
||
// Implementation of getNode to access a child node by index | ||
const BeamlineNode& Group::getNode(size_t index) const { | ||
if (index >= children.size()) { | ||
throw std::out_of_range("Index out of range in Group::getNode"); | ||
} | ||
return children[index]; | ||
} | ||
|
||
void Group::traverse(const std::function<void(const BeamlineNode&)>& callback) const { | ||
// Apply the callback to each child | ||
for (const auto& child : children) { | ||
callback(child); | ||
// If the child is a Group, recursively traverse it | ||
if (std::holds_alternative<Group>(child)) { | ||
std::get<Group>(child).traverse(callback); | ||
} | ||
} | ||
} | ||
|
||
// Add a child node (move semantics) | ||
void Group::addChild(BeamlineNode&& child) { children.push_back(std::move(child)); } | ||
|
||
// Add a child node (copy semantics) | ||
void Group::addChild(const BeamlineNode& child) { children.push_back(child); } | ||
|
||
// Retrieve all DesignElements (deep) | ||
std::vector<DesignElement> Group::getAllElements() const { | ||
std::vector<DesignElement> elements; | ||
traverse([&elements](const BeamlineNode& node) { | ||
if (std::holds_alternative<DesignElement>(node)) { | ||
elements.push_back(std::get<DesignElement>(node)); | ||
} | ||
}); | ||
return elements; | ||
} | ||
|
||
// Retrieve all DesignSources (deep) | ||
std::vector<DesignSource> Group::getAllSources() const { | ||
std::vector<DesignSource> sources; | ||
traverse([&sources](const BeamlineNode& node) { | ||
if (std::holds_alternative<DesignSource>(node)) { | ||
sources.push_back(std::get<DesignSource>(node)); | ||
} | ||
}); | ||
return sources; | ||
} | ||
|
||
// Retrieve all Groups (deep) | ||
std::vector<Group> Group::getAllGroups() const { | ||
std::vector<Group> groups; | ||
traverse([&groups](const BeamlineNode& node) { | ||
if (std::holds_alternative<Group>(node)) { | ||
groups.push_back(std::get<Group>(node)); | ||
} | ||
}); | ||
return groups; | ||
} | ||
|
||
} // namespace RAYX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <variant> | ||
#include <vector> | ||
|
||
namespace RAYX { | ||
|
||
class Group; | ||
class DesignElement; | ||
class DesignSource; | ||
|
||
using BeamlineNode = std::variant<DesignElement, DesignSource, Group>; | ||
|
||
enum class NodeType { OpticalElement, LightSource, Group }; | ||
|
||
// 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, DesignElement>) { | ||
return NodeType::OpticalElement; | ||
} else if constexpr (std::is_same_v<T, DesignSource>) { | ||
return NodeType::LightSource; | ||
} else if constexpr (std::is_same_v<T, Group>) { | ||
return NodeType::Group; | ||
} | ||
}, | ||
node); | ||
} | ||
|
||
class Group { | ||
public: | ||
NodeType getNodeType() const; | ||
const BeamlineNode& getNode(size_t index) const; | ||
|
||
void traverse(const std::function<void(const BeamlineNode&)>& callback) const; | ||
|
||
void addChild(BeamlineNode&& child); | ||
void addChild(const BeamlineNode& child); | ||
|
||
// New methods for retrieving elements, sources, and groups | ||
std::vector<DesignElement> getAllElements() const; | ||
std::vector<DesignSource> getAllSources() const; | ||
std::vector<Group> getAllGroups() const; | ||
|
||
private: | ||
std::vector<BeamlineNode> children; // Children of the node | ||
}; | ||
} // namespace RAYX |