summaryrefslogtreecommitdiffhomepage
path: root/tracker-trackhat/trackhat.hpp
blob: d94841d2ef44e1e969c98203dde2ebac066d204d (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
#pragma once

#include "../tracker-pt/pt-api.hpp"
#include "compat/macros.hpp"
#include "options/options.hpp"

#include <track_hat_driver.h>

#include <array>
#include <atomic>
#include <opencv2/core/mat.hpp>

enum model_type : int
{
    model_cap = 1,
    model_clip_left,
    model_clip_right,
    model_mini_clip_left,
    model_mini_clip_right,
    model_mystery_meat,
};

namespace trackhat_impl
{
using namespace options;

TH_ErrorCode log_error(TH_ErrorCode error, const char* source, const char* file, int line, const char* function);
#define th_check_(expr, expr2) ::trackhat_impl::log_error((expr), expr2)
#define th_check(expr) ::trackhat_impl::log_error((expr), #expr, __FILE__, __LINE__, function_name)

struct trackhat_settings : opts
{
    static constexpr int min_gain = 16, max_gain = 47,
                         min_exposure = 0x10, max_exposure = 0xff;
    static constexpr int num_exposure_steps = max_gain + max_exposure - min_gain - min_exposure;
    int effective_exposure() const;
    int effective_gain() const;
    trackhat_settings();
    value<slider_value> exposure{b, "exposure", {min_exposure, min_exposure, max_exposure + max_gain - min_gain}};
    //value<slider_value> threshold{b, "threshold", {0x97, 64, 0xfe}};
    value<model_type> model{b, "model", model_mini_clip_left};
    value<double> min_pt_size{b, "min-point-size", 10};
    value<double> max_pt_size{b, "max-point-size", 50};
    value<bool> enable_point_filter{b, "enable-point-filter", true };
    value<slider_value> point_filter_coefficient{b, "point-filter-coefficient", { 1.5, 1, 4 }};
    value<slider_value> point_filter_limit { b, "point-filter-limit", { 0.1, 0.01, 1 }};
    value<slider_value> point_filter_deadzone { b, "point-filter-deadzone", {0, 0, 1}};
};

class setting_receiver : public QObject
{
    Q_OBJECT

public:
    explicit setting_receiver(bool value);
    bool test_and_clear();
public slots:
    void settings_changed();
private:
    std::atomic<bool> changed{false};
};

} // ns trackhat_impl

using typename trackhat_impl::trackhat_settings;

struct trackhat_metadata final : pt_runtime_traits
{
    pt_runtime_traits::pointer<pt_camera> make_camera() const override;
    pt_runtime_traits::pointer<pt_point_extractor> make_point_extractor() const override;
    pt_runtime_traits::pointer<pt_frame> make_frame() const override;
    pt_runtime_traits::pointer<pt_preview> make_preview(int w, int h) const override;
    QString get_module_name() const override;

    OTR_DISABLE_MOVE_COPY(trackhat_metadata);

    trackhat_metadata() = default;
    ~trackhat_metadata() override = default;

    static const QString module_name;
};

struct point
{
    int brightness = 0, area, x, y, W, H;
    bool ok = false;
};

struct camera_handle final
{
    OTR_DISABLE_MOVE_COPY(camera_handle);
    trackHat_Device_t* operator->() { return &device_; }
    trackHat_Device_t& operator*() { return device_; }

    camera_handle() = default;
    ~camera_handle() = default;

    [[nodiscard]] bool ensure_connected();
    [[nodiscard]] bool ensure_device_exists();
    void disconnect();
private:
    trackHat_Device_t device_ = {};
    enum state { st_stopped, st_detected, st_streaming, };
    state state_ = st_stopped;
};

struct trackhat_camera final : pt_camera
{
    trackhat_camera();
    ~trackhat_camera() override;

    OTR_DISABLE_MOVE_COPY(trackhat_camera);

    bool start(const pt_settings& s) override;
    void stop() override;

    pt_camera::result get_frame(pt_frame& frame) override;
    pt_camera::result get_info() const override;
    pt_camera_info get_desired() const override;

    QString get_desired_name() const override;
    QString get_active_name() const override;

    void set_fov(f value) override;
    void show_camera_settings() override;

    f deadzone_amount() const override { return 10; }

    static constexpr int sensor_size = 2940;
    static constexpr int sensor_fov = 52;
    static constexpr int point_count = TRACK_HAT_NUMBER_OF_POINTS;
    static constexpr bool debug_mode = true;

private:
    trackhat_impl::setting_receiver sig{true};

    [[nodiscard]] bool init_regs();
    void set_pt_options();

    camera_handle device;
    pt_settings s{trackhat_metadata::module_name};
    trackhat_settings t;
};

struct trackhat_frame final : pt_frame
{
    void init_points(const trackHat_ExtendedPoints_t& points, double min_size, double max_size);
    trackhat_frame() = default;
    ~trackhat_frame() override = default;

    std::array<point, trackhat_camera::point_count> points;
};

struct trackhat_preview final : pt_preview
{
    QImage get_bitmap() override;
    void draw_head_center(f x, f y) override;
    void set_last_frame(const pt_frame&) override; // NOLINT(misc-unconventional-assign-operator)

    trackhat_preview(int w, int h);
    ~trackhat_preview() override = default;
    void draw_points();
    void draw_center();

    OTR_DISABLE_MOVE_COPY(trackhat_preview);

    cv::Mat frame_bgr, frame_bgra;
    numeric_types::vec2 center{-1, -1};
    std::array<point, trackhat_camera::point_count> points;
};

struct trackhat_extractor final : pt_point_extractor
{
    void extract_points(const pt_frame& data, pt_preview&, bool, std::vector<vec2>& points) override;

    OTR_DISABLE_MOVE_COPY(trackhat_extractor);

    trackhat_extractor() = default;
    ~trackhat_extractor() override = default;
};