view_controller.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QColor>
31 #include <QFont>
32 #include <QKeyEvent>
33 
34 #include <OgreCamera.h>
35 #include <OgreSceneManager.h>
36 #include <OgreSceneNode.h>
37 
38 #include <rviz/display_context.h>
39 #include <rviz/frame_manager.h>
40 #include <rviz/load_resource.h>
44 #include <rviz/render_panel.h>
46 #include <rviz/view_manager.h>
50 
51 #include <rviz/view_controller.h>
52 
53 namespace rviz
54 {
56  : context_(nullptr), camera_(nullptr), is_active_(false), type_property_(nullptr)
57 {
59  new FloatProperty("Near Clip Distance", 0.01f,
60  "Anything closer to the camera than this threshold will not get rendered.", this,
64 
65  stereo_enable_ = new BoolProperty("Enable Stereo Rendering", true,
66  "Render the main view in stereo if supported."
67  " On Linux this requires a recent version of Ogre and"
68  " an NVIDIA Quadro card with 3DVision glasses.",
70  stereo_eye_swap_ = new BoolProperty("Swap Stereo Eyes", false,
71  "Swap eyes if the monitor shows the left eye on the right.",
74  new FloatProperty("Stereo Eye Separation", 0.06f, "Distance between eyes for stereo rendering.",
77  new FloatProperty("Stereo Focal Distance", 1.0f,
78  "Distance from eyes to screen. For stereo rendering.", stereo_enable_,
80  invert_z_ =
81  new BoolProperty("Invert Z Axis", false, "Invert camera's Z axis for Z-down environments/models.",
83 }
84 
86 {
87  context_ = context;
88 
89  std::stringstream ss;
90  static int count = 0;
91  ss << "ViewControllerCamera" << count++;
92  camera_ = context_->getSceneManager()->createCamera(ss.str());
93  context_->getSceneManager()->getRootSceneNode()->attachObject(camera_);
94 
96  setReadOnly(true);
97 
98  // Do subclass initialization.
99  onInitialize();
100 
102 
104  standard_cursors_[Rotate2D] = makeIconCursor("package://rviz/icons/rotate.svg");
105  standard_cursors_[Rotate3D] = makeIconCursor("package://rviz/icons/rotate_cam.svg");
106  standard_cursors_[MoveXY] = makeIconCursor("package://rviz/icons/move2d.svg");
107  standard_cursors_[MoveZ] = makeIconCursor("package://rviz/icons/move_z.svg");
108  standard_cursors_[Zoom] = makeIconCursor("package://rviz/icons/zoom.svg");
109  standard_cursors_[Crosshair] = makeIconCursor("package://rviz/icons/crosshair.svg");
110 
113 
114  if (!RenderSystem::get()->isStereoSupported())
115  {
116  stereo_enable_->setBool(false);
117  stereo_enable_->hide();
118  }
119 }
120 
122 {
123  context_->getSceneManager()->destroyCamera(camera_);
124 }
125 
126 QString ViewController::formatClassId(const QString& class_id)
127 {
128  QStringList id_parts = class_id.split("/");
129  if (id_parts.size() != 2)
130  {
131  // Should never happen with pluginlib class ids, which are
132  // formatted like "package_name/class_name". Not worth crashing
133  // over though.
134  return class_id;
135  }
136  else
137  {
138  return id_parts[1] + " (" + id_parts[0] + ")";
139  }
140 }
141 
142 QVariant ViewController::getViewData(int column, int role) const
143 {
144  if (role == Qt::ForegroundRole)
145  return QVariant();
146 
147  if (is_active_)
148  {
149  switch (role)
150  {
151  case Qt::FontRole:
152  {
153  QFont font;
154  font.setBold(true);
155  return font;
156  }
157  }
158  }
159  return Property::getViewData(column, role);
160 }
161 
162 Qt::ItemFlags ViewController::getViewFlags(int column) const
163 {
164  if (is_active_)
165  {
166  return Property::getViewFlags(column);
167  }
168  else
169  {
170  return Property::getViewFlags(column) | Qt::ItemIsDragEnabled;
171  }
172 }
173 
175 {
176  is_active_ = true;
177  onActivate();
178 }
179 
181 {
182  Q_EMIT configChanged();
183 }
184 
185 void ViewController::load(const Config& config)
186 {
187  // Load the name by hand.
188  QString name;
189  if (config.mapGetString("Name", &name))
190  {
191  setName(name);
192  }
193  // Load all sub-properties the same way the base class does.
195 }
196 
197 void ViewController::save(Config config) const
198 {
199  config.mapSetValue("Class", getClassId());
200  config.mapSetValue("Name", getName());
201 
203 }
204 
205 void ViewController::handleKeyEvent(QKeyEvent* event, RenderPanel* panel)
206 {
207  if (event->key() == Qt::Key_F && panel->getViewport() && context_->getSelectionManager())
208  {
209  QPoint mouse_rel_panel = panel->mapFromGlobal(QCursor::pos());
210  Ogre::Vector3 point_rel_world; // output of get3DPoint().
211  if (context_->getSelectionManager()->get3DPoint(panel->getViewport(), mouse_rel_panel.x(),
212  mouse_rel_panel.y(), point_rel_world))
213  {
214  lookAt(point_rel_world);
215  }
216  }
217 
218  if (event->key() == Qt::Key_Z)
219  {
220  reset();
221  }
222 }
223 
225 {
226  cursor_ = standard_cursors_[cursor_type];
227 }
228 
229 void ViewController::lookAt(float x, float y, float z)
230 {
231  Ogre::Vector3 point(x, y, z);
232  lookAt(point);
233 }
234 
235 void ViewController::setStatus(const QString& message)
236 {
237  if (context_)
238  {
239  context_->setStatus(message);
240  }
241 }
242 
244 {
245  float n = near_clip_property_->getFloat();
246  camera_->setNearClipDistance(n);
247 }
248 
250 {
251  if (stereo_enable_->getBool())
252  {
253  float focal_dist = stereo_focal_distance_->getFloat();
254  float eye_sep = stereo_eye_swap_->getBool() ? -stereo_eye_separation_->getFloat() :
256  camera_->setFrustumOffset(0.5f * eye_sep, 0.0f);
257  camera_->setFocalLength(focal_dist);
261  }
262  else
263  {
264  camera_->setFrustumOffset(0.0f, 0.0f);
265  camera_->setFocalLength(1.0f);
269  }
270 }
271 
273 {
274  // We don't seem to need to do anything here.
275 }
276 
277 } // end namespace rviz
rviz::ViewController::is_active_
bool is_active_
Definition: view_controller.h:228
rviz::BoolProperty::getBool
virtual bool getBool() const
Definition: bool_property.cpp:48
rviz::ViewController::load
void load(const Config &config) override
Load the value of this property and/or its children from the given Config reference.
Definition: view_controller.cpp:185
render_system.h
rviz::getDefaultCursor
QCursor getDefaultCursor(bool)
Definition: load_resource.cpp:96
view_manager.h
rviz::ViewController::onInitialize
virtual void onInitialize()
Do subclass-specific initialization. Called by ViewController::initialize after context_ and camera_ ...
Definition: view_controller.h:194
window_manager_interface.h
frame_manager.h
rviz::Property::setName
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:155
rviz::RenderSystem::get
static RenderSystem * get()
Definition: render_system.cpp:76
rviz::ViewController::onActivate
virtual void onActivate()
called by activate().
Definition: view_controller.h:202
rviz::ViewController::Crosshair
@ Crosshair
Definition: view_controller.h:215
rviz::ViewController::near_clip_property_
FloatProperty * near_clip_property_
Definition: view_controller.h:235
rviz::Property::getViewData
virtual QVariant getViewData(int column, int role) const
Return data appropriate for the given column (0 or 1) and role for this Property.
Definition: property.cpp:241
rviz::Property::show
void show()
Show this Property in any PropertyTreeWidgets.
Definition: property.h:471
rviz::BoolProperty::setBool
bool setBool(bool value)
Definition: bool_property.h:84
rviz::BoolProperty
Property specialized to provide getter for booleans.
Definition: bool_property.h:38
rviz::FloatProperty::setMax
void setMax(float max)
Definition: float_property.cpp:57
viewport_mouse_event.h
float_property.h
rviz::ViewController::Rotate2D
@ Rotate2D
Definition: view_controller.h:210
selection_manager.h
bool_property.h
rviz::makeIconCursor
QCursor makeIconCursor(const QString &url, bool fill_cache)
Definition: load_resource.cpp:101
rviz::FloatProperty::setMin
void setMin(float min)
Definition: float_property.cpp:51
rviz::DisplayContext::getSceneManager
virtual Ogre::SceneManager * getSceneManager() const =0
Returns the Ogre::SceneManager used for the main RenderPanel.
rviz::ViewController::CursorType
CursorType
Definition: view_controller.h:207
f
f
rviz::ViewController::context_
DisplayContext * context_
Definition: view_controller.h:225
rviz::ViewController::stereo_enable_
BoolProperty * stereo_enable_
Definition: view_controller.h:236
rviz::ViewController::handleKeyEvent
virtual void handleKeyEvent(QKeyEvent *event, RenderPanel *panel)
Called by MoveTool and InteractionTool when keyboard events are passed to them.
Definition: view_controller.cpp:205
rviz::SelectionManager::get3DPoint
bool get3DPoint(Ogre::Viewport *viewport, const int x, const int y, Ogre::Vector3 &result_point)
Definition: selection_manager.cpp:179
rviz::FloatProperty
Property specialized to enforce floating point max/min.
Definition: float_property.h:37
rviz::Property::hide
void hide()
Hide this Property in any PropertyTreeWidgets.
Definition: property.h:462
rviz::ViewController::updateStereoProperties
void updateStereoProperties()
Definition: view_controller.cpp:249
rviz::ViewController::activate
void activate()
Called by RenderPanel when this view controller is about to be used.
Definition: view_controller.cpp:174
rviz::FloatProperty::getFloat
virtual float getFloat() const
Definition: float_property.h:79
rviz::ViewController::reset
virtual void reset()=0
rviz::DisplayContext::setStatus
virtual void setStatus(const QString &message)=0
rviz::Property::setValue
virtual bool setValue(const QVariant &new_value)
Set the new value for this property. Returns true if the new value is different from the old value,...
Definition: property.cpp:134
rviz
Definition: add_display_dialog.cpp:54
rviz::ViewController::standard_cursors_
QMap< CursorType, QCursor > standard_cursors_
Definition: view_controller.h:249
rviz::ViewController::initialize
void initialize(DisplayContext *context)
Do all setup that can't be done in the constructor.
Definition: view_controller.cpp:85
rviz::ViewController::updateInvertZAxis
void updateInvertZAxis()
Definition: view_controller.cpp:272
rviz::DisplayContext
Pure-virtual base class for objects which give Display subclasses context in which to work.
Definition: display_context.h:81
rviz::ViewController::ViewController
ViewController()
Definition: view_controller.cpp:55
rviz::ViewController::configChanged
void configChanged()
rviz::ViewController::invert_z_
BoolProperty * invert_z_
Definition: view_controller.h:240
render_panel.h
rviz::Property::getViewFlags
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:290
rviz::Property::getName
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:164
rviz::ViewController::getViewData
QVariant getViewData(int column, int role) const override
Overridden from Property to give a different background color and bold font if this view is active.
Definition: view_controller.cpp:142
rviz::ViewController::save
void save(Config config) const override
Write the value of this property and/or its children into the given Config reference.
Definition: view_controller.cpp:197
rviz::Property::setReadOnly
virtual void setReadOnly(bool read_only)
Prevent or allow users to edit this property from a PropertyTreeWidget.
Definition: property.h:497
rviz::ViewController::camera_
Ogre::Camera * camera_
Definition: view_controller.h:226
rviz::ViewController::emitConfigChanged
void emitConfigChanged()
Subclasses should call this whenever a change is made which would change the results of toString().
Definition: view_controller.cpp:180
rviz::ViewController::getViewFlags
Qt::ItemFlags getViewFlags(int column) const override
Overridden from Property to make this draggable if it is not active.
Definition: view_controller.cpp:162
rviz::ViewController::~ViewController
~ViewController() override
Definition: view_controller.cpp:121
rviz::ViewController::stereo_focal_distance_
FloatProperty * stereo_focal_distance_
Definition: view_controller.h:239
rviz::ViewController::Zoom
@ Zoom
Definition: view_controller.h:214
rviz::ViewController::Default
@ Default
Definition: view_controller.h:209
rviz::ViewController::formatClassId
static QString formatClassId(const QString &class_id)
Definition: view_controller.cpp:126
load_resource.h
view_controller.h
rviz::Property::save
virtual void save(Config config) const
Write the value of this property and/or its children into the given Config reference.
Definition: property.cpp:492
rviz::DisplayContext::getSelectionManager
virtual SelectionManager * getSelectionManager() const =0
Return a pointer to the SelectionManager.
display_context.h
rviz::RenderPanel
Definition: render_panel.h:74
rviz::ViewController::setCursor
void setCursor(CursorType cursor_type)
Definition: view_controller.cpp:224
rviz::ViewController::lookAt
void lookAt(float x, float y, float z)
Convenience function which calls lookAt(Ogre::Vector3).
Definition: view_controller.cpp:229
rviz::ViewController::MoveXY
@ MoveXY
Definition: view_controller.h:212
rviz::ViewController::stereo_eye_separation_
FloatProperty * stereo_eye_separation_
Definition: view_controller.h:238
rviz::ViewController::Rotate3D
@ Rotate3D
Definition: view_controller.h:211
rviz::ViewController::getClassId
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
Definition: view_controller.h:155
rviz::QtOgreRenderWindow::getViewport
Ogre::Viewport * getViewport() const
Definition: qt_ogre_render_window.cpp:214
rviz::ViewController::setStatus
void setStatus(const QString &message)
Definition: view_controller.cpp:235
config
config
rviz::Property::load
virtual void load(const Config &config)
Load the value of this property and/or its children from the given Config reference.
Definition: property.cpp:442
rviz::ViewController::stereo_eye_swap_
BoolProperty * stereo_eye_swap_
Definition: view_controller.h:237
rviz::Config
Configuration data storage class.
Definition: config.h:124
rviz::ViewController::cursor_
QCursor cursor_
Definition: view_controller.h:233
rviz::ViewController::MoveZ
@ MoveZ
Definition: view_controller.h:213
enum_property.h
rviz::ViewController::updateNearClipDistance
void updateNearClipDistance()
Definition: view_controller.cpp:243


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10