Version: 8.3.0
gui.graph.grid Class Reference

Public Member Functions

def __init__
 
def reset
 
def get
 
def getPath
 
def neighbours
 
def findPath
 

Public Attributes

 graph
 
 xs
 
 ys
 
 cols
 
 open
 

Detailed Description

Definition at line 297 of file graph.py.

Constructor & Destructor Documentation

def gui.graph.grid.__init__ (   self,
  graph 
)

Definition at line 298 of file graph.py.

299  def __init__(self,graph):
300  self.graph=graph
301  xs=set()
302  ys=set()
303  xmargin=5
304  ymargin=5
305  for n in graph:
306  h=n.height()
307  w=n.width()
308  x=n.x()
309  y=n.y()
310  xs.add(x-xmargin)
311  xs.add(x+w+xmargin)
312  ys.add(y-ymargin)
313  ys.add(y+h+ymargin)
314 
315  xs=list(xs)
316  xs.sort()
317  x0=xs[0]-36
318  xs.insert(0,x0)
319  x0=xs[-1]+36
320  xs.append(x0)
321 
322  ys=list(ys)
323  ys.sort()
324  y0=ys[0]-36
325  ys.insert(0,y0)
326  y0=ys[-1]+36
327  ys.append(y0)
329  self.xs=xs
330  self.ys=ys
331  self.cols=[]
332  for w in xrange(len(xs)-1):
333  col=[]
334  x=(xs[w]+xs[w+1])/2
335  for h in xrange(len(ys)-1):
336  y=(ys[h]+ys[h+1])/2
337  col.append(node((x,y),(w,h)))
338  self.cols.append(col)
339 
340  for n in graph:
341  h=n.height()
342  w=n.width()
343  x=n.x()
344  y=n.y()
345  l1,l2=bisect.bisect_left(ys,y-ymargin),bisect.bisect_left(ys,y+h+ymargin)
346  i1,i2=bisect.bisect_left(xs,x-xmargin),bisect.bisect_left(xs,x+w+xmargin)
347  for c in self.cols[i1:i2]:
348  for e in c[l1:l2]:
349  e.blocked=1
350 
351  #for col in self.cols:
352  # print [e.coord +(e.blocked,) for e in col]

Member Function Documentation

def gui.graph.grid.findPath (   self,
  fromLoc,
  toLoc 
)
Find shortest path from fromLoc to toLoc

Definition at line 407 of file graph.py.

References YACS::ENGINE::Logger.reset(), YACS::HMI::YACSGuiLoader.reset(), arguments.reset, and gui.graph.grid.reset().

408  def findPath(self,fromLoc,toLoc):
409  """Find shortest path from fromLoc to toLoc"""
410  self.reset()
411  self.open=[]
412  fromNode=self.get(fromLoc)
413  self.open.append((fromNode.total,fromNode))
414  toNode=self.get(toLoc)
415  if toNode.blocked:
416  print "toNode is blocked"
417  return []
418  destx,desty=toNode.coord
419  while self.open:
420  #if open is not void, take the best node (the first one)
421  t,node=self.open.pop(0)
422  node.close=1
423  if node == toNode:
424  #got toLoc
425  return self.getPath(node)
426 
427  for new_node in self.neighbours(node):
428  if new_node.close :
429  continue
430  x,y =new_node.coord
431  path_cost=node.path_cost+distance(node,new_node)
432  total=path_cost+h(x,y,destx,desty)
433  if new_node.open :
434  #the node is already in open
435  if total < new_node.total:
436  self.open.remove((new_node.total,new_node))
437  new_node.path_cost=path_cost
438  new_node.parent=node
439  new_node.total=total
440  bisect.insort(self.open,(new_node.total,new_node))
441  else:
442  #the node is not yet in open
443  new_node.path_cost=path_cost
444  new_node.parent=node
445  new_node.total=total
446  bisect.insort(self.open,(new_node.total,new_node))
447  new_node.open=1
448 
449  return []
450 
def gui.graph.grid.get (   self,
  coord 
)

Definition at line 360 of file graph.py.

References gui.graph.grid.cols, gui.graph.grid.xs, and gui.graph.grid.ys.

361  def get(self,coord):
362  x,y=coord
363  col= bisect.bisect_left(self.xs,x)-1
364  if col < 0 or col >= len(self.cols):
365  return None
366  col=self.cols[col]
367  row=bisect.bisect_left(self.ys,y)-1
368  if row < 0 or row >= len(col):
369  return None
370  return col[row]
def gui.graph.grid.getPath (   self,
  node 
)

Definition at line 371 of file graph.py.

372  def getPath(self,node):
373  path=[node.coord]
374  parent=node.parent
375  while parent:
376  prev=node
377  node=parent
378  parent=node.parent
379  if parent:
380  #if points are aligned don't keep the middle point
381  x,y=node.coord
382  x0,y0=prev.coord
383  x1,y1=parent.coord
384  if (x1-x)*(y0-y)-(y1-y)*(x0-x) == 0:
385  continue
386  path.append(node.coord)
387  path.reverse()
388  return path
def gui.graph.grid.neighbours (   self,
  node 
)

Definition at line 389 of file graph.py.

References gui.graph.grid.cols.

390  def neighbours(self,node):
391  col,row=node.index
392  l=[]
393  steps=((0, +1), (+1, 0), (0, -1), (-1, 0 ))
394  for step in steps:
395  c=col+step[0]
396  r=row+step[1]
397  if c < 0 or c >=len(self.cols):
398  continue
399  co=self.cols[c]
400  if r <0 or r >= len(co):
401  continue
402  n=co[r]
403  if n.blocked:
404  continue
405  l.append(n)
406  return l
def gui.graph.grid.reset (   self)

Definition at line 353 of file graph.py.

References gui.graph.grid.cols.

Referenced by gui.graph.grid.findPath().

354  def reset(self):
355  for c in self.cols:
356  for n in c:
357  n.open=n.close=0
358  n.total= n.path_cost=0
359  n.parent=None

Member Data Documentation

gui.graph.grid.cols

Definition at line 330 of file graph.py.

Referenced by gui.graph.grid.get(), gui.graph.grid.neighbours(), and gui.graph.grid.reset().

gui.graph.grid.graph

Definition at line 299 of file graph.py.

Referenced by gui.Items.ItemComposedNode.layout(), and gui.Items.ItemComposedNode.panel1().

gui.graph.grid.open

Definition at line 410 of file graph.py.

gui.graph.grid.xs

Definition at line 328 of file graph.py.

Referenced by gui.graph.grid.get().

gui.graph.grid.ys

Definition at line 329 of file graph.py.

Referenced by gui.graph.grid.get().


The documentation for this class was generated from the following file: