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
00029
00030 #include "pr2_interactive_manipulation/object_selection_frame.h"
00031
00032 #include "rviz_interaction_tools/image_tools.h"
00033
00034 #include "rviz_interaction_tools/mesh_object.h"
00035 #include "rviz_interaction_tools/camera_tools.h"
00036
00037 #include <ogre_tools/point_cloud.h>
00038
00039 #include <OGRE/OgreManualObject.h>
00040 #include <OGRE/OgreMaterialManager.h>
00041 #include <OGRE/OgreRenderWindow.h>
00042 #include <OGRE/OgreSceneNode.h>
00043 #include <OGRE/OgreSceneManager.h>
00044 #include <OGRE/OgreRoot.h>
00045
00046 #include <rviz/render_panel.h>
00047 #include <rviz/window_manager_interface.h>
00048 #include <rviz/visualization_manager.h>
00049
00050 #include "rviz_interaction_tools/image_overlay.h"
00051 #include "rviz_interaction_tools/camera_tools.h"
00052 #include "rviz_interaction_tools/unique_string_manager.h"
00053
00054 #include "household_objects_database_msgs/GetModelMesh.h"
00055
00056 using namespace rviz_interaction_tools;
00057 using namespace household_objects_database_msgs;
00058
00059 static const std::string mat_base_name = "object_selection_frame";
00060
00061 namespace pr2_interactive_manipulation
00062 {
00063
00064 ObjectSelectionFrame::ObjectSelectionFrame(rviz::VisualizationManager *visualization_manager, wxWindow *parent) :
00065 ObjectSelectionFrameBase( parent ),
00066 selected_object_(-1),
00067 action_state_(IDLE)
00068 {
00069 UniqueStringManager usm;
00070
00071
00072 scene_manager_ = Ogre::Root::getSingleton().createSceneManager( Ogre::ST_GENERIC, usm.unique("ObjectSelectionFrame") );
00073
00074
00075 scene_root_ = scene_manager_->getRootSceneNode()->createChildSceneNode();
00076
00077 image_overlay_ = new rviz_interaction_tools::ImageOverlay( scene_manager_->getRootSceneNode(), Ogre::RENDER_QUEUE_BACKGROUND );
00078
00079 createRenderPanel( visualization_manager );
00080
00081 createMaterials();
00082
00083 ray_scene_query_ = scene_manager_->createRayQuery(Ogre::Ray());
00084
00085 Hide();
00086 }
00087
00088
00089 ObjectSelectionFrame::~ObjectSelectionFrame()
00090 {
00091 cleanupAndHide();
00092
00093 scene_manager_->destroyQuery( ray_scene_query_ );
00094
00095 render_panel_->getRenderWindow()->setActive(false);
00096
00097 delete render_panel_;
00098 delete image_overlay_;
00099 }
00100
00101
00102 void ObjectSelectionFrame::cleanupAndHide()
00103 {
00104 meshes_.clear();
00105 scene_root_->removeAndDestroyAllChildren();
00106 Hide();
00107 }
00108
00109
00110 void ObjectSelectionFrame::createRenderPanel( rviz::VisualizationManager *visualization_manager )
00111 {
00112
00113 render_panel_ = new rviz::RenderPanel(this, false);
00114
00115 render_panel_->SetSize( panel_->GetRect() );
00116
00117
00118 render_panel_->Connect( wxEVT_MOTION,
00119 wxMouseEventHandler( ObjectSelectionFrame::onRenderWindowMouseEvents ), NULL, this );
00120 render_panel_->Connect( wxEVT_LEFT_DOWN,
00121 wxMouseEventHandler( ObjectSelectionFrame::onRenderWindowMouseEvents ), NULL, this );
00122 render_panel_->Connect( wxEVT_RIGHT_DOWN,
00123 wxMouseEventHandler( ObjectSelectionFrame::onRenderWindowMouseEvents ), NULL, this );
00124
00125 render_panel_->createRenderWindow();
00126 render_panel_->initialize(scene_manager_, visualization_manager);
00127
00128 render_panel_->setAutoRender(false);
00129 render_panel_->getViewport()->setOverlaysEnabled(false);
00130 render_panel_->getViewport()->setClearEveryFrame(true);
00131 render_panel_->getRenderWindow()->setAutoUpdated(false);
00132 render_panel_->getRenderWindow()->setActive(true);
00133
00134
00135 render_panel_->getCamera()->setPosition(0.0, 0.0, 0.0);
00136 render_panel_->getCamera()->lookAt(Ogre::Vector3(0, 0, 1));
00137 render_panel_->getCamera()->roll(Ogre::Radian(3.141592653));
00138 render_panel_->getCamera()->setFOVy(Ogre::Radian(0.518));
00139 render_panel_->getCamera()->setAspectRatio(640.0 / 480.0);
00140 render_panel_->getCamera()->setNearClipDistance( 0.01f );
00141 }
00142
00143
00144 void ObjectSelectionFrame::createMaterials()
00145 {
00146 Ogre::MaterialPtr white = Ogre::MaterialManager::getSingleton().getByName("BaseWhiteNoLighting");
00147
00148 if ( !Ogre::MaterialManager::getSingleton().getByName("object_selection_normal").isNull() )
00149 {
00150
00151 return;
00152 }
00153
00154 Ogre::Pass *pass;
00155
00156
00157 Ogre::MaterialPtr base_material = white->clone( mat_base_name+"_mesh_normal" );
00158
00159 pass = base_material->getTechnique(0)->getPass(0);
00160
00161 pass->setPolygonMode( Ogre::PM_WIREFRAME );
00162 pass->setPointSize( 2.0 );
00163 pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
00164 pass->setDepthWriteEnabled(false);
00165 pass->setDepthCheckEnabled(false);
00166 pass->setLightingEnabled(true);
00167 pass->setCullingMode( Ogre::CULL_NONE );
00168 pass->setAmbient(0.0, 0.0, 0.0);
00169 pass->setDiffuse(0.0, 0.0, 0.0, 0.3);
00170 pass->setSpecular(0.0, 0.0, 0.0, 0.3);
00171
00172 pass->setSelfIllumination( 0.9, 0.0, 0.0 );
00173
00174 pass = base_material->clone( mat_base_name+"_point_normal" )->getTechnique(0)->getPass(0);
00175 pass->setPolygonMode( Ogre::PM_POINTS );
00176
00177
00178 pass = base_material->clone( mat_base_name+"_mesh_highlighted" )->getTechnique(0)->getPass(0);
00179 pass->setSelfIllumination( 1.0, 0.4, 0.4 );
00180
00181 pass = base_material->clone( mat_base_name+"_point_highlighted" )->getTechnique(0)->getPass(0);
00182 pass->setSelfIllumination( 1.0, 0.4, 0.4 );
00183 pass->setPolygonMode( Ogre::PM_POINTS );
00184
00185
00186 pass = base_material->clone( mat_base_name+"_mesh_selected" )->getTechnique(0)->getPass(0);
00187 pass->setSelfIllumination( 0.4, 1.0, 0.4 );
00188
00189 pass = base_material->clone( mat_base_name+"_point_selected" )->getTechnique(0)->getPass(0);
00190 pass->setSelfIllumination( 0.4, 1.0, 0.4 );
00191 pass->setPolygonMode( Ogre::PM_POINTS );
00192 }
00193
00194
00195 void ObjectSelectionFrame::setData( pr2_interactive_object_detection::GraspableObjectListConstPtr received_objects )
00196 {
00197 if ( meshes_.size() != 0 )
00198 {
00199 cleanupAndHide();
00200 }
00201
00202 selected_object_ = -1;
00203
00204 const geometry_msgs::Pose &pose = received_objects->reference_to_camera;
00205 scene_root_->setPosition( pose.position.x, pose.position.y, pose.position.z );
00206 scene_root_->setOrientation( pose.orientation.w, pose.orientation.x, pose.orientation.y, pose.orientation.z );
00207
00208 for ( unsigned o=0; o<received_objects->meshes.size(); o++ )
00209 {
00210 if ( received_objects->meshes[o].vertices.size() > 0 )
00211 {
00212 addMesh( o, received_objects->meshes[o], received_objects->graspable_objects[o].potential_models[0].pose.pose );
00213 }
00214 else
00215 {
00216 addCluster( o, received_objects->graspable_objects[o].cluster );
00217 }
00218 }
00219
00220 rviz_interaction_tools::updateCamera( render_panel_->getCamera(), received_objects->camera_info );
00221
00222 image_overlay_->setImage( received_objects->image );
00223 image_overlay_->update();
00224
00225 action_state_ = ACTIVE;
00226 Show();
00227 }
00228
00229
00230 void ObjectSelectionFrame::addCluster( int object_index, const sensor_msgs::PointCloud &cluster )
00231 {
00232 UniqueStringManager usm;
00233
00234 MeshObject *mesh_object = new MeshObject( scene_manager_, scene_root_ );
00235
00236 mesh_object->loadPoints( usm.unique("object_selection_frame_object_"), cluster.points );
00237 mesh_object->setMaterialName(mat_base_name+"_point_normal");
00238
00239 Ogre::Entity* entity = mesh_object->getEntity();
00240 entity->getUserObjectBindings().setUserAny( "object", Ogre::Any(object_index) );
00241 entity->getUserObjectBindings().setUserAny( "type", Ogre::Any(std::string("point")) );
00242 entity->setRenderQueueGroup(Ogre::RENDER_QUEUE_MAIN );
00243
00244 meshes_[object_index] = boost::shared_ptr<rviz_interaction_tools::MeshObject>( mesh_object );
00245 }
00246
00247
00248 void ObjectSelectionFrame::addMesh( int object_index, const geometric_shapes_msgs::Shape &mesh,
00249 const geometry_msgs::Pose &pose )
00250 {
00251 UniqueStringManager usm;
00252
00253 MeshObject *mesh_object = new MeshObject( scene_manager_, scene_root_ );
00254
00255 ROS_INFO_STREAM( "Object " << object_index << " has "
00256 << mesh.triangles.size()/3 << " triangles and "
00257 << mesh.vertices.size() << " vertices." );
00258 ROS_INFO_STREAM( " Position:" << pose.position.x << " "
00259 << pose.position.y << " " << pose.position.z <<
00260 ". Orientation: " << pose.orientation.w << " " << pose.orientation.x
00261 << " " << pose.orientation.y << " " << pose.orientation.z );
00262
00263 mesh_object->loadMesh( usm.unique("object_selection_frame_object_"), mesh );
00264 mesh_object->setPose( pose );
00265 mesh_object->setMaterialName(mat_base_name+"_mesh_normal");
00266
00267 Ogre::Entity* entity = mesh_object->getEntity();
00268 entity->getUserObjectBindings().setUserAny( "object", Ogre::Any(object_index) );
00269 entity->getUserObjectBindings().setUserAny( "type", Ogre::Any(std::string("mesh")) );
00270 entity->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY);
00271
00272 meshes_[object_index] = boost::shared_ptr<rviz_interaction_tools::MeshObject>( mesh_object );
00273 }
00274
00275
00276 void ObjectSelectionFrame::update()
00277 {
00278 render_panel_->getRenderWindow()->update();
00279 }
00280
00281
00282 void ObjectSelectionFrame::onRenderWindowMouseEvents( wxMouseEvent& event )
00283 {
00284 int x = event.GetX();
00285 int y = event.GetY();
00286
00287
00288 wxSize size = render_panel_->GetSize();
00289 x = floor(x * image_overlay_->getWidth() / size.GetWidth() );
00290 y = floor(y * image_overlay_->getHeight() / size.GetHeight() );
00291
00292
00293 Ogre::Ray mouse_ray = render_panel_->getCamera()->getCameraToViewportRay(x/float(size.GetWidth()), y/float(size.GetHeight()));
00294 ray_scene_query_->setRay( mouse_ray );
00295
00296
00297 ray_scene_query_->setSortByDistance(true);
00298 Ogre::RaySceneQueryResult& result = ray_scene_query_->execute();
00299 Ogre::RaySceneQueryResult::iterator iter = result.begin();
00300
00301 unsigned highlighted_object = -1;
00302
00303
00304 while( iter != result.end() )
00305 {
00306 Ogre::MovableObject* movable_object = iter->movable;
00307 if ( movable_object )
00308 {
00309 Ogre::Any model_any = movable_object->getUserObjectBindings().getUserAny( "object" );
00310 if ( !model_any.isEmpty() )
00311 {
00312 highlighted_object = Ogre::any_cast<int>(model_any);
00313 if ( event.ButtonDown(wxMOUSE_BTN_LEFT) )
00314 {
00315 selected_object_ = highlighted_object;
00316 }
00317 break;
00318 }
00319 }
00320 iter++;
00321 }
00322
00323 std::map< unsigned, boost::shared_ptr<rviz_interaction_tools::MeshObject> >::iterator mesh_it = meshes_.begin();
00324 while( mesh_it != meshes_.end() )
00325 {
00326 Ogre::Any type_any = mesh_it->second->getEntity()->getUserObjectBindings().getUserAny( "type" );
00327 std::string type;
00328 if ( !type_any.isEmpty() )
00329 {
00330 type = Ogre::any_cast<std::string>(type_any);
00331 }
00332 else
00333 {
00334 ROS_ERROR( "Couldn't determine type of mesh object." );
00335 }
00336
00337 if ( mesh_it->first == selected_object_ )
00338 {
00339 mesh_it->second->setMaterialName(mat_base_name + "_" + type + "_selected");
00340 }
00341 else if ( mesh_it->first == highlighted_object )
00342 {
00343 mesh_it->second->setMaterialName(mat_base_name + "_" + type + "_highlighted");
00344 }
00345 else
00346 {
00347 mesh_it->second->setMaterialName(mat_base_name + "_" + type + "_normal");
00348 }
00349 mesh_it++;
00350 }
00351 }
00352
00353
00354 void ObjectSelectionFrame::acceptButtonClicked( wxCommandEvent& )
00355 {
00356 action_state_ = ACCEPTED;
00357 cleanupAndHide();
00358 }
00359
00360
00361 void ObjectSelectionFrame::cancelButtonClicked( wxCommandEvent& )
00362 {
00363 action_state_ = CANCELED;
00364 cleanupAndHide();
00365 }
00366
00367 }