7 #include "imstkTaskGraphVizWriter.h" 8 #include "imstkLogger.h" 9 #include "imstkTaskGraph.h" 10 #include "imstkColor.h" 19 if (m_inputGraph ==
nullptr)
21 LOG(WARNING) <<
"No input set, unable to write ComputeGraph";
26 double maxTime = std::numeric_limits<double>::min();
27 if (m_writeNodeComputeTimesColor)
29 for (
auto node : m_inputGraph->getNodes())
31 if (node->m_computeTime > maxTime)
33 maxTime = node->m_computeTime;
39 std::vector<Color> colorFunc = std::vector<Color>(3);
40 colorFunc[0] = Color::Blue;
41 colorFunc[1] = Color::Green;
42 colorFunc[2] = Color::Red;
43 const int colorFuncExtent =
static_cast<int>(colorFunc.size() - 1);
46 TaskNodeList critPath;
47 if (m_highlightCriticalPath)
52 auto edgeExists = [&](
const std::shared_ptr<TaskNode>& a,
const std::shared_ptr<TaskNode>& b)
54 TaskNodeList::iterator srcNode = std::find(critPath.begin(), critPath.end(), a);
56 return (srcNode != critPath.end() && *std::next(srcNode) == b);
62 file.open(m_fileName);
64 if (!file.is_open() || file.fail())
70 "digraph imstkTaskGraph\n" 74 "edge[arrowhead=vee, arrowtail=inv, arrowsize=.7, color=grey20];\n";
77 const TaskNodeVector& nodes = m_inputGraph->getNodes();
78 TaskNodeNameMap nodeIds;
79 for (
size_t i = 0; i < nodes.size(); i++)
81 const std::string nodeUniqueName =
"node" + std::to_string(i);
82 nodeIds[nodes[i]] = nodeUniqueName;
84 file <<
"\"" << nodeUniqueName <<
"\" [";
87 if (m_writeNodeComputeTimesText)
89 file <<
" label=\"" << nodes[i]->m_name <<
" (" << nodes[i]->m_computeTime <<
"ms)" <<
"\"";
93 file <<
" label=\"" << nodes[i]->m_name <<
'\"';
97 file <<
" style=filled";
100 if (m_writeNodeComputeTimesColor)
102 const double t = nodes[i]->m_computeTime / maxTime;
103 const int i1 =
static_cast<int>(t * colorFuncExtent);
104 const int i2 = std::min(colorFuncExtent, i1 + 1);
105 Color color = Color::lerpRgb(colorFunc[i1], colorFunc[i2], t);
106 file <<
" color=\"#" << color.
rgbHex() <<
"\"";
110 if (nodes[i]->m_isCritical)
112 file <<
" color=\"#8B2610\"";
116 file <<
" color=cornflowerblue";
119 file <<
"];" << std::endl;
123 const TaskNodeAdjList& adjList = m_inputGraph->getAdjList();
124 for (TaskNodeAdjList::const_iterator it = adjList.begin(); it != adjList.end(); it++)
126 std::shared_ptr<TaskNode> srcNode = it->first;
127 const TaskNodeSet& outputNodes = it->second;
128 for (TaskNodeSet::const_iterator jt = outputNodes.begin(); jt != outputNodes.end(); jt++)
130 std::shared_ptr<TaskNode> destNode = *jt;
131 file <<
'\"' << nodeIds[srcNode] <<
'\"' <<
" -> " <<
'\"' << nodeIds[destNode] <<
'\"';
133 if (m_highlightCriticalPath && edgeExists(srcNode, destNode))
135 file <<
"[color=red]";
void write()
Writes the graph to a file given the filename.
static TaskNodeList getCriticalPath(std::shared_ptr< TaskGraph > graph)
Computes the critical path.