Go to the documentation of this file.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
00032
00033
00034
00035
00036
00037 #ifndef MATRIXLOADER_HPP_
00038 #define MATRIXLOADER_HPP_
00039 #include <ros/ros.h>
00040 #include <Eigen/Dense>
00041
00042 #include <string>
00043
00044 namespace labust
00045 {
00046 namespace tools
00047 {
00048 template <class Derived, class XmlRpcVal>
00049 inline typename Eigen::MatrixBase<Derived>::Scalar xmlRpcConvert(XmlRpcVal& data, bool intType)
00050 {
00051 return static_cast< typename Eigen::MatrixBase<Derived>::Scalar >(
00052 intType?static_cast<int>(data):static_cast<double>(data));
00053 }
00054
00058 template <class Derived>
00059 std::pair<int,int> getMatrixParam_fixedsize(const ros::NodeHandle& nh,
00060 const std::string& name,
00061 const Eigen::MatrixBase<Derived>& matrix)
00062 {
00063 if (!nh.hasParam(name))
00064 {
00065 ROS_WARN("Configuration parameter %s not found.", name.c_str());
00066 return std::make_pair(-1,-1);
00067 }
00068 else
00069 {
00070 ROS_INFO("Found configuration parameter %s.", name.c_str());
00071 }
00072
00073 XmlRpc::XmlRpcValue data;
00074 nh.getParam(name, data);
00075 ROS_ASSERT(data.getType() == XmlRpc::XmlRpcValue::TypeArray);
00076
00077 size_t col(0),row(0);
00078 for(size_t i=0; i<data.size(); ++i)
00079 {
00080
00081 bool doubleType = data[i].getType() == XmlRpc::XmlRpcValue::TypeDouble;
00082 bool intType = data[i].getType() == XmlRpc::XmlRpcValue::TypeInt;
00083 bool vectorType = data[i].getType() == XmlRpc::XmlRpcValue::TypeArray;
00084 ROS_ASSERT(doubleType || intType || vectorType);
00085
00086 if (doubleType || intType)
00087 {
00088 const_cast< Eigen::MatrixBase<Derived>& >
00089 (matrix)(row,col++) = xmlRpcConvert<Derived>(data[i], intType);
00090
00091 if (col>=matrix.cols())
00092 {
00093 ++row;
00094 col = col%matrix.cols();
00095 }
00096 }
00097 else
00098 {
00099 row = i;
00100 col = 0;
00101 for(size_t j=0; j<data[i].size(); ++j)
00102 {
00103
00104 bool intType = data[i][j].getType() == XmlRpc::XmlRpcValue::TypeInt;
00105 const_cast< Eigen::MatrixBase<Derived>& >
00106 (matrix)(row,col++) = xmlRpcConvert<Derived>(data[i][j], intType);
00107 }
00108 }
00109 }
00110
00111 return std::make_pair(row,col);
00112 }
00113
00117 template <class Derived>
00118 std::pair<int,int> getMatrixParam_variablesize(const ros::NodeHandle& nh,
00119 const std::string& name,
00120 const Eigen::MatrixBase<Derived>& matrix)
00121 {
00122 if (!nh.hasParam(name))
00123 {
00124 ROS_WARN("Configuration parameter %s not found.", name.c_str());
00125 return std::make_pair(-1,-1);
00126 }
00127 else
00128 {
00129 ROS_INFO("Found configuration parameter %s.", name.c_str());
00130 }
00131
00132 XmlRpc::XmlRpcValue data;
00133 nh.getParam(name, data);
00134 ROS_ASSERT(data.getType() == XmlRpc::XmlRpcValue::TypeArray);
00135
00136 std::vector<typename Derived::Scalar> temp;
00137
00138 size_t col(0),row(0);
00139 bool expandDiagonal(false);
00140 for(size_t i=0; i<data.size(); ++i)
00141 {
00142 if (i==0 && (data[i].getType() == XmlRpc::XmlRpcValue::TypeString))
00143 {
00144 std::string matrix_type(static_cast<std::string>(data[i]));
00145 std::cout<<"Matrix type: "<<matrix_type<<std::endl;
00146 expandDiagonal = (matrix_type == "diagonal");
00147 continue;
00148 }
00149
00150 bool doubleType = data[i].getType() == XmlRpc::XmlRpcValue::TypeDouble;
00151 bool intType = data[i].getType() == XmlRpc::XmlRpcValue::TypeInt;
00152 bool vectorType = data[i].getType() == XmlRpc::XmlRpcValue::TypeArray;
00153 ROS_ASSERT(doubleType || intType || vectorType);
00154
00155 if (doubleType || intType)
00156 {
00157 temp.push_back(xmlRpcConvert<Derived>(data[i], intType));
00158 col++;
00159 }
00160 else
00161 {
00162 row = i;
00163 col = 0;
00164 for(size_t j=0; j<data[i].size(); ++j)
00165 {
00166
00167 bool intType = data[i][j].getType() == XmlRpc::XmlRpcValue::TypeInt;
00168 temp.push_back(xmlRpcConvert<Derived>(data[i][j], intType));
00169 col++;
00170 }
00171 }
00172 }
00173
00174
00175 if (expandDiagonal)
00176 {
00177 Eigen::Map< Eigen::Matrix<typename Derived::Scalar, Eigen::Dynamic, 1> > vector(&temp[0],col);
00178 Eigen::Matrix<typename Derived::Scalar, Eigen::Dynamic, Eigen::Dynamic> max(vector.asDiagonal());
00179 const_cast< Eigen::MatrixBase<Derived>& >(matrix) = max;
00180 }
00181 else
00182 {
00183 const_cast< Eigen::MatrixBase<Derived>& >(matrix) = matrix.derived().Map(&temp[0],col,row+1).transpose();
00184 }
00185
00186 return std::make_pair(row,col);
00187 }
00188
00194 template <class Derived>
00195 inline std::pair<int,int> getMatrixParam(const ros::NodeHandle& nh,
00196 const std::string& name,
00197 const Eigen::MatrixBase<Derived>& matrix)
00198 {
00199 if ((matrix.RowsAtCompileTime <= 0) ||
00200 (matrix.ColsAtCompileTime <=0))
00201 {
00202 return getMatrixParam_variablesize(nh,name,matrix);
00203 }
00204 else
00205 {
00206 return getMatrixParam_fixedsize(nh,name,matrix);
00207 }
00208 }
00209 }
00210 }
00211
00212
00213 #endif