utils3D.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. # aabb should have the attributes of P, E as center point and extents
  57. mid = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2, (p0[2] + p1[2]) / 2] # mid point
  58. I = [(p1[0] - p0[0]) / dist, (p1[1] - p0[1]) / dist, (p1[2] - p0[2]) / dist] # unit direction
  59. hl = dist / 2 # radius
  60. P = aabb.P # center of the AABB
  61. E = aabb.E # extents of AABB
  62. T = [P[0] - mid[0], P[1] - mid[1], P[2] - mid[2]]
  63. # do any of the principal axis form a separting axis?
  64. if abs(T[0]) > (E[0] + hl * abs(I[0])): return False
  65. if abs(T[1]) > (E[1] + hl * abs(I[1])): return False
  66. if abs(T[2]) > (E[2] + hl * abs(I[2])): return False
  67. # I.cross(x axis) ?
  68. r = E[1] * abs(I[2]) + E[2] * abs(I[1])
  69. if abs(T[1] * I[2] - T[2] * I[1]) > r: return False
  70. # I.cross(y axis) ?
  71. r = E[0] * abs(I[2]) + E[2] * abs(I[0])
  72. if abs(T[2] * I[0] - T[0] * I[2]) > r: return False
  73. # I.cross(z axis) ?
  74. r = E[0] * abs(I[1]) + E[1] * abs(I[0])
  75. if abs(T[0] * I[1] - T[1] * I[0]) > r: return False
  76. return True
  77. def OBBOBB(obb1, obb2):
  78. # https://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?print=1
  79. # each obb class should contain attributes:
  80. # E: extents along three principle axis in R3
  81. # P: position of the center axis in R3
  82. # O: orthornormal basis in R3*3
  83. a , b = np.array(obb1.E), np.array(obb2.E)
  84. Pa, Pb = np.array(obb1.P), np.array(obb2.P)
  85. A , B = np.array(obb1.O), np.array(obb2.O)
  86. # check if two oriented bounding boxes overlap
  87. # translation, in parent frame
  88. v = Pb - Pa
  89. # translation, in A's frame
  90. # vdotA[0],vdotA[1],vdotA[2]
  91. T = [v@B[0], v@B[1], v@B[2]]
  92. R = np.zeros([3,3])
  93. for i in range(0,3):
  94. for k in range(0,3):
  95. R[i][k] = A[i]@B[k]
  96. # use separating axis thm for all 15 separating axes
  97. # if the separating axis cannot be found, then overlap
  98. # A's basis vector
  99. for i in range(0,3):
  100. ra = a[i]
  101. rb = b[0]*abs(R[i][0]) + b[1]*abs(R[i][1]) + b[2]*abs(R[i][2])
  102. t = abs(T[i])
  103. if t > ra + rb:
  104. return False
  105. for k in range(0,3):
  106. ra = a[0]*abs(R[0][k]) + a[1]*abs(R[1][k]) + a[2]*abs(R[2][k])
  107. rb = b[k]
  108. t = abs(T[0]*R[0][k] + T[1]*R[1][k] + T[2]*R[2][k])
  109. if t > ra + rb:
  110. return False
  111. #9 cross products
  112. #L = A0 x B0
  113. ra = a[1]*abs(R[2][0]) + a[2]*abs(R[1][0])
  114. rb = b[1]*abs(R[0][2]) + b[2]*abs(R[0][1])
  115. t = abs(T[2]*R[1][0] - T[1]*R[2][0])
  116. if t > ra + rb:
  117. return False
  118. #L = A0 x B1
  119. ra = a[1]*abs(R[2][1]) + a[2]*abs(R[1][1])
  120. rb = b[0]*abs(R[0][2]) + b[2]*abs(R[0][0])
  121. t = abs(T[2]*R[1][1] - T[1]*R[2][1])
  122. if t > ra + rb:
  123. return False
  124. #L = A0 x B2
  125. ra = a[1]*abs(R[2][2]) + a[2]*abs(R[1][2])
  126. rb = b[0]*abs(R[0][1]) + b[1]*abs(R[0][0])
  127. t = abs(T[2]*R[1][2] - T[1]*R[2][2])
  128. if t > ra + rb:
  129. return False
  130. #L = A1 x B0
  131. ra = a[0]*abs(R[2][0]) + a[2]*abs(R[0][0])
  132. rb = b[1]*abs(R[1][2]) + b[2]*abs(R[1][1])
  133. t = abs( T[0]*R[2][0] - T[2]*R[0][0] )
  134. if t > ra + rb:
  135. return False
  136. # L = A1 x B1
  137. ra = a[0]*abs(R[2][1]) + a[2]*abs(R[0][1])
  138. rb = b[0]*abs(R[1][2]) + b[2]*abs(R[1][0])
  139. t = abs( T[0]*R[2][1] - T[2]*R[0][1] )
  140. if t > ra + rb:
  141. return False
  142. #L = A1 x B2
  143. ra = a[0]*abs(R[2][2]) + a[2]*abs(R[0][2])
  144. rb = b[0]*abs(R[1][1]) + b[1]*abs(R[1][0])
  145. t = abs( T[0]*R[2][2] - T[2]*R[0][2] )
  146. if t > ra + rb:
  147. return False
  148. #L = A2 x B0
  149. ra = a[0]*abs(R[1][0]) + a[1]*abs(R[0][0])
  150. rb = b[1]*abs(R[2][2]) + b[2]*abs(R[2][1])
  151. t = abs( T[1]*R[0][0] - T[0]*R[1][0] )
  152. if t > ra + rb:
  153. return False
  154. # L = A2 x B1
  155. ra = a[0]*abs(R[1][1]) + a[1]*abs(R[0][1])
  156. rb = b[0] *abs(R[2][2]) + b[2]*abs(R[2][0])
  157. t = abs( T[1]*R[0][1] - T[0]*R[1][1] )
  158. if t > ra + rb:
  159. return False
  160. #L = A2 x B2
  161. ra = a[0]*abs(R[1][2]) + a[1]*abs(R[0][2])
  162. rb = b[0]*abs(R[2][1]) + b[1]*abs(R[2][0])
  163. t = abs( T[1]*R[0][2] - T[0]*R[1][2] )
  164. if t > ra + rb:
  165. return False
  166. # no separating axis found,
  167. # the two boxes overlap
  168. return True
  169. def StateSpace(env, factor=0):
  170. boundary = env.boundary
  171. resolution = env.resolution
  172. xmin, xmax = boundary[0] + factor * resolution, boundary[3] - factor * resolution
  173. ymin, ymax = boundary[1] + factor * resolution, boundary[4] - factor * resolution
  174. zmin, zmax = boundary[2] + factor * resolution, boundary[5] - factor * resolution
  175. xarr = np.arange(xmin, xmax, resolution).astype(float)
  176. yarr = np.arange(ymin, ymax, resolution).astype(float)
  177. zarr = np.arange(zmin, zmax, resolution).astype(float)
  178. Space = set()
  179. for x in xarr:
  180. for y in yarr:
  181. for z in zarr:
  182. Space.add((x, y, z))
  183. return Space
  184. def g_Space(initparams):
  185. '''This function is used to get nodes and discretize the space.
  186. State space is by x*y*z,3 where each 3 is a point in 3D.'''
  187. g = {}
  188. Space = StateSpace(initparams.env)
  189. for v in Space:
  190. g[v] = np.inf # this hashmap initialize all g values at inf
  191. return g
  192. def isCollide(initparams, x, child, dist):
  193. '''see if line intersects obstacle'''
  194. '''specified for expansion in A* 3D lookup table'''
  195. if dist==None:
  196. dist = getDist(x, child)
  197. if not isinbound(initparams.env.boundary, child):
  198. return True, dist
  199. for i in range(len(initparams.env.AABB)):
  200. # if isinbound(initparams.env.blocks[i], child):
  201. # return True, dist
  202. if lineAABB(x, child, dist, initparams.env.AABB[i]):
  203. return True, dist
  204. for i in initparams.env.balls:
  205. # if isinball(i, child):
  206. # return True, dist
  207. if lineSphere(x, child, i):
  208. return True, dist
  209. return False, dist
  210. def children(initparams, x, settings = 0):
  211. # get the neighbor of a specific state
  212. allchild = []
  213. allcost = []
  214. resolution = initparams.env.resolution
  215. for direc in initparams.Alldirec:
  216. child = tuple(map(np.add, x, np.multiply(direc, resolution)))
  217. if any([isinball(i ,child) for i in initparams.env.balls]):
  218. continue
  219. if any([isinbound(i ,child) for i in initparams.env.blocks]):
  220. continue
  221. if isinbound(initparams.env.boundary, child):
  222. allchild.append(child)
  223. allcost.append((child,initparams.Alldirec[direc]*resolution))
  224. if settings == 0:
  225. return allchild
  226. if settings == 1:
  227. return allcost
  228. def obstacleFree(initparams, x):
  229. for i in initparams.env.blocks:
  230. if isinbound(i, x):
  231. return False
  232. for i in initparams.env.balls:
  233. if isinball(i, x):
  234. return False
  235. return True
  236. def cost(initparams, i, j, dist=None, settings='Euclidean'):
  237. collide, dist = isCollide(initparams, i, j, dist)
  238. # collide, dist= False, getDist(i, j)
  239. if settings == 'Euclidean':
  240. if collide:
  241. return np.inf
  242. else:
  243. return dist
  244. if settings == 'Manhattan':
  245. if collide:
  246. return np.inf
  247. else:
  248. return getManDist(i, j)
  249. def initcost(initparams):
  250. # initialize cost dictionary, could be modifed lateron
  251. c = defaultdict(lambda: defaultdict(dict)) # two key dicionary
  252. for xi in initparams.X:
  253. cdren = children(initparams, xi)
  254. for child in cdren:
  255. c[xi][child] = cost(initparams, xi, child)
  256. return c
  257. class obb(object):
  258. def __init__(self, P, E, O):
  259. self.P = P
  260. self.E = E
  261. self.O = O
  262. if __name__ == "__main__":
  263. obb1 = obb([0,0,0],[1,1,1],[[1,0,0],[0,1,0],[0,0,1]])
  264. obb2 = obb([1,1,0],[1,1,1],[[1/np.sqrt(3)*1,1/np.sqrt(3)*1,1/np.sqrt(3)*1],[np.sqrt(3/2)*(-1/3),np.sqrt(3/2)*2/3,np.sqrt(3/2)*(-1/3)],[np.sqrt(1/8)*(-2),0,np.sqrt(1/8)*2]])
  265. print(OBBOBB(obb1, obb2))