feat: add graph traversal function

This commit is contained in:
parker
2025-07-06 14:29:25 +01:00
parent 58ac608f84
commit d6b630bc08
7 changed files with 73 additions and 35 deletions

View File

@@ -3,6 +3,8 @@
#include "Engine/Types.h"
#include <iostream>
#include <memory>
#include <stack>
#include <algorithm>
enzo::nt::OpId enzo::nt::NetworkManager::addOperator()
{
@@ -41,8 +43,38 @@ bool enzo::nt::NetworkManager::isValidOp(nt::OpId opId)
void enzo::nt::NetworkManager::setDisplayOp(OpId opId)
{
displayOp_=opId;
getTraversalGraph(opId);
}
void enzo::nt::NetworkManager::cookOp(enzo::nt::OpId opId)
{
std::cout << "COOKING: " << opId << "\n";
}
std::vector<enzo::nt::OpId> enzo::nt::NetworkManager::getTraversalGraph(enzo::nt::OpId opId)
{
std::stack<enzo::nt::OpId> traversalBuffer;
std::vector<enzo::nt::OpId> traversalGraph;
traversalBuffer.push(opId);
while(traversalBuffer.size()!=0)
{
enzo::nt::OpId currentOp = traversalBuffer.top();
std::cout << "cooking node: " << currentOp << "\n";
traversalBuffer.pop();
auto inputConnections = getGeoOperator(currentOp).getInputConnections();
for(auto connection : inputConnections)
{
traversalBuffer.push(connection->getInputOpId());
traversalGraph.push_back(connection->getInputOpId());
}
}
std::reverse(traversalGraph.begin(), traversalGraph.end());
return traversalGraph;
}
std::optional<enzo::nt::OpId> enzo::nt::NetworkManager::getDisplayOp()
{
return displayOp_;

View File

@@ -25,6 +25,9 @@ private:
std::unordered_map<enzo::nt::OpId, std::unique_ptr<enzo::nt::GeometryOperator>> gopStore_;
void cookOp(enzo::nt::OpId opId);
std::vector<enzo::nt::OpId> getTraversalGraph(enzo::nt::OpId opId);
// the highest operator id currently stored
enzo::nt::OpId maxOpId_=0;

View File

@@ -11,16 +11,16 @@ void enzo::nt::connectOperators(enzo::nt::OpId inputOpId, unsigned int inputInde
// get network manager
auto nm = enzo::nt::NetworkManager::getInstance();
auto inputOp = nm->getGeoOperator(inputOpId);
auto outputOp = nm->getGeoOperator(outputOpId);
auto& inputOp = nm->getGeoOperator(inputOpId);
auto& outputOp = nm->getGeoOperator(outputOpId);
auto newConnection = std::make_shared<nt::GeometryConnection>(inputOpId, inputIndex, outputOpId, outputIndex);
// set output on the upper operator
outputOp.addOutputConnection(newConnection);
inputOp.addOutputConnection(newConnection);
// set input on the lower operator
inputOp.addInputConnection(newConnection);
outputOp.addInputConnection(newConnection);
}
nt::GeometryOperator::GeometryOperator()
@@ -30,30 +30,13 @@ nt::GeometryOperator::GeometryOperator()
maxOutputs_=4;
}
// bool nt::GeometryOperator::setInput(unsigned int inputNumber, nt::OpId opId)
// {
// if(inputNumber>=maxInputs_)
// {
// return false;
// }
// inputIds_[inputNumber] = opId;
// return true;
// }
// bool nt::GeometryOperator::setOutput(unsigned int outputNumber, nt::OpId opId)
// {
// if(outputNumber>=maxOutputs_)
// {
// return false;
// }
// inputIds_[outputNumber] = opId;
// return true;
// }
void nt::GeometryOperator::addInputConnection(std::shared_ptr<nt::GeometryConnection> connection)
{
std::cout << "Input connection added\nConnecting ops " << connection->getInputOpId() << " -> " << connection->getOutputOpId() << "\n";
std::cout << "Connecting index " << connection->getInputIndex() << " -> " << connection->getOutputIndex() << "\n";
inputConnections_.push_back(connection);
std::cout << "size: " << inputConnections_.size() << "\n";
}
void nt::GeometryOperator::addOutputConnection(std::shared_ptr<nt::GeometryConnection> connection)
@@ -61,8 +44,31 @@ void nt::GeometryOperator::addOutputConnection(std::shared_ptr<nt::GeometryConne
std::cout << "Output connection added\nConnecting ops " << connection->getInputOpId() << " -> " << connection->getOutputOpId() << "\n";
std::cout << "Connecting index " << connection->getInputIndex() << " -> " << connection->getOutputIndex() << "\n";
outputConnections_.push_back(connection);
std::cout << "size: " << outputConnections_.size() << "\n";
}
std::vector<std::shared_ptr<const nt::GeometryConnection>> nt::GeometryOperator::getInputConnections() const
{
std::vector<std::shared_ptr<const nt::GeometryConnection>> inputConnections;
std::cout << "input connections size: " << inputConnections_.size() <<"\n";
for(std::shared_ptr<nt::GeometryConnection> connection : inputConnections_)
{
inputConnections.push_back(connection);
}
return inputConnections;
}
std::vector<std::shared_ptr<const nt::GeometryConnection>> nt::GeometryOperator::getOutputConnections() const
{
std::vector<std::shared_ptr<const nt::GeometryConnection>> outputConnections;
for(std::shared_ptr<nt::GeometryConnection> connection : outputConnections_)
{
outputConnections.push_back(connection);
}
return outputConnections;
}
// std::optional<nt::OpId> nt::GeometryOperator::getInput(unsigned int inputNumber) const
// {

View File

@@ -11,13 +11,15 @@ class GeometryOperator
{
public:
GeometryOperator();
// bool setInput(unsigned int inputNumber, nt::OpId opId);
// bool setOutput(unsigned int outputNumber, nt::OpId opId);
// std::optional<nt::OpId> getInput(unsigned int inputNumber) const;
// std::optional<nt::OpId> getOutput(unsigned int outputNumber) const;
// disable copying
GeometryOperator(const GeometryOperator&) = delete;
GeometryOperator& operator=(const GeometryOperator&) = delete;
void addInputConnection(std::shared_ptr<nt::GeometryConnection> connection);
void addOutputConnection(std::shared_ptr<nt::GeometryConnection> connection);
std::vector<std::shared_ptr<const GeometryConnection>> getInputConnections() const;
std::vector<std::shared_ptr<const GeometryConnection>> getOutputConnections() const;