view_controller.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <QColor>
00031 #include <QFont>
00032 #include <QKeyEvent>
00033 
00034 #include <OgreCamera.h>
00035 #include <OgreSceneManager.h>
00036 #include <OgreSceneNode.h>
00037 
00038 #include "rviz/display_context.h"
00039 #include "rviz/frame_manager.h"
00040 #include "rviz/load_resource.h"
00041 #include "rviz/properties/enum_property.h"
00042 #include "rviz/properties/float_property.h"
00043 #include "rviz/properties/bool_property.h"
00044 #include "rviz/render_panel.h"
00045 #include "rviz/selection/selection_manager.h"
00046 #include "rviz/view_manager.h"
00047 #include "rviz/viewport_mouse_event.h"
00048 #include "rviz/window_manager_interface.h"
00049 #include "rviz/ogre_helpers/render_system.h"
00050 
00051 #include "rviz/view_controller.h"
00052 
00053 namespace rviz
00054 {
00055 
00056 ViewController::ViewController()
00057   : context_( NULL )
00058   , camera_( NULL )
00059   , is_active_( false )
00060   , type_property_( NULL )
00061 {
00062   near_clip_property_ = new FloatProperty( "Near Clip Distance", 0.01f,
00063                                       "Anything closer to the camera than this threshold will not get rendered.",
00064                                       this, SLOT( updateNearClipDistance() ) );
00065   near_clip_property_->setMin( 0.001 );
00066   near_clip_property_->setMax( 10000 );
00067 
00068   stereo_enable_ = new BoolProperty( "Enable Stereo Rendering", true,
00069                                       "Render the main view in stereo if supported."
00070                                       "  On Linux this requires a recent version of Ogre and"
00071                                       " an NVIDIA Quadro card with 3DVision glasses.",
00072                                       this, SLOT( updateStereoProperties() ) );
00073   stereo_eye_swap_ = new BoolProperty( "Swap Stereo Eyes", false,
00074                                       "Swap eyes if the monitor shows the left eye on the right.",
00075                                       stereo_enable_, SLOT( updateStereoProperties() ), this );
00076   stereo_eye_separation_ = new FloatProperty( "Stereo Eye Separation", 0.06f,
00077                                       "Distance between eyes for stereo rendering.",
00078                                       stereo_enable_, SLOT( updateStereoProperties() ), this );
00079   stereo_focal_distance_ = new FloatProperty( "Stereo Focal Distance", 1.0f,
00080                                       "Distance from eyes to screen.  For stereo rendering.",
00081                                       stereo_enable_, SLOT( updateStereoProperties() ), this );
00082 }
00083 
00084 void ViewController::initialize( DisplayContext* context )
00085 {
00086   context_ = context;
00087 
00088   std::stringstream ss;
00089   static int count = 0;
00090   ss << "ViewControllerCamera" << count++;
00091   camera_ = context_->getSceneManager()->createCamera( ss.str() );
00092   context_->getSceneManager()->getRootSceneNode()->attachObject( camera_ );
00093 
00094   setValue( formatClassId( getClassId() ));
00095   setReadOnly( true );
00096 
00097   // Do subclass initialization.
00098   onInitialize();
00099 
00100   cursor_ = getDefaultCursor();
00101 
00102   standard_cursors_[Default] = getDefaultCursor();
00103   standard_cursors_[Rotate2D] = makeIconCursor( "package://rviz/icons/rotate.svg" );
00104   standard_cursors_[Rotate3D] = makeIconCursor( "package://rviz/icons/rotate_cam.svg" );
00105   standard_cursors_[MoveXY] = makeIconCursor( "package://rviz/icons/move2d.svg" );
00106   standard_cursors_[MoveZ] = makeIconCursor( "package://rviz/icons/move_z.svg" );
00107   standard_cursors_[Zoom] = makeIconCursor( "package://rviz/icons/zoom.svg" );
00108   standard_cursors_[Crosshair] = makeIconCursor( "package://rviz/icons/crosshair.svg" );
00109 
00110   updateNearClipDistance();
00111   updateStereoProperties();
00112 
00113   if (!RenderSystem::get()->isStereoSupported())
00114   {
00115     stereo_enable_->setBool(false);
00116     stereo_enable_->hide();
00117   }
00118 }
00119 
00120 ViewController::~ViewController()
00121 {
00122   context_->getSceneManager()->destroyCamera( camera_ );
00123 }
00124 
00125 QString ViewController::formatClassId( const QString& class_id )
00126 {
00127   QStringList id_parts = class_id.split( "/" );
00128   if( id_parts.size() != 2 )
00129   {
00130     // Should never happen with pluginlib class ids, which are
00131     // formatted like "package_name/class_name".  Not worth crashing
00132     // over though.
00133     return class_id;
00134   }
00135   else
00136   {
00137     return id_parts[ 1 ] + " (" + id_parts[ 0 ] + ")";
00138   }
00139 }
00140 
00141 QVariant ViewController::getViewData( int column, int role ) const
00142 {
00143   if ( role == Qt::TextColorRole )
00144   {
00145     return QVariant();
00146   }
00147 
00148   if( is_active_ )
00149   {
00150     switch( role )
00151     {
00152     case Qt::BackgroundRole:
00153     {
00154       return QColor( 0xba, 0xad, 0xa4 );
00155     }
00156     case Qt::FontRole:
00157     {
00158       QFont font;
00159       font.setBold( true );
00160       return font;
00161     }
00162     }
00163   }
00164   return Property::getViewData( column, role );
00165 }
00166 
00167 Qt::ItemFlags ViewController::getViewFlags( int column ) const
00168 {
00169   if( is_active_ )
00170   {
00171     return Property::getViewFlags( column );
00172   }
00173   else
00174   {
00175     return Property::getViewFlags( column ) | Qt::ItemIsDragEnabled;
00176   }
00177 }
00178 
00179 void ViewController::activate()
00180 {
00181   is_active_ = true;
00182   onActivate();
00183 }
00184 
00185 void ViewController::emitConfigChanged()
00186 {
00187   Q_EMIT configChanged();
00188 }
00189 
00190 void ViewController::load( const Config& config )
00191 {
00192   // Load the name by hand.
00193   QString name;
00194   if( config.mapGetString( "Name", &name ))
00195   {
00196     setName( name );
00197   }
00198   // Load all sub-properties the same way the base class does.
00199   Property::load( config );
00200 }
00201 
00202 void ViewController::save( Config config ) const
00203 {
00204   config.mapSetValue( "Class", getClassId() );
00205   config.mapSetValue( "Name", getName() );
00206 
00207   Property::save( config );
00208 }
00209 
00210 void ViewController::handleKeyEvent( QKeyEvent* event, RenderPanel* panel )
00211 {
00212   if( event->key() == Qt::Key_F &&
00213       panel->getViewport() &&
00214       context_->getSelectionManager() )
00215   {
00216     QPoint mouse_rel_panel = panel->mapFromGlobal( QCursor::pos() );
00217     Ogre::Vector3 point_rel_world; // output of get3DPoint().
00218     if( context_->getSelectionManager()->get3DPoint( panel->getViewport(),
00219                                                      mouse_rel_panel.x(), mouse_rel_panel.y(),
00220                                                      point_rel_world ))
00221     {
00222       lookAt( point_rel_world );
00223     }
00224   }
00225 
00226   if( event->key() == Qt::Key_Z )
00227   {
00228     reset();
00229   }
00230 }
00231 
00232 void ViewController::setCursor( CursorType cursor_type )
00233 {
00234   cursor_=standard_cursors_[cursor_type];
00235 }
00236 
00237 void ViewController::lookAt( float x, float y, float z )
00238 {
00239   Ogre::Vector3 point( x, y, z );
00240   lookAt( point );
00241 }
00242 
00243 void ViewController::setStatus( const QString & message )
00244 {
00245   if ( context_ )
00246   {
00247     context_->setStatus( message );
00248   }
00249 }
00250 
00251 void ViewController::updateNearClipDistance()
00252 {
00253   float n = near_clip_property_->getFloat();
00254   camera_->setNearClipDistance( n );
00255 }
00256 
00257 void ViewController::updateStereoProperties()
00258 {
00259   if (stereo_enable_->getBool())
00260   {
00261     float focal_dist = stereo_focal_distance_->getFloat();
00262     float eye_sep = stereo_eye_swap_->getBool() ?
00263                     -stereo_eye_separation_->getFloat() :
00264                     stereo_eye_separation_->getFloat();
00265     camera_->setFrustumOffset(0.5f * eye_sep, 0.0f);
00266     camera_->setFocalLength(focal_dist);
00267     stereo_eye_swap_->show();
00268     stereo_eye_separation_->show();
00269     stereo_focal_distance_->show();
00270   }
00271   else
00272   {
00273     camera_->setFrustumOffset(0.0f,0.0f);
00274     camera_->setFocalLength(1.0f);
00275     stereo_eye_swap_->hide();
00276     stereo_eye_separation_->hide();
00277     stereo_focal_distance_->hide();
00278   }
00279 }
00280 
00281 
00282 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28