utils3D.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import numpy as np
  2. import pyrr
  3. from collections import defaultdict
  4. import copy
  5. def getRay(x, y):
  6. direc = [y[0] - x[0], y[1] - x[1], y[2] - x[2]]
  7. return np.array([x, direc])
  8. def getDist(pos1, pos2):
  9. return np.sqrt(sum([(pos1[0] - pos2[0]) ** 2, (pos1[1] - pos2[1]) ** 2, (pos1[2] - pos2[2]) ** 2]))
  10. def getManDist(pos1, pos2):
  11. return sum([abs(pos1[0] - pos2[0]), abs(pos1[1] - pos2[1]), abs(pos1[2] - pos2[2])])
  12. def getNearest(Space, pt):
  13. '''get the nearest point on the grid'''
  14. mindis, minpt = 1000, None
  15. for pts in Space:
  16. dis = getDist(pts, pt)
  17. if dis < mindis:
  18. mindis, minpt = dis, pts
  19. return minpt
  20. def Heuristic(Space, t):
  21. '''Max norm distance'''
  22. h = {}
  23. for k in Space.keys():
  24. h[k] = max([abs(t[0] - k[0]), abs(t[1] - k[1]), abs(t[2] - k[2])])
  25. return h
  26. def heuristic_fun(initparams, k, t=None):
  27. if t is None:
  28. t = initparams.goal
  29. return max([abs(t[0] - k[0]), abs(t[1] - k[1]), abs(t[2] - k[2])])
  30. def isinbound(i, x, mode = False, factor = 0, isarray = False):
  31. if mode == 'obb':
  32. return isinobb(i, x, isarray)
  33. if isarray:
  34. compx = (i[0] - factor <= x[:,0]) & (x[:,0] < i[3] + factor)
  35. compy = (i[1] - factor <= x[:,1]) & (x[:,1] < i[4] + factor)
  36. compz = (i[2] - factor <= x[:,2]) & (x[:,2] < i[5] + factor)
  37. return compx & compy & compz
  38. else:
  39. return i[0] - factor <= x[0] < i[3] + factor and i[1] - factor <= x[1] < i[4] + factor and i[2] - factor <= x[2] < i[5] + factor
  40. def isinball(i, x, factor = 0):
  41. if getDist(i[0:3], x) <= i[3] + factor:
  42. return True
  43. return False
  44. def isinobb(i, x, isarray = False):
  45. # transform the point from {W} to {body}
  46. if isarray:
  47. pts = (i.T@np.column_stack((x, np.ones(len(x)))).T).T[:,0:3]
  48. block = [- i.E[0],- i.E[1],- i.E[2],+ i.E[0],+ i.E[1],+ i.E[2]]
  49. return isinbound(block, pts, isarray = isarray)
  50. else:
  51. pt = i.T@np.append(x,1)
  52. block = [- i.E[0],- i.E[1],- i.E[2],+ i.E[0],+ i.E[1],+ i.E[2]]
  53. return isinbound(block, pt)
  54. def OBB2AABB(obb):
  55. # https://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?print=1
  56. aabb = copy.deepcopy(obb)
  57. P = obb.P
  58. a = obb.E
  59. A = obb.O
  60. # a1(A1 dot s) + a2(A2 dot s) + a3(A3 dot s)
  61. Ex = a[0]*abs(A[0][0]) + a[1]*abs(A[1][0]) + a[2]*abs(A[2][0])
  62. Ey = a[0]*abs(A[0][1]) + a[1]*abs(A[1][1]) + a[2]*abs(A[2][1])
  63. Ez = a[0]*abs(A[0][2]) + a[1]*abs(A[1][2]) + a[2]*abs(A[2][2])
  64. E = np.array([Ex, Ey, Ez])
  65. aabb.P = P
  66. aabb.E = E
  67. aabb.O = np.array([[1,0,0],[0,1,0],[0,0,1]])
  68. return aabb
  69. def lineSphere(p0, p1, ball):
  70. # https://cseweb.ucsd.edu/classes/sp19/cse291-d/Files/CSE291_13_CollisionDetection.pdf
  71. c, r = ball[0:3], ball[-1]
  72. line = [p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2]]
  73. d1 = [c[0] - p0[0], c[1] - p0[1], c[2] - p0[2]]
  74. t = (1 / (line[0] * line[0] + line[1] * line[1] + line[2] * line[2])) * (
  75. line[0] * d1[0] + line[1] * d1[1] + line[2] * d1[2])
  76. if t <= 0:
  77. if (d1[0] * d1[0] + d1[1] * d1[1] + d1[2] * d1[2]) <= r ** 2: return True
  78. elif t >= 1:
  79. d2 = [c[0] - p1[0], c[1] - p1[1], c[2] - p1[2]]
  80. if (d2[0] * d2[0] + d2[1] * d2[1] + d2[2] * d2[2]) <= r ** 2: return True
  81. elif 0 < t < 1:
  82. x = [p0[0] + t * line[0], p0[1] + t * line[1], p0[2] + t * line[2]]
  83. k = [c[0] - x[0], c[1] - x[1], c[2] - x[2]]
  84. if (k[0] * k[0] + k[1] * k[1] + k[2] * k[2]) <= r ** 2: return True
  85. return False
  86. def lineAABB(p0, p1, dist, aabb):
  87. # https://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?print=1
  88. # aabb should have the attributes of P, E as center point and extents
  89. mid = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2, (p0[2] + p1[2]) / 2] # mid point
  90. I = [(p1[0] - p0[0]) / dist, (p1[1] - p0[1]) / dist, (p1[2] - p0[2]) / dist] # unit direction
  91. hl = dist / 2 # radius
  92. T = [aabb.P[0] - mid[0], aabb.P[1] - mid[1], aabb.P[2] - mid[2]]
  93. # do any of the principal axis form a separting axis?
  94. if abs(T[0]) > (aabb.E[0] + hl * abs(I[0])): return False
  95. if abs(T[1]) > (aabb.E[1] + hl * abs(I[1])): return False
  96. if abs(T[2]) > (aabb.E[2] + hl * abs(I[2])): return False
  97. # I.cross(s axis) ?
  98. r = aabb.E[1] * abs(I[2]) + aabb.E[2] * abs(I[1])
  99. if abs(T[1] * I[2] - T[2] * I[1]) > r: return False
  100. # I.cross(y axis) ?
  101. r = aabb.E[0] * abs(I[2]) + aabb.E[2] * abs(I[0])
  102. if abs(T[2] * I[0] - T[0] * I[2]) > r: return False
  103. # I.cross(z axis) ?
  104. r = aabb.E[0] * abs(I[1]) + aabb.E[1] * abs(I[0])
  105. if abs(T[0] * I[1] - T[1] * I[0]) > r: return False
  106. return True
  107. def lineOBB(p0, p1, dist, obb):
  108. # transform points to obb frame
  109. res = obb.T@np.column_stack([np.array([p0,p1]),[1,1]]).T
  110. # record old position and set the position to origin
  111. oldP, obb.P= obb.P, [0,0,0]
  112. # calculate segment-AABB testing
  113. ans = lineAABB(res[0:3,0],res[0:3,1],dist,obb)
  114. # reset the position
  115. obb.P = oldP
  116. return ans
  117. def OBBOBB(obb1, obb2):
  118. # https://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?print=1
  119. # each obb class should contain attributes:
  120. # E: extents along three principle axis in R3
  121. # P: position of the center axis in R3
  122. # O: orthornormal basis in R3*3
  123. a , b = np.array(obb1.E), np.array(obb2.E)
  124. Pa, Pb = np.array(obb1.P), np.array(obb2.P)
  125. A , B = np.array(obb1.O), np.array(obb2.O)
  126. # check if two oriented bounding boxes overlap
  127. # translation, in parent frame
  128. v = Pb - Pa
  129. # translation, in A's frame
  130. # vdotA[0],vdotA[1],vdotA[2]
  131. T = [v@B[0], v@B[1], v@B[2]]
  132. R = np.zeros([3,3])
  133. for i in range(0,3):
  134. for k in range(0,3):
  135. R[i][k] = A[i]@B[k]
  136. # use separating axis thm for all 15 separating axes
  137. # if the separating axis cannot be found, then overlap
  138. # A's basis vector
  139. for i in range(0,3):
  140. ra = a[i]
  141. rb = b[0]*abs(R[i][0]) + b[1]*abs(R[i][1]) + b[2]*abs(R[i][2])
  142. t = abs(T[i])
  143. if t > ra + rb:
  144. return False
  145. for k in range(0,3):
  146. ra = a[0]*abs(R[0][k]) + a[1]*abs(R[1][k]) + a[2]*abs(R[2][k])
  147. rb = b[k]
  148. t = abs(T[0]*R[0][k] + T[1]*R[1][k] + T[2]*R[2][k])
  149. if t > ra + rb:
  150. return False
  151. #9 cross products
  152. #L = A0 s B0
  153. ra = a[1]*abs(R[2][0]) + a[2]*abs(R[1][0])
  154. rb = b[1]*abs(R[0][2]) + b[2]*abs(R[0][1])
  155. t = abs(T[2]*R[1][0] - T[1]*R[2][0])
  156. if t > ra + rb:
  157. return False
  158. #L = A0 s B1
  159. ra = a[1]*abs(R[2][1]) + a[2]*abs(R[1][1])
  160. rb = b[0]*abs(R[0][2]) + b[2]*abs(R[0][0])
  161. t = abs(T[2]*R[1][1] - T[1]*R[2][1])
  162. if t > ra + rb:
  163. return False
  164. #L = A0 s B2
  165. ra = a[1]*abs(R[2][2]) + a[2]*abs(R[1][2])
  166. rb = b[0]*abs(R[0][1]) + b[1]*abs(R[0][0])
  167. t = abs(T[2]*R[1][2] - T[1]*R[2][2])
  168. if t > ra + rb:
  169. return False
  170. #L = A1 s B0
  171. ra = a[0]*abs(R[2][0]) + a[2]*abs(R[0][0])
  172. rb = b[1]*abs(R[1][2]) + b[2]*abs(R[1][1])
  173. t = abs( T[0]*R[2][0] - T[2]*R[0][0] )
  174. if t > ra + rb:
  175. return False
  176. # L = A1 s B1
  177. ra = a[0]*abs(R[2][1]) + a[2]*abs(R[0][1])
  178. rb = b[0]*abs(R[1][2]) + b[2]*abs(R[1][0])
  179. t = abs( T[0]*R[2][1] - T[2]*R[0][1] )
  180. if t > ra + rb:
  181. return False
  182. #L = A1 s B2
  183. ra = a[0]*abs(R[2][2]) + a[2]*abs(R[0][2])
  184. rb = b[0]*abs(R[1][1]) + b[1]*abs(R[1][0])
  185. t = abs( T[0]*R[2][2] - T[2]*R[0][2] )
  186. if t > ra + rb:
  187. return False
  188. #L = A2 s B0
  189. ra = a[0]*abs(R[1][0]) + a[1]*abs(R[0][0])
  190. rb = b[1]*abs(R[2][2]) + b[2]*abs(R[2][1])
  191. t = abs( T[1]*R[0][0] - T[0]*R[1][0] )
  192. if t > ra + rb:
  193. return False
  194. # L = A2 s B1
  195. ra = a[0]*abs(R[1][1]) + a[1]*abs(R[0][1])
  196. rb = b[0] *abs(R[2][2]) + b[2]*abs(R[2][0])
  197. t = abs( T[1]*R[0][1] - T[0]*R[1][1] )
  198. if t > ra + rb:
  199. return False
  200. #L = A2 s B2
  201. ra = a[0]*abs(R[1][2]) + a[1]*abs(R[0][2])
  202. rb = b[0]*abs(R[2][1]) + b[1]*abs(R[2][0])
  203. t = abs( T[1]*R[0][2] - T[0]*R[1][2] )
  204. if t > ra + rb:
  205. return False
  206. # no separating axis found,
  207. # the two boxes overlap
  208. return True
  209. def StateSpace(env, factor=0):
  210. boundary = env.boundary
  211. resolution = env.resolution
  212. xmin, xmax = boundary[0] + factor * resolution, boundary[3] - factor * resolution
  213. ymin, ymax = boundary[1] + factor * resolution, boundary[4] - factor * resolution
  214. zmin, zmax = boundary[2] + factor * resolution, boundary[5] - factor * resolution
  215. xarr = np.arange(xmin, xmax, resolution).astype(float)
  216. yarr = np.arange(ymin, ymax, resolution).astype(float)
  217. zarr = np.arange(zmin, zmax, resolution).astype(float)
  218. Space = set()
  219. for x in xarr:
  220. for y in yarr:
  221. for z in zarr:
  222. Space.add((x, y, z))
  223. return Space
  224. def g_Space(initparams):
  225. '''This function is used to get nodes and discretize the space.
  226. State space is by s*y*z,3 where each 3 is a point in 3D.'''
  227. g = {}
  228. Space = StateSpace(initparams.env)
  229. for v in Space:
  230. g[v] = np.inf # this hashmap initialize all g values at inf
  231. return g
  232. def isCollide(initparams, x, child, dist):
  233. '''see if line intersects obstacle'''
  234. '''specified for expansion in A* 3D lookup table'''
  235. if dist==None:
  236. dist = getDist(x, child)
  237. # check in bound
  238. if not isinbound(initparams.env.boundary, child):
  239. return True, dist
  240. # check collision in AABB
  241. for i in range(len(initparams.env.AABB)):
  242. if lineAABB(x, child, dist, initparams.env.AABB[i]):
  243. return True, dist
  244. # check collision in ball
  245. for i in initparams.env.balls:
  246. if lineSphere(x, child, i):
  247. return True, dist
  248. # check collision with obb
  249. for i in initparams.env.OBB:
  250. if lineOBB(x, child, dist, i):
  251. return True, dist
  252. return False, dist
  253. def children(initparams, x, settings = 0):
  254. # get the neighbor of a specific state
  255. allchild = []
  256. allcost = []
  257. resolution = initparams.env.resolution
  258. for direc in initparams.Alldirec:
  259. child = tuple(map(np.add, x, np.multiply(direc, resolution)))
  260. if any([isinobb(i, child) for i in initparams.env.OBB]):
  261. continue
  262. if any([isinball(i ,child) for i in initparams.env.balls]):
  263. continue
  264. if any([isinbound(i ,child) for i in initparams.env.blocks]):
  265. continue
  266. if isinbound(initparams.env.boundary, child):
  267. allchild.append(child)
  268. allcost.append((child,initparams.Alldirec[direc]*resolution))
  269. if settings == 0:
  270. return allchild
  271. if settings == 1:
  272. return allcost
  273. def obstacleFree(initparams, x):
  274. for i in initparams.env.blocks:
  275. if isinbound(i, x):
  276. return False
  277. for i in initparams.env.balls:
  278. if isinball(i, x):
  279. return False
  280. return True
  281. def cost(initparams, i, j, dist=None, settings='Euclidean'):
  282. if initparams.settings == 'NonCollisionChecking':
  283. if dist==None:
  284. dist = getDist(i,j)
  285. collide = False
  286. else:
  287. collide, dist = isCollide(initparams, i, j, dist)
  288. # collide, dist= False, getDist(i, j)
  289. if settings == 'Euclidean':
  290. if collide:
  291. return np.inf
  292. else:
  293. return dist
  294. if settings == 'Manhattan':
  295. if collide:
  296. return np.inf
  297. else:
  298. return getManDist(i, j)
  299. def initcost(initparams):
  300. # initialize Cost dictionary, could be modifed lateron
  301. c = defaultdict(lambda: defaultdict(dict)) # two key dicionary
  302. for xi in initparams.X:
  303. cdren = children(initparams, xi)
  304. for child in cdren:
  305. c[xi][child] = cost(initparams, xi, child)
  306. return c
  307. if __name__ == "__main__":
  308. import time
  309. from env3D import R_matrix, obb
  310. obb1 = obb([2.6,2.5,1],[0.2,2,2],R_matrix(0,0,45))
  311. # 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]])
  312. p0, p1 = [2.9,2.5,1],[1.9,2.5,1]
  313. pts = np.array([[1,2,3],[4,5,6],[7,8,9],[2,2,2],[1,1,1],[3,3,3]])
  314. start = time.time()
  315. isinbound(obb1, pts, mode='obb', factor = 0, isarray = True)
  316. print(time.time() - start)