env3D.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. # from utils3D import OBB2AABB
  9. def R_matrix(z_angle, y_angle, x_angle):
  10. # s angle: row; y angle: pitch; z angle: yaw
  11. # generate rotation matrix in SO3
  12. # RzRyRx = R, ZYX intrinsic rotation
  13. # also (r1,r2,r3) in R3*3 in {W} frame
  14. # used in obb.O
  15. # [[R p]
  16. # [0T 1]] gives transformation from body to world
  17. return np.array(
  18. [[np.cos(z_angle), -np.sin(z_angle), 0.0], [np.sin(z_angle), np.cos(z_angle), 0.0], [0.0, 0.0, 1.0]]) @ \
  19. np.array(
  20. [[np.cos(y_angle), 0.0, np.sin(y_angle)], [0.0, 1.0, 0.0], [-np.sin(y_angle), 0.0, np.cos(y_angle)]]) @ \
  21. np.array(
  22. [[1.0, 0.0, 0.0], [0.0, np.cos(x_angle), -np.sin(x_angle)], [0.0, np.sin(x_angle), np.cos(x_angle)]])
  23. def getblocks():
  24. # AABBs
  25. block = [[3.10e+00, 0.00e+00, 2.10e+00, 3.90e+00, 5.00e+00, 6.00e+00],
  26. [9.10e+00, 0.00e+00, 2.10e+00, 9.90e+00, 5.00e+00, 6.00e+00],
  27. # [1.51e+01, 0.00e+00, 2.10e+00, 1.59e+01, 5.00e+00, 6.00e+00],
  28. # [1.00e-01, 0.00e+00, 0.00e+00, 9.00e-01, 5.00e+00, 3.90e+00],
  29. # [6.10e+00, 0.00e+00, 0.00e+00, 6.90e+00, 5.00e+00, 3.90e+00],
  30. [1.21e+01, 0.00e+00, 0.00e+00, 1.29e+01, 5.00e+00, 3.90e+00],
  31. [1.81e+01, 0.00e+00, 0.00e+00, 1.89e+01, 5.00e+00, 3.90e+00]]
  32. Obstacles = []
  33. for i in block:
  34. i = np.array(i)
  35. Obstacles.append([j for j in i])
  36. return np.array(Obstacles)
  37. def getAABB(blocks):
  38. # used for Pyrr package for detecting collision
  39. AABB = []
  40. for i in blocks:
  41. AABB.append(np.array([np.add(i[0:3], -0), np.add(i[3:6], 0)])) # make AABBs alittle bit of larger
  42. return AABB
  43. class aabb(object):
  44. # make AABB out of blocks,
  45. # P: center point
  46. # E: extents
  47. # O: Rotation matrix in SO(3), in {w}
  48. def __init__(self, AABB):
  49. self.P = [(AABB[3] + AABB[0]) / 2, (AABB[4] + AABB[1]) / 2, (AABB[5] + AABB[2]) / 2] # center point
  50. self.E = [(AABB[3] - AABB[0]) / 2, (AABB[4] - AABB[1]) / 2, (AABB[5] - AABB[2]) / 2] # extents
  51. self.O = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
  52. class obb(object):
  53. # P: center point
  54. # E: extents
  55. # O: Rotation matrix in SO(3), in {w}
  56. def __init__(self, P, E, O):
  57. self.P = P
  58. self.E = E
  59. self.O = O
  60. self.T = np.vstack([np.column_stack([self.O.T, -self.O.T @ self.P]), [0, 0, 0, 1]])
  61. def getAABB2(blocks):
  62. # used in lineAABB
  63. AABB = []
  64. for i in blocks:
  65. AABB.append(aabb(i))
  66. return AABB
  67. def getballs():
  68. spheres = [[16, 2.5, 4, 2], [10, 2.5, 1, 1]]
  69. Obstacles = []
  70. for i in spheres:
  71. Obstacles.append([j for j in i])
  72. return np.array(Obstacles)
  73. def add_block(block=[1.51e+01, 0.00e+00, 2.10e+00, 1.59e+01, 5.00e+00, 6.00e+00]):
  74. return block
  75. class env():
  76. def __init__(self, xmin=0, ymin=0, zmin=0, xmax=20, ymax=5, zmax=6, resolution=1):
  77. self.resolution = resolution
  78. self.boundary = np.array([xmin, ymin, zmin, xmax, ymax, zmax])
  79. self.blocks = getblocks()
  80. self.AABB = getAABB2(self.blocks)
  81. self.AABB_pyrr = getAABB(self.blocks)
  82. self.balls = getballs()
  83. self.OBB = np.array([obb([2.6, 2.5, 1], [0.2, 2, 1], R_matrix(0, 0, 45))])
  84. # self.OBB = np.squeeze(np.vstack([self.OBB,OBB2AABB(self.OBB[0])]))
  85. # print(self.OBB)
  86. # self.OBB = []
  87. self.start = np.array([0.5, 2.5, 5.5])
  88. self.goal = np.array([19.0, 2.5, 5.5])
  89. self.t = 0 # time
  90. def New_block(self):
  91. newblock = add_block()
  92. self.blocks = np.vstack([self.blocks, newblock])
  93. self.AABB = getAABB2(self.blocks)
  94. self.AABB_pyrr = getAABB(self.blocks)
  95. def move_start(self, x):
  96. self.start = x
  97. def move_block(self, a=[0, 0, 0], s=0, v=[0.1, 0, 0], theta=[0, 0, 0], block_to_move=0, obb_to_move=0,
  98. mode='uniform'):
  99. # t is time , v is velocity in R3, a is acceleration in R3, s is increment ini time,
  100. # R is an orthorgonal transform in R3*3, is the rotation matrix
  101. # (s',t') = (s + tv, t) is uniform transformation
  102. if mode == 'uniform':
  103. ori = np.array(self.blocks[block_to_move])
  104. self.blocks[block_to_move] = \
  105. np.array([ori[0] + self.t * v[0],
  106. ori[1] + self.t * v[1],
  107. ori[2] + self.t * v[2],
  108. ori[3] + self.t * v[0],
  109. ori[4] + self.t * v[1],
  110. ori[5] + self.t * v[2]])
  111. self.AABB[block_to_move].P = \
  112. [self.AABB[block_to_move].P[0] + self.t * v[0],
  113. self.AABB[block_to_move].P[1] + self.t * v[1],
  114. self.AABB[block_to_move].P[2] + self.t * v[2]]
  115. # return a range of block that the block might moved
  116. a = self.blocks[block_to_move]
  117. # return np.array([a[0] - self.resolution, a[1] - self.resolution, a[2] - self.resolution, \
  118. # a[3] + self.resolution, a[4] + self.resolution, a[5] + self.resolution]). \
  119. # np.array([ori[0] - self.resolution, ori[1] - self.resolution, ori[2] - self.resolution, \
  120. # ori[3] + self.resolution, ori[4] + self.resolution, ori[5] + self.resolution])
  121. return a, ori
  122. # (s',t') = (s + a, t + s) is a translation
  123. if mode == 'translation':
  124. ori = np.array(self.blocks[block_to_move])
  125. self.blocks[block_to_move] = \
  126. np.array([ori[0] + a[0],
  127. ori[1] + a[1],
  128. ori[2] + a[2],
  129. ori[3] + a[0],
  130. ori[4] + a[1],
  131. ori[5] + a[2]])
  132. self.AABB[block_to_move].P = \
  133. [self.AABB[block_to_move].P[0] + a[0],
  134. self.AABB[block_to_move].P[1] + a[1],
  135. self.AABB[block_to_move].P[2] + a[2]]
  136. self.t += s
  137. # return a range of block that the block might moved
  138. a = self.blocks[block_to_move]
  139. return np.array([a[0] - self.resolution, a[1] - self.resolution, a[2] - self.resolution,
  140. a[3] + self.resolution, a[4] + self.resolution, a[5] + self.resolution]), \
  141. np.array([ori[0] - self.resolution, ori[1] - self.resolution, ori[2] - self.resolution,
  142. ori[3] + self.resolution, ori[4] + self.resolution, ori[5] + self.resolution])
  143. # return a,ori
  144. # (s',t') = (Rx, t)
  145. if mode == 'rotation': # this makes an OBB rotate
  146. ori = [self.OBB[obb_to_move]]
  147. self.OBB[obb_to_move].O = R_matrix(z_angle=theta[0], y_angle=theta[1], x_angle=theta[2])
  148. self.OBB[obb_to_move].T = np.vstack(
  149. [np.column_stack([self.OBB[obb_to_move].O.T, -self.OBB[obb_to_move].O.T @ self.OBB[obb_to_move].P]),
  150. [0, 0, 0, 1]])
  151. return self.OBB[obb_to_move], ori[0]
  152. if __name__ == '__main__':
  153. newenv = env()