utils3D.py 3.5 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 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 getManDist(pos1, pos2):
  14. return sum([abs(pos1[0] - pos2[0]),abs(pos1[1] - pos2[1]),abs(pos1[2] - pos2[2])])
  15. def getNearest(Space,pt):
  16. '''get the nearest point on the grid'''
  17. mindis,minpt = 1000,None
  18. for strpts in Space.keys():
  19. pts = dehash(strpts)
  20. dis = getDist(pts,pt)
  21. if dis < mindis:
  22. mindis,minpt = dis,pts
  23. return minpt
  24. def Heuristic(Space,t):
  25. '''Max norm distance'''
  26. h = {}
  27. for k in Space.keys():
  28. h[k] = max(abs(t-dehash(k)))
  29. return h
  30. def hash3D(x):
  31. return str(x[0])+' '+str(x[1])+' '+str(x[2])
  32. def dehash(x):
  33. return np.array([float(i) for i in x.split(' ')])
  34. def isinbound(i, x):
  35. if i[0] <= x[0] < i[3] and i[1] <= x[1] < i[4] and i[2] <= x[2] < i[5]:
  36. return True
  37. return False
  38. def isinball(i, x):
  39. if getDist(i[0:3], x) <= i[3]:
  40. return True
  41. return False
  42. def StateSpace(initparams,factor=0):
  43. '''This function is used to get nodes and discretize the space.
  44. State space is by x*y*z,3 where each 3 is a point in 3D.'''
  45. boundary = initparams.env.boundary
  46. resolution = initparams.env.resolution
  47. xmin,xmax = boundary[0]+factor*resolution,boundary[3]-factor*resolution
  48. ymin,ymax = boundary[1]+factor*resolution,boundary[4]-factor*resolution
  49. zmin,zmax = boundary[2]+factor*resolution,boundary[5]-factor*resolution
  50. xarr = np.arange(xmin,xmax,resolution).astype(float)
  51. yarr = np.arange(ymin,ymax,resolution).astype(float)
  52. zarr = np.arange(zmin,zmax,resolution).astype(float)
  53. V = np.meshgrid(xarr,yarr,zarr)
  54. VV = np.reshape(V,[3,len(xarr)*len(yarr)*len(zarr)]) # all points in 3D
  55. Space = {}
  56. for v in VV.T:
  57. Space[hash3D(v)] = np.inf # this hashmap initialize all g values at inf
  58. return Space
  59. def isCollide(initparams, x, direc):
  60. '''see if line intersects obstacle'''
  61. resolution = initparams.env.resolution
  62. child = np.array(list(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.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