utils3D.py 2.9 KB

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