utils3D.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import numpy as np
  2. def getRay(x, y):
  3. direc = [y[0] - x[0], y[1] - x[1], y[2] - x[2]]
  4. return np.array([x, direc])
  5. def getAABB(blocks):
  6. AABB = []
  7. for i in blocks:
  8. AABB.append(np.array([np.add(i[0:3], -0), np.add(i[3:6], 0)])) # make AABBs alittle bit of larger
  9. return AABB
  10. def getDist(pos1, pos2):
  11. return np.sqrt(sum([(pos1[0] - pos2[0]) ** 2, (pos1[1] - pos2[1]) ** 2, (pos1[2] - pos2[2]) ** 2]))
  12. def getNearest(Space,pt):
  13. '''get the nearest point on the grid'''
  14. mindis,minpt = 1000,None
  15. for strpts in Space.keys():
  16. pts = dehash(strpts)
  17. dis = getDist(pts,pt)
  18. if dis < mindis:
  19. mindis,minpt = dis,pts
  20. return minpt
  21. def Heuristic(Space,t):
  22. '''Max norm distance'''
  23. h = {}
  24. for k in Space.keys():
  25. h[k] = max(abs(t-dehash(k)))
  26. return h
  27. def hash3D(x):
  28. return str(x[0])+' '+str(x[1])+' '+str(x[2])
  29. def dehash(x):
  30. return np.array([float(i) for i in x.split(' ')])
  31. def isinbound(i, x):
  32. if i[0] <= x[0] < i[3] and i[1] <= x[1] < i[4] and i[2] <= x[2] < i[5]:
  33. return True
  34. return False
  35. def StateSpace(boundary,factor=0):
  36. '''This function is used to get nodes and discretize the space.
  37. State space is by x*y*z,3 where each 3 is a point in 3D.'''
  38. xmin,xmax = boundary[0]+factor,boundary[3]-factor
  39. ymin,ymax = boundary[1]+factor,boundary[4]-factor
  40. zmin,zmax = boundary[2]+factor,boundary[5]-factor
  41. xarr = np.arange(xmin,xmax,1)
  42. yarr = np.arange(ymin,ymax,1)
  43. zarr = np.arange(zmin,zmax,1)
  44. V = np.meshgrid(xarr,yarr,zarr)
  45. VV = np.reshape(V,[3,len(xarr)*len(yarr)*len(zarr)]) # all points in 3D
  46. Space = {}
  47. for v in VV.T:
  48. Space[hash3D(v)] = 0 # this hashmap initialize all g values at 0
  49. return Space
  50. if __name__ == "__main__":
  51. from env3D import env
  52. env = env(resolution=1)
  53. Space = StateSpace(env.boundary,0)
  54. t = np.array([3.0,4.0,5.0])
  55. h = Heuristic(Space,t)
  56. print(h[hash3D(t)])