utils3D.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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(t[0] - k[0]), abs(t[1] - k[1]), abs(t[2] - k[2])])
  24. return h
  25. def heuristic_fun(initparams, k, t=None):
  26. if t is None:
  27. t = initparams.goal
  28. return max([abs(t[0] - k[0]), abs(t[1] - k[1]), abs(t[2] - k[2])])
  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])) * (
  43. line[0] * d1[0] + line[1] * d1[1] + line[2] * d1[2])
  44. if t <= 0:
  45. if (d1[0] * d1[0] + d1[1] * d1[1] + d1[2] * d1[2]) <= r ** 2: return True
  46. elif t >= 1:
  47. d2 = [c[0] - p1[0], c[1] - p1[1], c[2] - p1[2]]
  48. if (d2[0] * d2[0] + d2[1] * d2[1] + d2[2] * d2[2]) <= r ** 2: return True
  49. elif 0 < t < 1:
  50. x = [p0[0] + t * line[0], p0[1] + t * line[1], p0[2] + t * line[2]]
  51. k = [c[0] - x[0], c[1] - x[1], c[2] - x[2]]
  52. if (k[0] * k[0] + k[1] * k[1] + k[2] * k[2]) <= r ** 2: return True
  53. return False
  54. def lineAABB(p0, p1, dist, aabb):
  55. # https://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?print=1
  56. mid = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2, (p0[2] + p1[2]) / 2] # mid point
  57. I = [(p1[0] - p0[0]) / dist, (p1[1] - p0[1]) / dist, (p1[2] - p0[2]) / dist] # unit direction
  58. hl = dist / 2 # radius
  59. P = aabb.P # center of the AABB
  60. E = aabb.E # extents of AABB
  61. T = [P[0] - mid[0], P[1] - mid[1], P[2] - mid[2]]
  62. # do any of the principal axis form a separting axis?
  63. if abs(T[0]) > (E[0] + hl * abs(I[0])): return False
  64. if abs(T[1]) > (E[1] + hl * abs(I[1])): return False
  65. if abs(T[2]) > (E[2] + hl * abs(I[2])): return False
  66. # I.cross(x axis) ?
  67. r = E[1] * abs(I[2]) + E[2] * abs(I[1])
  68. if abs(T[1] * I[2] - T[2] * I[1]) > r: return False
  69. # I.cross(y axis) ?
  70. r = E[0] * abs(I[2]) + E[2] * abs(I[0])
  71. if abs(T[2] * I[0] - T[0] * I[2]) > r: return False
  72. # I.cross(z axis) ?
  73. r = E[0] * abs(I[1]) + E[1] * abs(I[0])
  74. if abs(T[0] * I[1] - T[1] * I[0]) > r: return False
  75. return True
  76. def StateSpace(env, factor=0):
  77. boundary = env.boundary
  78. resolution = env.resolution
  79. xmin, xmax = boundary[0] + factor * resolution, boundary[3] - factor * resolution
  80. ymin, ymax = boundary[1] + factor * resolution, boundary[4] - factor * resolution
  81. zmin, zmax = boundary[2] + factor * resolution, boundary[5] - factor * resolution
  82. xarr = np.arange(xmin, xmax, resolution).astype(float)
  83. yarr = np.arange(ymin, ymax, resolution).astype(float)
  84. zarr = np.arange(zmin, zmax, resolution).astype(float)
  85. Space = set()
  86. for x in xarr:
  87. for y in yarr:
  88. for z in zarr:
  89. Space.add((x, y, z))
  90. return Space
  91. def g_Space(initparams):
  92. '''This function is used to get nodes and discretize the space.
  93. State space is by x*y*z,3 where each 3 is a point in 3D.'''
  94. g = {}
  95. Space = StateSpace(initparams.env)
  96. for v in Space:
  97. g[v] = np.inf # this hashmap initialize all g values at inf
  98. return g
  99. def isCollide(initparams, x, child, dist):
  100. '''see if line intersects obstacle'''
  101. '''specified for expansion in A* 3D lookup table'''
  102. if dist==None:
  103. dist = getDist(x, child)
  104. if not isinbound(initparams.env.boundary, child):
  105. return True, dist
  106. for i in range(len(initparams.env.AABB)):
  107. # if isinbound(initparams.env.blocks[i], child):
  108. # return True, dist
  109. if lineAABB(x, child, dist, initparams.env.AABB[i]):
  110. return True, dist
  111. for i in initparams.env.balls:
  112. # if isinball(i, child):
  113. # return True, dist
  114. if lineSphere(x, child, i):
  115. return True, dist
  116. return False, dist
  117. def children(initparams, x):
  118. # get the neighbor of a specific state
  119. allchild = []
  120. resolution = initparams.env.resolution
  121. for direc in initparams.Alldirec:
  122. child = tuple(map(np.add, x, np.multiply(direc, resolution)))
  123. if any([isinball(i ,child) for i in initparams.env.balls]):
  124. continue
  125. if any([isinbound(i ,child) for i in initparams.env.blocks]):
  126. continue
  127. if isinbound(initparams.env.boundary, child):
  128. allchild.append(child)
  129. # initparams.Alldirec[direc]*resolution
  130. return allchild
  131. def obstacleFree(initparams, x):
  132. for i in initparams.env.blocks:
  133. if isinbound(i, x):
  134. return False
  135. for i in initparams.env.balls:
  136. if isinball(i, x):
  137. return False
  138. return True
  139. def cost(initparams, i, j, dist=None, settings=0):
  140. collide, dist = isCollide(initparams, i, j, dist)
  141. # collide, dist= False, getDist(i, j)
  142. if settings == 0:
  143. if collide:
  144. return np.inf
  145. else:
  146. return dist
  147. if settings == 1:
  148. if collide:
  149. return np.inf
  150. else:
  151. return getManDist(i, j)
  152. def initcost(initparams):
  153. # initialize cost dictionary, could be modifed lateron
  154. c = defaultdict(lambda: defaultdict(dict)) # two key dicionary
  155. for xi in initparams.X:
  156. cdren = children(initparams, xi)
  157. for child in cdren:
  158. c[xi][child] = cost(initparams, xi, child)
  159. return c
  160. if __name__ == "__main__":
  161. a = '()'
  162. print(list(a))