| 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 | |
| 45 | void ViewerWidget::init() { |
| 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. |
| 53 | restoreStateFromFile(); |
| 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 | } |
| 71 | |
| 72 | void ViewerWidget::resetView(){ |
| 73 | this->camera()->setOrientation((float) -M_PI_2, (float) M_PI_2); |
| 74 | this->showEntireScene(); |
| 75 | update(); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | QString ViewerWidget::helpString() const{ |
| 80 | QString help = "<h2>Octomap 3D viewer</h2>"; |
| 81 | |
| 82 | help +="The Octomap library implements a 3D occupancy grid mapping approach. " |
| 83 | "It provides data structures and mapping algorithms. The map is implemented " |
| 84 | "using an octree. 3D maps can be viewed an built using this 3D viewer." |
| 85 | "<br/><br/>" |
| 86 | "Octomap is available at https://octomap.github.io, and is actively " |
| 87 | "maintained by Kai M. Wurm and Armin Hornung. This 3D viewer is based on " |
| 88 | "libQGLViewer, available at http://www.libqglviewer.com/." |
| 89 | "<br/><br/>" |
| 90 | "Please refer to the \"Keyboard\" and \"Mouse\" tabs for instructions on " |
| 91 | "how to use the viewer."; |
| 92 | return help; |
| 93 | } |
| 94 | |
| 95 | void ViewerWidget::enableHeightColorMode (bool enabled) { |
| 96 | m_heightColorMode = enabled; |
| 97 | for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) { |
| 98 | (*it)->enableHeightColorMode(enabled); |
| 99 | } |
| 100 | update(); |
| 101 | } |
| 102 | |
| 103 | void ViewerWidget::enablePrintoutMode(bool enabled) { |
| 104 | m_printoutMode = enabled; |
| 105 | for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) { |
| 106 | (*it)->enablePrintoutMode(enabled); |
| 107 | } |
| 108 | update(); |
| 109 | } |
| 110 | |
| 111 | void ViewerWidget::enableSemanticColoring (bool enabled) { |
| 112 | m_semantic_coloring = enabled; |
| 113 | for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) { |
| 114 | (*it)->enableSemanticColoring(enabled); |
| 115 | } |
| 116 | update(); |
| 117 | } |
| 118 | |
| 119 | void ViewerWidget::enableSelectionBox(bool enabled) { |
| 120 | m_drawSelectionBox = enabled; |
| 121 | update(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | qglviewer::Quaternion ViewerWidget::poseToQGLQuaternion(const octomath::Pose6D& pose) { |
| 126 | // copying octomap::Quaternion parameters to qglviewer::Quaternion does not work (reason unknown) |
| 127 | // octomath::Quaternion quaternion = pose.rot().normalized(); |
| 128 | // return qglviewer::Quaternion(quaternion.x(), quaternion.y(), quaternion.z(), quaternion.u()); |
| 129 | |
| 130 | // Compute viewing direction and use code from libqglviewer's "look at" function |
| 131 | octomath::Vector3 dir = pose.rot().rotate(octomath::Vector3(1.,0.,0.)); |
| 132 | qglviewer::Vec direction(dir.x(), dir.y(), dir.z()); |
| 133 | //qglviewer::Vec xAxis = direction ^ camera()->upVector(); |
| 134 | // useing 0, 0, 1 as upvector instead: |
| 135 | qglviewer::Vec xAxis = direction ^ qglviewer::Vec(0.0, 0.0, 1.0); |
| 136 | |
| 137 | qglviewer::Quaternion q; |
| 138 | q.setFromRotatedBasis(xAxis, xAxis^direction, -direction); |
| 139 | return q; |
| 140 | } |
| 141 | |
| 142 | void ViewerWidget::setCamPosition(double x, double y, double z, double lookX, double lookY, double lookZ){ |
| 143 | this->camera()->setOrientation(-M_PI/2., M_PI/2.); |
| 144 | camera()->setPosition(qglviewer::Vec(x, y, z)); |
| 145 | camera()->lookAt(qglviewer::Vec(lookX, lookY, lookZ)); |
| 146 | camera()->setUpVector(qglviewer::Vec(0.0, 0.0, 1.0)); |
| 147 | update(); |
| 148 | } |
| 149 | |
| 150 | void ViewerWidget::setCamPose(const octomath::Pose6D& pose){ |
| 151 | octomath::Pose6D ahead = pose * octomath::Pose6D(octomath::Vector3(1,0,0), octomath::Quaternion()); |
| 152 | setCamPosition(pose.x(), pose.y(), pose.z(), ahead.x(), ahead.y(), ahead.z()); |
| 153 | } |
| 154 | |
| 155 | void ViewerWidget::jumpToCamFrame(int id, int frame) { |
| 156 | qglviewer::KeyFrameInterpolator *kfi = camera()->keyFrameInterpolator(id); |
| 157 | if(kfi && frame >= 0 && frame < kfi->numberOfKeyFrames()) { |
| 158 | camera()->setPosition(kfi->keyFrame(frame).position()); |
| 159 | camera()->setOrientation(kfi->keyFrame(frame).orientation()); |
| 160 | } else { |
| 161 | std::cerr << "Error: Could not jump to frame " << frame << " of " << kfi->numberOfKeyFrames() << std::endl; |
| 162 | } |
| 163 | update(); |
| 164 | } |
| 165 | |
| 166 | void ViewerWidget::deleteCameraPath(int id) { |
| 167 | if(camera()->keyFrameInterpolator(id)) { |
| 168 | disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(update())); |
| 169 | disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated())); |
| 170 | disconnect(camera()->keyFrameInterpolator(id), SIGNAL(endReached()), this, SLOT(cameraPathFinished())); |
| 171 | camera()->deletePath(id); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void ViewerWidget::appendToCameraPath(int id, const octomath::Pose6D& pose) { |
| 176 | qglviewer::Vec position(pose.trans().x(), pose.trans().y(), pose.trans().z()); |
| 177 | qglviewer::Quaternion quaternion = poseToQGLQuaternion(pose); |
| 178 | qglviewer::Frame frame(position, quaternion); |
| 179 | if(!camera()->keyFrameInterpolator(id)) { |
| 180 | camera()->setKeyFrameInterpolator(id, new qglviewer::KeyFrameInterpolator(camera()->frame())); |
| 181 | } |
| 182 | camera()->keyFrameInterpolator(id)->addKeyFrame(frame); |
| 183 | } |
| 184 | |
| 185 | void ViewerWidget::removeFromCameraPath(int id, int frame) { |
| 186 | qglviewer::KeyFrameInterpolator *old_kfi = camera()->keyFrameInterpolator(id); |
| 187 | if(old_kfi) { |
| 188 | qglviewer::KeyFrameInterpolator *new_kfi = new qglviewer::KeyFrameInterpolator(camera()->frame()); |
| 189 | for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) { |
| 190 | if(i != frame) { |
| 191 | new_kfi->addKeyFrame(old_kfi->keyFrame(i)); |
| 192 | } |
| 193 | } |
| 194 | deleteCameraPath(id); |
| 195 | camera()->setKeyFrameInterpolator(id, new_kfi); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void ViewerWidget::updateCameraPath(int id, int frame) { |
| 200 | qglviewer::KeyFrameInterpolator *old_kfi = camera()->keyFrameInterpolator(id); |
| 201 | if(old_kfi) { |
| 202 | qglviewer::KeyFrameInterpolator *new_kfi = new qglviewer::KeyFrameInterpolator(camera()->frame()); |
| 203 | for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) { |
| 204 | if(i != frame) { |
| 205 | new_kfi->addKeyFrame(old_kfi->keyFrame(i)); |
| 206 | } else { |
| 207 | new_kfi->addKeyFrame(*(camera()->frame())); |
| 208 | } |
| 209 | } |
| 210 | deleteCameraPath(id); |
| 211 | camera()->setKeyFrameInterpolator(id, new_kfi); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void ViewerWidget::appendCurrentToCameraPath(int id) { |
| 216 | int frame = 0; |
| 217 | if(camera()->keyFrameInterpolator(id)) frame = camera()->keyFrameInterpolator(id)->numberOfKeyFrames(); |
| 218 | addCurrentToCameraPath(id, frame); |
| 219 | } |
| 220 | |
| 221 | void ViewerWidget::addCurrentToCameraPath(int id, int frame) { |
| 222 | qglviewer::KeyFrameInterpolator *old_kfi = camera()->keyFrameInterpolator(id); |
| 223 | if(!old_kfi || frame >= old_kfi->numberOfKeyFrames()) { |
| 224 | camera()->addKeyFrameToPath(id); |
| 225 | } else { |
| 226 | qglviewer::KeyFrameInterpolator *new_kfi = new qglviewer::KeyFrameInterpolator(camera()->frame()); |
| 227 | for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) { |
| 228 | new_kfi->addKeyFrame(old_kfi->keyFrame(i)); |
| 229 | if(i == frame) { |
| 230 | new_kfi->addKeyFrame(camera()->frame()); |
| 231 | } |
| 232 | } |
| 233 | deleteCameraPath(id); |
| 234 | camera()->setKeyFrameInterpolator(id, new_kfi); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void ViewerWidget::playCameraPath(int id, int start_frame) { |
| 239 | qglviewer::KeyFrameInterpolator *kfi = camera()->keyFrameInterpolator(id); |
| 240 | if(kfi && !kfi->interpolationIsStarted() && start_frame >= 0 && start_frame < kfi->numberOfKeyFrames()) { |
| 241 | m_current_camera_path = id; |
| 242 | m_current_camera_frame = start_frame; |
| 243 | kfi->setInterpolationTime(kfi->keyFrameTime(start_frame)); |
| 244 | std::cout << "Playing path of length " << kfi->numberOfKeyFrames() << ", start time " << kfi->keyFrameTime(start_frame) << std::endl; |
| 245 | connect(kfi, SIGNAL(interpolated()), this, SLOT(update())); |
| 246 | connect(kfi, SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated())); |
| 247 | connect(kfi, SIGNAL(endReached()), this, SLOT(cameraPathFinished())); |
| 248 | kfi->startInterpolation(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void ViewerWidget::stopCameraPath(int id) { |
| 253 | if(camera()->keyFrameInterpolator(id) && camera()->keyFrameInterpolator(id)->interpolationIsStarted()) { |
| 254 | disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(update())); |
| 255 | disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated())); |
| 256 | disconnect(camera()->keyFrameInterpolator(id), SIGNAL(endReached()), this, SLOT(cameraPathFinished())); |
| 257 | camera()->keyFrameInterpolator(id)->stopInterpolation(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void ViewerWidget::cameraPathFinished() { |
| 262 | if(camera()->keyFrameInterpolator(m_current_camera_path)) { |
| 263 | disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(interpolated()), this, SLOT(update())); |
| 264 | disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated())); |
| 265 | disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(endReached()), this, SLOT(cameraPathFinished())); |
| 266 | emit cameraPathStopped(m_current_camera_path); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void ViewerWidget::cameraPathInterpolated() { |
| 271 | qglviewer::KeyFrameInterpolator *kfi = camera()->keyFrameInterpolator(m_current_camera_path); |
| 272 | if(kfi) { |
| 273 | int current_frame = m_current_camera_frame; |
| 274 | for(int i = m_current_camera_frame + 1; i < kfi->numberOfKeyFrames(); i++) { |
| 275 | if(kfi->keyFrameTime(current_frame) <= kfi->interpolationTime()) current_frame = i; |
| 276 | else break; |
| 277 | } |
| 278 | if(current_frame != m_current_camera_frame) { |
| 279 | m_current_camera_frame = current_frame; |
| 280 | emit cameraPathFrameChanged(m_current_camera_path, m_current_camera_frame); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void ViewerWidget::setSceneBoundingBox(const qglviewer::Vec& min, const qglviewer::Vec& max){ |
| 286 | m_zMin = min[2]; |
| 287 | m_zMax = max[2]; |
| 288 | QGLViewer::setSceneBoundingBox(min, max); |
| 289 | } |
| 290 | |
| 291 | void ViewerWidget::addSceneObject(SceneObject* obj){ |
| 292 | assert (obj); |
| 293 | m_sceneObjects.push_back(obj); |
| 294 | update(); |
| 295 | } |
| 296 | |
| 297 | void ViewerWidget::removeSceneObject(SceneObject* obj){ |
| 298 | assert(obj); |
| 299 | for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); |
| 300 | it != m_sceneObjects.end();){ |
| 301 | if (*it == obj) |
| 302 | it = m_sceneObjects.erase(it); |
| 303 | else |
| 304 | ++it; |
| 305 | } |
| 306 | update(); |
| 307 | } |
| 308 | |
| 309 | void ViewerWidget::clearAll(){ |
| 310 | // clear drawable objects: |
| 311 | for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++){ |
| 312 | (*it)->clear(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void ViewerWidget::draw(){ |
| 317 | |
| 318 | // debugging: draw light in scene |
| 319 | //drawLight(GL_LIGHT0); |
| 320 | |
| 321 | glEnable(GL_BLEND); |
| 322 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 323 | |
| 324 | glEnable(GL_LIGHTING); |
| 325 | if (m_printoutMode){ |
| 326 | glCullFace(GL_BACK); |
| 327 | } |
| 328 | |
| 329 | // draw drawable objects: |
| 330 | for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); |
| 331 | it != m_sceneObjects.end(); ++it){ |
| 332 | (*it)->draw(); |
| 333 | } |
| 334 | |
| 335 | if (m_drawSelectionBox){ |
| 336 | m_selectionBox.draw(); |
| 337 | |
| 338 | if (m_selectionBox.getGrabbedFrame() >= 0){ |
| 339 | setMouseBinding(Qt::LeftButton, FRAME, TRANSLATE); |
| 340 | } else { |
| 341 | setMouseBinding(Qt::LeftButton, FRAME, ROTATE); |
| 342 | } |
| 343 | |
| 344 | } |
| 345 | |
| 346 | } |
| 347 | |
| 348 | void ViewerWidget::drawWithNames(){ |
| 349 | |
| 350 | if (m_drawSelectionBox) |
| 351 | m_selectionBox.draw(true); |
| 352 | } |
| 353 | |
| 354 | void ViewerWidget::postDraw(){ |
| 355 | |
| 356 | // Reset model view matrix to world coordinates origin |
| 357 | glMatrixMode(GL_MODELVIEW); |
| 358 | glPushMatrix(); |
| 359 | camera()->loadModelViewMatrix(); |
| 360 | // TODO restore model loadProjectionMatrixStereo |
| 361 | |
| 362 | // Save OpenGL state |
| 363 | glPushAttrib(GL_ALL_ATTRIB_BITS); |
| 364 | |
| 365 | glDisable(GL_COLOR_MATERIAL); |
| 366 | |
| 367 | if (gridIsDrawn()){ |
| 368 | glLineWidth(1.0); |
| 369 | drawGrid(5.0, 10); |
| 370 | } |
| 371 | if (axisIsDrawn()){ |
| 372 | glLineWidth(2.0); |
| 373 | drawAxis(1.0); |
| 374 | } |
| 375 | |
| 376 | // Restore GL state |
| 377 | glPopAttrib(); |
| 378 | glPopMatrix(); |
| 379 | |
| 380 | m_drawAxis = axisIsDrawn(); |
| 381 | m_drawGrid = gridIsDrawn(); |
| 382 | setAxisIsDrawn(false); |
| 383 | setGridIsDrawn(false); |
| 384 | QGLViewer::postDraw(); |
| 385 | |
| 386 | setAxisIsDrawn(m_drawAxis); |
| 387 | setGridIsDrawn(m_drawGrid); |
| 388 | } |
| 389 | |
| 390 | void ViewerWidget::postSelection(const QPoint&) |
| 391 | { |
| 392 | |
| 393 | } |
| 394 | |
| 395 | |
| 396 | |
| 397 | } // namespace |
| 398 |