summaryrefslogtreecommitdiffhomepage
path: root/pose-widget/pose-widget.cpp
blob: 53ce5811e66231cd572e7b3d768ed80bc5c7a1b4 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Copyright (c) 2013, 2015 Stanislaw Halik <sthalik@misaki.pl>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#include "pose-widget.hpp"
#include "compat/check-visible.hpp"
#include "compat/math.hpp"

#include <QPainter>
#include <QtEvents>

#include <QDebug>
#include <QImage>

namespace pose_widget_impl {

pose_widget::pose_widget(QWidget* parent) : QWidget(parent)
{
}

void pose_widget::present(double yaw, double pitch, double roll, double x, double y, double z)
{
    T = { x, y, z };
    R = { yaw, pitch, roll };

    repaint();
}

void pose_widget::paintEvent(QPaintEvent*)
{
    auto [ yaw, pitch, roll ] = R;
    auto [ x, y, z ] = T;

    const QImage& img = std::fabs(pitch) > 90 || std::fabs(yaw) > 90
                        ? back
                        : front;

    int w = img.width(), h = img.height();

    QTransform t;

    t.translate(w*.5, h*.5);

    constexpr double z_scale = 1./100;
    constexpr double xy_scale = .0075;
    double xy = std::sqrt(w*w + h*h) * xy_scale;

    double s = clamp(.5 + -z * z_scale, .5, 2);
    t.scale(s, s);

    t.rotate(pitch, Qt::XAxis);
    t.rotate(yaw, Qt::YAxis);
    t.rotate(roll, Qt::ZAxis);

    t.translate(x * xy / s, y * xy / s);

    t.translate(w*-.5, h*-.5);

    QPainter p(this);
    p.setTransform(t);
    p.drawImage(rect(), img);
}

} // ns pose_widget_impl