00001
00002
00003
00004
00005
00006
00007
00008 #include "articulation_models/utils.h"
00009
00010 using namespace std;
00011
00012 using namespace Eigen;
00013
00014 namespace articulation_models {
00015
00016 int openChannel(articulation_msgs::TrackMsg &track,std::string name,bool autocreate) {
00017
00018 size_t i = 0;
00019 for(; i < track.channels.size(); i++) {
00020 if(track.channels[i].name == name)
00021 break;
00022 }
00023
00024 if( i == track.channels.size() ) {
00025 if(!autocreate) return -1;
00026
00027 sensor_msgs::ChannelFloat32 ch;
00028 ch.name = name;
00029 track.channels.push_back(ch);
00030 }
00031
00032 track.channels[i].values.resize( track.pose.size() );
00033
00034 return i;
00035 }
00036
00037 articulation_msgs::TrackMsg flipTrack(articulation_msgs::TrackMsg input, int corner) {
00038 articulation_msgs::TrackMsg output = input;
00039
00040
00041 int ch_input_w = openChannel(input,"width",false);
00042 int ch_input_h = openChannel(input,"height",false);
00043 int ch_output_w = openChannel(output,"width",false);
00044 int ch_output_h = openChannel(output,"height",false);
00045
00046 tf::Transform in_pose,out_pose;
00047 double in_w=0,out_w=0;
00048 double in_h=0,out_h=0;
00049
00050 for(size_t i=0;i<input.pose.size();i++) {
00051 in_pose = poseToTransform( input.pose[i] );
00052
00053 if(ch_input_w>=0) in_w = input.channels[ch_input_w].values[i];
00054 if(ch_input_h>=0) in_h = input.channels[ch_input_h].values[i];
00055
00056 switch(corner) {
00057 case 0:
00058 out_w = in_w;
00059 out_h = in_h;
00060 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 0 * M_PI
00061 / 2), tf::Vector3(0, 0, 0));
00062 break;
00063 case 1:
00064 case -1:
00065 out_w = in_w;
00066 out_h = in_h;
00067 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 2 * M_PI
00068 / 2), tf::Vector3(in_w, in_h, 0));
00069 break;
00070 case 2:
00071 case -3:
00072 out_w = in_h;
00073 out_h = in_w;
00074 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 1 * M_PI
00075 / 2), tf::Vector3(in_w, 0, 0));
00076 break;
00077 case 3:
00078 case -2:
00079 out_w = in_h;
00080 out_h = in_w;
00081 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 3 * M_PI
00082 / 2), tf::Vector3(0, in_h, 0));
00083 break;
00084 case 4:
00085 case -4:
00086 out_w = in_h;
00087 out_h = in_w;
00088 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(1, 1, 0), 2
00089 * M_PI / 2), tf::Vector3(0, 0, 0)) * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 0
00090 * M_PI / 2), tf::Vector3(0, 0, 0));
00091 break;
00092 case 5:
00093 case -5:
00094 out_w = in_h;
00095 out_h = in_w;
00096 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(1, 1, 0), 2
00097 * M_PI / 2), tf::Vector3(0, 0, 0)) * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 2
00098 * M_PI / 2), tf::Vector3(in_h, in_w, 0));
00099 break;
00100 case 6:
00101 case -7:
00102 out_w = in_w;
00103 out_h = in_h;
00104 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(1, 1, 0), 2
00105 * M_PI / 2), tf::Vector3(0, 0, 0)) * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 1
00106 * M_PI / 2), tf::Vector3(in_h, 0, 0));
00107 break;
00108 case 7:
00109 case -6:
00110 out_w = in_w;
00111 out_h = in_h;
00112 out_pose = in_pose * tf::Transform(tf::Quaternion(tf::Vector3(1, 1, 0), 2
00113 * M_PI / 2), tf::Vector3(0, 0, 0)) * tf::Transform(tf::Quaternion(tf::Vector3(0, 0, 1), 3
00114 * M_PI / 2), tf::Vector3(0, in_w, 0));
00115 break;
00116 default:
00117 std::cout << "WARNING: utils.cpp/flipTrack: invalid corner"<<std::endl;
00118 out_w = in_w;
00119 out_h = in_h;
00120 out_pose = in_pose;
00121 }
00122
00123 output.pose[i] = transformToPose(out_pose);
00124 if(ch_output_w>=0) output.channels[ch_output_w].values[i] = out_w;
00125 if(ch_output_h>=0) output.channels[ch_output_h].values[i] = out_h;
00126 }
00127
00128 return output;
00129 }
00130
00131 Eigen::VectorXd pointToEigen(geometry_msgs::Point p) {
00132 Eigen::VectorXd vec(3);
00133 vec << p.x , p.y , p.z;
00134 return vec;
00135 }
00136
00137 geometry_msgs::Point eigenToPoint(Eigen::VectorXd v) {
00138 geometry_msgs::Point p;
00139 p.x = v(0);
00140 p.y = v(1);
00141 p.z = v(2);
00142 return p;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 void setParamIfNotDefined(std::vector<articulation_msgs::ParamMsg> &vec,
00165 std::string name, double value, uint8_t type) {
00166 for (size_t i = 0; i < vec.size(); i++)
00167 if (vec[i].name == name)
00168 return;
00169 articulation_msgs::ParamMsg param;
00170 param.name = name;
00171 param.value = value;
00172 param.type = type;
00173 vec.push_back(param);
00174 }
00175
00176 void setParam(std::vector<articulation_msgs::ParamMsg> &vec,
00177 std::string name, double value, uint8_t type) {
00178 for (size_t i = 0; i < vec.size(); i++) {
00179 if (vec[i].name == name) {
00180 vec[i].value = value;
00181 vec[i].type = type;
00182 return;
00183 }
00184 }
00185 articulation_msgs::ParamMsg param;
00186 param.name = name;
00187 param.value = value;
00188 param.type = type;
00189 vec.push_back(param);
00190 }
00191
00192 double getParam(std::vector<articulation_msgs::ParamMsg> &vec,
00193 std::string name) {
00194 for (size_t i = 0; i < vec.size(); i++) {
00195 if (vec[i].name == name) {
00196 return vec[i].value;
00197 }
00198 }
00199 return 0;
00200 }
00201
00202 bool hasParam(std::vector<articulation_msgs::ParamMsg> &vec,
00203 std::string name) {
00204 for (size_t i = 0; i < vec.size(); i++) {
00205 if (vec[i].name == name) {
00206 return true;
00207 }
00208 }
00209 return false;
00210 }
00211
00212
00213 bool check_values(const tf::Vector3 &vec) {
00214 return(check_values(vec.x()) && check_values(vec.y()) && check_values(vec.z()) );
00215 }
00216 bool check_values(const tf::Quaternion &vec) {
00217 return(check_values(vec.x()) && check_values(vec.y()) && check_values(vec.z()) && check_values(vec.w()));
00218 }
00219 bool check_values(double v) {
00220 return(!isnan(v) && !isinf(v));
00221 }
00222 bool check_values(float v) {
00223 return(!isnan(v) && !isinf(v));
00224 }
00225
00226 }