Enzo
Loading...
Searching...
No Matches
NetworkGraph.h
1#pragma once
2#include "Engine/Core/Types.h"
3#include "Engine/NetworkGraph/NodeLink.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 NodeLink& nodeLink);
28
30 void disconnect(const NodeLink& nodeLink);
31
33 std::vector<NodeLink> getInputs(NodeId target) const;
34
37 std::optional<NodeLink> getInputNodeLink(NodeId target, unsigned int inputIndex) const;
38
40 std::vector<NodeLink> getOutputs(NodeId source) const;
41
43 std::vector<NodeLink> getNodeLinks() 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 NodeLinkMap = std::unordered_map<NodeId, std::vector<NodeLink>>;
76 using CapturedMap = std::unordered_map<Unit, std::vector<Unit>>;
77
79 static void eraseNodeLink_(NodeLinkMap& side, NodeId key, const NodeLink& nodeLink);
80
82 static void eraseNodeLinksTouching_(NodeLinkMap& 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 node links keyed by the downstream node.
107 NodeLinkMap byTarget_;
108 // Output node links keyed by the upstream node.
109 NodeLinkMap 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:335
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
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< NodeLink > getNodeLinks() const
Returns every node link in the graph, in no particular order.
Definition NetworkGraph.cpp:74
std::vector< NodeLink > getOutputs(NodeId source) const
Returns the node links leaving source.
Definition NetworkGraph.cpp:67
std::vector< Unit > getDependents(const Unit &changed) const
Returns everything that depends on changed, directly or through a chain.
Definition NetworkGraph.cpp:254
void disconnect(const NodeLink &nodeLink)
Removes a node link between two nodes.
Definition NetworkGraph.cpp:14
void setCapturedDependencies(const Unit &dependent, const std::vector< Unit > &dependencies)
Replaces every captured dependency of one parameter at once.
Definition NetworkGraph.cpp:82
void removeNode(NodeId nodeId)
Removes every node link and captured edge touching the node.
Definition NetworkGraph.cpp:142
void connect(const NodeLink &nodeLink)
Records a node link between two nodes.
Definition NetworkGraph.cpp:8
std::optional< NodeLink > getInputNodeLink(NodeId target, unsigned int inputIndex) const
Returns the node link on one input of target, if any.
Definition NetworkGraph.cpp:56
void clear()
Empties the graph.
Definition NetworkGraph.cpp:208
std::vector< NodeLink > getInputs(NodeId target) const
Returns the node links feeding target, ordered by input index.
Definition NetworkGraph.cpp:45
A point in the network graph, either a node output or a parameter.
Definition Unit.h:20