-
Notifications
You must be signed in to change notification settings - Fork 1
/
Segga.py
115 lines (90 loc) · 3.94 KB
/
Segga.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
import scipy.io as sio
from Network import Network
MaxNVertices = 1000
def loadFromFile(path):
'''
:param path: path to .mat output file from SEGGA
:return: Network structure
'''
mat = sio.loadmat(path)
vertexCellArray = mat["cellgeom"]["nodecellmap"][0][0]
vertexPositions = mat["cellgeom"]["nodes"][0][0]
# x and y coordinates are flipped, so:
realvertexPositions = vertexPositions[:,::-1]
# start cell & vertex indices at zero like in a decent programming language
vertexCellArray -= 1
return _networkFromNodeData(vertexCellArray, realvertexPositions)
def _networkFromNodeData(vertexCellArray, vertexPositions):
'''
:param vertexCellArray: Ex2 array with E being the number of directed edges; first column: cell id (starting at zero!), second column: node id (starting at zero!)
:param vertexPositions: Vx2 array with V being the number of vertices; first column: x coordinate, second column: y coordinate
:return: Network structure
'''
network = Network()
# create all elements
vertexDict = {}
lastCellId = None
for cellId, vertexId in vertexCellArray:
if cellId != lastCellId:
# new cell:
cell = network.addCell()
cell.seggaLoadId = cellId+1
# check if vertex already exists
if vertexId in vertexDict:
vertex = vertexDict[vertexId]
else:
vertex = network.addVertex(vertexPositions[vertexId])
vertexDict[vertexId] = vertex
# put the vertexId into the vertex, needed for the wiring later
# collision danger, so more complicated variable name
vertex.seggaLoadId = vertexId+1
# create directed edge
edge = network.addDirectedEdge(cell, vertex)
# append edge to cell, prepend to ensure ccw ordering, note that the y coordinates are from top to bottom
cell.edges.insert(0, edge)
lastCellId = cellId
# wire the edges amongst themselves
edgeDict = {}
for cell in network.cells:
if len(cell.edges)<2:
raise Exception("Only %d edges around cell %d!"%(len(cell.edges), cell.seggaLoadId))
lastEdge = cell.edges[-1]
for edge in cell.edges:
edge.previousEdgeAroundCell = lastEdge
# connect to twin
curVid = edge.vertex.seggaLoadId
lastVid = lastEdge.vertex.seggaLoadId
lastEdge.seggaLoadId = (lastVid, curVid)
edgeDict[lastEdge.seggaLoadId] = lastEdge
# check whether the twin is already there and wire if so:
indexTwinOfLastEdge = (curVid, lastVid)
if indexTwinOfLastEdge in edgeDict:
twin = edgeDict[indexTwinOfLastEdge]
lastEdge.twin = twin
twin.twin = lastEdge
lastEdge = edge
# finally, wire the vertices:
vertexDone = {}
for cell in network.cells:
for edge in cell.edges:
vertex = edge.vertex
if not vertex.seggaLoadId in vertexDone:
nextEdge = edge
while True:
vertex.edges.append(nextEdge)
nextEdge = nextEdge.previousEdgeAroundCell.twin
if nextEdge is None:
vertex.edges = None
break
if nextEdge.vertex.seggaLoadId != vertex.seggaLoadId:
raise Exception("Swiched vertex when trying to walk around vertex %d!"%vertex.seggaLoadId)
if nextEdge == edge:
break
if len(vertex.edges)>MaxNVertices:
print(vertex.edges)
raise Exception("More than %d edges around vertex %d!"%(MaxNVertices, vertex.seggaLoadId))
vertexDone[vertex.seggaLoadId] = None
network.computeCellCenters()
return network
if __name__=="__main__":
network = loadFromFile("data/20171010_s_T0001_new_Z0001.mat")