MAV2DIcon.cc 10.7 KB
Newer Older
1 2 3
#include "MAV2DIcon.h"
#include <QPainter>

4 5
#include <qmath.h>

6 7
#include "QGC.h"

8 9 10 11 12 13 14 15 16
MAV2DIcon::MAV2DIcon(mapcontrol::MapGraphicItem* map,mapcontrol::OPMapWidget* parent, UASInterface* uas, int radius, int type)
    : UAVItem(map,parent),
    yaw(0.0f),
    radius(radius),
    type(type),
    airframe(uas->getAirframe()),
    iconColor(uas->getColor()),
    selected(uas->getSelected()),
    uasid(uas->getUASID())
17
{
18
    //connect
19
    size = QSize(radius, radius);
20 21
    mypen = new QPen(uas->getColor());
    drawIcon(mypen);
22 23
}

24 25 26 27 28 29 30 31
MAV2DIcon::MAV2DIcon(mapcontrol::MapGraphicItem* map, mapcontrol::OPMapWidget* parent, qreal lat, qreal lon, qreal alt, QPen* pen)
    : UAVItem(map,parent),
    radius(20),
    type(0),
    airframe(0),
    iconColor(Qt::yellow),
    selected(false),
    uasid(0)
32
{
33 34 35 36 37 38 39 40 41
    if (pen == NULL)
    {
        mypen = new QPen(Qt::red);
        drawIcon(mypen);
    }
    else
    {
        drawIcon(pen);
    }
42 43 44 45 46
    size = QSize(radius, radius);
}

MAV2DIcon::~MAV2DIcon()
{
47
    //delete pic;
48 49 50
}

void MAV2DIcon::setPen(QPen* pen)
51
{
52 53 54 55 56
    if (pen != NULL)
    {
        mypen = pen;
        drawIcon(pen);
    }
57 58
}

59 60 61 62 63 64
void MAV2DIcon::setSelectedUAS(bool selected)
{
    this->selected = selected;
    drawIcon(mypen);
}

65
/**
66 67
 * Yaw changes of less than ±15 degrees are ignored.
 *
68
 * @param yaw in radians, 0 = north, positive = clockwise
69
 */
70
void MAV2DIcon::setYaw(float yaw)
71
{
pixhawk's avatar
pixhawk committed
72
    //qDebug() << "MAV2Icon" << yaw;
73
    float diff = fabs(yaw - this->yaw);
74
    while (diff > (float)M_PI) {
75
        diff -= (float)M_PI;
76 77
    }

78
    if (diff > 0.1f) {
79 80
        this->yaw = yaw;
        drawIcon(mypen);
81
        // FIXME
82
    }
83 84 85 86
}

void MAV2DIcon::drawIcon(QPen* pen)
{
pixhawk's avatar
pixhawk committed
87
    Q_UNUSED(pen);
88 89
    pic.fill(Qt::transparent);
    QPainter painter(&pic);
90 91 92 93
    painter.setRenderHint(QPainter::TextAntialiasing);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::HighQualityAntialiasing);

94 95
    radius = qMin(pic.width(), pic.height());

96 97 98 99
    // Rotate by yaw
    painter.translate(radius/2, radius/2);

    // Draw selected indicator
100
    if (selected) {
101 102 103
        //        qDebug() << "SYSTEM IS NOW SELECTED";
        //        QColor color(Qt::yellow);
        //        color.setAlpha(0.3f);
104
        painter.setBrush(Qt::NoBrush);
105 106 107
        //        QPen selPen(color);
        //        int width = 5;
        //        selPen.setWidth(width);
108 109 110
        QPen pen(Qt::yellow);
        pen.setWidth(2);
        painter.setPen(pen);
111 112 113 114
        painter.drawEllipse(QPoint(0, 0), radius/2-1, radius/2-1);
        //qDebug() << "Painting ellipse" << radius/2-width << width;
        //selPen->deleteLater();
    }
115 116
    drawAirframePolygon(airframe, painter, radius, iconColor, yaw);
}
117

118 119
void MAV2DIcon::drawAirframePolygon(int airframe, QPainter& painter, int radius, QColor& iconColor, float yaw)
{
120
    switch (airframe) {
121
    case UASInterface::QGC_AIRFRAME_PREDATOR:
122
    case UASInterface::QGC_AIRFRAME_REAPER: {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            // DRAW AIRPLANE

            // Rotate 180 degs more since the icon is oriented downwards
            //float yawRotate = (yaw/(float)M_PI)*180.0f + 180.0f+180.0f;

            const float yawDeg = ((yaw/M_PI)*180.0f)+180.0f+180.0f;
            int yawRotate = static_cast<int>(yawDeg) % 360;

            painter.rotate(yawRotate);

            //qDebug() << "ICON SIZE:" << radius;

            float iconSize = radius*0.9f;
            QPolygonF poly(24);
            poly.replace(0, QPointF(0.000000f*iconSize, -0.312500f*iconSize));
            poly.replace(1, QPointF(0.025000f*iconSize, -0.287500f*iconSize));
            poly.replace(2, QPointF(0.037500f*iconSize, -0.237500f*iconSize));
            poly.replace(3, QPointF(0.031250f*iconSize, -0.187500f*iconSize));
            poly.replace(4, QPointF(0.025000f*iconSize, -0.043750f*iconSize));
            poly.replace(5, QPointF(0.500000f*iconSize, -0.025000f*iconSize));
            poly.replace(6, QPointF(0.500000f*iconSize, 0.025000f*iconSize));
            poly.replace(7, QPointF(0.025000f*iconSize, 0.043750f*iconSize));
            poly.replace(8, QPointF(0.025000f*iconSize, 0.162500f*iconSize));
            poly.replace(9, QPointF(0.137500f*iconSize, 0.181250f*iconSize));
            poly.replace(10, QPointF(0.137500f*iconSize, 0.218750f*iconSize));
            poly.replace(11, QPointF(0.025000f*iconSize, 0.206250f*iconSize));
            poly.replace(12, QPointF(-0.025000f*iconSize, 0.206250f*iconSize));
            poly.replace(13, QPointF(-0.137500f*iconSize, 0.218750f*iconSize));
            poly.replace(14, QPointF(-0.137500f*iconSize, 0.181250f*iconSize));
            poly.replace(15, QPointF(-0.025000f*iconSize, 0.162500f*iconSize));
            poly.replace(16, QPointF(-0.025000f*iconSize, 0.043750f*iconSize));
            poly.replace(17, QPointF(-0.500000f*iconSize, 0.025000f*iconSize));
            poly.replace(18, QPointF(-0.500000f*iconSize, -0.025000f*iconSize));
            poly.replace(19, QPointF(-0.025000f*iconSize, -0.043750f*iconSize));
            poly.replace(20, QPointF(-0.031250f*iconSize, -0.187500f*iconSize));
            poly.replace(21, QPointF(-0.037500f*iconSize, -0.237500f*iconSize));
            poly.replace(22, QPointF(-0.031250f*iconSize, -0.262500f*iconSize));
            poly.replace(23, QPointF(0.000000f*iconSize, -0.312500f*iconSize));

            painter.setBrush(QBrush(iconColor));
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);

            painter.drawPolygon(poly);
            painter.rotate(-yawRotate);
        }
        break;
171
    case UASInterface::QGC_AIRFRAME_MIKROKOPTER:
172
    case UASInterface::QGC_AIRFRAME_CHEETAH: {
173 174 175 176
            // QUADROTOR
            float iconSize = radius*0.9f;
            const float yawDeg = ((yaw/M_PI)*180.0f)+180.0f;
            int yawRotate = static_cast<int>(yawDeg) % 360;
177

178
            painter.rotate(yawRotate);
179

180
            //qDebug() << "ICON SIZE:" << radius;
181

182 183 184 185 186 187 188 189
            QPointF front(0, 0.2);
            front = front *iconSize;
            QPointF left(-0.2, 0);
            left = left * iconSize;
            QPointF right(0.2, 0.0);
            right *= iconSize;
            QPointF back(0, -0.2);
            back *= iconSize;
190

191
            QPolygonF poly(0);
192 193 194



195 196 197 198
            painter.setBrush(QBrush(iconColor));
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);
199

200
            painter.drawPolygon(poly);
201

202 203 204
            painter.drawEllipse(left, radius/4/2, radius/4/2);
            painter.drawEllipse(right, radius/4/2, radius/4/2);
            painter.drawEllipse(back, radius/4/2, radius/4/2);
205

206 207 208 209 210
            painter.setBrush(Qt::red);
            painter.drawEllipse(front, radius/4/2, radius/4/2);
            painter.rotate(-yawRotate);
        }
        break;
211 212 213
    case UASInterface::QGC_AIRFRAME_EASYSTAR:
    case UASInterface::QGC_AIRFRAME_MERLIN:
    case UASInterface::QGC_AIRFRAME_TWINSTAR:
214
    case UASInterface::QGC_AIRFRAME_PTERYX: {
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
            // DRAW AIRPLANE

            const float yawDeg = ((yaw/M_PI)*180.0f)+180.0f;
            int yawRotate = static_cast<int>(yawDeg) % 360;
            painter.rotate(yawRotate);

            //qDebug() << "ICON SIZE:" << radius;

            float iconSize = radius*0.7f;

            QPolygonF poly(32);
            poly.replace(0, QPointF(0.000000f*iconSize, 0.362319f*iconSize));
            poly.replace(1, QPointF(0.018116f*iconSize, 0.340580f*iconSize));
            poly.replace(2, QPointF(0.028986f*iconSize, 0.318841f*iconSize));
            poly.replace(3, QPointF(0.036232f*iconSize, 0.166667f*iconSize));
            poly.replace(4, QPointF(0.326087f*iconSize, 0.170290f*iconSize));
            poly.replace(5, QPointF(0.420290f*iconSize, 0.130435f*iconSize));
            poly.replace(6, QPointF(0.456522f*iconSize, 0.108696f*iconSize));
            poly.replace(7, QPointF(0.500000f*iconSize, 0.000000f*iconSize));
            poly.replace(8, QPointF(0.456522f*iconSize, 0.021739f*iconSize));
            poly.replace(9, QPointF(0.391304f*iconSize, 0.021739f*iconSize));
            poly.replace(10, QPointF(0.028986f*iconSize, 0.021739f*iconSize));
            poly.replace(11, QPointF(0.021739f*iconSize, -0.239130f*iconSize));
            poly.replace(12, QPointF(0.094203f*iconSize, -0.246377f*iconSize));
            poly.replace(13, QPointF(0.123188f*iconSize, -0.268116f*iconSize));
            poly.replace(14, QPointF(0.144928f*iconSize, -0.304348f*iconSize));
            poly.replace(15, QPointF(0.000000f*iconSize, -0.307971f*iconSize));
            poly.replace(16, QPointF(0.000000f*iconSize, -0.307971f*iconSize));
            poly.replace(17, QPointF(-0.144928f*iconSize, -0.304348f*iconSize));
            poly.replace(18, QPointF(-0.123188f*iconSize, -0.268116f*iconSize));
            poly.replace(19, QPointF(-0.094203f*iconSize, -0.246377f*iconSize));
            poly.replace(20, QPointF(-0.021739f*iconSize, -0.239130f*iconSize));
            poly.replace(21, QPointF(-0.028986f*iconSize, 0.021739f*iconSize));
            poly.replace(22, QPointF(-0.391304f*iconSize, 0.021739f*iconSize));
            poly.replace(23, QPointF(-0.456522f*iconSize, 0.021739f*iconSize));
            poly.replace(24, QPointF(-0.500000f*iconSize, 0.000000f*iconSize));
            poly.replace(25, QPointF(-0.456522f*iconSize, 0.108696f*iconSize));
            poly.replace(26, QPointF(-0.420290f*iconSize, 0.130435f*iconSize));
            poly.replace(27, QPointF(-0.326087f*iconSize, 0.170290f*iconSize));
            poly.replace(28, QPointF(-0.036232f*iconSize, 0.166667f*iconSize));
            poly.replace(29, QPointF(-0.028986f*iconSize, 0.318841f*iconSize));
            poly.replace(30, QPointF(-0.018116f*iconSize, 0.340580f*iconSize));
            poly.replace(31, QPointF(-0.000000f*iconSize, 0.362319f*iconSize));

            painter.setBrush(QBrush(iconColor));
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);

            painter.drawPolygon(poly);
            painter.rotate(-yawRotate);
        }
        break;
268 269
    case UASInterface::QGC_AIRFRAME_GENERIC:
    default: {
270
            // GENERIC
271

272
            float yawRotate = (yaw/(float)M_PI)*180.0f + 180.0f;
273

274
            painter.rotate(yawRotate);
275

276
            //qDebug() << "ICON SIZE:" << radius;
277

278 279 280 281 282
            float iconSize = radius*0.9f;
            QPolygonF poly(3);
            poly.replace(0, QPointF(0.0f*iconSize, 0.3f*iconSize));
            poly.replace(1, QPointF(-0.2f*iconSize, -0.2f*iconSize));
            poly.replace(2, QPointF(0.2f*iconSize, -0.2f*iconSize));
283

284 285 286 287
            painter.setBrush(QBrush(iconColor));
            QPen iconPen(Qt::black);
            iconPen.setWidthF(1.0f);
            painter.setPen(iconPen);
288

289 290 291 292
            painter.drawPolygon(poly);
            painter.rotate(-yawRotate);
        }
        break;
293
    }
294
}