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 #include <octovis/ViewerWidget.h>
00026 #include <manipulatedCameraFrame.h>
00027
00028 #ifndef M_PI_2
00029 #define M_PI_2 1.5707963267948966192E0
00030 #endif
00031
00032 using namespace std;
00033
00034 namespace octomap {
00035
00036 ViewerWidget::ViewerWidget(QWidget* parent) :
00037 QGLViewer(parent), m_zMin(0.0),m_zMax(1.0) {
00038
00039 m_printoutMode = false;
00040 m_heightColorMode = false;
00041 m_semantic_coloring = false;
00042 m_drawSelectionBox = false;
00043 }
00044
00045 void ViewerWidget::init() {
00046
00047
00048
00049
00050 setMouseTracking(true);
00051
00052
00053 restoreStateFromFile();
00054
00055
00056 setManipulatedFrame( camera()->frame() );
00057
00058 camera()->frame()->setWheelSensitivity(-1.0);
00059
00060
00061
00062 glEnable(GL_LIGHT0);
00063
00064 float pos[4] = {-1.0, 1.0, 1.0, 0.0};
00065
00066 glLightfv(GL_LIGHT0, GL_POSITION, pos);
00067
00068
00069 this->setBackgroundColor( QColor(255,255,255) );
00070 this->qglClearColor( this->backgroundColor() );
00071 }
00072
00073 void ViewerWidget::resetView(){
00074 this->camera()->setOrientation((float) -M_PI_2, (float) M_PI_2);
00075 this->showEntireScene();
00076 updateGL();
00077 }
00078
00079
00080 QString ViewerWidget::helpString() const{
00081 QString help = "<h2>Octomap 3D viewer</h2>";
00082
00083 help +="The Octomap library implements a 3D occupancy grid mapping approach. "
00084 "It provides data structures and mapping algorithms. The map is implemented "
00085 "using an octree. 3D maps can be viewed an built using this 3D viewer."
00086 "<br/><br/>"
00087 "Octomap is available at http://octomap.github.com, and is actively "
00088 "maintained by Kai M. Wurm and Armin Hornung. This 3D viewer is based on "
00089 "libQGLViewer, available at http://www.libqglviewer.com/."
00090 "<br/><br/>"
00091 "Please refer to the \"Keyboard\" and \"Mouse\" tabs for instructions on "
00092 "how to use the viewer.";
00093 return help;
00094 }
00095
00096 void ViewerWidget::enableHeightColorMode (bool enabled) {
00097 m_heightColorMode = enabled;
00098 for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) {
00099 (*it)->enableHeightColorMode(enabled);
00100 }
00101 updateGL();
00102 }
00103
00104 void ViewerWidget::enablePrintoutMode(bool enabled) {
00105 m_printoutMode = enabled;
00106 for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) {
00107 (*it)->enablePrintoutMode(enabled);
00108 }
00109 updateGL();
00110 }
00111
00112 void ViewerWidget::enableSemanticColoring (bool enabled) {
00113 m_semantic_coloring = enabled;
00114 for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++) {
00115 (*it)->enableSemanticColoring(enabled);
00116 }
00117 updateGL();
00118 }
00119
00120 void ViewerWidget::enableSelectionBox(bool enabled) {
00121 m_drawSelectionBox = enabled;
00122 updateGL();
00123 }
00124
00125
00126 qglviewer::Quaternion ViewerWidget::poseToQGLQuaternion(const octomath::Pose6D& pose) {
00127
00128
00129
00130
00131
00132 octomath::Vector3 dir = pose.rot().rotate(octomath::Vector3(1.,0.,0.));
00133 qglviewer::Vec direction(dir.x(), dir.y(), dir.z());
00134
00135
00136 qglviewer::Vec xAxis = direction ^ qglviewer::Vec(0.0, 0.0, 1.0);
00137
00138 qglviewer::Quaternion q;
00139 q.setFromRotatedBasis(xAxis, xAxis^direction, -direction);
00140 return q;
00141 }
00142
00143 void ViewerWidget::setCamPosition(double x, double y, double z, double lookX, double lookY, double lookZ){
00144 this->camera()->setOrientation(-M_PI/2., M_PI/2.);
00145 camera()->setPosition(qglviewer::Vec(x, y, z));
00146 camera()->lookAt(qglviewer::Vec(lookX, lookY, lookZ));
00147 camera()->setUpVector(qglviewer::Vec(0.0, 0.0, 1.0));
00148 updateGL();
00149 }
00150
00151 void ViewerWidget::setCamPose(const octomath::Pose6D& pose){
00152 octomath::Pose6D ahead = pose * octomath::Pose6D(octomath::Vector3(1,0,0), octomath::Quaternion());
00153 setCamPosition(pose.x(), pose.y(), pose.z(), ahead.x(), ahead.y(), ahead.z());
00154 }
00155
00156 void ViewerWidget::jumpToCamFrame(int id, int frame) {
00157 qglviewer::KeyFrameInterpolator *kfi = camera()->keyFrameInterpolator(id);
00158 if(kfi && frame >= 0 && frame < kfi->numberOfKeyFrames()) {
00159 camera()->setPosition(kfi->keyFrame(frame).position());
00160 camera()->setOrientation(kfi->keyFrame(frame).orientation());
00161 } else {
00162 std::cerr << "Error: Could not jump to frame " << frame << " of " << kfi->numberOfKeyFrames() << std::endl;
00163 }
00164 updateGL();
00165 }
00166
00167 void ViewerWidget::deleteCameraPath(int id) {
00168 if(camera()->keyFrameInterpolator(id)) {
00169 disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(updateGL()));
00170 disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
00171 disconnect(camera()->keyFrameInterpolator(id), SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
00172 camera()->deletePath(id);
00173 }
00174 }
00175
00176 void ViewerWidget::appendToCameraPath(int id, const octomath::Pose6D& pose) {
00177 qglviewer::Vec position(pose.trans().x(), pose.trans().y(), pose.trans().z());
00178 qglviewer::Quaternion quaternion = poseToQGLQuaternion(pose);
00179 qglviewer::Frame frame(position, quaternion);
00180 if(!camera()->keyFrameInterpolator(id)) {
00181 camera()->setKeyFrameInterpolator(id, new qglviewer::KeyFrameInterpolator(camera()->frame()));
00182 }
00183 camera()->keyFrameInterpolator(id)->addKeyFrame(frame);
00184 }
00185
00186 void ViewerWidget::removeFromCameraPath(int id, int frame) {
00187 qglviewer::KeyFrameInterpolator *old_kfi = camera()->keyFrameInterpolator(id);
00188 if(old_kfi) {
00189 qglviewer::KeyFrameInterpolator *new_kfi = new qglviewer::KeyFrameInterpolator(camera()->frame());
00190 for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) {
00191 if(i != frame) {
00192 new_kfi->addKeyFrame(old_kfi->keyFrame(i));
00193 }
00194 }
00195 deleteCameraPath(id);
00196 camera()->setKeyFrameInterpolator(id, new_kfi);
00197 }
00198 }
00199
00200 void ViewerWidget::updateCameraPath(int id, int frame) {
00201 qglviewer::KeyFrameInterpolator *old_kfi = camera()->keyFrameInterpolator(id);
00202 if(old_kfi) {
00203 qglviewer::KeyFrameInterpolator *new_kfi = new qglviewer::KeyFrameInterpolator(camera()->frame());
00204 for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) {
00205 if(i != frame) {
00206 new_kfi->addKeyFrame(old_kfi->keyFrame(i));
00207 } else {
00208 new_kfi->addKeyFrame(*(camera()->frame()));
00209 }
00210 }
00211 deleteCameraPath(id);
00212 camera()->setKeyFrameInterpolator(id, new_kfi);
00213 }
00214 }
00215
00216 void ViewerWidget::appendCurrentToCameraPath(int id) {
00217 int frame = 0;
00218 if(camera()->keyFrameInterpolator(id)) frame = camera()->keyFrameInterpolator(id)->numberOfKeyFrames();
00219 addCurrentToCameraPath(id, frame);
00220 }
00221
00222 void ViewerWidget::addCurrentToCameraPath(int id, int frame) {
00223 qglviewer::KeyFrameInterpolator *old_kfi = camera()->keyFrameInterpolator(id);
00224 if(!old_kfi || frame >= old_kfi->numberOfKeyFrames()) {
00225 camera()->addKeyFrameToPath(id);
00226 } else {
00227 qglviewer::KeyFrameInterpolator *new_kfi = new qglviewer::KeyFrameInterpolator(camera()->frame());
00228 for(int i = 0; i < old_kfi->numberOfKeyFrames(); i++) {
00229 new_kfi->addKeyFrame(old_kfi->keyFrame(i));
00230 if(i == frame) {
00231 new_kfi->addKeyFrame(camera()->frame());
00232 }
00233 }
00234 deleteCameraPath(id);
00235 camera()->setKeyFrameInterpolator(id, new_kfi);
00236 }
00237 }
00238
00239 void ViewerWidget::playCameraPath(int id, int start_frame) {
00240 qglviewer::KeyFrameInterpolator *kfi = camera()->keyFrameInterpolator(id);
00241 if(kfi && !kfi->interpolationIsStarted() && start_frame >= 0 && start_frame < kfi->numberOfKeyFrames()) {
00242 m_current_camera_path = id;
00243 m_current_camera_frame = start_frame;
00244 kfi->setInterpolationTime(kfi->keyFrameTime(start_frame));
00245 std::cout << "Playing path of length " << kfi->numberOfKeyFrames() << ", start time " << kfi->keyFrameTime(start_frame) << std::endl;
00246 connect(kfi, SIGNAL(interpolated()), this, SLOT(updateGL()));
00247 connect(kfi, SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
00248 connect(kfi, SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
00249 kfi->startInterpolation();
00250 }
00251 }
00252
00253 void ViewerWidget::stopCameraPath(int id) {
00254 if(camera()->keyFrameInterpolator(id) && camera()->keyFrameInterpolator(id)->interpolationIsStarted()) {
00255 disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(updateGL()));
00256 disconnect(camera()->keyFrameInterpolator(id), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
00257 disconnect(camera()->keyFrameInterpolator(id), SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
00258 camera()->keyFrameInterpolator(id)->stopInterpolation();
00259 }
00260 }
00261
00262 void ViewerWidget::cameraPathFinished() {
00263 if(camera()->keyFrameInterpolator(m_current_camera_path)) {
00264 disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(interpolated()), this, SLOT(updateGL()));
00265 disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(interpolated()), this, SLOT(cameraPathInterpolated()));
00266 disconnect(camera()->keyFrameInterpolator(m_current_camera_path), SIGNAL(endReached()), this, SLOT(cameraPathFinished()));
00267 emit cameraPathStopped(m_current_camera_path);
00268 }
00269 }
00270
00271 void ViewerWidget::cameraPathInterpolated() {
00272 qglviewer::KeyFrameInterpolator *kfi = camera()->keyFrameInterpolator(m_current_camera_path);
00273 if(kfi) {
00274 int current_frame = m_current_camera_frame;
00275 for(int i = m_current_camera_frame + 1; i < kfi->numberOfKeyFrames(); i++) {
00276 if(kfi->keyFrameTime(current_frame) <= kfi->interpolationTime()) current_frame = i;
00277 else break;
00278 }
00279 if(current_frame != m_current_camera_frame) {
00280 m_current_camera_frame = current_frame;
00281 emit cameraPathFrameChanged(m_current_camera_path, m_current_camera_frame);
00282 }
00283 }
00284 }
00285
00286 void ViewerWidget::setSceneBoundingBox(const qglviewer::Vec& min, const qglviewer::Vec& max){
00287 m_zMin = min[2];
00288 m_zMax = max[2];
00289 QGLViewer::setSceneBoundingBox(min, max);
00290 }
00291
00292 void ViewerWidget::addSceneObject(SceneObject* obj){
00293 assert (obj);
00294 m_sceneObjects.push_back(obj);
00295 updateGL();
00296 }
00297
00298 void ViewerWidget::removeSceneObject(SceneObject* obj){
00299 assert(obj);
00300 for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin();
00301 it != m_sceneObjects.end();){
00302 if (*it == obj)
00303 it = m_sceneObjects.erase(it);
00304 else
00305 ++it;
00306 }
00307 updateGL();
00308 }
00309
00310 void ViewerWidget::clearAll(){
00311
00312 for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin(); it != m_sceneObjects.end(); it++){
00313 (*it)->clear();
00314 }
00315 }
00316
00317 void ViewerWidget::draw(){
00318
00319
00320
00321
00322 glEnable(GL_BLEND);
00323 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00324
00325 glEnable(GL_LIGHTING);
00326 if (m_printoutMode){
00327 glCullFace(GL_BACK);
00328 }
00329
00330
00331 for(std::vector<SceneObject*>::iterator it = m_sceneObjects.begin();
00332 it != m_sceneObjects.end(); ++it){
00333 (*it)->draw();
00334 }
00335
00336 if (m_drawSelectionBox){
00337 m_selectionBox.draw();
00338
00339 if (m_selectionBox.getGrabbedFrame() >= 0){
00340 setMouseBinding(Qt::LeftButton, FRAME, TRANSLATE);
00341 } else {
00342 setMouseBinding(Qt::LeftButton, FRAME, ROTATE);
00343 }
00344
00345 }
00346
00347 }
00348
00349 void ViewerWidget::drawWithNames(){
00350
00351 if (m_drawSelectionBox)
00352 m_selectionBox.draw(true);
00353 }
00354
00355 void ViewerWidget::postDraw(){
00356
00357
00358 glMatrixMode(GL_MODELVIEW);
00359 glPushMatrix();
00360 camera()->loadModelViewMatrix();
00361
00362
00363
00364 glPushAttrib(GL_ALL_ATTRIB_BITS);
00365
00366 glDisable(GL_COLOR_MATERIAL);
00367 qglColor(foregroundColor());
00368
00369 if (gridIsDrawn()){
00370 glLineWidth(1.0);
00371 drawGrid(5.0, 10);
00372 }
00373 if (axisIsDrawn()){
00374 glLineWidth(2.0);
00375 drawAxis(1.0);
00376 }
00377
00378
00379 glPopAttrib();
00380 glPopMatrix();
00381
00382 m_drawAxis = axisIsDrawn();
00383 m_drawGrid = gridIsDrawn();
00384 setAxisIsDrawn(false);
00385 setGridIsDrawn(false);
00386 QGLViewer::postDraw();
00387
00388 setAxisIsDrawn(m_drawAxis);
00389 setGridIsDrawn(m_drawGrid);
00390 }
00391
00392 void ViewerWidget::postSelection(const QPoint&)
00393 {
00394
00395 }
00396
00397
00398
00399 }
00400