from girder_worker.plugins.vtk import dict_to_vtkarrays, dict_to_vtkrow import vtk vtk_builder = vtk.vtkMutableDirectedGraph() node_fields = input['node_fields'] edge_fields = input['edge_fields'] dict_to_vtkarrays(input['node_data'], node_fields, vtk_builder.GetVertexData()) if 'children' in input and len(input['children']) > 0: if 'edge_data' in input['children'][0]: dict_to_vtkarrays(input['children'][0]['edge_data'], edge_fields, vtk_builder.GetEdgeData()) def process_node(vtknode, node): if 'children' in node: for n in node['children']: vtkchild = vtk_builder.AddVertex() vtk_builder.AddGraphEdge(vtknode, vtkchild).GetId() dict_to_vtkrow(n['node_data'], vtk_builder.GetVertexData()) if 'edge_data' in n: dict_to_vtkrow(n['edge_data'], vtk_builder.GetEdgeData()) process_node(vtkchild, n) vtk_builder.AddVertex() dict_to_vtkrow(input['node_data'], vtk_builder.GetVertexData()) process_node(0, input) output = vtk.vtkTree() output.ShallowCopy(vtk_builder)
# Merge default attributes into nodes and edges for (_, data) in nodes: for (field, field_type) in six.iteritems(node_field_types): if field not in data: data[field] = field_type() for (_, _, data) in edges: for (field, field_type) in six.iteritems(edge_field_types): if field not in data: data[field] = field_type() # Add vtkArrays to the output data for nodes and edges # We can just use the first node and edge since they all have same attr name/types if nodes: dict_to_vtkarrays(nodes[0][1], node_field_types.keys(), output.GetVertexData()) if edges: dict_to_vtkarrays(edges[0][2], edge_field_types.keys(), output.GetEdgeData()) # This is a mapping of NetworkX nodes to VTK Vertex IDs so we can refer to them # when adding edges between NetworkX nodes later node_to_ids = {} for (node, data) in nodes: node_to_ids[node] = output.AddVertex() dict_to_vtkrow(data, output.GetVertexData())
from girder_worker.plugins.vtk import dict_to_vtkarrays, dict_to_vtkrow import vtk output = vtk.vtkTable() if len(input['rows']) > 0: dict_to_vtkarrays(input['rows'][0], input['fields'], output.GetRowData()) for d in input['rows']: dict_to_vtkrow(d, output.GetRowData())