00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <srs_interaction_primitives/bounding_box.h>
00029
00030 using namespace std;
00031 using namespace interactive_markers;
00032 using namespace visualization_msgs;
00033 using namespace geometry_msgs;
00034 using namespace std_msgs;
00035
00036 namespace srs_interaction_primitives
00037 {
00038
00039 BoundingBox::BoundingBox(InteractiveMarkerServerPtr server, string frame_id, string name) :
00040 Primitive(server, frame_id, name, srs_interaction_primitives::PrimitiveType::BOUNDING_BOX)
00041 {
00042 description_ = "";
00043 }
00044
00045 void BoundingBox::bboxCallback(const InteractiveMarkerFeedbackConstPtr &feedback)
00046 {
00047 if (feedback->control_name != BOUNDING_BOX_CONTROL_NAME)
00048 {
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 defaultCallback(feedback);
00077 }
00078 }
00079
00080 void BoundingBox::menuCallback(const InteractiveMarkerFeedbackConstPtr &feedback)
00081 {
00082 MenuHandler::EntryHandle handle = feedback->menu_entry_id;
00083 MenuHandler::CheckState state;
00084 string title;
00085 menu_handler_.getCheckState(handle, state);
00086 menu_handler_.getTitle(handle, title);
00087
00088 updatePublisher_->publishMenuClicked(title, state);
00089
00090
00091
00092
00093
00094
00095
00096
00097 switch (feedback->menu_entry_id)
00098 {
00099 case 1:
00100
00101
00102
00103 if (state == MenuHandler::CHECKED)
00104 {
00105 showBoundingBoxControl(false);
00106 menu_handler_.setCheckState(handle, MenuHandler::UNCHECKED);
00107 }
00108 else
00109 {
00110 showBoundingBoxControl(true);
00111 menu_handler_.setCheckState(handle, MenuHandler::CHECKED);
00112 }
00113 break;
00114 case 2:
00115
00116
00117
00118 if (state == MenuHandler::CHECKED)
00119 {
00120 removeDescriptionControl();
00121 menu_handler_.setCheckState(handle, MenuHandler::UNCHECKED);
00122 }
00123 else
00124 {
00125 addDescriptionControl();
00126 menu_handler_.setCheckState(handle, MenuHandler::CHECKED);
00127 }
00128 break;
00129 case 3:
00130
00131
00132
00133 if (state == MenuHandler::CHECKED)
00134 {
00135 removeMeasureControl();
00136 menu_handler_.setCheckState(handle, MenuHandler::UNCHECKED);
00137 }
00138 else
00139 {
00140 addMeasureControl();
00141 menu_handler_.setCheckState(handle, MenuHandler::CHECKED);
00142 }
00143 break;
00144 case 5:
00145
00146
00147
00148 if (state == MenuHandler::CHECKED)
00149 {
00150 removeMovementControls();
00151 menu_handler_.setCheckState(handle, MenuHandler::UNCHECKED);
00152 }
00153 else
00154 {
00155 addMovementControls();
00156 menu_handler_.setCheckState(handle, MenuHandler::CHECKED);
00157 }
00158 break;
00159 case 6:
00160
00161
00162
00163 if (state == MenuHandler::CHECKED)
00164 {
00165 removeRotationControls();
00166 menu_handler_.setCheckState(handle, MenuHandler::UNCHECKED);
00167 }
00168 else
00169 {
00170 addRotationControls();
00171 menu_handler_.setCheckState(handle, MenuHandler::CHECKED);
00172 }
00173 break;
00174 case 8:
00175
00176
00177
00178 ROS_INFO("Take object");
00179 break;
00180 case 9:
00181
00182
00183
00184 ROS_INFO("Throw object");
00185 break;
00186 }
00187
00188 server_->insert(object_);
00189 menu_handler_.reApply(*server_);
00190 server_->applyChanges();
00191 }
00192
00193 void BoundingBox::createMenu()
00194 {
00195 if (!menu_created_)
00196 {
00197 menu_created_ = true;
00198 menu_handler_.setCheckState(
00199 menu_handler_.insert("Show Bounding Box", boost::bind(&BoundingBox::menuCallback, this, _1)),
00200 MenuHandler::CHECKED);
00201 menu_handler_.setCheckState(
00202 menu_handler_.insert("Show description", boost::bind(&BoundingBox::menuCallback, this, _1)),
00203 MenuHandler::UNCHECKED);
00204 menu_handler_.setCheckState(menu_handler_.insert("Show measure", boost::bind(&BoundingBox::menuCallback, this, _1)),
00205 MenuHandler::UNCHECKED);
00206
00207 MenuHandler::EntryHandle sub_menu_handle = menu_handler_.insert("Interaction");
00208 menu_handler_.setCheckState(
00209 menu_handler_.insert(sub_menu_handle, "Movement", boost::bind(&BoundingBox::menuCallback, this, _1)),
00210 MenuHandler::UNCHECKED);
00211 menu_handler_.setCheckState(
00212 menu_handler_.insert(sub_menu_handle, "Rotation", boost::bind(&BoundingBox::menuCallback, this, _1)),
00213 MenuHandler::UNCHECKED);
00214
00215 sub_menu_handle = menu_handler_.insert("Actions");
00216 menu_handler_.insert(sub_menu_handle, "Take object", boost::bind(&BoundingBox::menuCallback, this, _1));
00217 menu_handler_.insert(sub_menu_handle, "Throw object", boost::bind(&BoundingBox::menuCallback, this, _1));
00218 }
00219 }
00220
00221 void BoundingBox::createBoundingBoxControl()
00222 {
00223 createBoundingBoxControl(0.0f, 0.0f, 0.0f);
00224 }
00225
00226 void BoundingBox::createBoundingBoxControl(float trans_x, float trans_y, float trans_z)
00227 {
00228 Point p1, p2;
00229 double sx = scale_.x / 2;
00230 double sy = scale_.y / 2;
00231 double sz = scale_.z / 2;
00232
00233 bounding_box_.type = Marker::CUBE;
00234 bounding_box_.pose.position.x = trans_x;
00235 bounding_box_.pose.position.y = trans_y;
00236 bounding_box_.pose.position.z = trans_z;
00237 bounding_box_.scale = scale_;
00238 bounding_box_.color = color_;
00239 bounding_box_.color.a = BBOX_MAX_ALPHA;
00240
00241 wire_.points.clear();
00242
00243 wire_.type = Marker::LINE_LIST;
00244 wire_.scale = scale_;
00245 wire_.color.r = color_.b;
00246 wire_.color.g = color_.r;
00247 wire_.color.b = color_.g;
00248 wire_.color.a = BBOX_MAX_ALPHA;
00249 wire_.scale.x = 0.002;
00250
00251 p1.x = -sx + trans_x;
00252 p1.y = -sy + trans_y;
00253 p1.z = -sz + trans_z;
00254 p2.x = -sx + trans_x;
00255 p2.y = sy + trans_y;
00256 p2.z = -sz + trans_z;
00257 wire_.points.push_back(p1);
00258 wire_.points.push_back(p2);
00259 p2.x = -sx + trans_x;
00260 p2.y = -sy + trans_y;
00261 p2.z = sz + trans_z;
00262 wire_.points.push_back(p1);
00263 wire_.points.push_back(p2);
00264 p1.x = -sx + trans_x;
00265 p1.y = sy + trans_y;
00266 p1.z = sz + trans_z;
00267 p2.x = -sx + trans_x;
00268 p2.y = sy + trans_y;
00269 p2.z = -sz + trans_z;
00270 wire_.points.push_back(p1);
00271 wire_.points.push_back(p2);
00272 p2.x = -sx + trans_x;
00273 p2.y = -sy + trans_y;
00274 p2.z = sz + trans_z;
00275 wire_.points.push_back(p1);
00276 wire_.points.push_back(p2);
00277
00278 p1.x = sx + trans_x;
00279 p1.y = -sy + trans_y;
00280 p1.z = -sz + trans_z;
00281 p2.x = sx + trans_x;
00282 p2.y = sy + trans_y;
00283 p2.z = -sz + trans_z;
00284 wire_.points.push_back(p1);
00285 wire_.points.push_back(p2);
00286 p2.x = sx + trans_x;
00287 p2.y = -sy + trans_y;
00288 p2.z = sz + trans_z;
00289 wire_.points.push_back(p1);
00290 wire_.points.push_back(p2);
00291 p1.x = sx + trans_x;
00292 p1.y = sy + trans_y;
00293 p1.z = sz + trans_z;
00294 p2.x = sx + trans_x;
00295 p2.y = sy + trans_y;
00296 p2.z = -sz + trans_z;
00297 wire_.points.push_back(p1);
00298 wire_.points.push_back(p2);
00299 p2.x = sx + trans_x;
00300 p2.y = -sy + trans_y;
00301 p2.z = sz + trans_z;
00302 wire_.points.push_back(p1);
00303 wire_.points.push_back(p2);
00304
00305 p1.x = sx + trans_x;
00306 p1.y = sy + trans_y;
00307 p1.z = sz + trans_z;
00308 p2.x = -sx + trans_x;
00309 p2.y = sy + trans_y;
00310 p2.z = sz + trans_z;
00311 wire_.points.push_back(p1);
00312 wire_.points.push_back(p2);
00313
00314 p1.x = sx + trans_x;
00315 p1.y = -sy + trans_y;
00316 p1.z = sz + trans_z;
00317 p2.x = -sx + trans_x;
00318 p2.y = -sy + trans_y;
00319 p2.z = sz + trans_z;
00320 wire_.points.push_back(p1);
00321 wire_.points.push_back(p2);
00322
00323 p1.x = sx + trans_x;
00324 p1.y = sy + trans_y;
00325 p1.z = -sz + trans_z;
00326 p2.x = -sx + trans_x;
00327 p2.y = sy + trans_y;
00328 p2.z = -sz + trans_z;
00329 wire_.points.push_back(p1);
00330 wire_.points.push_back(p2);
00331
00332 p1.x = sx + trans_x;
00333 p1.y = -sy + trans_y;
00334 p1.z = -sz + trans_z;
00335 p2.x = -sx + trans_x;
00336 p2.y = -sy + trans_y;
00337 p2.z = -sz + trans_z;
00338 wire_.points.push_back(p1);
00339 wire_.points.push_back(p2);
00340
00341 control_.name = BOUNDING_BOX_CONTROL_NAME;
00342 control_.markers.push_back(bounding_box_);
00343 control_.markers.push_back(wire_);
00344 control_.interaction_mode = InteractiveMarkerControl::BUTTON;
00345 control_.always_visible = true;
00346
00347 object_.controls.push_back(control_);
00348 }
00349
00350 void BoundingBox::showBoundingBoxControl(bool show)
00351 {
00352 for (unsigned int i = 0; i < control_.markers.size(); i++)
00353 control_.markers[i].color.a = show ? BBOX_MAX_ALPHA : BBOX_MIN_ALPHA;
00354
00355 removeControl(BOUNDING_BOX_CONTROL_NAME);
00356 object_.controls.push_back(control_);
00357 }
00358
00359 void BoundingBox::create()
00360 {
00361 clearObject();
00362
00363 object_.header.frame_id = frame_id_;
00364 object_.name = name_;
00365
00366 object_.pose = pose_;
00367 object_.scale = srs_interaction_primitives::maxScale(scale_);
00368
00369 createBoundingBoxControl();
00370
00371 createMenu();
00372 }
00373
00374 void BoundingBox::insert()
00375 {
00376 create();
00377
00378 server_->insert(object_, boost::bind(&BoundingBox::bboxCallback, this, _1));
00379 menu_handler_.apply(*server_, name_);
00380 }
00381
00382 }