transformable_marker_operator.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include <QLineEdit>
4 #include <QPushButton>
5 #include <QVBoxLayout>
6 #include <QHBoxLayout>
7 #include <QTabWidget>
8 #include <QLabel>
9 
11 #include <rviz/frame_manager.h>
12 
14 #include <jsk_interactive_marker/GetMarkerDimensions.h>
15 #include <jsk_interactive_marker/GetTransformableMarkerFocus.h>
16 
18 
19 using namespace rviz;
20 namespace jsk_interactive_marker
21 {
22  TransformableMarkerOperatorAction::TransformableMarkerOperatorAction( QWidget* parent )
23  : rviz::Panel( parent )
24  {
25  layout = new QVBoxLayout;
26 
27  // server name
28  QHBoxLayout* server_name_layout = new QHBoxLayout;
29  server_name_layout->addWidget( new QLabel( "Server Name:" ));
30  server_name_editor_ = new QLineEdit;
31  server_name_layout->addWidget( server_name_editor_ );
32  layout->addLayout( server_name_layout );
33 
34  // topic name
35  QHBoxLayout* obj_array_topic_layout = new QHBoxLayout;
36  obj_array_topic_layout->addWidget( new QLabel( "ObjectArray Topic:" ));
37  topic_name_editor_ = new QLineEdit;
38  obj_array_topic_layout->addWidget( topic_name_editor_ );
39  layout->addLayout( obj_array_topic_layout );
40 
41  // tabs for operations
42  QTabWidget* tabs = new QTabWidget();
43 
44  QVBoxLayout* layout1 = new QVBoxLayout;
45  QVBoxLayout* layout2 = new QVBoxLayout;
46  QVBoxLayout* layout3 = new QVBoxLayout;
47 
48  QWidget* tab_1 = new QWidget();
49  QWidget* tab_2 = new QWidget();
50  QWidget* tab_3 = new QWidget();
51 
52  // tab_1, layout1: insert
53 
54  insert_box_button_ = new QPushButton("Insert New Box Marker");
55  layout1->addWidget( insert_box_button_ );
56 
57  insert_cylinder_button_ = new QPushButton("Insert New Cylinder Marker");
58  layout1->addWidget( insert_cylinder_button_ );
59 
60  insert_torus_button_ = new QPushButton("Insert New Torus Marker");
61  layout1->addWidget( insert_torus_button_ );
62 
63  insert_mesh_button_ = new QPushButton("Insert New Mesh Marker");
64  layout1->addWidget( insert_mesh_button_ );
65 
66  QHBoxLayout* object_layout = new QHBoxLayout;
67  object_layout->addWidget( new QLabel( "Object:" ));
68  object_editor_ = new QComboBox;
69  object_editor_->setIconSize(QSize(50, 50));
70  object_editor_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
71  object_layout->addWidget( object_editor_ );
72  layout1->addLayout( object_layout );
73 
74  QHBoxLayout* name_layout = new QHBoxLayout;
75  name_layout->addWidget( new QLabel( "Name:" ));
76  name_editor_ = new QLineEdit;
77  name_layout->addWidget( name_editor_ );
78  layout1->addLayout( name_layout );
79 
80  QHBoxLayout* description_layout = new QHBoxLayout;
81  description_layout->addWidget( new QLabel( "Description:" ));
82  description_editor_ = new QLineEdit;
83  description_layout->addWidget( description_editor_ );
84  layout1->addLayout( description_layout );
85 
86  QHBoxLayout* frame_layout = new QHBoxLayout;
87  frame_layout->addWidget( new QLabel( "Frame:" ));
88  frame_editor_ = new QLineEdit;
89  frame_layout->addWidget( frame_editor_ );
90  layout1->addLayout( frame_layout );
91 
92  // tab_2, layout2: transform
93  QVBoxLayout* transform_layout = new QVBoxLayout;
94  transform_layout->addWidget( new QLabel( "Object Name:" ));
95  transform_name_editor_ = new QLineEdit;
96  transform_layout->addWidget( transform_name_editor_ );
97  transform_layout->addWidget( new QLabel( "Dimension X:" ));
98  dimension_x_editor_ = new QLineEdit;
99  transform_layout->addWidget( dimension_x_editor_ );
100  transform_layout->addWidget( new QLabel( "Dimension Y:" ));
101  dimension_y_editor_ = new QLineEdit;
102  transform_layout->addWidget( dimension_y_editor_ );
103  transform_layout->addWidget( new QLabel( "Dimension Z:" ));
104  dimension_z_editor_ = new QLineEdit;
105  transform_layout->addWidget( dimension_z_editor_ );
106  transform_layout->addWidget( new QLabel( "Dimension Radius:" ));
107  dimension_radius_editor_ = new QLineEdit;
108  transform_layout->addWidget( dimension_radius_editor_ );
109  transform_layout->addWidget( new QLabel( "Dimension Small Radius:" ));
110  dimension_sm_radius_editor_ = new QLineEdit;
111  transform_layout->addWidget( dimension_sm_radius_editor_ );
112  layout2->addLayout( transform_layout );
113 
114  // tab_3, layout3: erase
115 
116  erase_with_id_button_ = new QPushButton("Erase with id");
117  layout3->addWidget( erase_with_id_button_ );
118 
119  QHBoxLayout* id_layout = new QHBoxLayout;
120  id_layout->addWidget( new QLabel( "Id:" ));
121  id_editor_ = new QLineEdit;
122  id_layout->addWidget( id_editor_ );
123  layout3->addLayout( id_layout );
124 
125  erase_all_button_ = new QPushButton("Erase all");
126  layout3->addWidget( erase_all_button_ );
127 
128  erase_focus_button_ = new QPushButton("Erase focus");
129  layout3->addWidget( erase_focus_button_ );
130 
131  tab_1->setLayout( layout1 );
132  tab_2->setLayout( layout2 );
133  tab_3->setLayout( layout3 );
134 
135  tabs->addTab(tab_1, QString("Insert"));
136  tabs->addTab(tab_2, QString("Transform"));
137  tabs->addTab(tab_3, QString("Erase"));
138 
139  layout->addWidget( tabs );
140  setLayout( layout );
141 
142  connect( insert_box_button_, SIGNAL( clicked() ), this, SLOT( insertBoxService ()));
143  connect( insert_cylinder_button_, SIGNAL( clicked() ), this, SLOT( insertCylinderService ()));
144  connect( insert_torus_button_, SIGNAL( clicked() ), this, SLOT( insertTorusService ()));
145  connect( insert_mesh_button_, SIGNAL( clicked() ), this, SLOT( insertMeshService ()));
146  connect( erase_with_id_button_, SIGNAL( clicked() ), this, SLOT( eraseWithIdService ()));
147  connect( erase_all_button_, SIGNAL( clicked() ), this, SLOT( eraseAllService ()));
148  connect( erase_focus_button_, SIGNAL( clicked() ), this, SLOT( eraseFocusService ()));
149 
150  connect( dimension_x_editor_, SIGNAL( editingFinished() ), this, SLOT( updateDimensionsService ()));
151  connect( dimension_y_editor_, SIGNAL( editingFinished() ), this, SLOT( updateDimensionsService ()));
152  connect( dimension_z_editor_, SIGNAL( editingFinished() ), this, SLOT( updateDimensionsService ()));
153  connect( dimension_radius_editor_, SIGNAL( editingFinished() ), this, SLOT( updateDimensionsService ()));
154  connect( dimension_sm_radius_editor_, SIGNAL( editingFinished() ), this, SLOT( updateDimensionsService ()));
155  connect( object_editor_, SIGNAL( currentIndexChanged(int)), SLOT( updateName ()));
156  connect( topic_name_editor_, SIGNAL( editingFinished() ), this, SLOT( updateObjectArrayTopic ()));
157  }
158 
159  void TransformableMarkerOperatorAction::objectArrayCb(const jsk_recognition_msgs::ObjectArray::ConstPtr& obj_array_msg) {
160  objects_ = obj_array_msg->objects;
161  int current_index = object_editor_->currentIndex();
162  object_editor_->clear();
163  for (size_t i = 0; i < obj_array_msg->objects.size(); i++) {
164  jsk_recognition_msgs::Object object = objects_[i];
165  // thumbnail
166  QPixmap pixmap = QPixmap();
167  if (object.image_resources.size() > 0)
168  {
169  std::string thumbnail = object.image_resources[0];
171  resource_retriever::MemoryResource mem = retriever.get(thumbnail);
172  pixmap.loadFromData(static_cast<unsigned char*>(mem.data.get()), mem.size);
173  }
174  // name
175  std::stringstream ss;
176  ss << object.id << ": " << object.name;
177  //
178  object_editor_->addItem(QIcon(pixmap), QString::fromStdString(ss.str()));
179  }
180  object_editor_->setCurrentIndex(current_index);
181  }
182 
184  connect( vis_manager_, SIGNAL( preUpdate() ), this, SLOT( update() ));
186  }
187 
191  updateFrameId();
192  // updateDimensionsService();
193  }
194 
197  std::string topic = topic_name_editor_->text().toStdString();
198  if (topic.empty()) {
200  ros::master::getTopics(topics);
201  for (size_t i = 0; i < topics.size(); i++) {
202  if (topics[i].datatype == "jsk_recognition_msgs/ObjectArray") {
203  topic = topics[i].name;
204  break;
205  }
206  }
207  topic_name_editor_->setText(QString::fromStdString(topic));
208  }
211  }
212 
214  jsk_rviz_plugins::RequestMarkerOperate operator_srv;
215  operator_srv.request.operate.type = jsk_rviz_plugins::TransformableMarkerOperate::BOX;
216  operator_srv.request.operate.action = jsk_rviz_plugins::TransformableMarkerOperate::INSERT;
217  operator_srv.request.operate.name = name_editor_->text().toStdString();
218  operator_srv.request.operate.description = description_editor_->text().toStdString();
219  operator_srv.request.operate.frame_id = frame_editor_->text().toStdString();
220  callRequestMarkerOperateService(operator_srv);
221  };
222 
224  jsk_rviz_plugins::RequestMarkerOperate operator_srv;
225  operator_srv.request.operate.type = jsk_rviz_plugins::TransformableMarkerOperate::CYLINDER;
226  operator_srv.request.operate.action = jsk_rviz_plugins::TransformableMarkerOperate::INSERT;
227  operator_srv.request.operate.name = name_editor_->text().toStdString();
228  operator_srv.request.operate.description = description_editor_->text().toStdString();
229  operator_srv.request.operate.frame_id = frame_editor_->text().toStdString();
230  callRequestMarkerOperateService(operator_srv);
231  };
232 
234  int current_index = object_editor_->currentIndex();
235  if (!(0 <= current_index && current_index < objects_.size())) {
236  ROS_ERROR("Invalid index for object selection: %d. Please select again.", current_index);
237  return;
238  }
239  jsk_recognition_msgs::Object object = objects_[current_index];
240  if (object.mesh_resource.empty()) {
241  ROS_ERROR("Mesh resource of object '%s' is empty, so skipping.", object.name.c_str());
242  return;
243  }
244 
245  jsk_rviz_plugins::RequestMarkerOperate operator_srv;
246  operator_srv.request.operate.type = jsk_rviz_plugins::TransformableMarkerOperate::MESH_RESOURCE;
247  operator_srv.request.operate.action = jsk_rviz_plugins::TransformableMarkerOperate::INSERT;
248  operator_srv.request.operate.name = object.name;
249  operator_srv.request.operate.description = description_editor_->text().toStdString();
250  operator_srv.request.operate.frame_id = frame_editor_->text().toStdString();
251  operator_srv.request.operate.mesh_resource = object.mesh_resource;
252  operator_srv.request.operate.mesh_use_embedded_materials = true;
253  callRequestMarkerOperateService(operator_srv);
254  };
255 
257  jsk_rviz_plugins::RequestMarkerOperate operator_srv;
258  operator_srv.request.operate.type = jsk_rviz_plugins::TransformableMarkerOperate::TORUS;
259  operator_srv.request.operate.action = jsk_rviz_plugins::TransformableMarkerOperate::INSERT;
260  operator_srv.request.operate.name = name_editor_->text().toStdString();
261  operator_srv.request.operate.description = description_editor_->text().toStdString();
262  operator_srv.request.operate.frame_id = frame_editor_->text().toStdString();
263  callRequestMarkerOperateService(operator_srv);
264  };
265 
267  jsk_rviz_plugins::RequestMarkerOperate operator_srv;
268  operator_srv.request.operate.action = jsk_rviz_plugins::TransformableMarkerOperate::ERASE;
269  operator_srv.request.operate.name = id_editor_->text().toStdString();
270  callRequestMarkerOperateService(operator_srv);
271  };
272 
274  jsk_rviz_plugins::RequestMarkerOperate operator_srv;
275  operator_srv.request.operate.action = jsk_rviz_plugins::TransformableMarkerOperate::ERASEALL;
276  callRequestMarkerOperateService(operator_srv);
277  };
278 
280  jsk_rviz_plugins::RequestMarkerOperate operator_srv;
281  operator_srv.request.operate.action = jsk_rviz_plugins::TransformableMarkerOperate::ERASEFOCUS;
282  callRequestMarkerOperateService(operator_srv);
283  };
284 
285  void TransformableMarkerOperatorAction::callRequestMarkerOperateService(jsk_rviz_plugins::RequestMarkerOperate srv){
286  std::string server_name = server_name_editor_->text().toStdString();
287  std::string service_name = server_name + "/request_marker_operate";
288  ros::ServiceClient client = nh_.serviceClient<jsk_rviz_plugins::RequestMarkerOperate>(service_name, true);
289  if(client.call(srv))
290  {
291  ROS_INFO("Call Success");
292  }
293  else{
294  ROS_ERROR("Service call FAIL: %s", service_name.c_str());
295  };
296  }
297 
299  std::string server_name = server_name_editor_->text().toStdString();
300  std::string service_name = server_name + "/set_dimensions";
301  ros::ServiceClient client = nh_.serviceClient<jsk_interactive_marker::SetMarkerDimensions>(service_name, true);
302 
303  jsk_interactive_marker::SetMarkerDimensions srv;
304  if (transform_name_editor_->text().toStdString().empty()) {
305  srv.request.dimensions.x = transform_name_editor_->placeholderText().toFloat();
306  } else {
307  srv.request.dimensions.x = transform_name_editor_->text().toFloat();
308  }
309  if (dimension_x_editor_->text().toStdString().empty()) {
310  srv.request.dimensions.x = dimension_x_editor_->placeholderText().toFloat();
311  } else {
312  srv.request.dimensions.x = dimension_x_editor_->text().toFloat();
313  }
314  if (dimension_y_editor_->text().toStdString().empty()) {
315  srv.request.dimensions.y = dimension_y_editor_->placeholderText().toFloat();
316  } else {
317  srv.request.dimensions.y = dimension_y_editor_->text().toFloat();
318  }
319  if (dimension_z_editor_->text().toStdString().empty()) {
320  srv.request.dimensions.z = dimension_z_editor_->placeholderText().toFloat();
321  } else {
322  srv.request.dimensions.z = dimension_z_editor_->text().toFloat();
323  }
324 
325  if (client.call(srv)) {
326  ROS_INFO("Call success: %s", service_name.c_str());
327  } else {
328  ROS_ERROR("Service call fail: %s", service_name.c_str());
329  }
330  }
331 
333  if (frame_editor_->text().isEmpty()) {
335  }
336  }
337 
339  int current_index = object_editor_->currentIndex();
340  if (0 <= current_index && current_index < objects_.size()) {
341  jsk_recognition_msgs::Object object = objects_[current_index];
342  name_editor_->setText(QString::fromStdString(object.name));
343  } else {
344  name_editor_->setText(QString(""));
345  }
346  }
347 
349  std::string server_name = server_name_editor_->text().toStdString();
350  if (server_name.empty() && !ros::service::exists("/request_marker_operate", false)) {
352  ros::master::getNodes(nodes);
353  for (size_t i=0; i<nodes.size(); i++) {
354  if (ros::service::exists(nodes[i] + "/request_marker_operate", false)) {
355  server_name_editor_->setText(QString::fromStdString(nodes[i]));
356  break;
357  }
358  }
359  }
360  }
361 
363  std::string server_name = server_name_editor_->text().toStdString();
364  ros::ServiceClient client_focus = nh_.serviceClient<jsk_interactive_marker::GetTransformableMarkerFocus>(
365  server_name + "/get_focus", true);
366  jsk_interactive_marker::GetTransformableMarkerFocus srv_focus;
367  ros::ServiceClient client_dim = nh_.serviceClient<jsk_interactive_marker::GetMarkerDimensions>(
368  server_name + "/get_dimensions", true);
369  jsk_interactive_marker::GetMarkerDimensions srv_dim;
370  if (client_focus.call(srv_focus) && client_dim.call(srv_dim)) {
371  transform_name_editor_->setPlaceholderText(QString::fromStdString(srv_focus.response.target_name));
372  dimension_x_editor_->setPlaceholderText(QString::number(srv_dim.response.dimensions.x, 'f', 4));
373  dimension_y_editor_->setPlaceholderText(QString::number(srv_dim.response.dimensions.y, 'f', 4));
374  dimension_z_editor_->setPlaceholderText(QString::number(srv_dim.response.dimensions.z, 'f', 4));
375  dimension_radius_editor_->setPlaceholderText(QString::number(srv_dim.response.dimensions.radius, 'f', 4));
376  dimension_sm_radius_editor_->setPlaceholderText(
377  QString::number(srv_dim.response.dimensions.small_radius, 'f', 4));
378  } else{
379  ROS_ERROR_THROTTLE(10, "Service call FAIL: %s", server_name.c_str());
380  }
381  }
382 
384  {
385  rviz::Panel::save( config );
386  config.mapSetValue( "ServerName", server_name_editor_->text().toStdString().c_str() );
387  }
388 
390  {
391  rviz::Panel::load( config );
392  QString server_name;
393  config.mapGetString( "ServerName", &server_name );
394  server_name_editor_->setText(server_name);
395  }
396 } // namespace jsk_interactive_marker
397 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
pointer SIGNAL(context *, int, pointer *)
VisualizationManager * vis_manager_
PLUGINLIB_EXPORT_CLASS(image_publisher::ImagePublisherNodelet, nodelet::Nodelet)
nodes
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool call(MReq &req, MRes &res)
ROSCPP_DECL bool getTopics(V_TopicInfo &topics)
std::vector< TopicInfo > V_TopicInfo
bool mapGetString(const QString &key, QString *value_out) const
void mapSetValue(const QString &key, QVariant value)
#define ROS_ERROR_THROTTLE(rate,...)
std::vector< std::string > V_string
current_index
boost::shared_array< uint8_t > data
#define ROS_INFO(...)
topic
topics
srv
void objectArrayCb(const jsk_recognition_msgs::ObjectArray::ConstPtr &obj_array_msg)
MemoryResource get(const std::string &url)
void callRequestMarkerOperateService(jsk_rviz_plugins::RequestMarkerOperate srv)
pointer SLOT(context *, int, pointer *)
virtual void save(Config config) const
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
ROSCPP_DECL bool getNodes(V_string &nodes)
virtual void load(const Config &config)
#define ROS_ERROR(...)
QString getFixedFrame() const


jsk_interactive_marker
Author(s): furuta
autogenerated on Sat Mar 20 2021 03:03:33