env3D.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # this is the three dimensional configuration space for rrt
  2. # !/usr/bin/env python3
  3. # -*- coding: utf-8 -*-
  4. """
  5. @author: yue qi
  6. """
  7. import numpy as np
  8. def getblocks():
  9. # AABBs
  10. block = [[3.10e+00, 0.00e+00, 2.10e+00, 3.90e+00, 5.00e+00, 6.00e+00],
  11. [9.10e+00, 0.00e+00, 2.10e+00, 9.90e+00, 5.00e+00, 6.00e+00],
  12. #[1.51e+01, 0.00e+00, 2.10e+00, 1.59e+01, 5.00e+00, 6.00e+00],
  13. #[1.00e-01, 0.00e+00, 0.00e+00, 9.00e-01, 5.00e+00, 3.90e+00],
  14. #[6.10e+00, 0.00e+00, 0.00e+00, 6.90e+00, 5.00e+00, 3.90e+00],
  15. [1.21e+01, 0.00e+00, 0.00e+00, 1.29e+01, 5.00e+00, 3.90e+00],
  16. [1.81e+01, 0.00e+00, 0.00e+00, 1.89e+01, 5.00e+00, 3.90e+00]]
  17. Obstacles = []
  18. for i in block:
  19. i = np.array(i)
  20. Obstacles.append([j for j in i])
  21. return np.array(Obstacles)
  22. # def getAABB(blocks):
  23. # # used for Pyrr package for detecting collision
  24. # AABB = []
  25. # for i in blocks:
  26. # AABB.append(np.array([np.add(i[0:3], -0), np.add(i[3:6], 0)])) # make AABBs alittle bit of larger
  27. # return AABB
  28. class aabb(object):
  29. def __init__(self,AABB):
  30. self.P = [(AABB[3] + AABB[0])/2, (AABB[4] + AABB[1])/2, (AABB[5] + AABB[2])/2]# center point
  31. self.E = [(AABB[3] - AABB[0])/2, (AABB[4] - AABB[1])/2, (AABB[5] - AABB[2])/2]# extents
  32. def getAABB2(blocks):
  33. # used in lineAABB
  34. AABB = []
  35. for i in blocks:
  36. AABB.append(aabb(i))
  37. return AABB
  38. def getballs():
  39. spheres = [[16,2.5,4,2],[10,2.5,1,1]]
  40. Obstacles = []
  41. for i in spheres:
  42. Obstacles.append([j for j in i])
  43. return np.array(Obstacles)
  44. def add_block(block = [1.51e+01, 0.00e+00, 2.10e+00, 1.59e+01, 5.00e+00, 6.00e+00]):
  45. return block
  46. class env():
  47. def __init__(self, xmin=0, ymin=0, zmin=0, xmax=20, ymax=5, zmax=6, resolution=1):
  48. self.resolution = resolution
  49. self.boundary = np.array([xmin, ymin, zmin, xmax, ymax, zmax])
  50. self.blocks = getblocks()
  51. self.AABB = getAABB2(self.blocks)
  52. self.balls = getballs()
  53. self.start = np.array([0.5, 2.5, 5.5])
  54. self.goal = np.array([19.0, 2.5, 5.5])
  55. self.t = 0 # time
  56. def New_block(self):
  57. newblock = add_block()
  58. self.blocks = np.vstack([self.blocks,newblock])
  59. self.AABB = getAABB2(self.blocks)
  60. def move_start(self, x):
  61. self.start = x
  62. def move_block(self, a = [0,0,0], s = 0, v = [0.1,0,0], G = None, block_to_move = 0, mode = 'uniform'):
  63. # t is time , v is velocity in R3, a is acceleration in R3, s is increment ini time,
  64. # G is an orthorgonal transform in R3*3, in the Galilean transformation
  65. # (x',t') = (x + tv, t) is uniform transformation
  66. if mode == 'uniform':
  67. ori = self.blocks[block_to_move]
  68. self.blocks[block_to_move] = \
  69. np.array([ori[0] + self.t * v[0],\
  70. ori[1] + self.t * v[1],\
  71. ori[2] + self.t * v[2],\
  72. ori[3] + self.t * v[0],\
  73. ori[4] + self.t * v[1],\
  74. ori[5] + self.t * v[2]])
  75. self.AABB[block_to_move].P = \
  76. [self.AABB[block_to_move].P[0] + self.t * v[0], \
  77. self.AABB[block_to_move].P[1] + self.t * v[1], \
  78. self.AABB[block_to_move].P[2] + self.t * v[2]]
  79. # (x',t') = (x + a, t + s) is a translation
  80. if mode == 'translation':
  81. ori = self.blocks[block_to_move]
  82. self.blocks[block_to_move] = \
  83. np.array([ori[0] + a[0],\
  84. ori[1] + a[1],\
  85. ori[2] + a[2],\
  86. ori[3] + a[0],\
  87. ori[4] + a[1],\
  88. ori[5] + a[2]])
  89. self.AABB[block_to_move].P = \
  90. [self.AABB[block_to_move].P[0] + a[0], \
  91. self.AABB[block_to_move].P[1] + a[1], \
  92. self.AABB[block_to_move].P[2] + a[2]]
  93. self.t += s
  94. # (x',t') = (Gx, t)
  95. if mode == 'rotation': # this makes AABB become a OBB
  96. #TODO: implement this with rotation matrix
  97. pass
  98. if __name__ == '__main__':
  99. newenv = env()
  100. print(newenv.balls)