utils3D.py 3.1 KB

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