transformable_object.cpp
Go to the documentation of this file.
00001 #include <jsk_interactive_marker/transformable_object.h>
00002 #include <eigen_conversions/eigen_msg.h>
00003 #include <math.h>
00004 
00005 #define PI 3.14159265
00006 
00007 using namespace jsk_interactive_marker;
00008 
00009 inline geometry_msgs::Pose inverse(geometry_msgs::Pose pose){
00010   Eigen::Affine3d pose_eigen;
00011   tf::poseMsgToEigen(pose, pose_eigen);
00012   tf::poseEigenToMsg(pose_eigen.inverse(), pose);
00013   return pose;
00014 }
00015 
00016 
00017 TransformableObject::TransformableObject(){
00018   ROS_INFO("Init TransformableObject");
00019   control_offset_pose_.orientation.x = 0;
00020   control_offset_pose_.orientation.y = 0;
00021   control_offset_pose_.orientation.z = 0;
00022   control_offset_pose_.orientation.w = 1;
00023   pose_ = control_offset_pose_;
00024   display_interactive_manipulator_ = true;
00025 }
00026 
00027 void TransformableObject::setDisplayInteractiveManipulator(bool v)
00028 {
00029   display_interactive_manipulator_ = v;
00030 }
00031 
00032 void TransformableObject::setInteractiveMarkerSetting(InteractiveSettingConfig config){
00033   display_interactive_manipulator_ = config.display_interactive_manipulator;
00034   interactive_manipulator_orientation_ = config.interactive_manipulator_orientation;
00035 }
00036 
00037 std::vector<visualization_msgs::InteractiveMarkerControl> TransformableObject::makeRotateTransFixControl(unsigned int orientation_mode){
00038   visualization_msgs::InteractiveMarkerControl control;
00039 
00040   std::vector<visualization_msgs::InteractiveMarkerControl> controls;
00041   control.orientation_mode = orientation_mode;
00042   control.orientation.w = 1;
00043   control.orientation.x = 1;
00044   control.orientation.y = 0;
00045   control.orientation.z = 0;
00046   control.name = "rotate_x";
00047   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00048   controls.push_back(control);
00049   control.name = "move_x";
00050   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00051   controls.push_back(control);
00052 
00053   control.orientation.w = 1;
00054   control.orientation.x = 0;
00055   control.orientation.y = 1;
00056   control.orientation.z = 0;
00057   control.name = "rotate_z";
00058   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00059   controls.push_back(control);
00060   control.name = "move_z";
00061   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00062   controls.push_back(control);
00063 
00064   control.orientation.w = 1;
00065   control.orientation.x = 0;
00066   control.orientation.y = 0;
00067   control.orientation.z = 1;
00068   control.name = "rotate_y";
00069   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00070   controls.push_back(control);
00071   control.name = "move_y";
00072   control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00073   controls.push_back(control);
00074 
00075   return controls;
00076 };
00077 
00078 void TransformableObject::addMarker(visualization_msgs::InteractiveMarker &int_marker, bool always_visible, unsigned int interaction_mode)
00079 {
00080   visualization_msgs::Marker marker = getVisualizationMsgMarker();
00081   visualization_msgs::InteractiveMarkerControl marker_control;
00082   marker_control.always_visible = always_visible;
00083   marker_control.markers.push_back(marker);
00084   marker_control.interaction_mode = interaction_mode;
00085   int_marker.controls.push_back(marker_control);
00086 };
00087 
00088 void TransformableObject::addControl(visualization_msgs::InteractiveMarker &int_marker)
00089 {
00090   if(display_interactive_manipulator_){
00091     std::vector<visualization_msgs::InteractiveMarkerControl> rotate_controls = makeRotateTransFixControl(interactive_manipulator_orientation_);
00092     int_marker.controls.insert(int_marker.controls.end(), rotate_controls.begin(), rotate_controls.end());
00093   }
00094 };
00095 
00096 visualization_msgs::InteractiveMarker TransformableObject::getInteractiveMarker(){
00097   visualization_msgs::InteractiveMarker int_marker;
00098 
00099   addMarker(int_marker);
00100   addControl(int_marker);
00101   int_marker.header.frame_id = frame_id_;
00102   int_marker.name = name_;
00103   int_marker.description = description_;
00104   int_marker.pose = pose_;
00105   int_marker.scale = getInteractiveMarkerScale();
00106   return int_marker;
00107 };
00108 
00109 void TransformableObject::setPose(geometry_msgs::Pose pose, bool for_interactive_control){
00110   if(for_interactive_control) {
00111     pose_ = pose;
00112   }
00113   else {
00114     Eigen::Affine3d control_offset_eigen;
00115     tf::poseMsgToEigen(control_offset_pose_, control_offset_eigen);
00116     Eigen::Affine3d pose_eigen;
00117     tf::poseMsgToEigen(pose, pose_eigen);
00118     tf::poseEigenToMsg(pose_eigen * control_offset_eigen, pose_);
00119   }
00120 }
00121 
00122 geometry_msgs::Pose TransformableObject::getPose(bool for_interactive_control){
00123   if(for_interactive_control) {
00124     return pose_;
00125   }
00126   else{
00127     geometry_msgs::Pose pose;
00128     Eigen::Affine3d control_offset_eigen;
00129     tf::poseMsgToEigen(control_offset_pose_, control_offset_eigen);
00130     Eigen::Affine3d pose_eigen;
00131     tf::poseMsgToEigen(pose_, pose_eigen);
00132     tf::poseEigenToMsg(pose_eigen * control_offset_eigen.inverse(), pose);
00133     //return pose;
00134     return pose;
00135   }
00136 }
00137 
00138 
00139 void TransformableObject::addPose(geometry_msgs::Pose msg, bool relative){
00140   Eigen::Vector3d original_p(msg.position.x, msg.position.y, msg.position.z);
00141   Eigen::Quaterniond original_q;
00142   tf::quaternionMsgToEigen(pose_.orientation, original_q);
00143   Eigen::Quaterniond diff_q;
00144   tf::quaternionMsgToEigen(msg.orientation, diff_q);
00145   Eigen::Quaterniond updated_q;
00146   if(relative) {
00147      original_p = Eigen::Affine3d(original_q) * original_p;
00148   }
00149   if(relative) {
00150     updated_q = original_q * diff_q;
00151   } else {
00152     updated_q = diff_q * original_q;
00153   }
00154   pose_.position.x += original_p[0];
00155   pose_.position.y += original_p[1];
00156   pose_.position.z += original_p[2];
00157   tf::quaternionEigenToMsg(updated_q, pose_.orientation);
00158 }
00159 
00160 void TransformableObject::publishTF(){
00161   tf::Transform transform;
00162   tf::poseMsgToTF(getPose(), transform);
00163   br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), frame_id_, name_));
00164 }
00165 
00166 namespace jsk_interactive_marker{
00167   TransformableCylinder::TransformableCylinder( float radius, float z, float r, float g, float b, float a, std::string frame, std::string name, std::string description){
00168     cylinder_radius_ = radius;
00169     cylinder_z_ = z;
00170     cylinder_r_ = r;
00171     cylinder_g_ = g;
00172     cylinder_b_ = b;
00173     cylinder_a_ = a;
00174     marker_.type = visualization_msgs::Marker::CYLINDER;
00175     type_ = jsk_rviz_plugins::TransformableMarkerOperate::CYLINDER;
00176 
00177     frame_id_ = frame;
00178     name_ = name;
00179     description_ = description;
00180   }
00181 
00182   visualization_msgs::Marker TransformableCylinder::getVisualizationMsgMarker(){
00183     marker_.scale.x = cylinder_radius_ * 2.0;
00184     marker_.scale.y = cylinder_radius_ * 2.0;
00185     marker_.scale.z = cylinder_z_;
00186     marker_.color.r = cylinder_r_;
00187     marker_.color.g = cylinder_g_;
00188     marker_.color.b = cylinder_b_;
00189     marker_.color.a = cylinder_a_;
00190     marker_.pose = control_offset_pose_;
00191     return marker_;
00192   }
00193 }
00194 
00195 namespace jsk_interactive_marker{
00196 
00197   TransformableTorus::TransformableTorus( float radius, float small_radius, int u_div, int v_div, float r, float g, float b, float a, std::string frame, std::string name, std::string description){
00198     torus_radius_ = radius;
00199     torus_small_radius_ = small_radius;
00200     torus_r_ = r;
00201     torus_g_ = g;
00202     torus_b_ = b;
00203     torus_a_ = a;
00204     marker_.type = visualization_msgs::Marker::TRIANGLE_LIST;
00205     type_ = jsk_rviz_plugins::TransformableMarkerOperate::TORUS;
00206 
00207     frame_id_ = frame;
00208     name_ = name;
00209     description_ = description;
00210 
00211     u_division_num_ = u_div;
00212     v_division_num_ = v_div;
00213   }
00214 
00215   std::vector<geometry_msgs::Point > TransformableTorus::calcurateTriangleMesh(){
00216     std::vector<geometry_msgs::Point> triangle_mesh;
00217     float center_x = 0;
00218     float center_y = 0;
00219     float u_division_num = u_division_num_;
00220     float v_division_num = v_division_num_;
00221     std::vector<std::vector<geometry_msgs::Point> > points_array;
00222     for (int i = 0; i < u_division_num; i ++){
00223       std::vector<geometry_msgs::Point> points;
00224       float target_circle_x = torus_radius_ * cos( ( i / u_division_num) * 2 * PI) ;
00225       float target_circle_y = torus_radius_ * sin( ( i / u_division_num) * 2 * PI) ;
00226       for (int j = 0; j < v_division_num; j++){
00227         geometry_msgs::Point new_point;
00228         new_point.x = target_circle_x + torus_small_radius_ * cos ( (j / v_division_num) * 2 * PI) * cos( ( i / u_division_num) * 2 * PI);
00229         new_point.y = target_circle_y + torus_small_radius_ * cos ( (j / v_division_num) * 2 * PI) * sin( ( i / u_division_num) * 2 * PI);
00230         new_point.z = torus_small_radius_ * sin ( (j / v_division_num) * 2 * PI);
00231         points.push_back(new_point);
00232       }
00233       points_array.push_back(points);
00234     }
00235 
00236     //create mesh list;
00237     for(int i = 0; i < u_division_num; i++){
00238       std::vector<geometry_msgs::Point> target_points = points_array[i];
00239       float prev_index = i - 1, next_index = i + 1;
00240       if(prev_index < 0)
00241         prev_index = u_division_num - 1;
00242       if(next_index > u_division_num - 1)
00243         next_index = 0;
00244       std::vector<geometry_msgs::Point> prev_points = points_array[prev_index];
00245       std::vector<geometry_msgs::Point> next_points = points_array[next_index];
00246       for(int j = 0; j < v_division_num; j++){
00247         float next_point_index = j + 1;
00248         if( next_point_index > v_division_num - 1)
00249           next_point_index = 0;
00250         //first pushes
00251         triangle_mesh.push_back(target_points[j]);
00252         triangle_mesh.push_back(next_points[j]);
00253         triangle_mesh.push_back(target_points[next_point_index]);
00254         //second pushes
00255         triangle_mesh.push_back(target_points[j]);
00256         triangle_mesh.push_back(target_points[next_point_index]);
00257         triangle_mesh.push_back(prev_points[next_point_index]);
00258       }
00259     }
00260 
00261     return triangle_mesh;
00262   }
00263 
00264   visualization_msgs::Marker TransformableTorus::getVisualizationMsgMarker(){
00265     marker_.points = calcurateTriangleMesh();
00266     marker_.color.r = torus_r_;
00267     marker_.color.g = torus_g_;
00268     marker_.color.b = torus_b_;
00269     marker_.color.a = torus_a_;
00270     return marker_;
00271   }
00272 }
00273 
00274 namespace jsk_interactive_marker{
00275 
00276   TransformableBox::TransformableBox( float length , float r, float g, float b, float a, std::string frame, std::string name, std::string description){
00277     box_x_ = box_y_ = box_z_ = length;
00278 
00279     box_r_ = r;
00280     box_g_ = g;
00281     box_b_ = b;
00282     box_a_ = a;
00283     marker_.type = visualization_msgs::Marker::CUBE;
00284 
00285     frame_id_ = frame;
00286     name_ = name;
00287     description_ = description;
00288   }
00289 
00290   TransformableBox::TransformableBox( float x, float y, float z , float r, float g, float b, float a, std::string frame, std::string name, std::string description){
00291     box_x_ = x;
00292     box_y_ = y;
00293     box_z_ = z;
00294     box_r_ = r;
00295     box_g_ = g;
00296     box_b_ = b;
00297     box_a_ = a;
00298     marker_.type = visualization_msgs::Marker::CUBE;
00299     type_ = jsk_rviz_plugins::TransformableMarkerOperate::BOX;
00300 
00301     frame_id_ = frame;
00302     name_ = name;
00303     description_ = description;
00304   }
00305 
00306   visualization_msgs::Marker TransformableBox::getVisualizationMsgMarker(){
00307     marker_.scale.x = box_x_;
00308     marker_.scale.y = box_y_;
00309     marker_.scale.z = box_z_;
00310     marker_.color.r = box_r_;
00311     marker_.color.g = box_g_;
00312     marker_.color.b = box_b_;
00313     marker_.color.a = box_a_;
00314     return marker_;
00315   }
00316 }
00317 
00318 namespace jsk_interactive_marker{
00319 
00320   TransformableMesh::TransformableMesh( std::string frame, std::string name, std::string description, std::string mesh_resource, bool mesh_use_embedded_materials){
00321     marker_scale_ = 0.5;
00322     marker_.type = visualization_msgs::Marker::MESH_RESOURCE;
00323     type_ = jsk_rviz_plugins::TransformableMarkerOperate::MESH_RESOURCE;
00324     marker_.mesh_resource = mesh_resource;
00325     marker_.mesh_use_embedded_materials = mesh_use_embedded_materials;
00326     frame_id_ = frame;
00327     name_ = name;
00328     description_ = description;
00329   }
00330 
00331   visualization_msgs::Marker TransformableMesh::getVisualizationMsgMarker(){
00332     marker_.color.r = mesh_r_;
00333     marker_.color.g = mesh_g_;
00334     marker_.color.b = mesh_b_;
00335     marker_.color.a = mesh_a_;
00336     marker_.pose = inverse(control_offset_pose_);
00337     return marker_;
00338   }
00339 }


jsk_interactive_marker
Author(s): furuta
autogenerated on Sun Sep 13 2015 22:29:27