transforms.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 #  \author Advait Jain (Healthcare Robotics Lab, Georgia Tech.)
00029 #  \author Hai Nguyen (Healthcare Robotics Lab, Georgia Tech.)
00030 
00031 import math
00032 import numpy as np
00033 
00034 import roslib; roslib.load_manifest('tf') # only depending on tf for transformations.py
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 #   v = np.matrix(v)
00044 #   v = v.reshape(3,1)
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 #   p = np.matrix(p)
00060 #   p = p.reshape(2,1)
00061 #   return np.row_stack((p, np.matrix([0.,1.]).T))
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 ## returns equivalent angle in 1st or 4th quadrant.
00109 # @param angle - in RADIANS
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 #-----------------   wrappers around functions in tf.transformation.py   -----------------
00190 
00191 ##
00192 # compute rotation matrix from axis and angle.
00193 # Example:
00194 # a1 = rot_angle_direction(math.radians(30), np.matrix([0.,1.,0.]).T)
00195 # a2 = Ry(math.radians(30))
00196 # np.allclose(a1.T, a2) # result is True.
00197 # @param angle - angle in RADIANS
00198 # @param direction - 3x1 np matrix
00199 # @return 3x3 np matrix that rotates a vector about the axis passing
00200 # through the origin, in the given direction through angle.
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 # convert a quaternion to a 3x3 rotation matrix.
00208 # @param q - quaternion (tf/transformation.py) (x,y,z,w)
00209 # @return 3x3 rotation matrix (np matrix)
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 # convert rotation matrix to axis and angle.
00222 # @param rmat - 3x3 np matrix.
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 #    print 'point:', point
00228     return ang, direc
00229 
00230 
00231 
00232 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06