Enzo
Loading...
Searching...
No Matches
NetworkGraph.h
1#pragma once
2#include "Engine/Core/Types.h"
3#include "Engine/NetworkGraph/Connection.h"
4#include "Engine/NetworkGraph/Unit.h"
5#include <optional>
6#include <unordered_map>
7#include <unordered_set>
8#include <vector>
9
10namespace enzo::nt {
11
24{
25 public:
27 void connect(const Connection& connection);
28
30 void disconnect(const Connection& connection);
31
33 std::vector<Connection> getInputs(NodeId target) const;
34
37 std::optional<Connection> getInputConnection(NodeId target, unsigned int inputIndex) const;
38
40 std::vector<Connection> getOutputs(NodeId source) const;
41
43 std::vector<Connection> getConnections() const;
44
48 void setCapturedDependencies(const Unit& dependent, const std::vector<Unit>& dependencies);
49
53 void setTimeDependent(const Unit& dependent, bool dependsOnTime);
54
57 std::vector<Unit> getTimeDependents() const;
58
60 void removeNode(NodeId nodeId);
61
63 void clear();
64
67 std::vector<NodeId> getCookOrder(NodeId target) const;
68
72 std::vector<Unit> getDependents(const Unit& changed) const;
73
74 private:
75 using ConnectionMap = std::unordered_map<NodeId, std::vector<Connection>>;
76 using CapturedMap = std::unordered_map<Unit, std::vector<Unit>>;
77
79 static void eraseConnection_(ConnectionMap& side, NodeId key, const Connection& connection);
80
82 static void eraseConnectionsTouching_(ConnectionMap& side, NodeId nodeId);
83
85 static void eraseCapturedTouching_(CapturedMap& map, NodeId nodeId);
86
88 static void eraseUnit_(CapturedMap& map, const Unit& key, const Unit& value);
89
91 static void addDependent_(
92 const Unit& unit,
93 std::vector<Unit>& dependents,
94 std::unordered_set<Unit>& seen,
95 std::vector<Unit>& pending
96 );
97
99 void addToCookOrder_(
100 NodeId nodeId,
101 std::vector<NodeId>& nodeOrder,
102 std::unordered_set<NodeId>& addedNodes,
103 std::unordered_set<NodeId>& nodesBeingAdded
104 ) const;
105
106 // Input connections keyed by the downstream node.
107 ConnectionMap byTarget_;
108 // Output connections keyed by the upstream node.
109 ConnectionMap bySource_;
110 // Captured edges are mixed granularity. The source is stored at node level
111 // since dirtying is node wide, while the reader is stored per parameter
112 // component so re-evaluating one component leaves the others intact.
113 // getDependents collapses the reader back to its node to bridge the two.
114
115 // Captured readers keyed by the node they read.
116 CapturedMap capturedDependents_;
117 // Captured reads keyed by the reading parameter component.
118 CapturedMap capturedDependencies_;
119
120 // The units that read the scene time, at the same mixed granularity as the
121 // captured maps.
122 std::unordered_set<Unit> timeDependents_;
123};
124
125} // namespace enzo::nt
Basic attribute, parameter, and node types for Enzo.
uint64_t NodeId
The unique ID assigned to each node in the network.
Definition Types.h:306
The single owner of the network's wiring and dependencies.
Definition NetworkGraph.h:24
std::vector< NodeId > getCookOrder(NodeId target) const
Returns the nodes to cook before target, in cook order.
Definition NetworkGraph.cpp:217
std::optional< Connection > getInputConnection(NodeId target, unsigned int inputIndex) const
Returns the connection on one input of target, if any.
Definition NetworkGraph.cpp:56
std::vector< Connection > getOutputs(NodeId source) const
Returns the connections leaving source.
Definition NetworkGraph.cpp:67
void disconnect(const Connection &connection)
Removes a wired connection between two nodes.
Definition NetworkGraph.cpp:14
void setTimeDependent(const Unit &dependent, bool dependsOnTime)
Records whether a unit reads the scene time.
Definition NetworkGraph.cpp:122
std::vector< Unit > getTimeDependents() const
Returns every unit the scene time reaches, in no particular order.
Definition NetworkGraph.cpp:130
std::vector< Unit > getDependents(const Unit &changed) const
Returns everything that depends on changed, directly or through a chain.
Definition NetworkGraph.cpp:254
std::vector< Connection > getInputs(NodeId target) const
Returns the connections feeding target, ordered by input index.
Definition NetworkGraph.cpp:45
void setCapturedDependencies(const Unit &dependent, const std::vector< Unit > &dependencies)
Replaces every captured dependency of one parameter at once.
Definition NetworkGraph.cpp:82
void connect(const Connection &connection)
Records a wired connection between two nodes.
Definition NetworkGraph.cpp:8
std::vector< Connection > getConnections() const
Returns every wired connection in the graph, in no particular order.
Definition NetworkGraph.cpp:74
void removeNode(NodeId nodeId)
Removes every connection and captured edge touching the node.
Definition NetworkGraph.cpp:142
void clear()
Empties the graph.
Definition NetworkGraph.cpp:208
One wired link between two nodes, the ground truth of the network's wiring.
Definition Connection.h:20
A point in the network graph, either a node output or a parameter.
Definition Unit.h:20