ViewerWidget.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OctoMap - An Efficient Probabilistic 3D Mapping
3  * Framework Based on Octrees
4  * http://octomap.github.io
5  *
6  * Copyright (c) 2009-2014, K.M. Wurm and A. Hornung, University of Freiburg
7  * All rights reserved. License for the viewer octovis: GNU GPL v2
8  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
9  *
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  * for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see http://www.gnu.org/licenses/.
23  */
24 
25 #include <octovis/ViewerWidget.h>
26 #include <manipulatedCameraFrame.h>
27 
28 #ifndef M_PI_2
29 #define M_PI_2 1.5707963267948966192E0
30 #endif
31 
32 using namespace std;
33 
34 namespace octomap {
35 
36 ViewerWidget::ViewerWidget(QWidget* parent) :
37  QGLViewer(parent), m_zMin(0.0),m_zMax(1.0) {
38 
39  m_printoutMode = false;
40  m_heightColorMode = false;
41  m_semantic_coloring = false;
42  m_drawSelectionBox = false;
43 }
44 
46 
47  // setHandlerKeyboardModifiers(QGLViewer::CAMERA, Qt::AltModifier);
48  // setHandlerKeyboardModifiers(QGLViewer::FRAME, Qt::NoModifier);
49  // setHandlerKeyboardModifiers(QGLViewer::CAMERA, Qt::ControlModifier);
50  setMouseTracking(true);
51 
52  // Restore previous viewer state.
54 
55  // Make camera the default manipulated frame.
56  setManipulatedFrame( camera()->frame() );
57  // invert mousewheel (more like Blender)
58  camera()->frame()->setWheelSensitivity(-1.0);
59 
60 
61  // Light initialization:
62  glEnable(GL_LIGHT0);
63 
64  float pos[4] = {-1.0, 1.0, 1.0, 0.0};
65  // Directional light
66  glLightfv(GL_LIGHT0, GL_POSITION, pos);
67 
68  // background color defaults to white
69  this->setBackgroundColor( QColor(255,255,255) );
70  this->qglClearColor( this->backgroundColor() );
71 }
72 
74  this->camera()->setOrientation((float) -M_PI_2, (float) M_PI_2);
75  this->showEntireScene();
76  updateGL();
77 }
78 
79 
80 QString ViewerWidget::helpString() const{
81  QString help = "<h2>Octomap 3D viewer</h2>";
82 
83  help +="The Octomap library implements a 3D occupancy grid mapping approach. "
84  "It provides data structures and mapping algorithms. The map is implemented "
85  "using an octree. 3D maps can be viewed an built using this 3D viewer."
86  "<br/><br/>"
87  "Octomap is available at http://octomap.github.com, and is actively "
88  "maintained by Kai M. Wurm and Armin Hornung. This 3D viewer is based on "
89  "libQGLViewer, available at http://www.libqglviewer.com/."
90  "<br/><br/>"
91  "Please refer to the \"Keyboard\" and \"Mouse\" tabs for instructions on "
92  "how to use the viewer.";
93  return help;
94 }
95 
97  m_heightColorMode = enabled;
98  for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) {
99  (*it)->enableHeightColorMode(enabled);
100  }
101  updateGL();
102 }
103 
105  m_printoutMode = enabled;
106  for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) {
107  (*it)->enablePrintoutMode(enabled);
108  }
109  updateGL();
110 }
111 
113  m_semantic_coloring = enabled;
114  for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) {
115  (*it)->enableSemanticColoring(enabled);
116  }
117  updateGL();
118 }
119 
121  m_drawSelectionBox = enabled;
122  updateGL();
123 }
124 
125 
127  // copying octomap::Quaternion parameters to qglviewer::Quaternion does not work (reason unknown)
128  // octomath::Quaternion quaternion = pose.rot().normalized();
129  // return qglviewer::Quaternion(quaternion.x(), quaternion.y(), quaternion.z(), quaternion.u());
130 
131  // Compute viewing direction and use code from libqglviewer's "look at" function
132  octomath::Vector3 dir = pose.rot().rotate(octomath::Vector3(1.,0.,0.));
133  qglviewer::Vec direction(dir.x(), dir.y(), dir.z());
134  //qglviewer::Vec xAxis = direction ^ camera()->upVector();
135  // useing 0, 0, 1 as upvector instead:
136  qglviewer::Vec xAxis = direction ^ qglviewer::Vec(0.0, 0.0, 1.0);
137 
139  q.setFromRotatedBasis(xAxis, xAxis^direction, -direction);
140  return q;
141 }
142 
143 void ViewerWidget::setCamPosition(double x, double y, double z, double lookX, double lookY, double lookZ){
144  this->camera()->setOrientation(-M_PI/2., M_PI/2.);
145  camera()->setPosition(qglviewer::Vec(x, y, z));
146  camera()->lookAt(qglviewer::Vec(lookX, lookY, lookZ));
147  camera()->setUpVector(qglviewer::Vec(0.0, 0.0, 1.0));
148  updateGL();
149 }
150 
153  setCamPosition(pose.x(), pose.y(), pose.z(), ahead.x(), ahead.y(), ahead.z());
154 }
155 
156 void ViewerWidget::jumpToCamFrame(int id, int frame) {
158  if(kfi && frame >= 0 && frame < kfi->numberOfKeyFrames()) {
159  camera()->setPosition(kfi->keyFrame(frame).position());
160  camera()->setOrientation(kfi->keyFrame(frame).orientation());
161  } else {
162  std::cerr << "Error: Could not jump to frame " << frame << " of " << kfi->numberOfKeyFrames() << std::endl;
163  }
164  updateGL();
165 }
166 
168  if(camera()->keyFrameInterpolator(id)) {
169  disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(updateGL()));
170  disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
171  disconnect(camera()->keyFrameInterpolator(id), SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
172  camera()->deletePath(id);
173  }
174 }
175 
177  qglviewer::Vec position(pose.trans().x(), pose.trans().y(), pose.trans().z());
178  qglviewer::Quaternion quaternion = poseToQGLQuaternion(pose);
179  qglviewer::Frame frame(position, quaternion);
180  if(!camera()->keyFrameInterpolator(id)) {
182  }
183  camera()->keyFrameInterpolator(id)->addKeyFrame(frame);
184 }
185 
186 void ViewerWidget::removeFromCameraPath(int id, int frame) {
188  if(old_kfi) {
190  for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) {
191  if(i != frame) {
192  new_kfi->addKeyFrame(old_kfi->keyFrame(i));
193  }
194  }
195  deleteCameraPath(id);
196  camera()->setKeyFrameInterpolator(id, new_kfi);
197  }
198 }
199 
200 void ViewerWidget::updateCameraPath(int id, int frame) {
202  if(old_kfi) {
204  for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) {
205  if(i != frame) {
206  new_kfi->addKeyFrame(old_kfi->keyFrame(i));
207  } else {
208  new_kfi->addKeyFrame(*(camera()->frame()));
209  }
210  }
211  deleteCameraPath(id);
212  camera()->setKeyFrameInterpolator(id, new_kfi);
213  }
214 }
215 
217  int frame = 0;
218  if(camera()->keyFrameInterpolator(id)) frame = camera()->keyFrameInterpolator(id)->numberOfKeyFrames();
219  addCurrentToCameraPath(id, frame);
220 }
221 
222 void ViewerWidget::addCurrentToCameraPath(int id, int frame) {
224  if(!old_kfi || frame >= old_kfi->numberOfKeyFrames()) {
225  camera()->addKeyFrameToPath(id);
226  } else {
228  for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) {
229  new_kfi->addKeyFrame(old_kfi->keyFrame(i));
230  if(i == frame) {
231  new_kfi->addKeyFrame(camera()->frame());
232  }
233  }
234  deleteCameraPath(id);
235  camera()->setKeyFrameInterpolator(id, new_kfi);
236  }
237 }
238 
239 void ViewerWidget::playCameraPath(int id, int start_frame) {
241  if(kfi && !kfi->interpolationIsStarted() && start_frame >= 0 && start_frame < kfi->numberOfKeyFrames()) {
243  m_current_camera_frame = start_frame;
244  kfi->setInterpolationTime(kfi->keyFrameTime(start_frame));
245  std::cout << "Playing path of length " << kfi->numberOfKeyFrames() << ", start time " << kfi->keyFrameTime(start_frame) << std::endl;
246  connect(kfi, SIGNAL(interpolated()), this, SLOT(updateGL()));
247  connect(kfi, SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
248  connect(kfi, SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
249  kfi->startInterpolation();
250  }
251 }
252 
254  if(camera()->keyFrameInterpolator(id) && camera()->keyFrameInterpolator(id)->interpolationIsStarted()) {
255  disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(updateGL()));
256  disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
257  disconnect(camera()->keyFrameInterpolator(id), SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
259  }
260 }
261 
263  if(camera()->keyFrameInterpolator(m_current_camera_path)) {
264  disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(interpolated()), this, SLOT(updateGL()));
265  disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
266  disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
268  }
269 }
270 
273  if(kfi) {
274  int current_frame = m_current_camera_frame;
275  for(int i = m_current_camera_frame + 1; i < kfi->numberOfKeyFrames(); i++) {
276  if(kfi->keyFrameTime(current_frame) <= kfi->interpolationTime()) current_frame = i;
277  else break;
278  }
279  if(current_frame != m_current_camera_frame) {
280  m_current_camera_frame = current_frame;
282  }
283  }
284 }
285 
287  m_zMin = min[2];
288  m_zMax = max[2];
290 }
291 
293  assert (obj);
294  m_sceneObjects.push_back(obj);
295  updateGL();
296 }
297 
299  assert(obj);
300  for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin();
301  it != m_sceneObjects.end();){
302  if (*it == obj)
303  it = m_sceneObjects.erase(it);
304  else
305  ++it;
306  }
307  updateGL();
308 }
309 
311  // clear drawable objects:
312  for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++){
313  (*it)->clear();
314  }
315 }
316 
318 
319  // debugging: draw light in scene
320  //drawLight(GL_LIGHT0);
321 
322  glEnable(GL_BLEND);
323  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
324 
325  glEnable(GL_LIGHTING);
326  if (m_printoutMode){
327  glCullFace(GL_BACK);
328  }
329 
330  // draw drawable objects:
331  for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin();
332  it != m_sceneObjects.end(); ++it){
333  (*it)->draw();
334  }
335 
336  if (m_drawSelectionBox){
338 
339  if (m_selectionBox.getGrabbedFrame() >= 0){
340  setMouseBinding(Qt::LeftButton, FRAME, TRANSLATE);
341  } else {
342  setMouseBinding(Qt::LeftButton, FRAME, ROTATE);
343  }
344 
345  }
346 
347 }
348 
350 
351  if (m_drawSelectionBox)
352  m_selectionBox.draw(true);
353 }
354 
356 
357  // Reset model view matrix to world coordinates origin
358  glMatrixMode(GL_MODELVIEW);
359  glPushMatrix();
361  // TODO restore model loadProjectionMatrixStereo
362 
363  // Save OpenGL state
364  glPushAttrib(GL_ALL_ATTRIB_BITS);
365 
366  glDisable(GL_COLOR_MATERIAL);
367  qglColor(foregroundColor());
368 
369  if (gridIsDrawn()){
370  glLineWidth(1.0);
371  drawGrid(5.0, 10);
372  }
373  if (axisIsDrawn()){
374  glLineWidth(2.0);
375  drawAxis(1.0);
376  }
377 
378  // Restore GL state
379  glPopAttrib();
380  glPopMatrix();
381 
384  setAxisIsDrawn(false);
385  setGridIsDrawn(false);
387 
390 }
391 
392 void ViewerWidget::postSelection(const QPoint&)
393 {
394 
395 }
396 
397 
398 
399 } // namespace
400 
std::vector< SceneObject * > m_sceneObjects
Definition: ViewerWidget.h:104
virtual void postDraw()
Definition: qglviewer.cpp:339
virtual void postSelection(const QPoint &)
int getGrabbedFrame() const
void setKeyFrameInterpolator(unsigned int i, KeyFrameInterpolator *const kfi)
Definition: camera.cpp:1690
virtual QString helpString() const
A keyFrame Catmull-Rom Frame interpolator.
Vec position() const
Definition: frame.cpp:537
static void drawGrid(qreal size=1.0, int nbSubdivisions=10)
Definition: qglviewer.cpp:3323
void addCurrentToCameraPath(int id, int frame)
virtual void setSceneBoundingBox(const qglviewer::Vec &min, const qglviewer::Vec &max)
bool axisIsDrawn() const
Definition: qglviewer.h:97
void enablePrintoutMode(bool enabled=true)
void updateCameraPath(int id, int frame)
void setOrientation(const Quaternion &q)
Definition: camera.cpp:1150
void enableSelectionBox(bool enabled=true)
virtual void drawWithNames()
void addKeyFrame(const Frame &frame)
void appendToCameraPath(int id, const octomath::Pose6D &pose)
void lookAt(const Vec &target)
Definition: camera.cpp:1008
void removeSceneObject(SceneObject *obj)
void draw(bool withNames=false)
void jumpToCamFrame(int id, int frame)
QColor foregroundColor() const
Definition: qglviewer.h:182
void appendCurrentToCameraPath(int id)
virtual void deletePath(unsigned int i)
Definition: camera.cpp:1762
void setWheelSensitivity(qreal sensitivity)
void setMouseBinding(unsigned int state, MouseHandler handler, MouseAction action, bool withConstraint=true)
Definition: qglviewer.cpp:2512
bool gridIsDrawn() const
Definition: qglviewer.h:101
virtual void loadModelViewMatrix(bool reset=true) const
Definition: camera.cpp:500
void setPosition(const Vec &pos)
Definition: camera.cpp:1240
#define M_PI
SelectionBox m_selectionBox
Definition: ViewerWidget.h:105
#define M_PI_2
virtual void init()
void enableHeightColorMode(bool enabled=true)
void setCamPosition(double x, double y, double z, double lookX, double lookY, double lookZ)
void setGridIsDrawn(bool draw=true)
Definition: qglviewer.h:130
void cameraPathFrameChanged(int id, int current_camera_frame)
void enableSemanticColoring(bool enabled=true)
static void drawAxis(qreal length=1.0)
Definition: qglviewer.cpp:3255
void deleteCameraPath(int id)
Vector3 rotate(const Vector3 &v) const
ManipulatedCameraFrame * frame() const
Definition: camera.h:334
The Vec class represents 3D positions and 3D vectors.
Definition: vec.h:65
void cameraPathStopped(int id)
void setManipulatedFrame(qglviewer::ManipulatedFrame *frame)
Definition: qglviewer.cpp:3057
QColor backgroundColor() const
Definition: qglviewer.h:168
void addSceneObject(SceneObject *obj)
void setAxisIsDrawn(bool draw=true)
Definition: qglviewer.h:128
void playCameraPath(int id, int start_frame)
KeyFrameInterpolator * keyFrameInterpolator(unsigned int i) const
Definition: camera.cpp:1664
A versatile 3D OpenGL viewer based on QGLWidget.
Definition: qglviewer.h:62
void stopCameraPath(int id)
Vector3 & trans()
virtual void postDraw()
void setBackgroundColor(const QColor &color)
Definition: qglviewer.h:186
void setCamPose(const octomath::Pose6D &pose)
The Quaternion class represents 3D rotations and orientations.
Definition: quaternion.h:66
void setFromRotatedBasis(const Vec &X, const Vec &Y, const Vec &Z)
Definition: quaternion.cpp:182
Quaternion orientation() const
Definition: frame.cpp:546
virtual void addKeyFrameToPath(unsigned int i)
Definition: camera.cpp:1710
virtual bool restoreStateFromFile()
Definition: qglviewer.cpp:3460
qglviewer::Quaternion poseToQGLQuaternion(const octomath::Pose6D &pose)
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition: frame.h:121
Quaternion & rot()
void setUpVector(const Vec &up, bool noMove=true)
Definition: camera.cpp:1116
void setSceneBoundingBox(const qglviewer::Vec &min, const qglviewer::Vec &max)
Definition: qglviewer.h:237
virtual void help()
Definition: qglviewer.cpp:1975
void showEntireScene()
Definition: qglviewer.h:242
void removeFromCameraPath(int id, int frame)
qglviewer::Camera * camera() const
Definition: qglviewer.h:250


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Wed Jun 5 2019 19:26:39