-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabletCalc.cpp
183 lines (119 loc) · 5.59 KB
/
tabletCalc.cpp
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
//
// Created by Ritik on 1/1/2024.
//
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <Windows.h>
#include "pixelFuncs.h"
#include "tabletCalc.h"
#include "screenRes.h"
#include "mouseFuncs.h"
using namespace std;
using namespace cv;
namespace tC {
extern vector<int> tabletRot = {0, ROTATE_90_CLOCKWISE, ROTATE_180, ROTATE_90_COUNTERCLOCKWISE};
extern vector<int> buttonColor = {0,255,0};
extern vector<int> importantColor = {0, 0, 255};
extern double importantPower = 1.0;
Mat warpedMat(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4, Mat& img, float width, float height, int rotation, bool mirror){
if(rotation % 2 == 1){
float temp = width;
width = height;
height = temp;
}
vector<Point2f> cameraPoints;
cameraPoints.push_back({(float)p1[0], (float)p1[1]});
cameraPoints.push_back({(float)p2[0], (float)p2[1]});
cameraPoints.push_back({(float)p3[0], (float)p3[1]});
cameraPoints.push_back({(float)p4[0], (float)p4[1]});
vector<Point2f> worldPoints;
worldPoints.push_back({0, 0});
worldPoints.push_back({width, 0});
worldPoints.push_back({width, height});
worldPoints.push_back({0, height});
Mat perspectiveMat_= findHomography(cameraPoints, worldPoints, RANSAC);
perspectiveTransform(cameraPoints, worldPoints, perspectiveMat_);
Mat out(width, height, CV_8UC3);
warpPerspective(img, out, perspectiveMat_, Size(width, height));
if(rotation > 0) rotate(out, out, tabletRot[rotation]);
if(mirror) flip(out, out, 1);
return out;
}
vector<float> getTabletPos(Mat& out, uint8_t*& tabletPtr, vector<int> baseColor, int baseDistance, int tipExtra, bool leftHanded){
vector<float> tabletCoord = {0,0};
int count = 0;
for(int y = 0; y < out.rows; y++){
int width = min(y, out.cols);
int mid = width / 2;
for(int i = 0; i < width; i++){
int p = i / 2;
if(i % 2 == 0) p = -p;
int x = mid + p;
int cX = out.cols - x;
if(!leftHanded) cX = x;
int nY = y - mid - p;
int pixel = nY * out.cols * 3 + cX * 3;
if(cX >= out.cols || cX < 0) cout << "1ripX" << endl;
if(y >= out.rows || y < 0) cout << "1ripY" << endl;
int distance = pF::rgbDistance(tabletPtr, baseColor, pixel);
if(distance < baseDistance) continue;
if(count < tipExtra){
tabletCoord[0] += cX;
tabletCoord[1] += nY;
count++;
pF::highlightPixel(tabletPtr, buttonColor, 1.0, pixel);
}
else{
pF::highlightPixel(tabletPtr, importantColor, 1.0, pixel);
}
}
}
for(int x = 0; x < out.cols; x++){
int leftOver = out.cols - x;
int height = min(leftOver, out.rows);
int mid = height/2;
for(int i = 0; i < height; i++){
int p = i / 2;
if(i % 2 == 0) p = -p;
int y = out.rows - 1 - mid + p;
int nX = x + mid - p;
int cX = out.cols - nX;
if(!leftHanded) cX = nX;
int pixel = y * out.cols * 3 + cX * 3;
if(cX >= out.cols || cX < 0) cout << "2ripX" << endl;
if(y >= out.rows) cout << "2ripY " + to_string(y) + " " + to_string(out.rows) << endl;
if(y < 0) cout << "2ripYn" << endl;
int distance = pF::rgbDistance(tabletPtr, baseColor, pixel);
if(distance < baseDistance) continue;
if(count < tipExtra){
pF::highlightPixel(tabletPtr, buttonColor, 1.0, pixel);
tabletCoord[0] += cX;
tabletCoord[1] += y;
count++;
}
else{
pF::highlightPixel(tabletPtr, importantColor, 1.0, pixel);
}
}
}
if(count == 0) return {-999999, -999999};
tabletCoord[0] /= count;
tabletCoord[1] /= count;
return tabletCoord;
}
void moveMouse(Mat& out, uint8_t*& tabletPtr, vector<int> baseColor, int baseDistance, int tipExtra, bool leftHanded, bool mouseEnabled, int screenAreaWidth, int screenAreaHeight, vector<int> screenAreaPivot){
vector<float> tabletCoord = tC::getTabletPos(out, tabletPtr, baseColor, baseDistance, tipExtra, leftHanded);
int tabletScreenX = sR::screenWidth * (tabletCoord[0] - screenAreaPivot[0]) / screenAreaWidth;
int tabletScreenY = sR::screenHeight * (tabletCoord[1] - screenAreaPivot[1]) / screenAreaHeight;
vector<int> tabletScreen = {tabletScreenX, tabletScreenY};
if(mouseEnabled && tabletCoord[0] != -999999){
if(tabletScreenX < 0) tabletScreenX = 0;
if(tabletScreenX >= sR::screenWidth) tabletScreenX = sR::screenWidth-1;
if(tabletScreenY < 0) tabletScreenY = 0;
if(tabletScreenY >= sR::screenHeight) tabletScreenY = sR::screenHeight-1;
mF::setCursorPosBypass(tabletScreenX,tabletScreenY);
}
}
} // tC