@@ -220,17 +220,42 @@ auto CFrontendIR::createNamedFresnel(const std::string_view name) -> typed_point
220220 return frH;
221221}
222222
223- void CFrontendIR::printDotGraph ( std::ostringstream& str) const
223+ void CFrontendIR::SDotPrinter::operator ()( std::ostringstream& str)
224224{
225225 str << " digraph {\n " ;
226+
227+ auto drainExprStack = [&]()->void
228+ {
229+ while (!exprStack.empty ())
230+ {
231+ const auto entry = exprStack.top ();
232+ exprStack.pop ();
233+ const auto nodeID = m_ir->getNodeID (entry);
234+ str << " \n\t " << m_ir->getLabelledNodeID (entry);
235+ const auto * node = m_ir->getObjectPool ().deref (entry);
236+ const auto childCount = node->getChildCount ();
237+ if (childCount)
238+ {
239+ for (auto childIx=0 ; childIx<childCount; childIx++)
240+ {
241+ const auto childHandle = node->getChildHandle (childIx);
242+ if (const auto child=m_ir->getObjectPool ().deref (childHandle); child)
243+ {
244+ str << " \n\t " << nodeID << " -> " << m_ir->getNodeID (childHandle) << " [label=\" " << node->getChildName_impl (childIx) << " \" ]" ;
245+ const auto visited = visitedNodes.find (childHandle);
246+ if (visited!=visitedNodes.end ())
247+ continue ;
248+ exprStack.push (childHandle);
249+ visitedNodes.insert (childHandle);
250+ }
251+ }
252+ }
253+ // special printing
254+ node->printDot (str,nodeID);
255+ }
256+ };
257+ drainExprStack ();
226258
227- core::unordered_set<typed_pointer_type<const INode>> visitedNodes;
228- // should probably size it better, if I knew total node count allocated or live
229- visitedNodes.reserve (m_rootNodes.size ()<<3 );
230- // TODO: track layering depth and indent accordingly?
231- // assign in reverse because we want materials to print in order
232- core::vector<typed_pointer_type<const CLayer>> layerStack (m_rootNodes.rbegin (),m_rootNodes.rend ());
233- core::stack<typed_pointer_type<const IExprNode>> exprStack;
234259 while (!layerStack.empty ())
235260 {
236261 const auto layerHandle = layerStack.back ();
@@ -240,22 +265,22 @@ void CFrontendIR::printDotGraph(std::ostringstream& str) const
240265 if (visited!=visitedNodes.end ())
241266 continue ;
242267 visitedNodes.insert (layerHandle);
243- const auto * layerNode = getObjectPool ().deref (layerHandle);
268+ const auto * layerNode = m_ir-> getObjectPool ().deref (layerHandle);
244269 //
245- const auto layerID = getNodeID (layerHandle);
246- str << " \n\t " << getLabelledNodeID (layerHandle);
270+ const auto layerID = m_ir-> getNodeID (layerHandle);
271+ str << " \n\t " << m_ir-> getLabelledNodeID (layerHandle);
247272 //
248273 if (layerNode->coated )
249274 {
250- str << " \n\t " << layerID << " -> " << getNodeID (layerNode->coated ) << " [label=\" coats\" ]\n " ;
275+ str << " \n\t " << layerID << " -> " << m_ir-> getNodeID (layerNode->coated ) << " [label=\" coats\" ]\n " ;
251276 layerStack.push_back (layerNode->coated );
252277 }
253278 auto pushExprRoot = [&](const typed_pointer_type<const IExprNode> root, const std::string_view edgeLabel)->void
254279 {
255280 if (!root)
256281 return ;
257282 // print the link from the layer to the expression
258- str << " \n\t " << layerID << " -> " << getNodeID (root) << " [label=\" " << edgeLabel << " \" ]" ;
283+ str << " \n\t " << layerID << " -> " << m_ir-> getNodeID (root) << " [label=\" " << edgeLabel << " \" ]" ;
259284 // but not the expression again
260285 const auto visited = visitedNodes.find (root);
261286 if (visited!=visitedNodes.end ())
@@ -266,33 +291,7 @@ void CFrontendIR::printDotGraph(std::ostringstream& str) const
266291 pushExprRoot (layerNode->brdfTop ," Top BRDF" );
267292 pushExprRoot (layerNode->btdf ," BTDF" );
268293 pushExprRoot (layerNode->brdfBottom ," Bottom BRDF" );
269- while (!exprStack.empty ())
270- {
271- const auto entry = exprStack.top ();
272- exprStack.pop ();
273- const auto nodeID = getNodeID (entry);
274- str << " \n\t " << getLabelledNodeID (entry);
275- const auto * node = getObjectPool ().deref (entry);
276- const auto childCount = node->getChildCount ();
277- if (childCount)
278- {
279- for (auto childIx=0 ; childIx<childCount; childIx++)
280- {
281- const auto childHandle = node->getChildHandle (childIx);
282- if (const auto child=getObjectPool ().deref (childHandle); child)
283- {
284- str << " \n\t " << nodeID << " -> " << getNodeID (childHandle) << " [label=\" " << node->getChildName_impl (childIx) << " \" ]" ;
285- const auto visited = visitedNodes.find (childHandle);
286- if (visited!=visitedNodes.end ())
287- continue ;
288- exprStack.push (childHandle);
289- visitedNodes.insert (childHandle);
290- }
291- }
292- }
293- // special printing
294- node->printDot (str,nodeID);
295- }
294+ drainExprStack ();
296295 }
297296
298297 // TODO: print image views
0 commit comments