-
Notifications
You must be signed in to change notification settings - Fork 16
/
layout.py
148 lines (132 loc) · 3.78 KB
/
layout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from ctypes import CDLL, CFUNCTYPE, POINTER, Structure, c_float, c_int, byref
dll = CDLL('_layout.so')
MAX_EDGES = 128
MAX_NODES = 128
WEIGHTS = {
'node_node': 100,
'node_edge': 100,
'edge_edge': 10,
'rank': 5,
'length': 1,
'area': 1,
}
class Node(Structure):
_fields_ = [
('rank', c_int),
('x', c_float),
('y', c_float),
]
class Edge(Structure):
_fields_ = [
('a', c_int),
('b', c_int),
]
class Model(Structure):
_fields_ = [
('edge_count', c_int),
('node_count', c_int),
('edges', Edge * MAX_EDGES),
('nodes', Node * MAX_NODES),
]
class Attrib(Structure):
_fields_ = [
('node_node', c_float),
('node_edge', c_float),
('edge_edge', c_float),
('rank', c_float),
('length', c_float),
('area', c_float),
]
CALLBACK_FUNC = CFUNCTYPE(None, POINTER(Model), c_float)
dll.anneal.restype = c_float
dll.anneal.argtypes = [
POINTER(Model), POINTER(Attrib),
c_float, c_float, c_int, CALLBACK_FUNC]
def anneal(model, weights, max_temp, min_temp, steps, callback_func):
return dll.anneal(
byref(model), byref(weights),
max_temp, min_temp, steps, CALLBACK_FUNC(callback_func))
def cyclic(nodes, inputs):
remaining = set(nodes)
while remaining:
count = 0
for node in nodes:
if node not in remaining:
continue
if remaining & inputs[node]:
continue
count += 1
remaining.remove(node)
if count == 0:
return True
return False
def rank(inputs, node, memo=None):
memo = memo or {}
if node in memo:
return memo[node]
elif inputs[node]:
result = min(rank(inputs, x, memo) for x in inputs[node]) + 1
else:
result = 1
memo[node] = result
return result
def topographical_sort(nodes, inputs):
if cyclic(nodes, inputs):
ranks = [0] * len(nodes)
else:
memo = {}
ranks = [rank(inputs, node, memo) for node in nodes]
return sorted(zip(ranks, nodes))
def create_model(edges):
nodes = set()
for a, b in edges:
nodes.add(a)
nodes.add(b)
nodes = sorted(nodes)
inputs = dict((x, set()) for x in nodes)
outputs = dict((x, set()) for x in nodes)
for a, b in edges:
inputs[b].add(a)
outputs[a].add(b)
topo = topographical_sort(nodes, inputs)
ranks = dict((node, rank) for rank, node in topo)
lookup = dict((node, index) for index, node in enumerate(nodes))
model = Model()
model.edge_count = len(edges)
model.node_count = len(nodes)
for index, node in enumerate(nodes):
model.nodes[index].rank = ranks[node]
model.nodes[index].x = 0
model.nodes[index].y = 0
for index, (a, b) in enumerate(edges):
model.edges[index].a = lookup[a]
model.edges[index].b = lookup[b]
return model, lookup
def create_attrib(data):
keys = [
'node_node',
'node_edge',
'edge_edge',
'rank',
'length',
'area',
]
result = Attrib()
for key in keys:
setattr(result, key, data.get(key, WEIGHTS[key]))
return result
def layout(edges, weights=None, steps=100000, listener=None):
model, lookup = create_model(edges)
weights = create_attrib(weights or {})
def create_result(model):
nodes = {}
for key, index in lookup.items():
node = model.nodes[index]
nodes[key] = (node.x, node.y)
return nodes
def callback_func(model, energy):
if listener is not None:
result = create_result(model.contents)
listener(result, energy)
anneal(model, weights, 100, 0.01, steps, callback_func)
return create_result(model)