Q3DWidget.cc 19.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of the class Q3DWidget.
 *
 *   @author Lionel Heng <hengli@student.ethz.ch>
 *
 */

lm's avatar
lm committed
32
#include "Q3DWidget.h"
33
#include "QGC.h"
lm's avatar
lm committed
34

35 36 37
#include <osg/Geometry>
#include <osg/LineWidth>
#include <osg/MatrixTransform>
38
#ifdef QGC_OSG_QT_ENABLED
39
#include <osgQt/QFontImplementation>
40
#endif
lm's avatar
lm committed
41
#ifdef Q_OS_MACX
42
#include <Carbon/Carbon.h>
lm's avatar
lm committed
43
#endif
lm's avatar
lm committed
44 45

Q3DWidget::Q3DWidget(QWidget* parent)
46 47 48 49 50 51 52
    : QGLWidget(parent)
    , root(new osg::Group())
    , allocentricMap(new osg::Switch())
    , rollingMap(new osg::Switch())
    , egocentricMap(new osg::Switch())
    , robotPosition(new osg::PositionAttitudeTransform())
    , robotAttitude(new osg::PositionAttitudeTransform())
53
    , hudGroup(new osg::Switch())
54
    , hudProjectionMatrix(new osg::Projection)
pixhawk's avatar
pixhawk committed
55
    , fps(30.0f)
56 57
{
    // set initial camera parameters
58
    cameraParams.minZoomRange = 2.0f;
59 60
    cameraParams.cameraFov = 30.0f;
    cameraParams.minClipRange = 1.0f;
61
    cameraParams.maxClipRange = 10000.0f;
62
#ifdef QGC_OSG_QT_ENABLED
63 64
    osg::ref_ptr<osgText::Font::FontImplementation> fontImpl;
    fontImpl = new osgQt::QFontImplementation(QFont(":/general/vera.ttf"));
65 66 67 68
#else
    osg::ref_ptr<osgText::Font::FontImplementation> fontImpl;
    fontImpl = 0;//new osgText::Font::Font("images/Vera.ttf");
#endif
69 70
    font = new osgText::Font(fontImpl);

71 72 73 74 75
    osgGW = new osgViewer::GraphicsWindowEmbedded(0, 0, width(), height());

    setThreadingModel(osgViewer::Viewer::SingleThreaded);

    setFocusPolicy(Qt::StrongFocus);
76
    setMouseTracking(true);
lm's avatar
lm committed
77 78 79 80 81 82 83 84
}

Q3DWidget::~Q3DWidget()
{

}

void
85
Q3DWidget::init(float fps)
lm's avatar
lm committed
86
{
pixhawk's avatar
pixhawk committed
87 88
    this->fps = fps;

89
    getCamera()->setGraphicsContext(osgGW);
90 91 92

    // manually specify near and far clip planes
    getCamera()->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
93

94
    setLightingMode(osg::View::SKY_LIGHT);
lm's avatar
lm committed
95

96 97 98 99
    // set up various maps
    // allocentric - world map
    // rolling - map aligned to the world axes and centered on the robot
    // egocentric - vehicle-centric map
100 101 102 103 104
    root->addChild(allocentricMap);
    allocentricMap->addChild(robotPosition);
    robotPosition->addChild(rollingMap);
    rollingMap->addChild(robotAttitude);
    robotAttitude->addChild(egocentricMap);
lm's avatar
lm committed
105

106
    setSceneData(root);
lm's avatar
lm committed
107

108 109
    // set up HUD
    root->addChild(createHUD());
lm's avatar
lm committed
110

111
    // set up robot
112
    egocentricMap->addChild(createRobot());
lm's avatar
lm committed
113

114
    // set up camera control
115
    cameraManipulator = new GCManipulator();
116
    setCameraManipulator(cameraManipulator);
117 118
    cameraManipulator->setMinZoomRange(cameraParams.minZoomRange);
    cameraManipulator->setDistance(cameraParams.minZoomRange * 2.0);
lm's avatar
lm committed
119

120
    connect(&timer, SIGNAL(timeout()), this, SLOT(redraw()));
121
    // DO NOT START TIMER IN INITIALIZATION! IT IS STARTED IN THE SHOW EVENT
lm's avatar
lm committed
122 123
}

pixhawk's avatar
pixhawk committed
124 125 126 127
void Q3DWidget::showEvent(QShowEvent* event)
{
    // React only to internal (pre/post-display)
    // events
128 129 130 131 132 133 134 135 136 137
    Q_UNUSED(event)
    timer.start(static_cast<int>(floorf(1000.0f / fps)));
}

void Q3DWidget::hideEvent(QHideEvent* event)
{
    // React only to internal (pre/post-display)
    // events
    Q_UNUSED(event)
    timer.stop();
pixhawk's avatar
pixhawk committed
138 139
}

140 141
osg::ref_ptr<osg::Geode>
Q3DWidget::createRobot(void)
lm's avatar
lm committed
142
{
143 144 145 146 147 148 149
    // draw x,y,z-axes
    osg::ref_ptr<osg::Geode> geode(new osg::Geode());
    osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry());
    geode->addDrawable(geometry.get());

    osg::ref_ptr<osg::Vec3Array> coords(new osg::Vec3Array(6));
    (*coords)[0] = (*coords)[2] = (*coords)[4] =
150
                                      osg::Vec3(0.0f, 0.0f, 0.0f);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    (*coords)[1] = osg::Vec3(0.15f, 0.0f, 0.0f);
    (*coords)[3] = osg::Vec3(0.0f, 0.3f, 0.0f);
    (*coords)[5] = osg::Vec3(0.0f, 0.0f, -0.15f);

    geometry->setVertexArray(coords);

    osg::Vec4 redColor(1.0f, 0.0f, 0.0f, 0.0f);
    osg::Vec4 greenColor(0.0f, 1.0f, 0.0f, 0.0f);
    osg::Vec4 blueColor(0.0f, 0.0f, 1.0f, 0.0f);

    osg::ref_ptr<osg::Vec4Array> color(new osg::Vec4Array(6));
    (*color)[0] = redColor;
    (*color)[1] = redColor;
    (*color)[2] = greenColor;
    (*color)[3] = greenColor;
    (*color)[4] = blueColor;
    (*color)[5] = blueColor;

    geometry->setColorArray(color);
    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 6));

    osg::ref_ptr<osg::StateSet> stateset(new osg::StateSet);
    osg::ref_ptr<osg::LineWidth> linewidth(new osg::LineWidth());
    linewidth->setWidth(3.0f);
    stateset->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    geometry->setStateSet(stateset);

    return geode;
182 183
}

184 185
osg::ref_ptr<osg::Node>
Q3DWidget::createHUD(void)
lm's avatar
lm committed
186
{
187
    hudProjectionMatrix->setMatrix(osg::Matrix::ortho(0.0, width(),
188 189
                                   0.0, height(),
                                   -10.0, 10.0));
190 191

    osg::ref_ptr<osg::MatrixTransform> hudModelViewMatrix(
192
        new osg::MatrixTransform);
193 194 195 196
    hudModelViewMatrix->setMatrix(osg::Matrix::identity());
    hudModelViewMatrix->setReferenceFrame(osg::Transform::ABSOLUTE_RF);

    hudProjectionMatrix->addChild(hudModelViewMatrix);
197
    hudModelViewMatrix->addChild(hudGroup);
198 199

    osg::ref_ptr<osg::StateSet> hudStateSet(new osg::StateSet);
200
    hudGroup->setStateSet(hudStateSet);
201
    hudStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
202
    hudStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
203
    hudStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
204 205 206 207
    hudStateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
    hudStateSet->setRenderBinDetails(11, "RenderBin");

    return hudProjectionMatrix;
lm's avatar
lm committed
208 209 210
}

void
211 212
Q3DWidget::setCameraParams(float minZoomRange, float cameraFov,
                           float minClipRange, float maxClipRange)
lm's avatar
lm committed
213
{
214 215 216 217
    cameraParams.minZoomRange = minZoomRange;
    cameraParams.cameraFov = cameraFov;
    cameraParams.minClipRange = minClipRange;
    cameraParams.maxClipRange = maxClipRange;
lm's avatar
lm committed
218 219 220
}

void
221
Q3DWidget::moveCamera(double dx, double dy, double dz)
222 223 224 225 226
{
    cameraManipulator->move(dx, dy, dz);
}

void
227
Q3DWidget::recenterCamera(double x, double y, double z)
lm's avatar
lm committed
228
{
229
    cameraManipulator->setCenter(osg::Vec3d(x, y, z));
lm's avatar
lm committed
230 231 232
}

void
233
Q3DWidget::setDisplayMode3D(void)
lm's avatar
lm committed
234
{
235 236
    double aspect = static_cast<double>(width())
                    / static_cast<double>(height());
lm's avatar
lm committed
237

238 239
    getCamera()->setViewport(new osg::Viewport(0, 0, width(), height()));
    getCamera()->setProjectionMatrixAsPerspective(cameraParams.cameraFov,
240 241 242
            aspect,
            cameraParams.minClipRange,
            cameraParams.maxClipRange);
lm's avatar
lm committed
243 244
}

245
std::pair<double,double>
246
Q3DWidget::getGlobalCursorPosition(int32_t cursorX, int32_t cursorY, double z)
lm's avatar
lm committed
247
{
248
    osgUtil::LineSegmentIntersector::Intersections intersections;
lm's avatar
lm committed
249

250
    // normalize cursor position to value between -1 and 1
251
    double x = -1.0f + static_cast<double>(2 * cursorX)
252
               / static_cast<double>(width());
253
    double y = -1.0f + static_cast<double>(2 * (height() - cursorY))
254
               / static_cast<double>(height());
lm's avatar
lm committed
255

256
    // compute matrix which transforms screen coordinates to world coordinates
257 258 259 260 261 262
    osg::Matrixd m = getCamera()->getViewMatrix()
                     * getCamera()->getProjectionMatrix();
    osg::Matrixd invM = osg::Matrixd::inverse(m);

    osg::Vec3d nearPoint = osg::Vec3d(x, y, -1.0) * invM;
    osg::Vec3d farPoint = osg::Vec3d(x, y, 1.0) * invM;
lm's avatar
lm committed
263

264
    osg::ref_ptr<osg::LineSegment> line =
265
        new osg::LineSegment(nearPoint, farPoint);
lm's avatar
lm committed
266

267
    osg::Plane p(osg::Vec3d(0.0, 0.0, 1.0), osg::Vec3d(0.0, 0.0, z));
lm's avatar
lm committed
268

269 270
    osg::Vec3d projectedPoint;
    getPlaneLineIntersection(p.asVec4(), *line, projectedPoint);
lm's avatar
lm committed
271

272
    return std::make_pair(projectedPoint.y(), projectedPoint.x());
273 274
}

275 276
void
Q3DWidget::redraw(void)
lm's avatar
lm committed
277
{
278 279 280
#if (QGC_EVENTLOOP_DEBUG)
    qDebug() << "EVENTLOOP:" << __FILE__ << __LINE__;
#endif
281
    updateGL();
lm's avatar
lm committed
282 283 284
}

int
285
Q3DWidget::getMouseX(void)
lm's avatar
lm committed
286
{
287
    return mapFromGlobal(cursor().pos()).x();
lm's avatar
lm committed
288 289 290
}

int
291
Q3DWidget::getMouseY(void)
lm's avatar
lm committed
292
{
293
    return mapFromGlobal(cursor().pos()).y();
lm's avatar
lm committed
294 295
}

296 297
void
Q3DWidget::resizeGL(int width, int height)
lm's avatar
lm committed
298
{
299
    hudProjectionMatrix->setMatrix(osg::Matrix::ortho(0.0, width,
300 301
                                   0.0, height,
                                   -10.0, 10.0));
lm's avatar
lm committed
302

303 304
    osgGW->getEventQueue()->windowResize(0, 0, width, height);
    osgGW->resized(0 , 0, width, height);
lm's avatar
lm committed
305 306
}

307 308
void
Q3DWidget::paintGL(void)
lm's avatar
lm committed
309
{
310 311
    setDisplayMode3D();

312 313
    getCamera()->setClearColor(osg::Vec4f(0.0f, 0.0f, 0.0f, 0.0f));
    getCamera()->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
314

315
    display();
316 317

    frame();
lm's avatar
lm committed
318 319
}

320 321 322 323 324 325 326 327
void
Q3DWidget::display(void)
{

}

void
Q3DWidget::keyPressEvent(QKeyEvent* event)
lm's avatar
lm committed
328
{
329
    QWidget::keyPressEvent(event);
330
    if (event->isAccepted()) {
331
        return;
332
    }
333

334
    if (event->text().isEmpty()) {
335
        osgGW->getEventQueue()->keyPress(convertKey(event->key()));
336
    } else {
337
        osgGW->getEventQueue()->keyPress(
338 339
            static_cast<osgGA::GUIEventAdapter::KeySymbol>(
                *(event->text().toAscii().data())));
340
    }
lm's avatar
lm committed
341 342
}

343 344
void
Q3DWidget::keyReleaseEvent(QKeyEvent* event)
lm's avatar
lm committed
345
{
346
    QWidget::keyReleaseEvent(event);
347
    if (event->isAccepted()) {
348
        return;
349
    }
350
    if (event->text().isEmpty()) {
351
        osgGW->getEventQueue()->keyRelease(convertKey(event->key()));
352
    } else {
353
        osgGW->getEventQueue()->keyRelease(
354 355
            static_cast<osgGA::GUIEventAdapter::KeySymbol>(
                *(event->text().toAscii().data())));
356
    }
lm's avatar
lm committed
357 358
}

359 360
void
Q3DWidget::mousePressEvent(QMouseEvent* event)
lm's avatar
lm committed
361
{
362
    int button = 0;
363
    switch (event->button()) {
364 365 366 367 368 369 370 371 372 373 374 375 376
    case Qt::LeftButton:
        button = 1;
        break;
    case Qt::MidButton:
        button = 2;
        break;
    case Qt::RightButton:
        button = 3;
        break;
    case Qt::NoButton:
        button = 0;
        break;
    default:
377
    {}
378
    }
379
    osgGW->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
lm's avatar
lm committed
380 381
}

382 383
void
Q3DWidget::mouseReleaseEvent(QMouseEvent* event)
lm's avatar
lm committed
384
{
385
    int button = 0;
386
    switch (event->button()) {
387 388 389 390 391 392 393 394 395 396 397 398 399
    case Qt::LeftButton:
        button = 1;
        break;
    case Qt::MidButton:
        button = 2;
        break;
    case Qt::RightButton:
        button = 3;
        break;
    case Qt::NoButton:
        button = 0;
        break;
    default:
400
    {}
401
    }
402
    osgGW->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
lm's avatar
lm committed
403 404
}

405 406
void
Q3DWidget::mouseMoveEvent(QMouseEvent* event)
lm's avatar
lm committed
407
{
408
    osgGW->getEventQueue()->mouseMotion(event->x(), event->y());
lm's avatar
lm committed
409
}
pixhawk's avatar
pixhawk committed
410

411 412
void
Q3DWidget::wheelEvent(QWheelEvent* event)
pixhawk's avatar
pixhawk committed
413
{
414
    osgGW->getEventQueue()->mouseScroll((event->delta() > 0) ?
415 416
                                        osgGA::GUIEventAdapter::SCROLL_UP :
                                        osgGA::GUIEventAdapter::SCROLL_DOWN);
pixhawk's avatar
pixhawk committed
417 418
}

419 420
osgGA::GUIEventAdapter::KeySymbol
Q3DWidget::convertKey(int key) const
pixhawk's avatar
pixhawk committed
421
{
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    switch (key) {
    case Qt::Key_Space :
        return osgGA::GUIEventAdapter::KEY_Space;
    case Qt::Key_Backspace :
        return osgGA::GUIEventAdapter::KEY_BackSpace;
    case Qt::Key_Tab :
        return osgGA::GUIEventAdapter::KEY_Tab;
    case Qt::Key_Clear :
        return osgGA::GUIEventAdapter::KEY_Clear;
    case Qt::Key_Return :
        return osgGA::GUIEventAdapter::KEY_Return;
    case Qt::Key_Enter :
        return osgGA::GUIEventAdapter::KEY_KP_Enter;
    case Qt::Key_Pause :
        return osgGA::GUIEventAdapter::KEY_Pause;
    case Qt::Key_ScrollLock :
        return osgGA::GUIEventAdapter::KEY_Scroll_Lock;
    case Qt::Key_SysReq :
        return osgGA::GUIEventAdapter::KEY_Sys_Req;
    case Qt::Key_Escape :
        return osgGA::GUIEventAdapter::KEY_Escape;
    case Qt::Key_Delete :
        return osgGA::GUIEventAdapter::KEY_Delete;
    case Qt::Key_Home :
        return osgGA::GUIEventAdapter::KEY_Home;
    case Qt::Key_Left :
        return osgGA::GUIEventAdapter::KEY_Left;
    case Qt::Key_Up :
        return osgGA::GUIEventAdapter::KEY_Up;
    case Qt::Key_Right :
        return osgGA::GUIEventAdapter::KEY_Right;
    case Qt::Key_Down :
        return osgGA::GUIEventAdapter::KEY_Down;
    case Qt::Key_PageUp :
        return osgGA::GUIEventAdapter::KEY_Page_Up;
    case Qt::Key_PageDown :
        return osgGA::GUIEventAdapter::KEY_Page_Down;
    case Qt::Key_End :
        return osgGA::GUIEventAdapter::KEY_End;
    case Qt::Key_Select :
        return osgGA::GUIEventAdapter::KEY_Select;
    case Qt::Key_Print :
        return osgGA::GUIEventAdapter::KEY_Print;
    case Qt::Key_Execute :
        return osgGA::GUIEventAdapter::KEY_Execute;
    case Qt::Key_Insert :
        return osgGA::GUIEventAdapter::KEY_Insert;
    case Qt::Key_Menu :
        return osgGA::GUIEventAdapter::KEY_Menu;
    case Qt::Key_Cancel :
        return osgGA::GUIEventAdapter::KEY_Cancel;
    case Qt::Key_Help :
        return osgGA::GUIEventAdapter::KEY_Help;
    case Qt::Key_Mode_switch :
        return osgGA::GUIEventAdapter::KEY_Mode_switch;
    case Qt::Key_NumLock :
        return osgGA::GUIEventAdapter::KEY_Num_Lock;
    case Qt::Key_Equal :
        return osgGA::GUIEventAdapter::KEY_KP_Equal;
    case Qt::Key_Asterisk :
        return osgGA::GUIEventAdapter::KEY_KP_Multiply;
    case Qt::Key_Plus :
        return osgGA::GUIEventAdapter::KEY_KP_Add;
    case Qt::Key_Minus :
        return osgGA::GUIEventAdapter::KEY_KP_Subtract;
    case Qt::Key_Comma :
        return osgGA::GUIEventAdapter::KEY_KP_Decimal;
    case Qt::Key_Slash :
        return osgGA::GUIEventAdapter::KEY_KP_Divide;
    case Qt::Key_0 :
        return osgGA::GUIEventAdapter::KEY_KP_0;
    case Qt::Key_1 :
        return osgGA::GUIEventAdapter::KEY_KP_1;
    case Qt::Key_2 :
        return osgGA::GUIEventAdapter::KEY_KP_2;
    case Qt::Key_3 :
        return osgGA::GUIEventAdapter::KEY_KP_3;
    case Qt::Key_4 :
        return osgGA::GUIEventAdapter::KEY_KP_4;
    case Qt::Key_5 :
        return osgGA::GUIEventAdapter::KEY_KP_5;
    case Qt::Key_6 :
        return osgGA::GUIEventAdapter::KEY_KP_6;
    case Qt::Key_7 :
        return osgGA::GUIEventAdapter::KEY_KP_7;
    case Qt::Key_8 :
        return osgGA::GUIEventAdapter::KEY_KP_8;
    case Qt::Key_9 :
        return osgGA::GUIEventAdapter::KEY_KP_9;
    case Qt::Key_F1 :
        return osgGA::GUIEventAdapter::KEY_F1;
    case Qt::Key_F2 :
        return osgGA::GUIEventAdapter::KEY_F2;
    case Qt::Key_F3 :
        return osgGA::GUIEventAdapter::KEY_F3;
    case Qt::Key_F4 :
        return osgGA::GUIEventAdapter::KEY_F4;
    case Qt::Key_F5 :
        return osgGA::GUIEventAdapter::KEY_F5;
    case Qt::Key_F6 :
        return osgGA::GUIEventAdapter::KEY_F6;
    case Qt::Key_F7 :
        return osgGA::GUIEventAdapter::KEY_F7;
    case Qt::Key_F8 :
        return osgGA::GUIEventAdapter::KEY_F8;
    case Qt::Key_F9 :
        return osgGA::GUIEventAdapter::KEY_F9;
    case Qt::Key_F10 :
        return osgGA::GUIEventAdapter::KEY_F10;
    case Qt::Key_F11 :
        return osgGA::GUIEventAdapter::KEY_F11;
    case Qt::Key_F12 :
        return osgGA::GUIEventAdapter::KEY_F12;
    case Qt::Key_F13 :
        return osgGA::GUIEventAdapter::KEY_F13;
    case Qt::Key_F14 :
        return osgGA::GUIEventAdapter::KEY_F14;
    case Qt::Key_F15 :
        return osgGA::GUIEventAdapter::KEY_F15;
    case Qt::Key_F16 :
        return osgGA::GUIEventAdapter::KEY_F16;
    case Qt::Key_F17 :
        return osgGA::GUIEventAdapter::KEY_F17;
    case Qt::Key_F18 :
        return osgGA::GUIEventAdapter::KEY_F18;
    case Qt::Key_F19 :
        return osgGA::GUIEventAdapter::KEY_F19;
    case Qt::Key_F20 :
        return osgGA::GUIEventAdapter::KEY_F20;
    case Qt::Key_F21 :
        return osgGA::GUIEventAdapter::KEY_F21;
    case Qt::Key_F22 :
        return osgGA::GUIEventAdapter::KEY_F22;
    case Qt::Key_F23 :
        return osgGA::GUIEventAdapter::KEY_F23;
    case Qt::Key_F24 :
        return osgGA::GUIEventAdapter::KEY_F24;
    case Qt::Key_F25 :
        return osgGA::GUIEventAdapter::KEY_F25;
    case Qt::Key_F26 :
        return osgGA::GUIEventAdapter::KEY_F26;
    case Qt::Key_F27 :
        return osgGA::GUIEventAdapter::KEY_F27;
    case Qt::Key_F28 :
        return osgGA::GUIEventAdapter::KEY_F28;
    case Qt::Key_F29 :
        return osgGA::GUIEventAdapter::KEY_F29;
    case Qt::Key_F30 :
        return osgGA::GUIEventAdapter::KEY_F30;
    case Qt::Key_F31 :
        return osgGA::GUIEventAdapter::KEY_F31;
    case Qt::Key_F32 :
        return osgGA::GUIEventAdapter::KEY_F32;
    case Qt::Key_F33 :
        return osgGA::GUIEventAdapter::KEY_F33;
    case Qt::Key_F34 :
        return osgGA::GUIEventAdapter::KEY_F34;
    case Qt::Key_F35 :
        return osgGA::GUIEventAdapter::KEY_F35;
    case Qt::Key_Shift :
        return osgGA::GUIEventAdapter::KEY_Shift_L;
583
//    case Qt::Key_Shift_R : return osgGA::GUIEventAdapter::KEY_Shift_R;
584 585
    case Qt::Key_Control :
        return osgGA::GUIEventAdapter::KEY_Control_L;
586
//    case Qt::Key_Control_R : return osgGA::GUIEventAdapter::KEY_Control_R;
587 588 589 590
    case Qt::Key_CapsLock :
        return osgGA::GUIEventAdapter::KEY_Caps_Lock;
    case Qt::Key_Meta :
        return osgGA::GUIEventAdapter::KEY_Meta_L;
591
//    case Qt::Key_Meta_R: return osgGA::GUIEventAdapter::KEY_Meta_R;
592 593
    case Qt::Key_Alt :
        return osgGA::GUIEventAdapter::KEY_Alt_L;
594
//    case Qt::Key_Alt_R : return osgGA::GUIEventAdapter::KEY_Alt_R;
595 596 597 598 599 600 601 602
    case Qt::Key_Super_L :
        return osgGA::GUIEventAdapter::KEY_Super_L;
    case Qt::Key_Super_R :
        return osgGA::GUIEventAdapter::KEY_Super_R;
    case Qt::Key_Hyper_L :
        return osgGA::GUIEventAdapter::KEY_Hyper_L;
    case Qt::Key_Hyper_R :
        return osgGA::GUIEventAdapter::KEY_Hyper_R;
603 604
    default:
        return static_cast<osgGA::GUIEventAdapter::KeySymbol>(key);
605
    }
pixhawk's avatar
pixhawk committed
606 607
}

608 609 610 611
bool
Q3DWidget::getPlaneLineIntersection(const osg::Vec4d& plane,
                                    const osg::LineSegment& line,
                                    osg::Vec3d& isect)
pixhawk's avatar
pixhawk committed
612
{
613 614
    osg::Vec3d lineStart = line.start();
    osg::Vec3d lineEnd = line.end();
615

616 617 618
    const double deltaX = lineEnd.x() - lineStart.x();
    const double deltaY = lineEnd.y() - lineStart.y();
    const double deltaZ = lineEnd.z() - lineStart.z();
619

620 621 622
    const double denominator = plane[0] * deltaX
                               + plane[1] * deltaY
                               + plane[2] * deltaZ;
623
    if (!denominator) {
624
        return false;
pixhawk's avatar
pixhawk committed
625 626
    }

627 628 629 630
    const double C = (plane[0] * lineStart.x()
                      + plane[1] * lineStart.y()
                      + plane[2] * lineStart.z()
                      + plane[3]) / denominator;
pixhawk's avatar
pixhawk committed
631

632 633 634 635 636
    isect.x() = lineStart.x() - deltaX * C;
    isect.y() = lineStart.y() - deltaY * C;
    isect.z() = lineStart.z() - deltaZ * C;

    return true;
pixhawk's avatar
pixhawk committed
637
}