utils3D.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import numpy as np
  2. import pyrr
  3. def getRay(x, y):
  4. direc = [y[0] - x[0], y[1] - x[1], y[2] - x[2]]
  5. return np.array([x, direc])
  6. def getDist(pos1, pos2):
  7. return np.sqrt(sum([(pos1[0] - pos2[0]) ** 2, (pos1[1] - pos2[1]) ** 2, (pos1[2] - pos2[2]) ** 2]))
  8. def getManDist(pos1, pos2):
  9. return sum([abs(pos1[0] - pos2[0]),abs(pos1[1] - pos2[1]),abs(pos1[2] - pos2[2])])
  10. def getNearest(Space,pt):
  11. '''get the nearest point on the grid'''
  12. mindis,minpt = 1000,None
  13. for strpts in Space.keys():
  14. pts = dehash(strpts)
  15. dis = getDist(pts,pt)
  16. if dis < mindis:
  17. mindis,minpt = dis,pts
  18. return minpt
  19. def Heuristic(Space,t):
  20. '''Max norm distance'''
  21. h = {}
  22. for k in Space.keys():
  23. h[k] = max(abs(t-dehash(k)))
  24. return h
  25. def hash3D(x):
  26. return str(x[0])+' '+str(x[1])+' '+str(x[2])
  27. def dehash(x):
  28. return np.array([float(i) for i in x.split(' ')])
  29. def isinbound(i, x):
  30. if i[0] <= x[0] < i[3] and i[1] <= x[1] < i[4] and i[2] <= x[2] < i[5]:
  31. return True
  32. return False
  33. def isinball(i, x):
  34. if getDist(i[0:3], x) <= i[3]:
  35. return True
  36. return False
  37. def StateSpace(initparams,factor=0):
  38. '''This function is used to get nodes and discretize the space.
  39. State space is by x*y*z,3 where each 3 is a point in 3D.'''
  40. boundary = initparams.env.boundary
  41. resolution = initparams.env.resolution
  42. xmin,xmax = boundary[0]+factor*resolution,boundary[3]-factor*resolution
  43. ymin,ymax = boundary[1]+factor*resolution,boundary[4]-factor*resolution
  44. zmin,zmax = boundary[2]+factor*resolution,boundary[5]-factor*resolution
  45. xarr = np.arange(xmin,xmax,resolution).astype(float)
  46. yarr = np.arange(ymin,ymax,resolution).astype(float)
  47. zarr = np.arange(zmin,zmax,resolution).astype(float)
  48. V = np.meshgrid(xarr,yarr,zarr)
  49. VV = np.reshape(V,[3,len(xarr)*len(yarr)*len(zarr)]) # all points in 3D
  50. Space = {}
  51. for v in VV.T:
  52. Space[hash3D(v)] = np.inf # this hashmap initialize all g values at inf
  53. return Space
  54. def isCollide(initparams, x, direc):
  55. '''see if line intersects obstacle'''
  56. resolution = initparams.env.resolution
  57. child = np.array(list(map(np.add,x,np.multiply(direc,resolution))))
  58. ray , dist = getRay(x, child) , getDist(x, child)
  59. if not isinbound(initparams.env.boundary,child):
  60. return True, child
  61. for i in initparams.env.AABB:
  62. shot = pyrr.geometric_tests.ray_intersect_aabb(ray, i)
  63. if shot is not None:
  64. dist_wall = getDist(x, shot)
  65. if dist_wall <= dist: # collide
  66. return True, child
  67. for i in initparams.env.balls:
  68. if isinball(i, child):
  69. return True, child
  70. shot = pyrr.geometric_tests.ray_intersect_sphere(ray, i)
  71. if shot != []:
  72. dists_ball = [getDist(x, j) for j in shot]
  73. if all(dists_ball <= dist): # collide
  74. return True, child
  75. return False, child
  76. def obstacleFree(initparams,x):
  77. for i in initparams.env.blocks:
  78. if isinbound(i,x):
  79. return False
  80. for i in initparams.env.balls:
  81. if isinball(i,x):
  82. return False
  83. return True
  84. def cost(i,j,settings=0):
  85. if settings == 0:
  86. return getDist(i,j)
  87. if settings == 1:
  88. return getManDist(i,j)
  89. if __name__ == "__main__":
  90. from env3D import env