00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import math
00032 import numpy as np
00033
00034 import roslib; roslib.load_manifest('tf')
00035 import tf.transformations as tft
00036
00037 def homogeneousToxyz(p):
00038 return p[0:3,:] / p[3,:]
00039
00040 def xyzToHomogenous(v, floating_vector=False):
00041 """ convert 3XN matrix, to 4XN matrix in homogeneous coords
00042 """
00043
00044
00045 if floating_vector == False:
00046 return np.row_stack((v, np.ones(v.shape[1])))
00047 else:
00048 return np.row_stack((v, np.zeros(v.shape[1])))
00049
00050 def xyToxyz(v):
00051 '''convert 2XN matrix, to 3XN matrix in homogeneous coords
00052 '''
00053 return np.row_stack((v, np.zeros(v.shape[1])))
00054
00055
00056 def xyToHomogenous(v):
00057 """ convert 2XN matrix to 4XN homogeneous
00058 """
00059
00060
00061
00062 return np.row_stack((v, np.zeros(v.shape[1]), np.ones(v.shape[1])))
00063
00064 def invertHomogeneousTransform(t):
00065 """ Inverts homogeneous transform
00066 """
00067 rot = t[0:3, 0:3]
00068 disp = t[0:3, 3]
00069 scale = t[3,3]
00070 rot = rot.transpose()
00071 disp = -1*rot*disp
00072 te = np.matrix([0.,0.,0.,1./scale])
00073 t_inv = np.row_stack((np.column_stack((rot, disp)),te))
00074 return t_inv
00075
00076 def getRotSubMat(t):
00077 """ returns rotation submatrix from homogeneous transformation t
00078 """
00079 return t[0:3, 0:3]
00080
00081 def getDispSubMat(t):
00082 """ returns displacement submatrix from homogeneous transformation t
00083 """
00084 return t[0:3, 3]
00085
00086 def composeHomogeneousTransform(rot, disp):
00087 """ Composes homogeneous transform from rotation and disp
00088 """
00089 disp = np.matrix(disp)
00090 disp = disp.reshape(3,1)
00091
00092 te = np.matrix([0.,0.,0.,1.])
00093 t = np.row_stack((np.column_stack((rot, disp)),te))
00094 return t
00095
00096 def angle_within_mod180(angle):
00097 ''' angle in radians.
00098 returns angle within -pi and +pi
00099 '''
00100 ang_deg = math.degrees(angle)%360
00101 if ang_deg > 180:
00102 ang_deg = ang_deg-360
00103 elif ang_deg <= -180:
00104 ang_deg = ang_deg+360
00105
00106 return math.radians(ang_deg)
00107
00108
00109
00110 def angle_within_plus_minus_90(angle):
00111 ang_deg = math.degrees(angle)%360
00112 if ang_deg<=90.:
00113 return math.radians(ang_deg)
00114 elif ang_deg<=180:
00115 return math.radians(ang_deg-180)
00116 elif ang_deg<=270:
00117 return math.radians(ang_deg-180)
00118 else:
00119 return math.radians(ang_deg-360)
00120
00121 def Rx(theta):
00122 """ returns Rotation matrix which transforms from XYZ -> frame rotated by theta about the x-axis.
00123 2 <--- Rx <--- 1
00124 theta is in radians.
00125 Rx = rotX'
00126 """
00127 st = math.sin(theta)
00128 ct = math.cos(theta)
00129 return np.matrix([[ 1., 0., 0. ],
00130 [ 0., ct, st ],
00131 [ 0., -st, ct ]])
00132
00133 def Ry(theta):
00134 """ returns Rotation matrix which transforms from XYZ -> frame rotated by theta about the y-axis.
00135 theta is in radians.
00136 Ry = rotY'
00137 """
00138 st = math.sin(theta)
00139 ct = math.cos(theta)
00140 return np.matrix([[ ct, 0., -st ],
00141 [ 0., 1., 0. ],
00142 [ st, 0., ct ]])
00143
00144 def Rz(theta):
00145 """ returns Rotation matrix which transforms from XYZ -> frame rotated by theta about the z-axis.
00146 theta is in radians.
00147 Rz = rotZ'
00148 """
00149 st = math.sin(theta)
00150 ct = math.cos(theta)
00151 return np.matrix([[ ct, st, 0. ],
00152 [ -st, ct, 0. ],
00153 [ 0., 0., 1. ]])
00154
00155 def rotX(theta):
00156 """ returns Rotation matrix such that R*v -> v', v' is rotated about x axis through theta.
00157 theta is in radians.
00158 rotX = Rx'
00159 """
00160 st = math.sin(theta)
00161 ct = math.cos(theta)
00162 return np.matrix([[ 1., 0., 0. ],
00163 [ 0., ct, -st ],
00164 [ 0., st, ct ]])
00165
00166 def rotY(theta):
00167 """ returns Rotation matrix such that R*v -> v', v' is rotated about y axis through theta_d.
00168 theta is in radians.
00169 rotY = Ry'
00170 """
00171 st = math.sin(theta)
00172 ct = math.cos(theta)
00173 return np.matrix([[ ct, 0., st ],
00174 [ 0., 1., 0. ],
00175 [ -st, 0., ct ]])
00176
00177 def rotZ(theta):
00178 """ returns Rotation matrix such that R*v -> v', v' is rotated about z axis through theta_d.
00179 theta is in radians.
00180 rotZ = Rz'
00181 """
00182 st = math.sin(theta)
00183 ct = math.cos(theta)
00184 return np.matrix([[ ct, -st, 0. ],
00185 [ st, ct, 0. ],
00186 [ 0., 0., 1. ]])
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 def rot_angle_direction(angle, direction):
00202 direction = direction/np.linalg.norm(direction)
00203 r = tft.rotation_matrix(angle, direction.A1)
00204 return np.matrix(r[0:3,0:3])
00205
00206
00207
00208
00209
00210 def quaternion_to_matrix(q):
00211 arr = tft.quaternion_matrix(q)
00212 return np.matrix(arr[0:3, 0:3])
00213
00214
00215 def matrix_to_quaternion(r):
00216 m = np.column_stack((r, np.matrix([0.,0.,0.]).T))
00217 m = np.row_stack((m, np.matrix([0.,0.,0.,1.])))
00218 return tft.quaternion_from_matrix(m)
00219
00220
00221
00222
00223 def matrix_to_axis_angle(rmat):
00224 rmat = np.column_stack((rmat, np.matrix([0,0,0]).T))
00225 rmat = np.row_stack((rmat, np.matrix([0,0,0,1])))
00226 ang, direc, point = tft.rotation_from_matrix(rmat)
00227
00228 return ang, direc
00229
00230
00231
00232