env3D.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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([[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]])@ \
  18. np.array([[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)]])@ \
  19. np.array([[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)]])
  20. def getblocks():
  21. # AABBs
  22. block = [[4.00e+00, 1.20e+01, 0.00e+00, 5.00e+00, 2.00e+01, 5.00e+00],
  23. [5.5e+00, 1.20e+01, 0.00e+00, 1.00e+01, 1.30e+01, 5.00e+00],
  24. [1.00e+01, 1.20e+01, 0.00e+00, 1.40e+01, 1.30e+01, 5.00e+00],
  25. [1.00e+01, 9.00e+00, 0.00e+00, 2.00e+01, 1.00e+01, 5.00e+00],
  26. [9.00e+00, 6.00e+00, 0.00e+00, 1.00e+01, 1.00e+01, 5.00e+00]]
  27. Obstacles = []
  28. for i in block:
  29. i = np.array(i)
  30. Obstacles.append([j for j in i])
  31. return np.array(Obstacles)
  32. def getballs():
  33. spheres = [[2.0,6.0,2.5,1.0],[14.0,14.0,2.5,2]]
  34. Obstacles = []
  35. for i in spheres:
  36. Obstacles.append([j for j in i])
  37. return np.array(Obstacles)
  38. def getAABB(blocks):
  39. # used for Pyrr package for detecting collision
  40. AABB = []
  41. for i in blocks:
  42. AABB.append(np.array([np.add(i[0:3], -0), np.add(i[3:6], 0)])) # make AABBs alittle bit of larger
  43. return AABB
  44. def getAABB2(blocks):
  45. # used in lineAABB
  46. AABB = []
  47. for i in blocks:
  48. AABB.append(aabb(i))
  49. return AABB
  50. def add_block(block = [1.51e+01, 0.00e+00, 2.10e+00, 1.59e+01, 5.00e+00, 6.00e+00]):
  51. return block
  52. class aabb(object):
  53. # make AABB out of blocks,
  54. # P: center point
  55. # E: extents
  56. # O: Rotation matrix in SO(3), in {w}
  57. def __init__(self,AABB):
  58. self.P = [(AABB[3] + AABB[0])/2, (AABB[4] + AABB[1])/2, (AABB[5] + AABB[2])/2]# center point
  59. self.E = [(AABB[3] - AABB[0])/2, (AABB[4] - AABB[1])/2, (AABB[5] - AABB[2])/2]# extents
  60. self.O = [[1,0,0],[0,1,0],[0,0,1]]
  61. class obb(object):
  62. # P: center point
  63. # E: extents
  64. # O: Rotation matrix in SO(3), in {w}
  65. def __init__(self, P, E, O):
  66. self.P = P
  67. self.E = E
  68. self.O = O
  69. self.T = np.vstack([np.column_stack([self.O.T,-self.O.T@self.P]),[0,0,0,1]])
  70. class env():
  71. def __init__(self, xmin=0, ymin=0, zmin=0, xmax=20, ymax=20, zmax=5, resolution=1):
  72. # def __init__(self, xmin=-5, ymin=0, zmin=-5, xmax=10, ymax=5, zmax=10, resolution=1):
  73. self.resolution = resolution
  74. self.boundary = np.array([xmin, ymin, zmin, xmax, ymax, zmax])
  75. self.blocks = getblocks()
  76. self.AABB = getAABB2(self.blocks)
  77. self.AABB_pyrr = getAABB(self.blocks)
  78. self.balls = getballs()
  79. self.OBB = np.array([obb([5.0,7.0,2.5],[0.5,2.0,2.5],R_matrix(135,0,0)),
  80. obb([12.0,4.0,2.5],[0.5,2.0,2.5],R_matrix(45,0,0))])
  81. self.start = np.array([2.0, 2.0, 2.0])
  82. self.goal = np.array([6.0, 16.0, 0.0])
  83. self.t = 0 # time
  84. def New_block(self):
  85. newblock = add_block()
  86. self.blocks = np.vstack([self.blocks,newblock])
  87. self.AABB = getAABB2(self.blocks)
  88. self.AABB_pyrr = getAABB(self.blocks)
  89. def move_start(self, x):
  90. self.start = x
  91. def move_block(self, a = [0,0,0], s = 0, v = [0.1,0,0], block_to_move = 0, mode = 'translation'):
  92. # t is time , v is velocity in R3, a is acceleration in R3, s is increment ini time,
  93. # R is an orthorgonal transform in R3*3, is the rotation matrix
  94. # (s',t') = (s + tv, t) is uniform transformation
  95. # (s',t') = (s + a, t + s) is a translation
  96. if mode == 'translation':
  97. ori = np.array(self.blocks[block_to_move])
  98. self.blocks[block_to_move] = \
  99. np.array([ori[0] + a[0],\
  100. ori[1] + a[1],\
  101. ori[2] + a[2],\
  102. ori[3] + a[0],\
  103. ori[4] + a[1],\
  104. ori[5] + a[2]])
  105. self.AABB[block_to_move].P = \
  106. [self.AABB[block_to_move].P[0] + a[0], \
  107. self.AABB[block_to_move].P[1] + a[1], \
  108. self.AABB[block_to_move].P[2] + a[2]]
  109. self.t += s
  110. # return a range of block that the block might moved
  111. a = self.blocks[block_to_move]
  112. return np.array([a[0] - self.resolution, a[1] - self.resolution, a[2] - self.resolution, \
  113. a[3] + self.resolution, a[4] + self.resolution, a[5] + self.resolution]), \
  114. np.array([ori[0] - self.resolution, ori[1] - self.resolution, ori[2] - self.resolution, \
  115. ori[3] + self.resolution, ori[4] + self.resolution, ori[5] + self.resolution])
  116. # return a,ori
  117. # (s',t') = (Rx, t)
  118. def move_OBB(self, obb_to_move = 0, theta=[0,0,0], translation=[0,0,0]):
  119. # theta stands for rotational angles around three principle axis in world frame
  120. # translation stands for translation in the world frame
  121. ori = [self.OBB[obb_to_move]]
  122. # Calculate orientation
  123. self.OBB[obb_to_move].O = R_matrix(z_angle=theta[0],y_angle=theta[1],x_angle=theta[2])
  124. # generating transformation matrix
  125. self.OBB[obb_to_move].T = np.vstack([np.column_stack([self.OBB[obb_to_move].O.T,\
  126. -self.OBB[obb_to_move].O.T@self.OBB[obb_to_move].P]),[translation[0],translation[1],translation[2],1]])
  127. return self.OBB[obb_to_move], ori[0]
  128. if __name__ == '__main__':
  129. newenv = env()