utils3D.py 3.3 KB

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