summaryrefslogtreecommitdiffhomepage
path: root/tracker-pt/ftnoir_tracker_pt_dialog.cpp
blob: 160eb8315d424f1f692dcabb8031958a4f682fed (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
268
269
270
271
272
273
274
275
276
277
278
279
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
/* Copyright (c) 2012 Patrick Ruoff
 * Copyright (c) 2014-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 "ftnoir_tracker_pt_dialog.h"
#include "compat/math.hpp"
#include "video/camera.hpp"

#include <QString>
#include <QtGlobal>
#include <QDebug>

using namespace options;

static void init_resources() { Q_INIT_RESOURCE(tracker_pt_base); }

namespace pt_impl {

TrackerDialog_PT::TrackerDialog_PT(const QString& module_name) :
    s(module_name),
    tracker(nullptr),
    timer(this),
    trans_calib(1, 2)
{
    init_resources();

    ui.setupUi(this);

    for (const QString& str : video::camera_names())
        ui.camdevice_combo->addItem(str);

    tie_setting(s.camera_name, ui.camdevice_combo);
    tie_setting(s.cam_res_x, ui.res_x_spin);
    tie_setting(s.cam_res_y, ui.res_y_spin);
    tie_setting(s.cam_fps, ui.fps_spin);
    tie_setting(s.use_mjpeg, ui.use_mjpeg);

    tie_setting(s.threshold_slider, ui.threshold_slider);

    tie_setting(s.min_point_size, ui.mindiam_spin);
    tie_setting(s.max_point_size, ui.maxdiam_spin);

    tie_setting(s.clip_by, ui.clip_bheight_spin);
    tie_setting(s.clip_bz, ui.clip_blength_spin);
    tie_setting(s.clip_ty, ui.clip_theight_spin);
    tie_setting(s.clip_tz, ui.clip_tlength_spin);

    tie_setting(s.cap_x, ui.cap_width_spin);
    tie_setting(s.cap_y, ui.cap_height_spin);
    tie_setting(s.cap_z, ui.cap_length_spin);

    tie_setting(s.m01_x, ui.m1x_spin);
    tie_setting(s.m01_y, ui.m1y_spin);
    tie_setting(s.m01_z, ui.m1z_spin);

    tie_setting(s.m02_x, ui.m2x_spin);
    tie_setting(s.m02_y, ui.m2y_spin);
    tie_setting(s.m02_z, ui.m2z_spin);

    tie_setting(s.t_MH_x, ui.tx_spin);
    tie_setting(s.t_MH_y, ui.ty_spin);
    tie_setting(s.t_MH_z, ui.tz_spin);

    tie_setting(s.fov, ui.fov);

    tie_setting(s.active_model_panel, ui.model_tabs);

    tie_setting(s.dynamic_pose, ui.dynamic_pose);
    tie_setting(s.init_phase_timeout, ui.init_phase_timeout);

    tie_setting(s.auto_threshold, ui.auto_threshold);

    connect(ui.tcalib_button,SIGNAL(toggled(bool)), this, SLOT(startstop_trans_calib(bool)));

    connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK()));
    connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel()));

    connect(ui.camdevice_combo, &QComboBox::currentTextChanged, this, &TrackerDialog_PT::set_camera_settings_available);
    set_camera_settings_available(ui.camdevice_combo->currentText());
    connect(ui.camera_settings, &QPushButton::clicked, this, &TrackerDialog_PT::show_camera_settings);

    connect(&timer, &QTimer::timeout, this, &TrackerDialog_PT::poll_tracker_info_impl);
    timer.setInterval(250);

    connect(&calib_timer, &QTimer::timeout, this, &TrackerDialog_PT::trans_calib_step);
    calib_timer.setInterval(35);

    poll_tracker_info_impl();

    constexpr pt_color_type color_types[] = {
        pt_color_bt709,
        pt_color_hardware,
        pt_color_red_only,
        pt_color_green_only,
        pt_color_blue_only,
        pt_color_red_chromakey,
        pt_color_green_chromakey,
        pt_color_blue_chromakey,
        pt_color_cyan_chromakey,
        pt_color_yellow_chromakey,
        pt_color_magenta_chromakey,
    };

    for (unsigned k = 0; k < std::size(color_types); k++)
        ui.blob_color->setItemData(k, int(color_types[k]));

    tie_setting(s.blob_color, ui.blob_color);

    tie_setting(s.threshold_slider, ui.threshold_value_display, [this](const slider_value& val) {
        return threshold_display_text(int(val));
    });

    // refresh threshold display on auto-threshold checkbox state change
    tie_setting(s.auto_threshold, this, [this](bool) { s.threshold_slider.notify_(); });

    tie_setting(s.enable_point_filter, ui.enable_point_filter);
    tie_setting(s.point_filter_coefficient, ui.point_filter_slider);
    tie_setting(s.point_filter_limit, ui.point_filter_limit_slider);
    connect(&s.point_filter_coefficient, value_::value_changed<slider_value>(),
            ui.point_filter_label, [this] { ui.point_filter_label->setValue(*s.point_filter_coefficient); } );
    connect(&s.point_filter_limit, value_::value_changed<slider_value>(), ui.point_filter_limit_label,
        [this] { ui.point_filter_limit_label->setValue(*s.point_filter_limit); }, Qt::QueuedConnection);
    ui.point_filter_label->setValue(*s.point_filter_coefficient);
    ui.point_filter_limit_label->setValue(*s.point_filter_limit);

    tie_setting(s.point_filter_deadzone, ui.point_filter_deadzone_slider);
    ui.point_filter_deadzone_label->setValue(*s.point_filter_deadzone);

    connect(&s.point_filter_deadzone, value_::value_changed<slider_value>(), ui.point_filter_deadzone_label,
        [this] { ui.point_filter_deadzone_label->setValue(*s.point_filter_deadzone); }, Qt::QueuedConnection);
}

QString TrackerDialog_PT::threshold_display_text(int threshold_value)
{
    if (!s.auto_threshold)
        return tr("Brightness %1/255").arg(threshold_value);
    else
    {
        pt_camera_info info;
        int w = s.cam_res_x, h = s.cam_res_y;

        if (w * h <= 0)
        {
            w = 640;
            h = 480;
        }

        if (tracker && tracker->get_cam_info(info) && info.res_x * info.res_y != 0)
        {
            w = info.res_x;
            h = info.res_y;
        }

        double value = (double)pt_point_extractor::threshold_radius_value(w, h, threshold_value);

        return tr("LED radius %1 pixels").arg(value, 0, 'f', 2);
    }
}

void TrackerDialog_PT::startstop_trans_calib(bool start)
{
    QMutexLocker l(&calibrator_mutex);

    if (start)
    {
        qDebug() << "pt: starting translation calibration";
        calib_timer.start();
        trans_calib.reset();
        s.t_MH_x = 0;
        s.t_MH_y = 0;
        s.t_MH_z = 0;

        ui.sample_count_display->setText(QString());
    }
    else
    {
        calib_timer.stop();
        qDebug() << "pt: stopping translation calibration";
        {
            auto [tmp, nsamples] = trans_calib.get_estimate();
            s.t_MH_x = int(tmp[0]);
            s.t_MH_y = int(tmp[1]);
            s.t_MH_z = int(tmp[2]);

            constexpr int min_yaw_samples = 15;
            constexpr int min_pitch_samples = 15;
            constexpr int min_samples = min_yaw_samples+min_pitch_samples;

            // Don't bother counting roll samples. Roll calibration is hard enough
            // that it's a hidden unsupported feature anyway.

            QString sample_feedback;
            if (nsamples[0] < min_yaw_samples)
                sample_feedback = tr("%1 yaw samples. Yaw more to %2 samples for stable calibration.").arg(nsamples[0]).arg(min_yaw_samples);
            else if (nsamples[1] < min_pitch_samples)
                sample_feedback = tr("%1 pitch samples. Pitch more to %2 samples for stable calibration.").arg(nsamples[1]).arg(min_pitch_samples);
            else
            {
                const int nsamples_total = nsamples[0] + nsamples[1];
                sample_feedback = tr("%1 samples. Over %2, good!").arg(nsamples_total).arg(min_samples);
            }

            ui.sample_count_display->setText(sample_feedback);
        }
    }
    ui.tx_spin->setEnabled(!start);
    ui.ty_spin->setEnabled(!start);
    ui.tz_spin->setEnabled(!start);

    if (start)
        ui.tcalib_button->setText(tr("Stop calibration"));
    else
        ui.tcalib_button->setText(tr("Start calibration"));
}

void TrackerDialog_PT::poll_tracker_info_impl()
{
    pt_camera_info info;
    if (tracker && tracker->get_cam_info(info))
    {
        ui.caminfo_label->setText(tr("%1x%2 @ %3 FPS").arg(info.res_x).arg(info.res_y).arg(iround(info.fps)));

        // display point info
        const int n_points = tracker->get_n_points();
        ui.pointinfo_label->setText((n_points == 3 ? tr("%1 OK!") : tr("%1 BAD!")).arg(n_points));
    }
    else
    {
        ui.caminfo_label->setText(tr("Tracker offline"));
        ui.pointinfo_label->setText(QString());
    }
}

void TrackerDialog_PT::set_camera_settings_available(const QString& /* camera_name */)
{
    ui.camera_settings->setEnabled(true);
}

void TrackerDialog_PT::show_camera_settings()
{
    if (tracker)
        tracker->open_camera_dialog_flag = true;
    else
        (void)video::show_dialog(s.camera_name);
}

void TrackerDialog_PT::trans_calib_step()
{
    QMutexLocker l(&calibrator_mutex);

    if (tracker)
    {
        Affine X_CM = tracker->pose();
        trans_calib.update(X_CM.R, X_CM.t);
    }
    else
        startstop_trans_calib(false);
}

void TrackerDialog_PT::save()
{
    s.b->save();
}

void TrackerDialog_PT::doOK()
{
    save();
    close();
}

void TrackerDialog_PT::doCancel()
{
    close();
}

void TrackerDialog_PT::register_tracker(ITracker *t)
{
    tracker = static_cast<Tracker_PT*>(t);
    ui.tcalib_button->setEnabled(true);
    poll_tracker_info_impl();
    timer.start();
}

void TrackerDialog_PT::unregister_tracker()
{
    tracker = nullptr;
    ui.tcalib_button->setEnabled(false);
    poll_tracker_info_impl();
    timer.stop();
}

void TrackerDialog_PT::set_buttons_visible(bool x)
{
    ui.buttonBox->setVisible(x);
}

void TrackerDialog_PT::reload()
{
    s.b->reload();
}

} // ns pt_impl