graph layout
This commit is contained in:
parent
3cc66a4402
commit
c858529ddc
|
@ -115,6 +115,7 @@ class DecompositionView(QtGui.QDialog):
|
||||||
self.optimiseGraphLayout()
|
self.optimiseGraphLayout()
|
||||||
|
|
||||||
def optimiseGraphLayout(self):
|
def optimiseGraphLayout(self):
|
||||||
|
|
||||||
# create a graph of clusters and connections
|
# create a graph of clusters and connections
|
||||||
graph = geosolver.graph.Graph()
|
graph = geosolver.graph.Graph()
|
||||||
if self.ui.graphicsScene != None:
|
if self.ui.graphicsScene != None:
|
||||||
|
@ -123,39 +124,74 @@ class DecompositionView(QtGui.QDialog):
|
||||||
graph.add_vertex(item)
|
graph.add_vertex(item)
|
||||||
item.force = numpy.array([0.0,0.0])
|
item.force = numpy.array([0.0,0.0])
|
||||||
elif isinstance(item, CVConnection):
|
elif isinstance(item, CVConnection):
|
||||||
graph.add_edge(item.nodeFrom, item.nodeTo)
|
graph.add_edge(item.nodeFrom, item.nodeTo, item)
|
||||||
|
|
||||||
#for cluster in graph.vertices():
|
|
||||||
# print "cluster",cluster
|
|
||||||
#for connection in graph.edges():
|
|
||||||
# print "connection", connection
|
|
||||||
|
|
||||||
# determine forces due to overlapping cluster boxes
|
|
||||||
l = list(graph.vertices())
|
l = list(graph.vertices())
|
||||||
n = len(l)
|
|
||||||
for i in range(n):
|
# iteratively implove layout
|
||||||
for j in range(i+1,n):
|
for i in range(100):
|
||||||
c1 = l[i]
|
# clear forces
|
||||||
c2 = l[j]
|
for c in l:
|
||||||
|
c.force = numpy.array([random.random()*0, random.random()*0])
|
||||||
|
|
||||||
|
# determine forces due to overlapping cluster boxes
|
||||||
|
n = len(l)
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(i+1,n):
|
||||||
|
c1 = l[i]
|
||||||
|
c2 = l[j]
|
||||||
|
box1 = c1.paintRect.translated(c1.position)
|
||||||
|
box1.setWidth(2*box1.width())
|
||||||
|
box1.setHeight(2*box1.height())
|
||||||
|
box2 = c2.paintRect.translated(c2.position)
|
||||||
|
box2.setWidth(2*box2.width())
|
||||||
|
box2.setHeight(2*box2.height())
|
||||||
|
#print "box 1", box1
|
||||||
|
#print "box 2", box2
|
||||||
|
if box1.intersects(box2):
|
||||||
|
#print "intersects"
|
||||||
|
force = box1.intersected(box2).width() + box1.intersected(box2).height()
|
||||||
|
centerdiff = box2.center()-box1.center()
|
||||||
|
direction = numpy.array([centerdiff.x(),centerdiff.y()])
|
||||||
|
norm = numpy.linalg.norm(direction)
|
||||||
|
if norm != 0:
|
||||||
|
direction = direction / numpy.linalg.norm(direction)
|
||||||
|
direction[1] = 0.0
|
||||||
|
c1.force += force*direction;
|
||||||
|
c2.force += -force*direction;
|
||||||
|
#print "force 1", c1.force
|
||||||
|
#print "force 2", c2.force
|
||||||
|
|
||||||
|
# determine forces due to connections
|
||||||
|
for e in graph.edges():
|
||||||
|
c1 = e[0]
|
||||||
|
c2 = e[1]
|
||||||
box1 = c1.paintRect.translated(c1.position)
|
box1 = c1.paintRect.translated(c1.position)
|
||||||
box2 = c2.paintRect.translated(c2.position)
|
box2 = c2.paintRect.translated(c2.position)
|
||||||
print "box 1", box1
|
centerdiff = box2.center()-box1.center()
|
||||||
print "box 2", box2
|
direction = numpy.array([centerdiff.x(),centerdiff.y()])
|
||||||
if box1.intersects(box2):
|
norm = numpy.linalg.norm(direction)
|
||||||
print "intersects"
|
if norm != 0:
|
||||||
force = box1.intersected(box2).width() + box1.intersected(box2).height()
|
direction = direction / numpy.linalg.norm(direction)
|
||||||
centerdiff = box2.center()-box1.center()
|
#goal = box1.height() + box2.height() + box1.width() + box2.width()
|
||||||
direction = numpy.array([centerdiff.x(),centerdiff.y()])
|
#goal = 0
|
||||||
c1.force += force*direction;
|
force = (norm - goal) / 1.0
|
||||||
c2.force += -force*direction;
|
direction[1] = 0.0
|
||||||
print "force 1", c1.force
|
c1.force += force*direction;
|
||||||
print "force 2", c2.force
|
c2.force += -force*direction;
|
||||||
|
#print "force ", force
|
||||||
|
|
||||||
# apply forces
|
# apply forces
|
||||||
for c in l:
|
for c in l:
|
||||||
move = QtCore.QPointF(c.force[0],c.force[1])
|
move = QtCore.QPointF(c.force[0],c.force[1])/2
|
||||||
c.position += move
|
c.position += move
|
||||||
c.translate(move.x(), move.y())
|
c.translate(move.x(), move.y())
|
||||||
|
# done iterating
|
||||||
|
|
||||||
|
# uppate connectors
|
||||||
|
for e in graph.edges():
|
||||||
|
connector = graph.get(e[0],e[1])
|
||||||
|
connector.determinePath()
|
||||||
|
|
||||||
def updateViewports(self):
|
def updateViewports(self):
|
||||||
self.viewportManager.updateViewports()
|
self.viewportManager.updateViewports()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user