utils3D.py 6.0 KB

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