Version: 9.15.0
gui.graph.grid Class Reference

Public Member Functions

def __init__ (self, graph)
 
def reset (self)
 
def get (self, coord)
 
def getPath (self, node)
 
def neighbours (self, node)
 
def findPath (self, fromLoc, toLoc)
 

Public Attributes

 graph
 
 xs
 
 ys
 
 cols
 
 open
 

Detailed Description

Definition at line 297 of file graph.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 298 of file graph.py.

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

Member Function Documentation

◆ findPath()

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

Definition at line 407 of file graph.py.

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

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

◆ get()

def gui.graph.grid.get (   self,
  coord 
)

Definition at line 360 of file graph.py.

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

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

◆ getPath()

def gui.graph.grid.getPath (   self,
  node 
)

Definition at line 371 of file graph.py.

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

◆ neighbours()

def gui.graph.grid.neighbours (   self,
  node 
)

Definition at line 389 of file graph.py.

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

References gui.graph.grid.cols.

◆ reset()

def gui.graph.grid.reset (   self)

Definition at line 353 of file graph.py.

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

References gui.graph.grid.cols.

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

Member Data Documentation

◆ cols

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().

◆ graph

gui.graph.grid.graph

◆ open

gui.graph.grid.open

Definition at line 410 of file graph.py.

◆ xs

gui.graph.grid.xs

Definition at line 328 of file graph.py.

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

◆ ys

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: