/
graph.py
619 lines (552 loc) · 25.6 KB
/
graph.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# This file is part of Architype.
# Architype is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Architype is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Architype. If not, see <http://www.gnu.org/licenses/>.
# Author Jonathan Byrne 2014
""" creates graphs using methods from geometry class and turtle
graphics. built on the graph class from networkx
node methods:
pass in point[x,y,z], if new point then add to graph, return node_id
edge methods:
pass in node_ids, if no edge between nodes then add edge
Copyright (c) 2010 Jonathan Byrne, Erik Hemberg and James
McDermott Hereby licensed under the GNU GPL v3."""
import networkx as nx
import copy, math, time, os
from geometry import *
class GraphState(object):
"""This class is used for generating graphs using turtle graphics
methods. It stores the current position, orientation, and node.
Idea is: there's a "cursor", like a turtle, which moves around,
rotates, etc. There's also current_node which isn't used for now.
As in L-systems, we have save and restore functions for state,
which is stored in a stack."""
def __init__(self, pos=None, orientation=None):
if pos is None:
# x, y, z
self.position = 0, 0, 0
else:
self.position = pos
if orientation is None:
# orientation is a vector
self.orientation = 1, 0, 0
else:
self.orientation = orientation
self.current_node = 0
class graph(nx.Graph):
""" An extension of the networkx class that we use for
representing our designs as a graph with xyz coordinates stored in
the nodes. It was originally built for generating turtle-graphics"""
def __init__(self, *args, **kwargs):
super(graph, self).__init__(*args, **kwargs)
self.states = [GraphState()]
self.state = self.states[0]
#You can debug your graphs by setting self.save=True. This
#generates a graph in the population folder every time a node
#is added, then movie.py will turn it into an avi
self.save = True
self.pop_folder = os.getcwd() + "/population/"
self.frame_count = 0
self.insulators = True
###########Turtle Methods############
def initial_edge(self, dist):
"""This function ensures that every graph is non-empty."""
self.add_node(0, xyz=self.state.position)
self.project(dist)
self.add_node(1, xyz=self.state.position)
self.add_edge(0, 1)
self.save_graph()
def rotate(self, a, b, c):
""""change the turtle's orientation. Not actual rotation"""
self.state.orientation = (self.state.orientation[0] + a,
self.state.orientation[1] + b,
self.state.orientation[2] + c)
def move(self, x_dist, y_dist, z_dist):
""" Move the turtle, don't add any edge. """
self.state.position = (self.state.position[0] + x_dist,
self.state.position[1] + y_dist,
self.state.position[2] + z_dist)
def project(self, dist):
"""Move the turtle, taking account of orientation. Don't add
an edge."""
self.move(dist * self.state.orientation[0],
dist * self.state.orientation[1],
dist * self.state.orientation[2])
def nearest_node_id(self):
""" What node id is nearest turtle's current position? Unused in
blender_graph.bnf for now."""
pos = self.state.position
dist = lambda k: euclidean_distance(self.node[k]['xyz'], pos)
return min(self.node, key=dist)
def incr_current_node(self):
"""New current_node."""
self.state.current_node += 1
self.state.current_node %= self.order()
def add_node_connect2(self, id1, id2):
"""create a node and connect it to two existing nodes"""
nodeid = self.get_unused_nodeid()
self.add_node(nodeid, xyz=self.state.position)
self.add_edge(nodeid, id1)
self.add_edge(nodeid, id2)
self.save_graph()
def save_state(self):
"""save and restore methods for the stack of states."""
self.states.append(copy.copy(self.state))
def restore_state(self):
"""pop current state or create graph if None"""
self.state = self.states.pop()
if self.state is None:
self.state = GraphState()
######### GRAPH METHODS ########
def get_unused_nodeid(self):
"""The order of a graph is the number of nodes. Nodes are
numbered sequentially as we add them (see add_edge), so the
order is the number of the next free id."""
return self.order()
def add_edge(self, node_a, node_b, attr_dict=None, **attr):
"""overriding graph add edge so that it can save graphs"""
nx.Graph.add_edge(self, node_a, node_b, attr_dict, **attr)
self.save_graph()
# @profile
def add_unique_node(self, coords, node_type):
"""Add a node and connect it to two others, ensuring that it's
not lonesome"""
#TODO Assert that all nodes are lists of ints and not empty, when? Catch exceptions
new = True
x, y, z = coords[0], coords[1], coords[2]
a_coords = [int(x), int(y), int(z)]
for node in self.node:
b_coords = self.node[node]['xyz']
if a_coords == b_coords:
new = False
node_id = node
break
if new:
node_id = self.get_unused_nodeid()
self.add_node(node_id, xyz=a_coords, label=node_type)
return node_id
def connect_by_height(self):
"""connect all nodes in the graph that share the
same height (z-axis)"""
for node_a in self.node:
for node_b in self.node:
if not self.has_edge(node_a, node_b):
pt1 = self.node[node_a]["xyz"]
pt2 = self.node[node_b]["xyz"]
if pt1[2] == pt2[2]:
self.add_edge(node_a, node_b)
def connect_by_offset_height(self):
"""connect all nodes in the graph that share the
same height (z-axis)but are offset"""
for node_a in self.node:
for node_b in self.node:
if not self.has_edge(node_a, node_b):
pt1 = self.node[node_a]["xyz"]
pt2 = self.node[node_b]["xyz"]
if pt1[2] == pt2[2]:
if not pt1[1] == pt2[1]:
if not pt1[0] == pt2[0]:
self.add_edge(node_a, node_b)
def connect_neighbours(self, node_list, length, range=False):
"""connect all nodes on a list within a given range"""
for node_a in node_list:
for node_b in node_list:
if not self.has_edge(node_a, node_b):
pt1 = self.node[node_a]["xyz"]
pt2 = self.node[node_b]["xyz"]
if range:
if int(distance(pt1, pt2)) < length:
self.add_edge(node_a, node_b)
else:
if int(distance(pt1, pt2)) == length:
self.add_edge(node_a, node_b)
def connect_all_neighbours(self, length):
"""connect all nodes in the graph within a given range
CAUTION: This is an expensive operation on big graphs"""
for node_a in self.node:
for node_b in self.node:
if not self.has_edge(node_a, node_b):
pt1 = self.node[node_a]["xyz"]
pt2 = self.node[node_b]["xyz"]
if int(distance(pt1, pt2)) == length:
self.add_edge(node_a, node_b)
def connect_nodes(self, node_list):
"""adds edges between nodes that are adjacent on a list"""
for i in range(len(node_list) - 1):
self.add_edge(node_list[i], node_list[i + 1])
def connect_sorted_nodes(self, node_list):
"""adds edges between nodes that are sorted in order of creation"""
node_list.sort()
for i in range(len(node_list) - 1):
self.add_edge(node_list[i], node_list[i + 1])
def add_nodes(self, point_list, node_type, connect=False):
"""adds all coords on a list to the graph"""
id_list = []
for point in point_list:
idx = self.add_unique_node(point, node_type)
id_list.append(idx)
if connect:
self.connect_nodes(id_list)
return id_list
def weave_nodes(self, a_list, b_list, offset):
for i in range(len(a_list)):
off = (i + offset) % len(b_list)
self.add_edge(a_list[i], b_list[i])
self.add_edge(a_list[i], b_list[off])
self.add_edge(a_list[off], b_list[i])
def sierpinski(self, tri, node_type, depth):
if depth > 1:
node_ids = []
triangles = []
triangles.append((tri[0],midpoint(tri[0], tri[1]),
midpoint(tri[0], tri[2])))
triangles.append((tri[1],midpoint(tri[1], tri[0]),
midpoint(tri[1], tri[2])))
triangles.append((tri[2],midpoint(tri[2], tri[0]),
midpoint(tri[2], tri[1])))
triangles.append((midpoint(tri[0], tri[1]),
midpoint(tri[1], tri[2]),
midpoint(tri[0], tri[2])))
for idx, triangle in enumerate(triangles):
ids = self.sierpinski(triangle, node_type, depth-1)
node_ids.extend(ids)
return node_ids
else:
triangle = [tri[0], tri[1], tri[2], tri[0]]
ids = self.add_nodes(triangle, node_type, True)
return ids
def varinski(self, tri, node_type, depths):
triangles = []
node_ids = []
triangles.append((tri[0],midpoint(tri[0], tri[1]),
midpoint(tri[0], tri[2])))
triangles.append((tri[1],midpoint(tri[1], tri[0]),
midpoint(tri[1], tri[2])))
triangles.append((tri[2],midpoint(tri[2], tri[0]),
midpoint(tri[2], tri[1])))
triangles.append((midpoint(tri[0], tri[1]),
midpoint(tri[1], tri[2]),
midpoint(tri[0], tri[2])))
for idx, triangle in enumerate(triangles):
ids = self.sierpinski(triangle, node_type, depths[idx])
node_ids.extend(ids)
return node_ids
def add_star_to_node(self, node, npts, node_type="none"):
"""Given a node id, and a radius, add a number of new nodes
and edges from each to the central node."""
node = self.get_node_idx_mod(node)
xyz = self.get_node_data(node)
id_list = []
for i in range(npts):
theta = 2 * math.pi * i / npts
newxyz = (xyz[0] + math.cos(theta), xyz[1] + 3, xyz[2]
+ math.sin(theta))
new_nodeid = self.get_unused_nodeid()
self.add_node(new_nodeid, xyz=newxyz, label=node_type)
self.add_edge(node, new_nodeid)
id_list.append(new_nodeid)
return id_list
def add_edge_between_existing_nodes(self, x_node, y_node):
"""this function assumes x and y exist."""
self.add_edge(x_node, y_node)
def get_node_idx_mod(self, x):
"""Given an integer, return a node id guaranteed to exist."""
return x % self.order()
def get_node_idx_mod_exclude_y(self, x, y):
"""Given an integer x, return a node id guaranteed to exist,
and NOT node id y"""
tmp = x % (self.order() - 1)
if tmp >= y:
tmp += 1
return tmp
def get_node_idx_float(self, x):
"""Given a number, return a node id guaranteed to exist."""
return int(x * self.order())
def get_node_idx_float_exclude_y(self, x, y):
"""Given a number, return an existing node id, not y"""
tmp = int(x * (self.order() - 1))
if tmp >= y:
tmp += 1
return tmp
def get_nodes_with_n_edges(self, n):
"""What nodes have exactly n edges?"""
return [node for node in self.adj
if self.degree(node) == n]
def get_nth_node_with_m_edges_mod(self, n, m):
"""Return a particular node of those with m edges"""
nodes = self.get_nodes_with_n_edges(m)
if len(nodes):
return nodes[n % len(nodes)]
else:
return None
def check_graph(self):
"""Scans graph for duplicates which would break slffea.
This is an expensive method so its only used for debugging"""
duplicate_count = 0
for a_node in self.node:
for b_node in self.node:
if a_node != b_node:
a_coord = self.node[a_node]["xyz"]
b_coord = self.node[b_node]["xyz"]
if a_coord == b_coord:
duplicate_count += 1
if duplicate_count > 0:
print "duplicates!", duplicate_count
else:
print "no duplicates"
def get_node_data(self, nodeid):
"""Get the cartesian coordinates of a node."""
return self.node[nodeid]["xyz"]
def get_label(self, nodeid):
"""return label info, used for identifying different parts of
the structure"""
return self.node[nodeid]["label"]
def transpose_xz_coords(self):
for node_id in self.node:
xyz = self.node[node_id]['xyz']
new_xyz = [xyz[2], xyz[1], xyz[0]]
self.node[node_id]['xyz'] = new_xyz
def rotate_points_around_xy_plane(self, points, sectors):
tangent = int(360 / sectors)
for i in range(1, sectors):
tangent_angle = tangent * i
rotated_points = xy_rotate_points(points, tangent_angle)
rotated_ids = self.add_nodes(rotated_points, "rotate")
self.connect_nodes(rotated_ids)
def copy_and_rotate_around_xy_plane(self, original, tangent):
#tangent = int(360 / sectors)
orig_copy = original.copy()
offset_copy = original.copy()
for nodeid in offset_copy.node:
xyz = offset_copy.node[nodeid]["xyz"]
xyz = xy_rotate(xyz, tangent)
xyz = [int(round(xyz[0], 0)), int(round(xyz[1], 0)),
int(round(xyz[2], 0))]
offset_copy.node[nodeid]["xyz"] = xyz
new_graph = nx.union(orig_copy, offset_copy, rename=("G-", "H-"))
new_graph.frame_count = original.frame_count
return new_graph
def sanitise_pylon(self, original, width):
new_graph = original.copy()
new_graph.frame_count = original.frame_count
excess_nodes = []
insulator_nodes = []
for nodeid in new_graph.node:
xyz = new_graph.node[nodeid]['xyz']
if xyz[2] == 0:
new_graph.node[nodeid]['label'] = 'base'
if new_graph.node[nodeid]['label'] == ('line'):
if abs(new_graph.node[nodeid]['xyz'][0]) > width:
found = self.traverse_node_type(new_graph,nodeid, 'arm')
excess_nodes.extend(found)
elif self.insulators:
insulator_nodes.append(nodeid)
#add insulators to the pylon
for nodeid in insulator_nodes:
xyz = new_graph.node[nodeid]['xyz']
offset = pt_plus_pt(xyz,(0,0,-4000))
new_id = new_graph.add_unique_node(offset, "insulator")
new_graph.add_edge(nodeid, new_id)
excess_nodes = list(set(excess_nodes))
for node in excess_nodes:
new_graph.remove_node(node)
arm_ids = []
mirror_ids = []
for nodeid in new_graph.node:
if new_graph.node[nodeid]['label'] == ('arm'):
arm_ids.append(nodeid)
xyz = new_graph.node[nodeid]['xyz']
mirror = [- xyz[0], xyz[1], xyz[2]]
mirror_id = new_graph.add_unique_node(mirror, 'arm')
mirror_ids.append(mirror_id)
new_graph.add_edge(nodeid, mirror_id)
new_graph = nx.convert_node_labels_to_integers(new_graph)
return new_graph
def traverse_node_type(self, new_graph, starting_node, node_type):
unvisited = [starting_node]
visited = []
visited_edges = []
while len(unvisited) > 0:
next = unvisited.pop()
visited.append(next)
neighbours = new_graph.neighbors(next)
for neighbour in neighbours:
#check for self linking
if not neighbour == next:
# only look at certain node type
if new_graph.node[neighbour]['label'] == node_type:
#check its not in the edgelist
if not set((neighbour, next)) in visited_edges:
visited_edges.append(set((neighbour, next)))
# nodes can have many incoming edges
if not neighbour in unvisited:
unvisited.append(neighbour)
return visited
def copy_and_offset_with_mirror(self, original, offset_val, reflect=False):
"""Add a copy of the graph, offsetting all nodes by a given
vector. For nodes with the "rung" attribute, add an edge
between existing node and its offset copy."""
# make an unchanged copy and an offset/mirrored copy
orig_copy = original.copy()
offset_copy = original.copy()
for nodeid in offset_copy.node:
# perform an offset
xyz = offset_copy.node[nodeid]["xyz"]
xyz = pt_plus_pt(xyz, offset_val)
if reflect:
## also perform a mirror in the y axis
xyz = [xyz[0], - xyz[1], xyz[2]]
offset_copy.node[nodeid]["xyz"] = xyz
# make a union of the original and copy, renaming nodes
# note that this requires nx to be updated to svn 1520 or above
# which fixes a bug where union discards node attributes
new_graph = nx.union(orig_copy, offset_copy, rename=("G-", "H-"))
# make edges between nodes in original and copy depending on label
for nodeid in new_graph.node:
if nodeid.startswith("G-"):
h_node_id = nodeid.replace("G", "H")
#connect nodes labelled walkway or join
if new_graph.node[nodeid]['label'] == 'walkway':
new_graph.node[h_node_id]['label'] = 'walkway'
new_graph.add_edge(nodeid, h_node_id, label='walkway')
if new_graph.node[nodeid]['label'] == 'join':
new_graph.node[h_node_id]['label'] = 'join'
new_graph.add_edge(nodeid, h_node_id, label='join')
new_graph.frame_count = original.frame_count
return new_graph
def replace_graph(self, new_graph):
"""replaces self.graph with sanitised new graph, removes any
duplicates and only attaches only nodes that are connected to
edges"""
new_graph = nx.convert_node_labels_to_integers(new_graph)
self.clear()
#only add nodes that are connected to edges
for edge in new_graph.edges_iter(data=True):
if edge[0] == edge[1]:
new_graph.remove_edge(edge[0], edge[0])
else:
node_a = self.add_unique_node(new_graph.node[edge[0]]["xyz"],
new_graph.node[edge[0]]["label"])
node_b = self.add_unique_node(new_graph.node[edge[1]]["xyz"],
new_graph.node[edge[1]]["label"])
self.add_edge(node_a, node_b, edge[2])
######## UTILITY METHODS ##########
def save_picture(self):
"""Save a 2d-visualisation of the graph (ignores 3D
coordinates). Requires matplotlib"""
nx.draw_graphviz(self)
#plt.show()
def create_mesh(self, name):
"""generates a .mesh file from the graph"""
node_list = []
edge_list = []
for node_id in self.nodes():
node = {}
xyz = self.node[node_id]['xyz']
x, y, z = xyz[0], xyz[1], xyz[2]
label = self.node[node_id]['label']
node = {'id': str(node), 'x': x, 'y': y, 'z': z, 'label': label}
node_list.append(node)
for idx, edge in enumerate(self.edges_iter()):
edge = {'id': idx, 'pt_a': edge[0], 'pt_b': edge[1]}
edge_list.append(edge)
filename = name + '.mesh'
mesh = open(filename, 'w')
mesh.write("MeshVersionFormatted 1\nDimension\n3 \n")
mesh.write("Vertices\n" + str(len(node_list)) + " \n")
for node in node_list:
mesh.write(str(node['x']) + " " + str(node['y'])
+ " " + str(node['z']) + " 0 \n")
mesh.write("Edges\n" + str(len(edge_list)) + " \n")
for edge in edge_list:
pt_a, pt_b = edge['pt_a'], edge['pt_b']
if type(pt_a) == int and type(pt_b) == int:
mesh.write(str(pt_a + 1) + " " + str(pt_b + 1) + " 0 \n")
mesh.write("End\n")
mesh.close()
def save_graph(self, name=None):
if not name == None:
self.create_mesh(name)
elif self.save:
filename = self.pop_folder + "img%04d" % (self.frame_count)
self.create_mesh(filename)
self.frame_count += 1
########## PYLON GRAPH ############
class pylon_graph(graph):
"""Graph for a pylon that contains structures for line points and
checks the validity of the graph when unique nodes are added."""
LINE_LABEL = 'line'
def __init__(self, *args, **kwargs):
super(pylon_graph, self).__init__(*args, **kwargs)
self.line_nodes = []
self.valid = True
def add_unique_node(self, coords, node_type):
"""Add a node and connect it to two others, ensuring that it's
not lonesome. It is imperative for slffea that there are no
duplicate nodes, this function has problems with floating
points so try and set the xyz values as integers. Verifies
that all the constraints are passed, otherwise an Exception is
raised."""
#TODO good import placement?
import constraints
new = True
x, y, z = coords[0], coords[1], coords[2]
a_coords = [int(round(x, 0)), int(round(y, 0)), int(round(z, 0))]
#TODO use more memory and save the node_list structure in the
#class (let the profiler decide)
node_list = self.analyser_node_structure(self)
for node in self.node:
coords_n = self.node[node]['xyz']
x, y, z = coords_n[0], coords_n[1], coords_n[2]
b_coords = [int(round(x, 0)), int(round(y, 0)), int(round(z, 0))]
if a_coords == b_coords:
new = False
node_id = node
break
#Check constraints
if new:
node_id = self.get_unused_nodeid()
self.add_node(node_id, xyz=a_coords, label=node_type)
new_node = {'id': str(node_id), 'x': a_coords[0], 'y': a_coords[1],'z': a_coords[2], 'label': node_type}
if node_type == self.LINE_LABEL:
self.line_nodes.append(new_node)
self.valid = constraints.check_line_constraint(self.line_nodes)
if not self.valid and __debug__:
# raise ValueError('Violating line constraint:',self.line_nodes, self.node)
print('Violating line constraint:',self.line_nodes, self.node)
if self.valid and __debug__:
#Only need to verify the newly added node
self.valid = constraints.check_insulator_constraint([new_node], self)
if not self.valid and __debug__:
# raise ValueError('Violating insulator constraint:',new_node)
print('Violating insulator constraint:',new_node)
#Check if the line node violates the nodes
self.valid = constraints.check_structure_constraint(node_list, [new_node])
if not self.valid and __debug__:
# raise ValueError('Violating structure constraint:',new_node, node_list)
print('Violating structure constraint:',new_node, node_list)
else:
#Only check if the new node violates the line nodes
self.valid = constraints.check_structure_constraint([new_node], self.line_nodes)
if not self.valid and __debug__:
# raise ValueError('Violating structure constraint:',[new_node])
print('Violating structure constraint:',[new_node])
return node_id
def analyser_node_structure(self, my_graph):
"""Transfer my_graph nodes to a list of dictionaries, which
contain the node information."""
node_list = []
for node in my_graph.nodes():
xyz = my_graph.get_node_data(node)
label = my_graph.node[node]['label']
node = {'id': str(node), 'x': xyz[0], 'y': xyz[1],
'z': xyz[2], 'label': label}
node_list.append(node)
return node_list