table_marker.cpp
Go to the documentation of this file.
00001 
00060 #include <cob_3d_visualization/table_marker.h>
00061 
00062 TableMarker::TableMarker (boost::shared_ptr<interactive_markers::InteractiveMarkerServer> server,cob_3d_mapping_msgs::Shape& table,int ctr/*,
00063     tabletop_object_detector::Table& tableMsg*/)
00064 {
00065   id_ = ctr ;
00066   table_im_server_ = server ;
00067   table_ = table ;
00068   //goal_pub_ = nh_.advertise<geometry_msgs::PoseStamped>("/move_base_linear_simple/goal", 1);
00069 
00070   createTableMenu();
00071   createInteractiveMarkerForTable();
00072   //Table msg
00073   //table_msg_ = tableMsg ;
00074 }
00075 
00076 void TableMarker::createInteractiveMarkerForTable ()
00077 {
00078   cob_3d_mapping::Polygon p;
00079   cob_3d_mapping::fromROSMsg (table_, p);
00080 
00081 
00082 
00083   /* create interactive marker for *this shape */
00084   stringstream ss;
00085   ss << "table_"<< id_ ; //ctr_ ;
00086   table_int_marker_.name = ss.str ();
00087   table_int_marker_.header = table_.header;
00088   table_int_marker_.header.stamp = ros::Time::now() ;
00089 
00090   ss.str ("");
00091   im_ctrl.always_visible = true;
00092   ss << "table_" << id_ << "_control";
00093   im_ctrl.name = ss.str ();
00094   im_ctrl.description = "table_markers";
00095   im_ctrl.interaction_mode = visualization_msgs::InteractiveMarkerControl::BUTTON;
00096 
00097   /* create marker */
00098   createMarkerforTable (im_ctrl);
00099 
00100   table_int_marker_.controls.push_back (im_ctrl);
00101   table_im_server_->insert (table_int_marker_);
00102 
00103 
00104   table_menu_handler_.apply (*table_im_server_, table_int_marker_.name);
00105   table_im_server_ ->applyChanges() ;
00106 
00107 }
00108 
00109 void
00110 TableMarker::createMarkerforTable (visualization_msgs::InteractiveMarkerControl& im_ctrl)
00111 {
00112   float offset(0.1);
00113 
00114   //  /* transform shape points to 2d and store 2d point in triangle list */
00115   TPPLPartition pp;
00116   list<TPPLPoly> polys, tri_list;
00117 
00118   Eigen::Vector3f v, normal, origin;
00119   if (table_.params.size () == 4)
00120   {
00121     normal (0) = table_.params[0];
00122     normal (1) = table_.params[1];
00123     normal (2) = table_.params[2];
00124     origin (0) = table_.centroid.x + offset ;
00125     origin (1) = table_.centroid.y + offset ;
00126     origin (2) = table_.centroid.z + offset ;
00127     v = normal.unitOrthogonal ();
00128 
00129     pcl::getTransformationFromTwoUnitVectorsAndOrigin (v, normal, origin, transformation_);
00130     transformation_inv_ = transformation_.inverse ();
00131   }
00132 
00133   for (size_t i = 0; i < table_.points.size (); i++)
00134   {
00135     pcl::PointCloud<pcl::PointXYZ> pc;
00136     TPPLPoly poly;
00137     pcl::fromROSMsg (table_.points[i], pc);
00138     poly.Init (pc.points.size ());
00139     poly.SetHole (table_.holes[i]);
00140 
00141     for (size_t j = 0; j < pc.points.size (); j++)
00142     {
00143       poly[j] = msgToPoint2DforTable (pc[j]);
00144     }
00145     if (table_.holes[i])
00146       poly.SetOrientation (TPPL_CW);
00147     else
00148       poly.SetOrientation (TPPL_CCW);
00149 
00150     polys.push_back (poly);
00151   }
00152   pp.Triangulate_EC (&polys, &tri_list);
00153   TPPLPoint pt;
00154   for (std::list<TPPLPoly>::iterator it = tri_list.begin (); it != tri_list.end (); it++)
00155   {
00156     table_marker_.id = id_ ; //ctr_;
00157 
00158     table_marker_.header = table_.header;
00159     table_marker_.header.stamp = ros::Time::now() ;
00160 
00161     table_marker_.type = visualization_msgs::Marker::TRIANGLE_LIST;
00162     table_marker_.ns = "table visualization";
00163     table_marker_.action = visualization_msgs::Marker::ADD;
00164     table_marker_.lifetime = ros::Duration ();
00165 
00166     //set color
00167     table_marker_.color.r = 1 ;
00168     table_marker_.color.g = 0;
00169     table_marker_.color.b = 0;
00170     table_marker_.color.a = 1;
00171 
00172     //set scale
00173     table_marker_.scale.x = 1;
00174     table_marker_.scale.y = 1;
00175     table_marker_.scale.z = 1;
00176 
00177     //set pose
00178     Eigen::Quaternionf quat (transformation_inv_.rotation ());
00179     Eigen::Vector3f trans (transformation_inv_.translation ());
00180 
00181     table_marker_.pose.position.x = trans (0);
00182     table_marker_.pose.position.y = trans (1);
00183     table_marker_.pose.position.z = trans (2);
00184 
00185     table_marker_.pose.orientation.x = quat.x ();
00186     table_marker_.pose.orientation.y = quat.y ();
00187     table_marker_.pose.orientation.z = quat.z ();
00188     table_marker_.pose.orientation.w = quat.w ();
00189 
00190     //draw each triangle
00191     table_marker_.points.resize (it->GetNumPoints ());
00192     for (long i = 0; i < it->GetNumPoints (); i++)
00193     {
00194       pt = it->GetPoint (i);
00195       table_marker_.points[i].x = pt.x;
00196       table_marker_.points[i].y = pt.y;
00197       table_marker_.points[i].z = 0;
00198     }
00199     im_ctrl.markers.push_back (table_marker_);
00200   }
00201 }
00202 
00203 TPPLPoint
00204 TableMarker::msgToPoint2DforTable (const pcl::PointXYZ &point)
00205 {
00206   //ROS_INFO(" transform 3D point to 2D ");
00207   TPPLPoint pt;
00208   Eigen::Vector3f p = transformation_ * point.getVector3fMap ();
00209   pt.x = p (0);
00210   pt.y = p (1);
00211   //std::cout << "\n transformed point : \n" << p << std::endl;
00212   return pt;
00213 }
00214 
00215 void TableMarker::tableFeedbackCallback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback) {
00216 
00217   ROS_INFO("%s position : x= %f, y= %f, z= %f", table_int_marker_.name.c_str(), table_.centroid.x,table_.centroid.y,table_.centroid.z);
00218 
00219 }
00220 void TableMarker::createTableMenu() {
00221 
00222   interactive_markers::MenuHandler::EntryHandle eh_1;
00223   eh_1 = table_menu_handler_.insert ("Move to this table",boost::bind (&TableMarker::MoveToTheTable, this, _1));
00224 
00225   table_menu_handler_.setVisible (eh_1, true);
00226   table_menu_handler_.setCheckState (eh_1, interactive_markers::MenuHandler::NO_CHECKBOX);
00227 }
00228 
00229 void TableMarker::MoveToTheTable(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback) {
00230   std::cout << "in MovingToTable..." << "\n" ;
00231 
00232   ROS_INFO("Moving to table %d...",id_) ;
00233 
00234   cob_3d_mapping_msgs::MoveToTable::Request  reqMoveToTable;
00235   cob_3d_mapping_msgs::MoveToTable::Response resMoveToTable;
00236 
00237   //reqMoveToTable.targetTable = table_ ;
00238   /*reqMoveToTable.tableCentroid.position.x = table_.centroid.x ;
00239   reqMoveToTable.tableCentroid.position.y = table_.centroid.y ;
00240   reqMoveToTable.tableCentroid.position.z = table_.centroid.z ;*/
00241 
00242   if (ros::service::call("/move_to_table",reqMoveToTable,resMoveToTable)){
00243     // Calling move_to_table Service...
00244   }
00245 
00246 }
00247 


cob_3d_visualization
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:04:10