summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_tracker_ht/ftnoir_tracker_ht.cpp
blob: 1ec0e13f7ae93f712a396753186adc3fbea41597 (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
#include "stdafx.h"
#include "headtracker-ftnoir.h"
#include "ftnoir_tracker_ht.h"
#include "ftnoir_tracker_ht_dll.h"
#include "ui_ht-trackercontrols.h"
#include "opentrack/plugin-api.hpp"
#include <cmath>
#include "opentrack/camera-names.hpp"

typedef struct {
	int width;
	int height;
} resolution_tuple;

static resolution_tuple resolution_choices[] = {
	{ 640, 480 },
	{ 320, 240 },
	{ 320, 200 },
	{ 0, 0 }
};

void Tracker::load_settings(ht_config_t* config)
{
    int nframes = 0;
    switch (static_cast<int>(s.fps))
    {
    default:
    case 0:
        nframes = 0;
        break;
    case 1:
        nframes = 30;
        break;
    case 2:
        nframes = 60;
        break;
    case 3:
        nframes = 120;
        break;
    case 4:
        nframes = 180;
        break;
    }

    static constexpr float pi = 3.14159265358979323846f;
    config->classification_delay = 500;
    config->field_of_view = s.fov;
    config->max_keypoints = 150;
    config->keypoint_distance = 3.4;
    config->force_fps = nframes;
    config->camera_index = camera_name_to_index(s.camera_name);

    config->ransac_max_reprojection_error = 8;
    config->ransac_max_inlier_error = 8;

    config->pyrlk_pyramids = 0;
    config->pyrlk_win_size_w = config->pyrlk_win_size_h = 21;

    config->ransac_max_mean_error = 999;
    config->ransac_abs_max_mean_error = 999;

    config->debug = 0;
    config->ransac_min_features = 0.85;
    config->ransac_num_iters = 300;

    int res = s.resolution;
    if (res < 0 || res >= (int)(sizeof(resolution_choices) / sizeof(resolution_tuple)))
		res = 0;
	resolution_tuple r = resolution_choices[res];
	config->force_width = r.width;
    config->force_height = r.height;
    config->flandmark_delay = 50;
    for (int i = 0; i < 5; i++)
        config->dist_coeffs[i] = 0;
}

Tracker::Tracker() :
    ht(nullptr),
    ypr {0,0,0, 0,0,0},
    videoWidget(nullptr),
    layout(nullptr),
    should_stop(false)
{
}

Tracker::~Tracker()
{
    should_stop = true;
    wait();
    ht_free_context(ht);
	if (layout)
		delete layout;
	if (videoWidget)
		delete videoWidget;
}

void Tracker::start_tracker(QFrame* videoframe)
{
    videoframe->show();
    videoWidget = new HTVideoWidget(videoframe);
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(videoWidget);
    if (videoframe->layout())
        delete videoframe->layout();
    videoframe->setLayout(layout);
    videoWidget->show();
    this->layout = layout;

    load_settings(&conf);
    ht = ht_make_context(&conf, nullptr);
    start();
}

void Tracker::run()
{
    while (!should_stop)
    {
        ht_result_t euler;
        euler.filled = false;
        {
            QMutexLocker l(&camera_mtx);

            if (!ht_cycle(ht, &euler))
                break;
        }
        if (euler.filled)
        {
            QMutexLocker l(&ypr_mtx);
            ypr[TX] = euler.tx;
            ypr[TY] = euler.ty;
            ypr[TZ] = euler.tz;
            ypr[Yaw] = euler.rotx;
            ypr[Pitch] = euler.roty;
            ypr[Roll] = euler.rotz;
        }
        {
            const cv::Mat frame_ = ht_get_bgr_frame(ht);
            if (frame_.cols <= HT_MAX_VIDEO_WIDTH && frame_.rows <= HT_MAX_VIDEO_HEIGHT && frame_.channels() <= HT_MAX_VIDEO_CHANNELS)
            {
                QMutexLocker l(&frame_mtx);

                const int cols = frame_.cols;
                const int rows = frame_.rows;
                const int pitch = cols * 3;
                for (int y = 0; y < rows; y++)
                {
                    for (int x = 0; x < cols; x++)
                    {
                        unsigned char* dest = &frame.frame[y * pitch + 3 * x];
                        const cv::Vec3b& elt = frame_.at<cv::Vec3b>(y, x);
                        const cv::Scalar elt2 = static_cast<cv::Scalar>(elt);
                        dest[0] = elt2.val[0];
                        dest[1] = elt2.val[1];
                        dest[2] = elt2.val[2];
                    }
                }
                frame.channels = frame_.channels();
                frame.width = frame_.cols;
                frame.height = frame_.rows;
            }
        }
    }
}

void Tracker::data(double* data)
{
    {
        QMutexLocker l(&frame_mtx);

        if (frame.width > 0)
        {
            videoWidget->update_image(frame.frame, frame.width, frame.height);
            frame.width = 0;
        }
    }

    {
        QMutexLocker l(&ypr_mtx);

        for (int i = 0; i < 6; i++)
            data[i] = ypr[i];
    }
}

extern "C" OPENTRACK_EXPORT Metadata* GetMetadata()
{
	return new TrackerDll;
}

extern "C" OPENTRACK_EXPORT ITracker* GetConstructor()
{
    return new Tracker;
}

extern "C" OPENTRACK_EXPORT ITrackerDialog* GetDialog( )
{
    return new TrackerControls;
}

TrackerControls::TrackerControls() : tracker(nullptr)
{
	ui.setupUi(this);
    ui.cameraName->clear();
    QList<QString> names = get_camera_names();
    names.prepend("Any available");
    ui.cameraName->addItems(names);
    tie_setting(s.camera_name, ui.cameraName);
    tie_setting(s.fps, ui.cameraFPS);
    tie_setting(s.fov, ui.cameraFOV);
    tie_setting(s.resolution, ui.resolution);
    connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel()));
    connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK()));
    connect(ui.camera_settings, SIGNAL(pressed()), this, SLOT(camera_settings()));
}

void TrackerControls::doOK()
{
    s.b->save();
	this->close();
}

void TrackerControls::doCancel()
{
    s.b->reload();
    this->close();
}

void TrackerControls::camera_settings()
{
    if (tracker)
    {
        cv::VideoCapture* cap = ht_capture(tracker->ht);
        open_camera_settings(cap, s.camera_name, &tracker->camera_mtx);
    }
    else
        open_camera_settings(nullptr, s.camera_name, nullptr);
}