diff options
Diffstat (limited to 'pose-widget')
-rw-r--r-- | pose-widget/CMakeLists.txt | 2 | ||||
-rw-r--r-- | pose-widget/ReadMe.txt | 26 | ||||
-rw-r--r-- | pose-widget/images/side1.png | bin | 540324 -> 35513 bytes | |||
-rw-r--r-- | pose-widget/images/side6.png | bin | 543073 -> 35720 bytes | |||
-rw-r--r-- | pose-widget/lang/de_DE.ts | 4 | ||||
-rw-r--r-- | pose-widget/lang/zh_CN.ts | 2 | ||||
-rw-r--r-- | pose-widget/pose-widget.cpp | 488 | ||||
-rw-r--r-- | pose-widget/pose-widget.hpp | 95 |
8 files changed, 149 insertions, 468 deletions
diff --git a/pose-widget/CMakeLists.txt b/pose-widget/CMakeLists.txt index fd109bc3..28dc918b 100644 --- a/pose-widget/CMakeLists.txt +++ b/pose-widget/CMakeLists.txt @@ -1,2 +1,2 @@ otr_module(pose-widget BIN) -target_link_libraries(opentrack-pose-widget) +target_link_libraries(${self}) diff --git a/pose-widget/ReadMe.txt b/pose-widget/ReadMe.txt new file mode 100644 index 00000000..c504ff41 --- /dev/null +++ b/pose-widget/ReadMe.txt @@ -0,0 +1,26 @@ +Hi everyone! + +In the extreme version of OpenTrack-2.3.12, the Octopus pose is still displayed incorrectly. +The pose-widget is responsible for displaying the Octopus pose in OpenTrack. I have fixed this widget. + +Fixed bugs: +- The turns and movements of the Octopussy are now performed truly independently of each other, as it should be in 6DOF. +- When cornering, there is no "gimbal lock" at Pitch = +/- 90 degrees. +- Fixed directions of axes of rotations and positions. Now the Octopus pose displays the actual direction of view on the plane. +- Fixed display of the back (green) surface of the Octopus. Previously, it was displayed mirrored. + +Additional features: +- Applied "perspective projection", the picture becomes more voluminous. +- Added lighting effect from above, with the same purpose. +- Added background fill for the widget. This makes it possible to see the borders of the widget. +- Added X and Y axes. This helps to estimate how far the Octopus is deviated from the center. +- Added [Mirror] checkbox, mirroring positions and rotations. It is often more convenient to observe the Octopussy's mirror pose. +- If before compilation in the file "pose-widget.hpp" include line 19: "#define TEST", then a rectangular frame will be drawn around the Octopus. This is useful when testing a pose-widget to assess distortion and size. + +The corrected pose-widget now displays the Octopus pose correctly and can be used to check OpenTrack settings, sometimes even without launching the flight simulator. + +A video of the corrected pose-widget is available here: + +https://youtu.be/my4_VOwGmq4 + +fixed by GO63-samara <go1@list.ru> <github.com/GO63-samara> 2020 diff --git a/pose-widget/images/side1.png b/pose-widget/images/side1.png Binary files differindex d15c3658..2955bc01 100644 --- a/pose-widget/images/side1.png +++ b/pose-widget/images/side1.png diff --git a/pose-widget/images/side6.png b/pose-widget/images/side6.png Binary files differindex 7338690d..3bae0e50 100644 --- a/pose-widget/images/side6.png +++ b/pose-widget/images/side6.png diff --git a/pose-widget/lang/de_DE.ts b/pose-widget/lang/de_DE.ts new file mode 100644 index 00000000..1552582e --- /dev/null +++ b/pose-widget/lang/de_DE.ts @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE TS> +<TS version="2.1" language="de_DE"> +</TS> diff --git a/pose-widget/lang/zh_CN.ts b/pose-widget/lang/zh_CN.ts index 6401616d..e5ca8aa9 100644 --- a/pose-widget/lang/zh_CN.ts +++ b/pose-widget/lang/zh_CN.ts @@ -1,4 +1,4 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="zh_CN"> </TS> diff --git a/pose-widget/pose-widget.cpp b/pose-widget/pose-widget.cpp index 60d41aa5..ac3aa74a 100644 --- a/pose-widget/pose-widget.cpp +++ b/pose-widget/pose-widget.cpp @@ -6,420 +6,134 @@ */ #include "pose-widget.hpp" -#include "compat/timer.hpp" -#include "compat/sleep.hpp" #include "compat/check-visible.hpp" #include "compat/math.hpp" -#include <cmath> -#include <algorithm> - #include <QPainter> -#include <QPaintEvent> +#include <QtEvents> #include <QDebug> +#include <QQuaternion> +#include <QMatrix4x4> -// XXX this needs rewriting in terms of scanline rendering -sh 20180105 -// see: <https://mikro.naprvyraz.sk/docs/Coding/2/TEXTURE4.TXT> - -using namespace pose_widget_impl; - -pose_transform::pose_transform(QWidget* dst) : - dst(dst), - front(":/images/side1.png", nullptr), - back(":/images/side6.png", nullptr), - image(w, h, QImage::Format_ARGB32), - image2(w, h, QImage::Format_ARGB32), - fresh(false) -{ - image.fill(Qt::transparent); - image2.fill(Qt::transparent); -} +namespace pose_widget_impl { -pose_transform::~pose_transform() +pose_widget::pose_widget(QWidget* parent) : QWidget(parent) { - requestInterruption(); - wait(); -} - -void pose_widget::paintEvent(QPaintEvent* event) -{ - QPainter p(this); - - xform.with_image_lock([&](const QImage& image) - { - p.drawImage(event->rect(), image, QRect(0, 0, pose_transform::w, pose_transform::h)); - }); - - if (!xform.isRunning()) - xform.start(QThread::LowPriority); -} - -void pose_transform::run() -{ - for (;;) - { - if (isInterruptionRequested()) - break; - - { - lock_guard l(mtx); + QPainter p; +#ifdef TEST + //draw rectangle frame around of Octopus, only if TEST defined + p.begin(&front); + p.setPen(QPen(Qt::red, 3, Qt::SolidLine)); + p.drawRect(0, 0, front.width()-1, front.height()-1); + p.end(); - if (fresh) - goto end; - - rotation = rotation_; - translation = translation_; - } - - project_quad_texture(); - -end: - portable::sleep(23); - } -} - -pose_widget::pose_widget(QWidget* parent) : QWidget(parent), xform(this) -{ - rotate_sync(0,0,0, 0,0,0); -} - -pose_widget::~pose_widget() -{ -} - -void pose_widget::rotate_async(double xAngle, double yAngle, double zAngle, double x, double y, double z) -{ - if (!check_is_visible()) - return; - - bool expected = true; - if (xform.fresh.compare_exchange_weak(expected, false)) - { - repaint(); - xform.rotate_async(xAngle, yAngle, zAngle, x, y, z); - } -} - -template<typename F> -void pose_transform::with_rotate(F&& fun, double xAngle, double yAngle, double zAngle, double x, double y, double z) -{ - using std::sin; - using std::cos; - - constexpr double d2r = M_PI / 180; - - euler::euler_t euler(-zAngle * d2r, xAngle * d2r, -yAngle * d2r); - euler::rmat r = euler::euler_to_rmat(euler); - - lock_guard l(mtx); - - for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) - rotation_(i, j) = num(r(i, j)); - - translation_ = vec3(x, y, z); - - fun(); -} + p.begin(&back); + p.setPen(QPen(Qt::darkGreen, 3, Qt::SolidLine)); + p.drawRect(0, 0, back.width()-1, back.height()-1); + p.end(); +#endif -void pose_widget::rotate_sync(double xAngle, double yAngle, double zAngle, double x, double y, double z) -{ - xform.rotate_sync(xAngle, yAngle, zAngle, x, y, z); -} + //draw Octopus shine + shine.fill(QColor(255,255,255)); + p.begin(&shine); + p.setCompositionMode(QPainter::CompositionMode_DestinationIn); + p.drawImage(QPointF(0,0), front); + p.end(); -void pose_transform::rotate_async(double xAngle, double yAngle, double zAngle, double x, double y, double z) -{ - with_rotate([this]() {}, xAngle, yAngle, zAngle, x, y, z); -} + //draw Octopus shadow + shadow.fill(QColor(0,0,0)); + p.begin(&shadow); + p.setCompositionMode(QPainter::CompositionMode_DestinationIn); + p.drawImage(QPointF(0,0), front); + p.end(); -void pose_transform::rotate_sync(double xAngle, double yAngle, double zAngle, double x, double y, double z) -{ - with_rotate([this]() { - rotation = rotation_; - translation = translation_; - project_quad_texture(); - dst->repaint(); - }, xAngle, yAngle, zAngle, x, y, z); + mirror.setFocusPolicy(Qt::NoFocus); } -Triangle::Triangle(const vec2& p1, const vec2& p2, const vec2& p3) +void pose_widget::present(double yaw, double pitch, double roll, double x, double y, double z) { - origin = p1; + T = { x, y, z }; + R = { yaw, pitch, roll }; - v0 = vec2(p3 - p1); - v1 = vec2(p2 - p1); - - dot00 = v0.dot(v0); - dot01 = v0.dot(v1); - dot11 = v1.dot(v1); - - const num denom = dot00 * dot11 - dot01 * dot01; - - invDenom = 1 / denom; + repaint(); } -bool Triangle::barycentric_coords(const vec2& px, vec2& uv, int& i) const +void pose_widget::resizeEvent(QResizeEvent *event) { - i = 0; - const vec2 v2 = px - origin; - const num dot12 = v1.dot(v2); - const num dot02 = v0.dot(v2); - num u = (dot11 * dot02 - dot01 * dot12) * invDenom; - num v = (dot00 * dot12 - dot01 * dot02) * invDenom; - - if (!(u >= 0 && v >= 0)) - return false; + // adapt to widget size + float w = event->size().width(); + float h = event->size().height(); - if (u + v > 1) - { - i = 1; - - u = 1 - u; - v = 1 - v; - } - uv = vec2(u, v); - return u >= 0 && v >= 0 && u + v <= 1; + // move the mirror checkbox in the lower right corner of the widget + mirror.setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + mirror.move(w - mirror.width(), h - mirror.height()); } -std::pair<vec2i, vec2i> pose_transform::get_bounds(const vec2& size) +void pose_widget::paintEvent(QPaintEvent*) { - const int x = size.x(), y = size.y(); - - const vec3 corners[] = { - { -x, -y, 0 }, - { x, -y, 0 }, - { -x, y, 0 }, - { x, y, 0 }, - }; + // widget settings: + constexpr float scale = 0.5; // scale of Octopus height, when x = y = z = 0.0 + constexpr float XYZmax = 50.0; // -XYZmax < x,y,z < +XYZmax (offset the Octopus by one body) + constexpr float Kz = 0.25; // Z scale change limit (simulate camera focus length) - vec2 min(w-1, h-1), max(0, 0); + // get a local copy of input data + auto [ yaw, pitch, roll ] = R; + auto [ x, y, z ] = T; - for (unsigned k = 0; k < 4; k++) - { - const vec2 pt = project(corners[k]) + vec2(w/2, h/2); - - min.x() = std::fmin(min.x(), pt.x()); - min.y() = std::fmin(min.y(), pt.y()); - - max.x() = std::fmax(max.x(), pt.x()); - max.y() = std::fmax(max.y(), pt.y()); - } - - min.x() = clamp(min.x(), 0, w-1); - min.y() = clamp(min.y(), 0, h-1); - max.x() = clamp(max.x(), 0, w-1); - max.y() = clamp(max.y(), 0, h-1); - -#if 0 - { - QPainter p(&image); - p.drawRect(min.x(), min.y(), max.x()-min.x(), max.y()-min.y()); - } -#endif - - return std::make_pair(vec2i(iround(min.x()), iround(min.y())), - vec2i(iround(max.x()), iround(max.y()))); -} - -void pose_transform::project_quad_texture() -{ - vec3 bgcolor; - { - QColor bg = dst->palette().background().color(); - image.fill(bg); - bgcolor = vec3(bg.red(), bg.green(), bg.blue()); - } - - num dir; - vec2 pt[4]; - - vec2i min, max; - - { - constexpr double c = 85/100.; - - const int sx_ = (w - std::max(0, (w - h)/2)) * 5/9; - const int sy_ = (h - std::max(0, (h - w)/2)) * 5/9; - - std::tie(min, max) = get_bounds(vec2(sx_/2.*c, sy_/2)); - - const vec3 dst_corners[] = - { - { -sx_/2. * c, -sy_/2., 0 }, - { sx_/2. * c, -sy_/2., 0 }, - { -sx_/2. * c, sy_/2., 0 }, - { sx_/2. * c, sy_/2., 0 }, - }; - - for (int i = 0; i < 4; i++) - pt[i] = project(dst_corners[i]) + vec2(w/2, h/2); - - { - vec3 foo[3]; - for (int i = 0; i < 3; i++) - foo[i] = project2(dst_corners[i]); - - vec3 p1 = foo[1] - foo[0]; - vec3 p2 = foo[2] - foo[0]; - dir = p1.x() * p2.y() - p1.y() * p2.x(); // Z part of the cross product - } - } - - // rotation of (0, 90, 0) makes it numerically unstable - if (std::fabs(dir) < 1e-3f) - { - lock_guard l(mtx2); - image.swap(image2); - fresh = true; - return; - } - - const QImage& tex = dir < 0 ? back : front; - - Triangle t(pt[0], pt[1], pt[2]); - - const unsigned orig_pitch = tex.bytesPerLine(); - const unsigned dest_pitch = image.bytesPerLine(); - - unsigned char const* restrict_ptr orig = tex.constBits(); - unsigned char* restrict_ptr dest = image.bits(); - - const int orig_depth = tex.depth() / 8; - const int dest_depth = image.depth() / 8; - constexpr int const_depth = 4; - - if (unlikely(orig_depth != const_depth || dest_depth != const_depth)) - { - qDebug() << "pose-widget: octopus must be saved as .png with 32 bits depth"; - qDebug() << "pose-widget: target texture must be ARGB32"; - return; - } - - const vec2u dist(max.x() - min.x(), max.y() - min.y()); - - if (int(uv_vec.size()) < dist.x() * dist.y()) - uv_vec.resize(dist.x() * dist.y()); - - for (int y = 0; y < dist.y(); y++) - for (int x = 0; x < dist.x(); x++) - { - uv_* restrict_ptr uv = &uv_vec[y * dist.x() + x]; - if (!t.barycentric_coords(vec2(x + min.x(), y + min.y()), uv->coords, uv->i)) - uv->i = -1; - } - - const int ow = tex.width(), oh = tex.height(); - - vec2 const origs[2][3] = - { - { - { 0, 0 }, - { ow-1, 0 }, - { 0, oh-1 }, - }, - { - { ow-1, oh-1 }, - vec2(0, oh-1) - vec2(ow-1, oh-1), - vec2(ow-1, 0) - vec2(ow-1, oh-1), - } - }; - - for (int y_ = 0, dy = dist.y(); y_ < dy; y_++) - { - for (int x_ = 0, dx = dist.x(); x_ < dx; x_++) - { - const int y = y_ + min.y(), x = x_ + min.x(); - const uv_* restrict_ptr uv__ = &uv_vec[y_ * dx + x_]; - - if (uv__->i != -1) - { - using uc = unsigned char; - - vec2 const& uv = uv__->coords; - int const i = uv__->i; - - float fx = origs[i][0].x() - + uv.x() * origs[i][2].x() - + uv.y() * origs[i][1].x(); - float fy = origs[i][0].y() - + uv.x() * origs[i][2].y() - + uv.y() * origs[i][1].y(); - -//#define BILINEAR_FILTER - -#if defined BILINEAR_FILTER - const unsigned px_ = fx + 1; - const unsigned py_ = fy + 1; -#endif - const unsigned px = fx; - const unsigned py = fy; - - const unsigned orig_pos = py * orig_pitch + px * const_depth; -#if defined BILINEAR_FILTER - const unsigned orig_pos_ = py_ * orig_pitch + px_ * const_depth; - const unsigned orig_pos__ = py * orig_pitch + px_ * const_depth; - const unsigned orig_pos___ = py_ * orig_pitch + px * const_depth; -#endif - - // 1, 0 -- ax_, ay - // 0, 1 -- ax, ay_ - // 1, 1 -- ax_, ay_ - // 0, 0 -- ax, ay - //const uc alpha = (a1 * ax + a3 * ax_) * ay + (a4 * ax + a2 * ax_) * ay_; - -#if defined BILINEAR_FILTER - const float ax_ = fx - unsigned(fx); - const float ay_ = fy - unsigned(fy); - const float ax = 1 - ax_; - const float ay = 1 - ay_; -#endif - - const unsigned pos = y * dest_pitch + x * const_depth; - - if (orig[orig_pos + 3] == uc(255)) // alpha - for (int k = 0; k < 3; k++) - dest[pos + k] = orig[orig_pos + k]; - else - for (int k = 0; k < 3; k++) - dest[pos + k] = bgcolor(k); - } - } - } + QPainter p(this); + #ifdef TEST + // use antialiasing for correct frame around the Octopus, only if TEST defined + p.setRenderHint(QPainter::Antialiasing, true); + #endif { - lock_guard l2(mtx2); - image.swap(image2); - fresh = true; + p.fillRect(rect(), palette().brush(backgroundRole())); + // draw axes + p.save(); + p.setPen(QPen(Qt::gray, 1, Qt::SolidLine)); + int w = width(), h = height(); + p.drawLine(w/2, 0, w/2, h); + p.drawLine( 0, h/2, w, h/2); + p.restore(); } -} - -vec2 pose_transform::project(const vec3 &point) -{ - using std::fabs; - - vec3 ret = rotation * point; - num z = std::fmax(num(.5), 1 + translation.z()/-80); - num w_ = w, h_ = h; - num x = w_ * translation.x() / 2 / -80; - if (fabs(x) > w_/2) - x = x > 0 ? w_/2 : w_/-2; - num y = h_ * translation.y() / 2 / -80; - if (fabs(y) > h_/2) - y = y > 0 ? h_/2 : h_/-2; - return vec2(z * (ret.x() + x), z * (ret.y() + y)); -} - -vec3 pose_transform::project2(const vec3 &point) -{ - return rotation * point; -} - -template<typename F> -inline void pose_transform::with_image_lock(F&& fun) -{ - lock_guard l(mtx2); - - fun(image2); -} + // check mirror state + if (mirror.checkState() == Qt::Checked) x = -x; + else { yaw = -yaw; roll = -roll; } + y = -y; + + // rotations + QQuaternion q = QQuaternion::fromEulerAngles(pitch, yaw, roll); + QMatrix4x4 m = QMatrix4x4(q.toRotationMatrix()); + + // x and y positions + const float Kxy = (float)front.height() / XYZmax; + QVector3D v(Kxy*x, Kxy*y, 0.0); + v = m.transposed().map(v); + m.translate(v); + + // perspective projection to x-y plane + QTransform t = m.toTransform(1024).translate(-.5 * front.width(), -.5 * front.height()); + + // z position by setViewport + const float mz = scale * height()/front.height()/exp(1.0) * exp(1.0 - z * (Kz/XYZmax)); + p.setViewport(QRect(.5 * width(), .5 * height(), width()*mz, height()*mz)); + + // define forward or backward side by cross product of mapped x and y axes + QPointF point0 = t.map(QPointF(0, 0)); + QPointF x_dir = (t.map(QPointF(1, 0)) -= point0); + QPointF y_dir = (t.map(QPointF(0, 1)) -= point0); + const bool forward = x_dir.ry()*y_dir.rx() - x_dir.rx()*y_dir.ry() < 0 ? true : false; + + // draw red or green Octopus + p.setTransform(t); + p.drawImage(QPointF(0,0), forward ? front : back); + + // top lighting simulation + const float alpha = sin(pitch * M_PI / 180.0); + p.setOpacity(0.333 * fabs(alpha)); + p.drawImage(QPointF(0,0), forward == (alpha >= 0.0) ? shine : shadow); +} + +} // ns pose_widget_impl diff --git a/pose-widget/pose-widget.hpp b/pose-widget/pose-widget.hpp index fa956b47..9152e960 100644 --- a/pose-widget/pose-widget.hpp +++ b/pose-widget/pose-widget.hpp @@ -12,96 +12,33 @@ #include "export.hpp" -#include <tuple> -#include <mutex> -#include <atomic> -#include <vector> - #include <QWidget> -#include <QThread> #include <QImage> +#include <QCheckBox> +//#define TEST namespace pose_widget_impl { -using num = float; -using vec3 = Mat<num, 3, 1>; -using vec2 = Mat<num, 2, 1>; -using vec2i = Mat<int, 2, 1>; -using vec2u = Mat<int, 2, 1>; - -using rmat = Mat<num, 3, 3>; - using namespace euler; -using lock_guard = std::unique_lock<std::mutex>; - -class pose_widget; - -class Triangle { - num dot00, dot01, dot11, invDenom; - vec2 v0, v1, origin; -public: - Triangle(const vec2& p1, const vec2& p2, const vec2& p3); - bool barycentric_coords(const vec2& px, vec2& uv, int& i) const; -}; - -struct pose_transform final : QThread -{ - pose_transform(QWidget* dst); - ~pose_transform(); - - void rotate_async(double xAngle, double yAngle, double zAngle, double x, double y, double z); - void rotate_sync(double xAngle, double yAngle, double zAngle, double x, double y, double z); - - template<typename F> - void with_rotate(F&& fun, double xAngle, double yAngle, double zAngle, double x, double y, double z); - - void run() override; - - vec2 project(const vec3& point); - vec3 project2(const vec3& point); - void project_quad_texture(); - std::pair<vec2i, vec2i> get_bounds(const vec2& size); - - template<typename F> - inline void with_image_lock(F&& fun); - - rmat rotation, rotation_; - vec3 translation, translation_; - - std::mutex mtx, mtx2; - - QWidget* dst; - - QImage front, back; - QImage image, image2; - - struct uv_ - { - vec2 coords; - int i; - }; - - std::vector<uv_> uv_vec; - - std::atomic<bool> fresh; - - static constexpr inline int w = 320, h = 240; -}; - -class OTR_POSE_WIDGET_EXPORT pose_widget final : public QWidget +struct OTR_POSE_WIDGET_EXPORT pose_widget final : QWidget { public: - pose_widget(QWidget *parent = nullptr); - ~pose_widget(); - void rotate_async(double xAngle, double yAngle, double zAngle, double x, double y, double z); - void rotate_sync(double xAngle, double yAngle, double zAngle, double x, double y, double z); - + explicit pose_widget(QWidget *parent = nullptr); + void present(double xAngle, double yAngle, double zAngle, double x, double y, double z); + QCheckBox mirror{"Mirror", this}; private: - pose_transform xform; - void paintEvent(QPaintEvent *event) override; + void resizeEvent(QResizeEvent *event) override; + void paintEvent(QPaintEvent*) override; + + Pose_ R, T; + QImage front{QImage{":/images/side1.png"}.convertToFormat(QImage::Format_ARGB32)}; + QImage back {QImage{":/images/side6.png"}.convertToFormat(QImage::Format_ARGB32) + .mirrored(true,false)}; + QImage shine {QImage{front.width(), front.height(), QImage::Format_ARGB32}}; + QImage shadow{QImage{front.width(), front.height(), QImage::Format_ARGB32}}; }; } -using pose_widget_impl::pose_widget; +using pose_widget = pose_widget_impl::pose_widget; |