roi_manager_frame.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roboception GmbH
3  *
4  * Author: Carlos Xavier Garcia Briones
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  * may be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "roi_manager_frame.h"
34 #include "new_roi_frame.h"
35 
36 #include <wx/button.h>
37 #include <wx/panel.h>
38 #include <wx/sizer.h>
39 #include <wx/msgdlg.h>
40 #include <ros/ros.h>
41 #include <rc_pick_client/GetRegionsOfInterest.h>
42 #include <rc_pick_client/SetRegionOfInterest.h>
43 #include <rc_pick_client/DeleteRegionsOfInterest.h>
44 
45 #include "event_ids.h"
46 namespace rc_roi_manager_gui
47 {
48 RoiManagerFrame::RoiManagerFrame(const wxString& title)
49  : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500, 300))
50 {
51  nh_ = std::make_shared<ros::NodeHandle>();
52  ros::Duration(1).sleep();
53 
54  if (!nh_->getParam("/rc_roi_manager_gui/pick_module", pick_module_))
55  {
56  ROS_ERROR("Ros parameter: /rc_roi_manager_gui/pick_module musst be set. Either rc_itempick or rc_boxpick.");
57  return;
58  }
59  ROS_INFO_STREAM("Using: " << pick_module_ << " as pick module.");
60  ROS_ASSERT((pick_module_ == "rc_itempick") || (pick_module_ == "rc_boxpick"));
62  nh_->serviceClient<rc_pick_client::GetRegionsOfInterest>("/" + pick_module_ + "/get_regions_of_interest");
64  nh_->serviceClient<rc_pick_client::DeleteRegionsOfInterest>("/" + pick_module_ + "/delete_regions_of_interest");
65 
67  interactive_roi_server_ = std::make_shared<InteractiveRoiSelection>();
68 
69  wxPanel* panel = new wxPanel(this, -1);
70  item_list_ = new wxDataViewListCtrl(panel, wxID_ANY, wxPoint(-1, -1), wxSize(-1, -1));
71  item_list_->AppendTextColumn("Name", wxDATAVIEW_CELL_INERT, 200, wxALIGN_LEFT,
72  wxDATAVIEW_COL_RESIZABLE | wxDATAVIEW_COL_SORTABLE);
73  item_list_->AppendTextColumn("Pose Frame", wxDATAVIEW_CELL_INERT, 120, wxALIGN_LEFT,
74  wxDATAVIEW_COL_RESIZABLE | wxDATAVIEW_COL_SORTABLE);
75  item_list_->AppendTextColumn("Shape", wxDATAVIEW_CELL_INERT, 80, wxALIGN_LEFT,
76  wxDATAVIEW_COL_RESIZABLE | wxDATAVIEW_COL_SORTABLE);
77 
78  wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
79  auto* data_box = new wxBoxSizer(wxHORIZONTAL);
80  data_box->Add(item_list_, 1, wxEXPAND);
81  vbox->Add(data_box, 1, wxLEFT | wxRIGHT | wxEXPAND, 10);
82 
83  auto* button_box = new wxBoxSizer(wxHORIZONTAL);
84  auto* new_button = new wxButton(panel, ID_NewButton, "New");
85  auto* set_button = new wxButton(panel, ID_SetButton, "Update");
86  auto* edit_button = new wxButton(panel, ID_EditButton, "Edit");
87  auto* delete_button = new wxButton(panel, ID_DeleteButton, "Delete");
88  button_box->Add(new_button, 1);
89  button_box->Add(set_button, 1);
90  button_box->Add(edit_button, 1);
91  button_box->Add(delete_button, 1);
92  button_box->Add(-1, 0, wxEXPAND);
93  vbox->Add(button_box, 0, wxTOP | wxLEFT | wxRIGHT | wxBOTTOM, 10);
94 
95  panel->SetSizer(vbox);
96  updateGui();
97  Centre();
98  Connect(ID_NewButton, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(RoiManagerFrame::onNewButton));
99  Connect(ID_EditButton, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(RoiManagerFrame::onEditButton));
100  Connect(ID_SetButton, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(RoiManagerFrame::onUpdateButton));
101  Connect(ID_DeleteButton, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(RoiManagerFrame::onDeleteButton));
102 }
103 
104 void RoiManagerFrame::onDeleteButton(wxCommandEvent&)
105 {
106  int selected_row = item_list_->GetSelectedRow();
107  rc_pick_client::DeleteRegionsOfInterest srv;
108  if (selected_row != wxNOT_FOUND)
109  {
110  srv.request.region_of_interest_ids.emplace_back(item_list_->GetTextValue(selected_row, 0));
111  if (!client_delete_roi_.call(srv))
112  {
113  ROS_ERROR("Unable to call the service: delete_regions_of_interest");
114  }
115  }
116  updateGui();
117 }
118 
119 void RoiManagerFrame::onUpdateButton(wxCommandEvent&)
120 {
121  updateGui();
122 }
123 
124 bool RoiManagerFrame::isItemInList(wxString name)
125 {
126  for (int i = 0; i < item_list_->GetItemCount(); i++)
127  {
128  auto item_name = item_list_->GetTextValue(i, 0);
129  if (wxString(name) == item_name)
130  return true;
131  }
132  return false;
133 }
134 
135 void RoiManagerFrame::onEditButton(wxCommandEvent&)
136 {
137  int selected_row = item_list_->GetSelectedRow();
138  if (selected_row != wxNOT_FOUND)
139  {
140  rc_pick_client::GetRegionsOfInterest srv;
141  srv.request.region_of_interest_ids.emplace_back(item_list_->GetTextValue(selected_row, 0));
142  if (client_get_roi_.call(srv))
143  {
144  create_roi_frame_ = new NewRoiFrame("New Roi Frame", this, pick_module_);
145  create_roi_frame_->setRoi(srv.response.regions_of_interest[0], true);
146  create_roi_frame_->Show(true);
147  }
148  else
149  {
150  ROS_ERROR("Failed to call service: get_regions_of_interest");
151  }
152  }
153 }
154 
155 void RoiManagerFrame::onNewButton(wxCommandEvent&)
156 {
157  create_roi_frame_ = new NewRoiFrame("New Roi Frame", this, pick_module_);
158  rc_pick_client::RegionOfInterest roi;
159  roi.id = "";
160  roi.primitive.dimensions = { 0.1, 0.1, 0.1 };
161  roi.pose.pose.orientation.w = 1;
162  roi.pose.header.frame_id = "camera";
163  roi.primitive.type = 1;
164  create_roi_frame_->setRoi(roi, false);
165  create_roi_frame_->Show(true);
166 }
167 
169 {
170  item_list_->DeleteAllItems();
171  rc_pick_client::GetRegionsOfInterest srv;
172  wxString shapes[] = { "Box", "Sphere" };
173  ros::Duration(0.01).sleep();
174  if (client_get_roi_.call(srv))
175  {
176  ROS_DEBUG("Size of vector of regions of interest: %zu", srv.response.regions_of_interest.size());
177  for (rc_pick_client::RegionOfInterest roi : srv.response.regions_of_interest)
178  {
179  wxVector<wxVariant> item;
180  wxString pose_frame = wxString(roi.pose.header.frame_id);
181  wxString name = wxString(roi.id);
182  wxString shape = shapes[roi.primitive.type - 1];
183  item.push_back(name);
184  item.push_back(pose_frame);
185  item.push_back(shape);
186  item_list_->AppendItem(item);
187  }
188  }
189  else
190  {
191  ROS_ERROR("UpdateGui: Failed to call service get_regions_of_interest");
192  }
193 }
194 } // namespace rc_roi_manager_gui
void onDeleteButton(wxCommandEvent &)
Event handler for the delete button.
void onNewButton(wxCommandEvent &)
Event handler for the new button.
void updateGui()
Update list of shown rois on the gui.
std::shared_ptr< InteractiveRoiSelection > interactive_roi_server_
bool sleep() const
bool call(MReq &req, MRes &res)
std::shared_ptr< ros::NodeHandle > nh_
void onUpdateButton(wxCommandEvent &)
Event handler for the update button.
void onEditButton(wxCommandEvent &)
Event handler for the edit button.
bool isItemInList(wxString name)
Search if an roi is already present.
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
void setRoi(rc_pick_client::RegionOfInterest roi, bool edit)
sets the member roi_
RoiManagerFrame(const wxString &title)
Constructor.
#define ROS_INFO_STREAM(args)
#define ROS_ASSERT(cond)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


rc_roi_manager_gui
Author(s): Carlos Xavier Garcia Briones
autogenerated on Sat Feb 13 2021 03:42:01