QMap3DWidget.cc 19.9 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 QMap3DWidget.
 *
 *   @author Lionel Heng <hengli@student.ethz.ch>
 *
 */

lm's avatar
lm committed
32 33
#include "QMap3DWidget.h"

34
#include <QCheckBox>
lm's avatar
lm committed
35 36
#include <sys/time.h>

pixhawk's avatar
pixhawk committed
37
//#include "QGCGlut.h"
38
#include "CheetahModel.h"
lm's avatar
lm committed
39 40
#include "UASManager.h"
#include "UASInterface.h"
lm's avatar
lm committed
41
#include "QGC.h"
lm's avatar
lm committed
42 43

QMap3DWidget::QMap3DWidget(QWidget* parent)
44 45 46
     : Q3DWidget(parent)
     , uas(NULL)
     , lastRedrawTime(0.0)
47
     , displayGrid(true)
48
     , displayImagery(false)
49
     , displayTrail(false)
50 51 52
     , lockCamera(true)
     , updateLastUnlockedPose(true)
     , displayTarget(false)
lm's avatar
lm committed
53
     , displayWaypoints(true)
54
     , imagery(0)
lm's avatar
lm committed
55 56 57
{
    setFocusPolicy(Qt::StrongFocus);

58
    initialize(10, 10, 1000, 900, 15.0f);
59
    setCameraParams(0.05f, 0.5f, 0.01f, 0.5f, 30.0f, 0.01f, 1000000.0f);
lm's avatar
lm committed
60 61

    setDisplayFunc(display, this);
62
    setMouseFunc(mouse, this);
lm's avatar
lm committed
63 64
    addTimerFunc(100, timer, this);

65
    buildLayout();
lm's avatar
lm committed
66 67 68 69 70 71 72 73 74 75

    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)),
            this, SLOT(setActiveUAS(UASInterface*)));
}

QMap3DWidget::~QMap3DWidget()
{

}

76
void
77
QMap3DWidget::buildLayout(void)
78
{
79 80 81 82 83 84 85 86
    QCheckBox* gridCheckBox = new QCheckBox(this);
    gridCheckBox->setText("Grid");
    gridCheckBox->setChecked(displayGrid);

    QCheckBox* trailCheckBox = new QCheckBox(this);
    trailCheckBox->setText("Trail");
    trailCheckBox->setChecked(displayTrail);

lm's avatar
lm committed
87 88 89 90
    QCheckBox* waypointsCheckBox = new QCheckBox(this);
    waypointsCheckBox->setText("Waypoints");
    waypointsCheckBox->setChecked(displayWaypoints);

91 92 93
    QLabel* imageryLabel = new QLabel(this);
    imageryLabel->setText("Imagery");

94
    imageryComboBox = new QComboBox(this);
95 96 97
    imageryComboBox->addItem("None");
    imageryComboBox->addItem("Map (Google)");
    imageryComboBox->addItem("Satellite (Google)");
98
    imageryComboBox->addItem("Satellite (Swisstopo)");
99

100 101 102 103 104 105 106
    QPushButton* recenterButton = new QPushButton(this);
    recenterButton->setText("Recenter Camera");

    QCheckBox* lockCameraCheckBox = new QCheckBox(this);
    lockCameraCheckBox->setText("Lock Camera");
    lockCameraCheckBox->setChecked(lockCamera);

107 108
    //positionLabel = new QLabel(this);
    //positionLabel->setText(tr("Waiting for first position update.. "));
pixhawk's avatar
pixhawk committed
109

110 111 112 113 114
    QGridLayout* layout = new QGridLayout(this);
    layout->setMargin(0);
    layout->setSpacing(2);
    layout->addWidget(gridCheckBox, 1, 0);
    layout->addWidget(trailCheckBox, 1, 1);
lm's avatar
lm committed
115 116
    layout->addWidget(waypointsCheckBox, 1, 2);
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 1, 3);
117 118 119 120 121
    layout->addWidget(imageryLabel, 1, 4);
    layout->addWidget(imageryComboBox, 1, 5);
    layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 1, 6);
    layout->addWidget(recenterButton, 1, 7);
    layout->addWidget(lockCameraCheckBox, 1, 8);
122 123
    layout->setRowStretch(0, 100);
    layout->setRowStretch(1, 1);
124 125
    layout->setColumnStretch(3, 50);
    layout->setColumnStretch(6, 50);
126
    setLayout(layout);
127

128 129 130 131
    connect(gridCheckBox, SIGNAL(stateChanged(int)),
            this, SLOT(showGrid(int)));
    connect(trailCheckBox, SIGNAL(stateChanged(int)),
            this, SLOT(showTrail(int)));
132 133
    connect(imageryComboBox, SIGNAL(currentIndexChanged(const QString &)),
            this, SLOT(showImagery(const QString &)));
134 135 136
    connect(recenterButton, SIGNAL(clicked()), this, SLOT(recenterCamera()));
    connect(lockCameraCheckBox, SIGNAL(stateChanged(int)),
            this, SLOT(toggleLockCamera(int)));
137 138
}

lm's avatar
lm committed
139 140 141 142 143 144 145
void
QMap3DWidget::display(void* clientData)
{
    QMap3DWidget* map3d = reinterpret_cast<QMap3DWidget *>(clientData);
    map3d->displayHandler();
}

146 147 148 149 150 151 152


//void QMap3DWidget::paintEvent(QPaintEvent *event)
//{
//    Q_UNUSED(event);
//}

lm's avatar
lm committed
153 154 155
void
QMap3DWidget::displayHandler(void)
{
lm's avatar
lm committed
156
    if (cheetahModel.data() == 0)
lm's avatar
lm committed
157 158 159 160 161
    {
        cheetahModel.reset(new CheetahModel);
        cheetahModel->init(1.0f, 1.0f, 1.0f);
    }

162 163 164
    float robotX = 0.0f, robotY = 0.0f, robotZ = 0.0f;
    float robotRoll = 0.0f, robotPitch = 0.0f, robotYaw = 0.0f;
    if (uas != NULL)
lm's avatar
lm committed
165
    {
166 167 168 169 170 171
        robotX = uas->getLocalX();
        robotY = uas->getLocalY();
        robotZ = uas->getLocalZ();
        robotRoll = uas->getRoll();
        robotPitch = uas->getPitch();
        robotYaw = uas->getYaw();
lm's avatar
lm committed
172 173
    }

174
    if (updateLastUnlockedPose && uas != NULL)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    {
        lastUnlockedPose.x = robotX;
        lastUnlockedPose.y = robotY;
        lastUnlockedPose.z = robotZ;

        camOffset.x = 0.0f;
        camOffset.y = 0.0f;
        camOffset.z = 0.0f;

        updateLastUnlockedPose = false;
    }

    if (!lockCamera)
    {
        camOffset.x = robotX - lastUnlockedPose.x;
        camOffset.y = robotY - lastUnlockedPose.y;
        camOffset.z = robotZ - lastUnlockedPose.z;
    }
193

lm's avatar
lm committed
194
    // turn on smooth lines
195
    makeCurrent();
lm's avatar
lm committed
196 197 198 199 200 201 202 203 204
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // clear window
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

205 206 207
    glPushMatrix();
    glTranslatef(camOffset.x, camOffset.y, camOffset.z);

lm's avatar
lm committed
208
    // draw Cheetah model
209
    drawPlatform(robotRoll, robotPitch, robotYaw);
lm's avatar
lm committed
210

211 212
    if (displayGrid)
    {
213
        drawGrid(-camOffset.x, -camOffset.y, robotZ);
214
    }
lm's avatar
lm committed
215

216 217 218 219
    if (displayTrail)
    {
        drawTrail(robotX, robotY, robotZ);
    }
220

221 222 223 224 225
    if (displayTarget)
    {
        drawTarget(robotX, robotY, robotZ);
    }

lm's avatar
lm committed
226 227 228 229 230
    if (displayWaypoints)
    {
        drawWaypoints();
    }

231 232
    if (displayImagery)
    {
233
        drawImagery(robotX, robotY, robotZ, "32T", true);
234 235
    }

236 237
    glPopMatrix();

238
    // switch to 2D
239 240
    setDisplayMode2D();

241 242
    drawLegend();

243
    // display pose information
244
    glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
245 246
    glBegin(GL_POLYGON);
    glVertex2f(0.0f, 0.0f);
247 248
    glVertex2f(0.0f, 30.0f);
    glVertex2f(getWindowWidth(), 30.0f);
249 250
    glVertex2f(getWindowWidth(), 0.0f);
    glEnd();
251 252 253 254 255 256 257
    glColor4f(0.1f, 0.1f, 0.1f, 1.0f);
    glBegin(GL_POLYGON);
    glVertex2f(0.0f, getWindowHeight());
    glVertex2f(0.0f, getWindowHeight() - 25.0f);
    glVertex2f(getWindowWidth(), getWindowHeight() - 25.0f);
    glVertex2f(getWindowWidth(), getWindowHeight());
    glEnd();
258

259 260
    glFlush();

261 262 263
    std::pair<float,float> mouseWorldCoords =
            getPositionIn3DMode(getMouseX(), getMouseY());

264 265 266 267 268 269
    // QT QPAINTER OPENGL PAINTING

    QPainter painter;
    painter.begin(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
270
    paintText(QString("x = %1 y = %2 z = %3 r = %4 p = %5 y = %6 Cursor [%7 %8]").arg(robotX, 0, 'f', 2).arg(robotY, 0, 'f', 2).arg(robotZ, 0, 'f', 2).arg(robotRoll, 0, 'f', 2).arg(robotPitch, 0, 'f', 2).arg(robotYaw, 0, 'f', 2).arg( mouseWorldCoords.first + robotX, 0, 'f', 2).arg( mouseWorldCoords.second + robotY, 0, 'f', 2),
271
              QColor(255, 255, 255),
272
              11,
273 274 275
              5,
              5,
              &painter);
276
    painter.end();
277
}
278

279
void QMap3DWidget::drawWaypoints(void) const
lm's avatar
lm committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
{
    if (uas)
    {
        const QVector<Waypoint*>& list = uas->getWaypointManager().getWaypointList();
        QColor color;

        QPointF lastWaypoint;

        for (int i = 0; i < list.size(); i++)
        {
            QPointF in(list.at(i)->getX(), list.at(i)->getY());
            // Transform from world to body coordinates
            //in = metricWorldToBody(in);

            // DRAW WAYPOINT
            float waypointRadius = 0.1f;// = vwidth / 20.0f * 2.0f;


            // Select color based on if this is the current waypoint
            if (list.at(i)->getCurrent())
            {
                color = QGC::colorCyan;//uas->getColor();

            }
            else
            {
                color = uas->getColor();

            }

            //float radius = (waypointSize/2.0f) * 0.8 * (1/sqrt(2.0f));
            // Draw yaw
            // Draw sphere





            static double radius = 0.2;

            glPushMatrix();
            glTranslatef(in.x() - uas->getLocalX(), in.y() - uas->getLocalY(), 0.0f);
322
            glColor3f(1.0f, 0.3f, 0.3f);
lm's avatar
lm committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            glLineWidth(1.0f);

            wireSphere(radius, 10, 10);

            glPopMatrix();

            // DRAW CONNECTING LINE
            // Draw line from last waypoint to this one
            if (!lastWaypoint.isNull())
            {
                // OpenGL line
            }
            lastWaypoint = in;
        }
    }
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
void
QMap3DWidget::drawLegend(void)
{
    // draw marker outlines
    glColor3f(1.0f, 1.0f, 1.0f);
    glLineWidth(3.0f);
    glBegin(GL_LINES);
    glVertex2f(20.0f, 60.0f);
    glVertex2f(20.0f, 80.0f);
    glVertex2f(20.0f, 70.0f);
    glVertex2f(100.0f, 70.0f);
    glVertex2f(100.0f, 60.0f);
    glVertex2f(100.0f, 80.0f);
    glEnd();

    // draw markers
    glColor3f(0.0f, 0.0f, 0.0f);
    glLineWidth(1.5f);
    glBegin(GL_LINES);
    glVertex2f(20.0f, 60.0f);
    glVertex2f(20.0f, 80.0f);
    glVertex2f(20.0f, 70.0f);
    glVertex2f(100.0f, 70.0f);
    glVertex2f(100.0f, 60.0f);
    glVertex2f(100.0f, 80.0f);
    glEnd();

    float f = windowHeight / 2.0f / tanf(d2r(cameraParams.cameraFov / 2.0f));
    float dist = cameraPose.distance / f * 80.0f;

    QPainter painter;
    painter.begin(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);

    QColor rgb(255, 255, 255);
    if (imageryComboBox->currentText().compare("Map (Google)") == 0)
    {
        rgb.setRgb(0, 0, 0);
    }

    paintText(QString("%1 m").arg(dist, 0, 'f', 2),
              rgb,
              10,
              25,
              getWindowHeight() - 65,
              &painter);
387
    painter.end();
388 389
}

390 391 392
void
QMap3DWidget::paintText(QString text, QColor color, float fontSize,
                        float refX, float refY, QPainter* painter) const
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
{
    QPen prevPen = painter->pen();

    float pPositionX = refX;
    float pPositionY = refY;

    QFont font("Bitstream Vera Sans");
    // Enforce minimum font size of 5 pixels
    int fSize = qMax(5, (int)(fontSize));
    font.setPixelSize(fSize);

    QFontMetrics metrics = QFontMetrics(font);
    int border = qMax(4, metrics.leading());
    QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
                                      Qt::AlignLeft | Qt::TextWordWrap, text);
    painter->setPen(color);
    painter->setFont(font);
    painter->setRenderHint(QPainter::TextAntialiasing);
    painter->drawText(pPositionX, pPositionY,
                      rect.width(), rect.height(),
                      Qt::AlignCenter | Qt::TextWordWrap, text);
    painter->setPen(prevPen);
lm's avatar
lm committed
415 416
}

417 418 419
void
QMap3DWidget::mouse(Qt::MouseButton button, MouseState state,
                    int32_t x, int32_t y, void* clientData)
lm's avatar
lm committed
420
{
421 422 423 424 425 426 427 428 429
    QMap3DWidget* map3d = reinterpret_cast<QMap3DWidget *>(clientData);
    map3d->mouseHandler(button, state, x, y);
}

void
QMap3DWidget::mouseHandler(Qt::MouseButton button, MouseState state,
                           int32_t x, int32_t y)
{
    if (button == Qt::RightButton && state == MOUSE_STATE_DOWN)
lm's avatar
lm committed
430
    {
431
        QMenu menu(this);
432
        QAction* targetAction = menu.addAction(tr("Mark as Target"));
433 434
        connect(targetAction, SIGNAL(triggered()), this, SLOT(markTarget()));
        menu.exec(mapToGlobal(QPoint(x, y)));
lm's avatar
lm committed
435 436 437 438 439 440 441 442 443 444 445 446 447
    }
}

void
QMap3DWidget::timer(void* clientData)
{
    QMap3DWidget* map3d = reinterpret_cast<QMap3DWidget *>(clientData);
    map3d->timerHandler();
}

void
QMap3DWidget::timerHandler(void)
{
448 449 450 451 452
    if (imagery.isNull())
    {
        imagery.reset(new Imagery);
    }

lm's avatar
lm committed
453 454 455
    double timeLapsed = getTime() - lastRedrawTime;
    if (timeLapsed > 0.1)
    {
456
        imagery->update();
lm's avatar
lm committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
        forceRedraw();
        lastRedrawTime = getTime();
    }
    addTimerFunc(100, timer, this);
}

double
QMap3DWidget::getTime(void) const
{
     struct timeval tv;

     gettimeofday(&tv, NULL);

     return static_cast<double>(tv.tv_sec) +
             static_cast<double>(tv.tv_usec) / 1000000.0;
}
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
/**
 *
 * @param uas the UAS/MAV to monitor/display with the HUD
 */
void QMap3DWidget::setActiveUAS(UASInterface* uas)
{
    if (this->uas != NULL && this->uas != uas)
    {
        // Disconnect any previously connected active MAV
        //disconnect(uas, SIGNAL(valueChanged(UASInterface*,QString,double,quint64)), this, SLOT(updateValue(UASInterface*,QString,double,quint64)));
    }

    this->uas = uas;
}

void
QMap3DWidget::markTarget(void)
{
    std::pair<float,float> mouseWorldCoords =
            getPositionIn3DMode(getLastMouseX(), getLastMouseY());

    float robotX = 0.0f, robotY = 0.0f, robotZ = 0.0f;
    if (uas != NULL)
    {
        robotX = uas->getLocalX();
        robotY = uas->getLocalY();
        robotZ = uas->getLocalZ();
    }

    targetPosition.x = mouseWorldCoords.first + robotX;
    targetPosition.y = mouseWorldCoords.second + robotY;
    targetPosition.z = robotZ;

    displayTarget = true;

509 510 511 512 513
    if (uas)
    {
        uas->setTargetPosition(targetPosition.x, targetPosition.y,
                               targetPosition.z, 0.0f);
    }
514 515
}

516 517 518 519 520 521 522 523 524 525 526 527 528
void
QMap3DWidget::showGrid(int32_t state)
{
    if (state == Qt::Checked)
    {
        displayGrid = true;
    }
    else
    {
        displayGrid = false;
    }
}

529 530 531 532 533 534 535 536 537 538 539
void
QMap3DWidget::showImagery(const QString& text)
{
    if (text.compare("None") == 0)
    {
        displayImagery = false;
    }
    else
    {
        if (text.compare("Map (Google)") == 0)
        {
540
            imagery->setImageryType(Imagery::GOOGLE_MAP);
541 542 543
        }
        else if (text.compare("Satellite (Google)") == 0)
        {
544 545 546 547 548
            imagery->setImageryType(Imagery::GOOGLE_SATELLITE);
        }
        else if (text.compare("Satellite (Swisstopo)") == 0)
        {
            imagery->setImageryType(Imagery::SWISSTOPO_SATELLITE);
549 550 551 552 553 554
        }
        displayImagery = true;
    }
}


555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
void
QMap3DWidget::showTrail(int32_t state)
{
    if (state == Qt::Checked)
    {
        if (!displayTrail)
        {
            trail.clear();
        }

        displayTrail = true;
    }
    else
    {
        displayTrail = false;
    }
}

void
QMap3DWidget::recenterCamera(void)
{
576 577
    updateLastUnlockedPose = true;

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    recenter();
}

void
QMap3DWidget::toggleLockCamera(int32_t state)
{
    if (state == Qt::Checked)
    {
        lockCamera = true;
    }
    else
    {
        lockCamera = false;
    }
}

void
595
QMap3DWidget::drawPlatform(float roll, float pitch, float yaw) const
596 597 598
{
    glPushMatrix();

599 600 601
    glRotatef(yaw * 180.0f / M_PI, 0.0f, 0.0f, 1.0f);
    glRotatef(pitch * 180.0f / M_PI, 0.0f, 1.0f, 0.0f);
    glRotatef(roll * 180.0f / M_PI, 1.0f, 0.0f, 0.0f);
602 603

    glLineWidth(3.0f);
604 605 606

    // X AXIS
    glColor3f(1.0f, 0.0f, 0.0f);
607 608 609 610 611
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.3f, 0.0f, 0.0f);
    glEnd();

612 613 614 615 616 617 618 619 620 621 622 623 624 625
    // Y AXIS
    glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.15f, 0.0f);
    glEnd();

    // Z AXIS
    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_LINES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 0.15f);
    glEnd();

626 627 628 629 630 631
    cheetahModel->draw();

    glPopMatrix();
}

void
632
QMap3DWidget::drawGrid(float x, float y, float z) const
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
{
    float radius = 10.0f;
    float resolution = 0.25f;

    glPushMatrix();

    // draw a 20m x 20m grid with 0.25m resolution
    glColor3f(0.5f, 0.5f, 0.5f);
    for (float i = -radius; i <= radius; i += resolution)
    {
        if (fabsf(i - roundf(i)) < 0.01f)
        {
            glLineWidth(2.0f);
        }
        else
        {
            glLineWidth(0.25f);
        }

        glBegin(GL_LINES);
653 654 655 656
        glVertex3f(x + i, y - radius, -z);
        glVertex3f(x + i, y + radius, -z);
        glVertex3f(x - radius, y + i, -z);
        glVertex3f(x + radius, y + i, -z);
657 658 659 660 661 662
        glEnd();
    }

    glPopMatrix();
}

663
void
664 665
QMap3DWidget::drawImagery(double originX, double originY, double originZ,
                          const QString& zone, bool prefetch) const
666 667 668 669
{
    glPushMatrix();
    glEnable(GL_BLEND);

670
    glTranslatef(0, 0, -originZ);
671

672
    double viewingRadius = cameraPose.distance / 4000.0 * 3000.0;
673 674 675 676 677
    if (viewingRadius < 100.0)
    {
        viewingRadius = 100.0;
    }

678 679 680 681 682 683 684 685 686 687 688 689
    double minResolution = 0.25;
    double centerResolution = cameraPose.distance / 100.0;
    double maxResolution = 1048576.0;

    if (imageryComboBox->currentText().compare("Map (Google)") == 0)
    {
        minResolution = 0.25;
    }
    else if (imageryComboBox->currentText().compare("Satellite (Google)") == 0)
    {
        minResolution = 0.5;
    }
690 691 692 693 694
    else if (imageryComboBox->currentText().compare("Satellite (Swisstopo)") == 0)
    {
        minResolution = 0.25;
        maxResolution = 0.25;
    }
695 696 697 698 699 700 701 702 703 704 705

    double resolution = minResolution;
    while (resolution * 2.0 < centerResolution)
    {
        resolution *= 2.0;
    }
    if (resolution > maxResolution)
    {
        resolution = maxResolution;
    }

706 707
    imagery->draw3D(viewingRadius, resolution, originX, originY,
                    cameraPose.xOffset, cameraPose.yOffset, zone);
708 709 710 711 712 713 714

    if (prefetch)
    {
        if (resolution / 2.0 >= minResolution)
        {
            imagery->prefetch3D(viewingRadius / 2.0, resolution / 2.0,
                                originX, originY,
715
                                cameraPose.xOffset, cameraPose.yOffset, zone);
716 717 718 719 720
        }
        if (resolution * 2.0 <= maxResolution)
        {
            imagery->prefetch3D(viewingRadius * 2.0, resolution * 2.0,
                                originX, originY,
721
                                cameraPose.xOffset, cameraPose.yOffset, zone);
722 723 724 725 726 727 728
        }
    }

    glDisable(GL_BLEND);
    glPopMatrix();
}

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
void
QMap3DWidget::drawTrail(float x, float y, float z)
{
    bool addToTrail = false;
    if (trail.size() > 0)
    {
        if (fabsf(x - trail[trail.size() - 1].x) > 0.01f ||
            fabsf(y - trail[trail.size() - 1].y) > 0.01f ||
            fabsf(z - trail[trail.size() - 1].z) > 0.01f)
        {
            addToTrail = true;
        }
    }
    else
    {
        addToTrail = true;
    }

    if (addToTrail)
    {
        Pose3D p = {x, y, z};
        if (trail.size() == trail.capacity())
        {
            memcpy(trail.data(), trail.data() + 1,
                   (trail.size() - 1) * sizeof(Pose3D));
            trail[trail.size() - 1] = p;
        }
        else
        {
            trail.append(p);
        }
    }

    glColor3f(1.0f, 0.0f, 0.0f);
    glLineWidth(1.0f);
    glBegin(GL_LINE_STRIP);
    for (int32_t i = 0; i < trail.size(); ++i)
    {
        glVertex3f(trail[i].x - x, trail[i].y - y, trail[i].z - z);
    }
    glEnd();
}
771 772

void
773
QMap3DWidget::drawTarget(float x, float y, float z) const
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
{
    static double radius = 0.2;
    static bool expand = true;

    if (radius < 0.1)
    {
        expand = true;
    }
    else if (radius > 0.25)
    {
        expand = false;
    }

    glPushMatrix();
    glTranslatef(targetPosition.x - x, targetPosition.y - y, 0.0f);
    glColor3f(0.0f, 0.7f, 1.0f);
    glLineWidth(1.0f);
pixhawk's avatar
pixhawk committed
791

lm's avatar
lm committed
792
    wireSphere(radius, 10, 10);
793 794 795 796 797 798 799 800 801 802 803 804

    if (expand)
    {
        radius += 0.02;
    }
    else
    {
        radius -= 0.02;
    }

    glPopMatrix();
}