utils3D.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import numpy as np
  2. import pyrr
  3. from collections import defaultdict
  4. def getRay(x, y):
  5. direc = [y[0] - x[0], y[1] - x[1], y[2] - x[2]]
  6. return np.array([x, direc])
  7. def getDist(pos1, pos2):
  8. return np.sqrt(sum([(pos1[0] - pos2[0]) ** 2, (pos1[1] - pos2[1]) ** 2, (pos1[2] - pos2[2]) ** 2]))
  9. def getManDist(pos1, pos2):
  10. return sum([abs(pos1[0] - pos2[0]),abs(pos1[1] - pos2[1]),abs(pos1[2] - pos2[2])])
  11. def getNearest(Space,pt):
  12. '''get the nearest point on the grid'''
  13. mindis,minpt = 1000,None
  14. for pts in Space:
  15. dis = getDist(pts,pt)
  16. if dis < mindis:
  17. mindis,minpt = dis,pts
  18. return minpt
  19. def Heuristic(Space,t):
  20. '''Max norm distance'''
  21. h = {}
  22. for k in Space.keys():
  23. h[k] = max(abs(np.array([t[0]-k[0],t[1]-k[1],t[2]-k[2]])))
  24. return h
  25. def hash3D(x):
  26. return str(x[0])+' '+str(x[1])+' '+str(x[2])
  27. def dehash(x):
  28. return np.array([float(i) for i in x.split(' ')])
  29. def isinbound(i, x):
  30. if i[0] <= x[0] < i[3] and i[1] <= x[1] < i[4] and i[2] <= x[2] < i[5]:
  31. return True
  32. return False
  33. def isinball(i, x):
  34. if getDist(i[0:3], x) <= i[3]:
  35. return True
  36. return False
  37. def lineSphere(p0,p1,ball):
  38. # https://cseweb.ucsd.edu/classes/sp19/cse291-d/Files/CSE291_13_CollisionDetection.pdf
  39. c, r= ball[0:3],ball[-1]
  40. line = [p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2]]
  41. d1 = [c[0] - p0[0], c[1] - p0[1], c[2] - p0[2]]
  42. t = (1 / (line[0]*line[0] + line[1]*line[1] + line[2]*line[2])) * (line[0]*d1[0] + line[1]*d1[1] + line[2]*d1[2])
  43. if t <= 0:
  44. if (d1[0] * d1[0] + d1[1] * d1[1] + d1[2] * d1[2]) <= r ** 2: return True
  45. elif t >= 1:
  46. d2 = [c[0] - p1[0], c[1] - p1[1], c[2] - p1[2]]
  47. if (d2[0] * d2[0] + d2[1] * d2[1] + d2[2] * d2[2]) <= r ** 2: return True
  48. elif 0 < t < 1:
  49. x = [p0[0] + t * line[0], p0[1] + t * line[1], p0[2] + t * line[2]]
  50. k = [c[0] - x[0], c[1] - x[1], c[2] - x[2]]
  51. if (k[0] * k[0] + k[1] * k[1] + k[2] * k[2]) <= r**2: return True
  52. return False
  53. def lineAABB(p0,p1,dist,AABB):
  54. #https://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?print=1
  55. P = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2, (p0[2] + p1[2]) / 2] # mid point
  56. D = [(p1[0] - p0[0]) / dist, (p1[1] - p0[1]) / dist, (p1[2] - p0[2]) / dist] # unit direction
  57. t = dist / 2 # radius
  58. # TODO: implement this
  59. def StateSpace(env, factor = 0):
  60. boundary = env.boundary
  61. resolution = env.resolution
  62. xmin,xmax = boundary[0]+factor*resolution,boundary[3]-factor*resolution
  63. ymin,ymax = boundary[1]+factor*resolution,boundary[4]-factor*resolution
  64. zmin,zmax = boundary[2]+factor*resolution,boundary[5]-factor*resolution
  65. xarr = np.arange(xmin,xmax,resolution).astype(float)
  66. yarr = np.arange(ymin,ymax,resolution).astype(float)
  67. zarr = np.arange(zmin,zmax,resolution).astype(float)
  68. Space = set()
  69. for x in xarr:
  70. for y in yarr:
  71. for z in zarr:
  72. Space.add((x,y,z))
  73. return Space
  74. def g_Space(initparams):
  75. '''This function is used to get nodes and discretize the space.
  76. State space is by x*y*z,3 where each 3 is a point in 3D.'''
  77. g = {}
  78. Space = StateSpace(initparams.env)
  79. for v in Space:
  80. g[v] = np.inf # this hashmap initialize all g values at inf
  81. return g
  82. def isCollide(initparams, x, child):
  83. '''see if line intersects obstacle'''
  84. ray , dist = getRay(x, child) , getDist(x, child)
  85. if not isinbound(initparams.env.boundary,child):
  86. return True, dist
  87. for i in initparams.env.AABB:
  88. shot = pyrr.geometric_tests.ray_intersect_aabb(ray, i)
  89. if shot is not None:
  90. dist_wall = getDist(x, shot)
  91. if dist_wall <= dist: # collide
  92. return True, dist
  93. for i in initparams.env.balls:
  94. if isinball(i, child):
  95. return True, dist
  96. # shot = pyrr.geometric_tests.ray_intersect_sphere(ray, i)
  97. # if shot != []:
  98. # dists_ball = [getDist(x, j) for j in shot]
  99. # if all(dists_ball <= dist): # collide
  100. # return True, dist
  101. if lineSphere(x, child, i): return True, dist
  102. return False, dist
  103. def children(initparams, x):
  104. # get the neighbor of a specific state
  105. allchild = []
  106. resolution = initparams.env.resolution
  107. for direc in initparams.Alldirec:
  108. child = tuple(map(np.add,x,np.multiply(direc,resolution)))
  109. if isinbound(initparams.env.boundary,child):
  110. allchild.append(child)
  111. return allchild
  112. def obstacleFree(initparams,x):
  113. for i in initparams.env.blocks:
  114. if isinbound(i,x):
  115. return False
  116. for i in initparams.env.balls:
  117. if isinball(i,x):
  118. return False
  119. return True
  120. def cost(initparams, i,j,settings=0):
  121. collide, dist = isCollide(initparams,i,j)
  122. if settings == 0:
  123. if collide: return np.inf
  124. else: return dist
  125. if settings == 1:
  126. if collide: return np.inf
  127. else: return getManDist(i,j)
  128. def initcost(initparams):
  129. # initialize cost dictionary, could be modifed lateron
  130. c = defaultdict(lambda: defaultdict(dict)) # two key dicionary
  131. for xi in initparams.X:
  132. cdren = children(initparams, xi)
  133. for child in cdren:
  134. c[xi][child] = cost(initparams, xi, child)
  135. return c
  136. if __name__ == "__main__":
  137. a = [10,2.5,1,1]
  138. lineAABB([0,0,0],[1,1,1],)