Dstar3D.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import os
  4. import sys
  5. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
  6. from Search_3D.env3D import env
  7. from Search_3D import Astar3D
  8. from Search_3D.utils3D import getDist, getRay
  9. import pyrr
  10. def StateSpace(env, factor = 0):
  11. boundary = env.boundary
  12. resolution = env.resolution
  13. xmin,xmax = boundary[0]+factor*resolution,boundary[3]-factor*resolution
  14. ymin,ymax = boundary[1]+factor*resolution,boundary[4]-factor*resolution
  15. zmin,zmax = boundary[2]+factor*resolution,boundary[5]-factor*resolution
  16. xarr = np.arange(xmin,xmax,resolution).astype(float)
  17. yarr = np.arange(ymin,ymax,resolution).astype(float)
  18. zarr = np.arange(zmin,zmax,resolution).astype(float)
  19. g = {}
  20. for x in xarr:
  21. for y in yarr:
  22. for z in zarr:
  23. g[(x,y,z)] = np.inf
  24. return g
  25. def Heuristic(initparams,x):
  26. h = {}
  27. x = np.array(x)
  28. for xi in initparams.g.keys():
  29. h[xi] = max(abs(x-np.array(xi)))
  30. return h
  31. def getNearest(Space,pt):
  32. '''get the nearest point on the grid'''
  33. mindis,minpt = 1000,None
  34. for pts in Space.keys():
  35. dis = getDist(pts,pt)
  36. if dis < mindis:
  37. mindis,minpt = dis,pts
  38. return minpt
  39. class D_star(object):
  40. def __init__(self,resolution = 1):
  41. self.Alldirec = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1],
  42. [-1, 0, 0], [0, -1, 0], [0, 0, -1], [-1, -1, 0], [-1, 0, -1], [0, -1, -1],
  43. [-1, -1, -1],
  44. [1, -1, 0], [-1, 1, 0], [1, 0, -1], [-1, 0, 1], [0, 1, -1], [0, -1, 1],
  45. [1, -1, -1], [-1, 1, -1], [-1, -1, 1], [1, 1, -1], [1, -1, 1], [-1, 1, 1]])
  46. self.env = env(resolution = resolution)
  47. self.g = StateSpace(self.env)
  48. self.x0, self.xt = getNearest(self.g, self.env.start), getNearest(self.g, self.env.goal)
  49. self.h = Heuristic(self,self.x0) # getting heuristic for x0
  50. if __name__ == '__main__':
  51. D = D_star(1)
  52. print(D.h[D.x0])