utils3D.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(env, factor = 0):
  38. boundary = env.boundary
  39. resolution = env.resolution
  40. xmin,xmax = boundary[0]+factor*resolution,boundary[3]-factor*resolution
  41. ymin,ymax = boundary[1]+factor*resolution,boundary[4]-factor*resolution
  42. zmin,zmax = boundary[2]+factor*resolution,boundary[5]-factor*resolution
  43. xarr = np.arange(xmin,xmax,resolution).astype(float)
  44. yarr = np.arange(ymin,ymax,resolution).astype(float)
  45. zarr = np.arange(zmin,zmax,resolution).astype(float)
  46. Space = set()
  47. for x in xarr:
  48. for y in yarr:
  49. for z in zarr:
  50. Space.add((x,y,z))
  51. return Space
  52. def g_Space(initparams):
  53. '''This function is used to get nodes and discretize the space.
  54. State space is by x*y*z,3 where each 3 is a point in 3D.'''
  55. g = {}
  56. Space = StateSpace(initparams.env)
  57. for v in Space:
  58. g[hash3D(v)] = np.inf # this hashmap initialize all g values at inf
  59. return g
  60. def isCollide(initparams, x, direc):
  61. '''see if line intersects obstacle'''
  62. resolution = initparams.env.resolution
  63. child = np.array(list(map(np.add,x,np.multiply(direc,resolution))))
  64. ray , dist = getRay(x, child) , getDist(x, child)
  65. if not isinbound(initparams.env.boundary,child):
  66. return True, child
  67. for i in initparams.env.AABB:
  68. shot = pyrr.geometric_tests.ray_intersect_aabb(ray, i)
  69. if shot is not None:
  70. dist_wall = getDist(x, shot)
  71. if dist_wall <= dist: # collide
  72. return True, child
  73. for i in initparams.env.balls:
  74. if isinball(i, child):
  75. return True, child
  76. shot = pyrr.geometric_tests.ray_intersect_sphere(ray, i)
  77. if shot != []:
  78. dists_ball = [getDist(x, j) for j in shot]
  79. if all(dists_ball <= dist): # collide
  80. return True, child
  81. return False, child
  82. def obstacleFree(initparams,x):
  83. for i in initparams.env.blocks:
  84. if isinbound(i,x):
  85. return False
  86. for i in initparams.env.balls:
  87. if isinball(i,x):
  88. return False
  89. return True
  90. def cost(i,j,settings=0):
  91. if settings == 0:
  92. return getDist(i,j)
  93. if settings == 1:
  94. return getManDist(i,j)
  95. if __name__ == "__main__":
  96. from env3D import env