1515#include " Framework/LogParsingHelpers.h"
1616#include " Framework/PaletteHelpers.h"
1717#include " FrameworkGUIDeviceInspector.h"
18+ #include " ../src/WorkflowHelpers.h"
1819#include " DebugGUI/imgui.h"
1920#include < algorithm>
2021#include < cmath>
@@ -95,6 +96,59 @@ void displayGrid(bool show_grid, ImVec2 offset, ImDrawList* draw_list)
9596 }
9697}
9798
99+ // Private helper struct for the graph model
100+ struct Node {
101+ int ID;
102+ char Name[32 ];
103+ ImVec2 Size;
104+ float Value;
105+ ImVec4 Color;
106+ int InputsCount, OutputsCount;
107+
108+ Node (int id, const char * name, float value, const ImVec4& color, int inputs_count, int outputs_count)
109+ {
110+ ID = id;
111+ strncpy (Name, name, 31 );
112+ Name[31 ] = 0 ;
113+ Value = value;
114+ Color = color;
115+ InputsCount = inputs_count;
116+ OutputsCount = outputs_count;
117+ }
118+ };
119+
120+ // Private helper struct for the layout of the graph
121+ struct NodePos {
122+ ImVec2 pos;
123+ static ImVec2 GetInputSlotPos (ImVector<Node> const & infos, ImVector<NodePos> const & positions, int nodeId, int slot_no)
124+ {
125+ ImVec2 const & pos = positions[nodeId].pos ;
126+ ImVec2 const & size = infos[nodeId].Size ;
127+ float inputsCount = infos[nodeId].InputsCount ;
128+ return ImVec2 (pos.x , pos.y + size.y * ((float )slot_no + 1 ) / (inputsCount + 1 ));
129+ }
130+ static ImVec2 GetOutputSlotPos (ImVector<Node> const & infos, ImVector<NodePos> const & positions, int nodeId, int slot_no)
131+ {
132+ ImVec2 const & pos = positions[nodeId].pos ;
133+ ImVec2 const & size = infos[nodeId].Size ;
134+ float outputsCount = infos[nodeId].OutputsCount ;
135+ return ImVec2 (pos.x + size.x , pos.y + size.y * ((float )slot_no + 1 ) / (outputsCount + 1 ));
136+ }
137+ };
138+
139+ // Private helper struct for the edges in the graph
140+ struct NodeLink {
141+ int InputIdx, InputSlot, OutputIdx, OutputSlot;
142+
143+ NodeLink (int input_idx, int input_slot, int output_idx, int output_slot)
144+ {
145+ InputIdx = input_idx;
146+ InputSlot = input_slot;
147+ OutputIdx = output_idx;
148+ OutputSlot = output_slot;
149+ }
150+ };
151+
98152void showTopologyNodeGraph (WorkspaceGUIState& state,
99153 const std::vector<DeviceInfo>& infos,
100154 const std::vector<DeviceSpec>& specs,
@@ -110,58 +164,6 @@ void showTopologyNodeGraph(WorkspaceGUIState& state,
110164
111165 ImGui::Begin (" Physical topology view" , nullptr , ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
112166
113- // Dummy
114- struct Node {
115- int ID;
116- char Name[32 ];
117- ImVec2 Size;
118- float Value;
119- ImVec4 Color;
120- int InputsCount, OutputsCount;
121-
122- Node (int id, const char * name, float value, const ImVec4& color, int inputs_count, int outputs_count)
123- {
124- ID = id;
125- strncpy (Name, name, 31 );
126- Name[31 ] = 0 ;
127- Value = value;
128- Color = color;
129- InputsCount = inputs_count;
130- OutputsCount = outputs_count;
131- }
132-
133- };
134-
135- struct NodePos {
136- ImVec2 pos;
137- static ImVec2 GetInputSlotPos (ImVector<Node> const & infos, ImVector<NodePos> const & positions, int nodeId, int slot_no)
138- {
139- ImVec2 const & pos = positions[nodeId].pos ;
140- ImVec2 const & size = infos[nodeId].Size ;
141- float inputsCount = infos[nodeId].InputsCount ;
142- return ImVec2 (pos.x , pos.y + size.y * ((float )slot_no + 1 ) / (inputsCount + 1 ));
143- }
144- static ImVec2 GetOutputSlotPos (ImVector<Node> const & infos, ImVector<NodePos> const & positions, int nodeId, int slot_no)
145- {
146- ImVec2 const & pos = positions[nodeId].pos ;
147- ImVec2 const & size = infos[nodeId].Size ;
148- float outputsCount = infos[nodeId].OutputsCount ;
149- return ImVec2 (pos.x + size.x , pos.y + size.y * ((float )slot_no + 1 ) / (outputsCount + 1 ));
150- }
151- };
152-
153- struct NodeLink {
154- int InputIdx, InputSlot, OutputIdx, OutputSlot;
155-
156- NodeLink (int input_idx, int input_slot, int output_idx, int output_slot)
157- {
158- InputIdx = input_idx;
159- InputSlot = input_slot;
160- OutputIdx = output_idx;
161- OutputSlot = output_slot;
162- }
163- };
164-
165167 static ImVector<Node> nodes;
166168 static ImVector<NodeLink> links;
167169 static ImVector<NodePos> positions;
@@ -180,18 +182,17 @@ void showTopologyNodeGraph(WorkspaceGUIState& state,
180182 for (int si = 0 ; si < specs.size (); ++si) {
181183 int oi = 0 ;
182184 for (auto && output : specs[si].outputChannels ) {
183- linkToIndex.insert (std::make_pair (output.name , LinkInfo{si, oi}));
185+ linkToIndex.insert (std::make_pair (output.name , LinkInfo{ si, oi }));
184186 oi += 1 ;
185187 }
186188 }
189+ // Do matching between inputs and outputs
187190 for (int si = 0 ; si < specs.size (); ++si) {
188191 auto & spec = specs[si];
189- // FIXME: display nodes using topological sort
190192 nodeList.push_back (Node (si, spec.id .c_str (), 0 .5f ,
191193 ImColor (255 , 100 , 100 ),
192194 spec.inputChannels .size (),
193195 spec.outputChannels .size ()));
194- positions.push_back (NodePos{ImVec2 (40 + 120 * si, 50 + (120 * si) % 500 )});
195196 int ii = 0 ;
196197 for (auto & input : spec.inputChannels ) {
197198 auto const & outName = input.name ;
@@ -204,6 +205,34 @@ void showTopologyNodeGraph(WorkspaceGUIState& state,
204205 ii += 1 ;
205206 }
206207 }
208+
209+ // ImVector does boudary checks, so I bypass the case there is no
210+ // edges.
211+ std::vector<TopoIndexInfo> sortedNodes = { { 0 , 0 } };
212+ if (links.size ()) {
213+ sortedNodes = WorkflowHelpers::topologicalSort (specs.size (), &(links[0 ].InputIdx ), &(links[0 ].OutputIdx ), sizeof (links[0 ]), links.size ());
214+ }
215+ // / We resort them again, this time with the added layer information
216+ std::sort (sortedNodes.begin (), sortedNodes.end ());
217+
218+ std::vector<int > layerEntries (1024 , 0 );
219+ std::vector<int > layerMax (1024 , 0 );
220+ for (auto & node : sortedNodes) {
221+ layerMax[node.layer < 1023 ? node.layer : 1023 ] += 1 ;
222+ }
223+
224+ assert (specs.size () == sortedNodes.size ());
225+ // FIXME: display nodes using topological sort
226+ // Update positions
227+ for (int si = 0 ; si < specs.size (); ++si) {
228+ auto & node = sortedNodes[si];
229+ assert (node.index == si);
230+ int xpos = 40 + 240 * node.layer ;
231+ int ypos = 300 + (600 / (layerMax[node.layer ] + 1 )) * (layerEntries[node.layer ] - layerMax[node.layer ] / 2 );
232+ positions.push_back (NodePos{ ImVec2 (xpos, ypos) });
233+ layerEntries[node.layer ] += 1 ;
234+ }
235+
207236 };
208237
209238 if (!inited) {
0 commit comments