multiarray_to_matrix.py
Go to the documentation of this file.
00001 #   Copyright 2013 Georgia Tech Research Corporation
00002 #
00003 #   Licensed under the Apache License, Version 2.0 (the "License");
00004 #   you may not use this file except in compliance with the License.
00005 #   You may obtain a copy of the License at
00006 #
00007 #     http://www.apache.org/licenses/LICENSE-2.0
00008 #
00009 #   Unless required by applicable law or agreed to in writing, software
00010 #   distributed under the License is distributed on an "AS IS" BASIS,
00011 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 #   See the License for the specific language governing permissions and
00013 #   limitations under the License.
00014 #
00015 #  http://healthcare-robotics.com/
00016 
00017 #!/usr/bin/env python
00018 
00019 ## @package hrl_haptic_mpc
00020 # 
00021 # @author Jeff Hawke
00022 # @version 0.1
00023 # @copyright Apache 2.0
00024 
00025 import roslib
00026 roslib.load_manifest('std_msgs')
00027 import rospy
00028 
00029 import std_msgs.msg
00030 
00031 import numpy
00032 
00033 ## Helper class to convert numpy matrix lists to and from a Float64MultiArray message object. 
00034 class MultiArrayConverter():
00035    
00036   ## Take a Float64 MultiArray message, convert it into a list of numpy matrices
00037   def multiArrayToMatrixList(self, ma_msg):
00038     dim = len(ma_msg.layout.dim)
00039     offset = ma_msg.layout.data_offset
00040     
00041     if dim != 3:
00042       print "Error: Must be 3 dimensions"
00043     
00044     if (ma_msg.layout.dim[0].label != "matrix"):
00045       print "Error: dim[0] should be the matrices"
00046     num_matrices = ma_msg.layout.dim[0].size
00047     
00048     if (ma_msg.layout.dim[1].label != "row"):
00049       print "Error: dim[1] should be the rows"
00050     rows = ma_msg.layout.dim[1].size
00051     
00052     if (ma_msg.layout.dim[2].label != "column"):
00053       print "Error: dim[2] should be the columns"
00054     columns = ma_msg.layout.dim[2].size
00055     
00056     ## Initialise empty matrix based on number of row/columns.
00057     #
00058     # NB: THIS IS ASSUMED TO BE CONSTANT FOR A GIVEN MESSAGE (or the multiarray structure breaks)
00059     mat = numpy.matrix(numpy.empty([rows,columns]))
00060     mat.fill(numpy.nan)
00061     
00062     matrix_list = [mat]*num_matrices
00063     
00064     for i in range(0, num_matrices):
00065       for j in range(0, rows):
00066         for k in range(0, columns):
00067           matrix = matrix_list[i]
00068           data_index = ma_msg.layout.data_offset + (rows*columns) * i + (columns) * j + k
00069           matrix[j,k] = ma_msg.data[data_index] 
00070     
00071     return matrix_list
00072     
00073     
00074   ## Convert a list of 2D numpy matrices to a Float64MultiArray message
00075   #
00076   # Assumption: Each numpy matrix must be the same size (rows, columns)
00077   def matrixListToMultiarray(self, matrix_list):
00078     num_matrices = len(matrix_list)
00079     if num_matrices > 0:
00080       rows, columns = matrix_list[0].shape
00081     else:
00082       rows = 0
00083       columns = 0
00084     
00085     msg = std_msgs.msg.Float64MultiArray()
00086     
00087     # Set up layout
00088     msg.layout.data_offset = 0
00089     
00090     # Set up layout dimensions
00091     matrix_dim = std_msgs.msg.MultiArrayDimension()
00092     matrix_dim.label = "matrix"
00093     matrix_dim.size = num_matrices
00094     matrix_dim.stride = columns * rows * num_matrices
00095     
00096     row_dim = std_msgs.msg.MultiArrayDimension()
00097     row_dim.label = "row"
00098     row_dim.size = rows
00099     row_dim.stride = columns * rows
00100     
00101     col_dim = std_msgs.msg.MultiArrayDimension()
00102     col_dim.label = "column"
00103     col_dim.size = columns
00104     col_dim.stride = columns
00105     
00106     msg.layout.dim = [matrix_dim, row_dim, col_dim]
00107     
00108     # Copy data from matrices into the msg data block
00109     msg_data = [float('NaN')] * (msg.layout.data_offset + num_matrices * rows * columns)
00110     
00111     for i in range(0, num_matrices):
00112       for j in range(0, rows):
00113         for k in range(0, columns):
00114           matrix = matrix_list[i]
00115           data_index = msg.layout.data_offset + (rows*columns) * i + (columns) * j + k
00116           msg_data[data_index] = matrix[j,k]
00117           
00118     msg.data = msg_data
00119     
00120     return msg
00121     
00122     
00123     


hrl_haptic_mpc
Author(s): Jeff Hawke, Phillip Grice, Marc Killpack, Advait Jain. Advisor and Co-author: Prof. Charlie Kemp. Healthcare Robotics Lab, Georgia Tech
autogenerated on Wed Nov 27 2013 12:27:09