utils3D.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 , dist = getRay(x, child) , getDist(x, child)
  58. if not isinbound(initparams.env.boundary,child):
  59. return True, child
  60. for i in initparams.AABB:
  61. shot = pyrr.geometric_tests.ray_intersect_aabb(ray, i)
  62. if shot is not None:
  63. dist_wall = getDist(x, shot)
  64. if dist_wall <= dist: # collide
  65. return True, child
  66. for i in initparams.env.balls:
  67. shot = pyrr.geometric_tests.ray_intersect_sphere(ray, i)
  68. if shot != []:
  69. dists_ball = [getDist(x, j) for j in shot]
  70. if all(dists_ball <= dist): # collide
  71. return True, child
  72. return False, child
  73. def cost(i,j):
  74. return getDist(i,j)
  75. if __name__ == "__main__":
  76. from env3D import env