summaryrefslogtreecommitdiffhomepage
path: root/pose-widget/pose-widget.cpp
blob: 3ad475cf8ccb1c1c3d6a3ad918813e4faa872828 (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
68
69
70
71
72
73
/* 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;

    constexpr double scale = .75;
    int w = img.width(), h = iround(img.height()*scale);

    QTransform t;

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

    constexpr double z_scale = 1./250;
    constexpr double xy_scale = .0075;
    double xy = std::sqrt(w*w + h*h) * xy_scale;
    double sx = clamp(.4 + -z * z_scale, .1, 2), sy = sx * 1/scale;

    t.scale(sx, sy);

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

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

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

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

QSize pose_widget::sizeHint() const
{
    return { 1 << 16, 1 << 16 };
}

} // ns pose_widget_impl