-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkinect_cursor.cpp
More file actions
147 lines (120 loc) · 4.42 KB
/
kinect_cursor.cpp
File metadata and controls
147 lines (120 loc) · 4.42 KB
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
#include <chrono>
#include <csignal>
#include <iostream>
#include <thread>
#include <vector>
#include "frame_grabber.hpp"
#ifdef __APPLE__
#include <ApplicationServices/ApplicationServices.h>
#endif
namespace {
volatile std::sig_atomic_t g_should_stop = 0;
#ifdef __APPLE__
struct ClickGestureState {
double baseline_depth = 0.0;
double last_delta = 0.0;
bool initialized = false;
bool armed = true;
int click_count = 0;
std::chrono::steady_clock::time_point last_click_at = std::chrono::steady_clock::now();
};
void PerformLeftClick(double x, double y) {
CGPoint point = CGPointMake(x, y);
CGEventRef down = CGEventCreateMouseEvent(nullptr, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventRef up = CGEventCreateMouseEvent(nullptr, kCGEventLeftMouseUp, point, kCGMouseButtonLeft);
if (down != nullptr) {
CGEventPost(kCGHIDEventTap, down);
CFRelease(down);
}
if (up != nullptr) {
CGEventPost(kCGHIDEventTap, up);
CFRelease(up);
}
}
bool MaybeTriggerAirTap(ClickGestureState& state, const blog_demos::BlobStats& blob, double x, double y) {
if (!blob.found) {
state.initialized = false;
state.armed = true;
state.last_delta = 0.0;
return false;
}
if (!state.initialized) {
state.baseline_depth = blob.nearest_depth;
state.initialized = true;
}
state.baseline_depth = blog_demos::Lerp(state.baseline_depth, blob.nearest_depth, state.armed ? 0.04 : 0.18);
state.last_delta = state.baseline_depth - blob.nearest_depth;
const auto now = std::chrono::steady_clock::now();
const bool cooled_down = (now - state.last_click_at) > std::chrono::milliseconds(350);
if (state.armed && cooled_down && state.last_delta > 95.0 && blob.active_pixels > 300) {
PerformLeftClick(x, y);
state.armed = false;
state.last_click_at = now;
++state.click_count;
return true;
}
if (!state.armed && state.last_delta < 35.0) {
state.armed = true;
}
return false;
}
#endif
void HandleSignal(int) {
g_should_stop = 1;
}
} // namespace
int main() {
#ifndef __APPLE__
std::cerr << "This demo uses CoreGraphics and currently only targets macOS.\n";
return 1;
#else
std::signal(SIGINT, HandleSignal);
std::cout << "kinect_cursor: move your hand to move the cursor, air-tap forward to click, Ctrl+C to quit.\n";
std::cout << "Tip: keep one hand roughly 40-90cm from the sensor.\n";
Freenect::Freenect freenect;
auto& device = freenect.createDevice<blog_demos::FrameGrabber>(0);
device.setDepthFormat(FREENECT_DEPTH_11BIT);
device.startDepth();
const CGRect screen = CGDisplayBounds(CGMainDisplayID());
const double screen_width = CGRectGetWidth(screen);
const double screen_height = CGRectGetHeight(screen);
std::vector<uint16_t> depth(blog_demos::kFrameWidth * blog_demos::kFrameHeight);
double smooth_x = screen_width / 2.0;
double smooth_y = screen_height / 2.0;
auto last_log = std::chrono::steady_clock::now();
ClickGestureState click_state;
while (!g_should_stop) {
if (!device.GetLatestDepth(depth)) {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
continue;
}
const auto blob = blog_demos::FindForegroundBlob(depth, 120, 2);
if (!blob.found) {
MaybeTriggerAirTap(click_state, blob, smooth_x, smooth_y);
continue;
}
const double normalized_x =
blog_demos::Clamp(blob.center_x / (blog_demos::kFrameWidth - 1.0), 0.0, 1.0);
const double normalized_y =
blog_demos::Clamp(blob.center_y / (blog_demos::kFrameHeight - 1.0), 0.0, 1.0);
const double target_x = normalized_x * screen_width;
const double target_y = normalized_y * screen_height;
smooth_x = blog_demos::Lerp(smooth_x, target_x, 0.18);
smooth_y = blog_demos::Lerp(smooth_y, target_y, 0.18);
CGWarpMouseCursorPosition(CGPointMake(smooth_x, smooth_y));
const bool clicked = MaybeTriggerAirTap(click_state, blob, smooth_x, smooth_y);
const auto now = std::chrono::steady_clock::now();
if (clicked || now - last_log > std::chrono::milliseconds(600)) {
std::cout << "cursor x=" << static_cast<int>(smooth_x)
<< " y=" << static_cast<int>(smooth_y)
<< " depth=" << static_cast<int>(blob.nearest_depth)
<< " delta=" << static_cast<int>(click_state.last_delta)
<< " clicks=" << click_state.click_count
<< (clicked ? " [click]" : "") << "\n";
last_log = now;
}
}
device.stopDepth();
return 0;
#endif
}