-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQwopAnalyzer.cpp
More file actions
299 lines (223 loc) · 6.66 KB
/
QwopAnalyzer.cpp
File metadata and controls
299 lines (223 loc) · 6.66 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
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
#include "QwopAnalyzer.h"
#include <opencv2/highgui/highgui.hpp>
#include <sstream>
using namespace cv;
const float QwopAnalyzer::distTestaBacino = 121.8;
const float QwopAnalyzer::centroY = 280.0;
const float QwopAnalyzer::testaCom = 87.5;
const float QwopAnalyzer::piedeLontanoThresh = 12000.0;
const int QwopAnalyzer::shoeHeightThresh = 45;
float distance(Point a,Point b)
{
float dist = (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
return dist;
}
std::pair<int,float> findNearest(std::vector<cv::Point> lista,cv::Point punto)
{
std::pair<int,float> result(-1,800.0);
for(unsigned int i = 0; i<lista.size(); i++) {
float dist = distance(lista[i],punto);
if(dist < result.second) {
result.first = i;
result.second = dist;
}
}
return result;
}
void QwopAnalyzer::processFeet(std::vector<cv::Point> &piedi)
{
if(piedi.size() == 0)
return;
{
auto dati = findNearest(piedi,body);
if(dati.first > -1 && dati.second < 800) {
sign = piedi[dati.first];
piedi.erase(piedi.begin()+dati.first);
}
}
if(piedi.size() == 0)
return;
if(piedi.size() == 2) {
shoe1 = piedi[0];
shoe2 = piedi[1];
}
if(piedi.size() > 2) {
if(oddIteration) {
auto dati = findNearest(piedi,shoe1);
if(dati.first > -1 ) {
shoe1 = piedi[dati.first];
piedi.erase(piedi.begin()+dati.first);
}
}
{
auto dati = findNearest(piedi,shoe2);
if(dati.first > -1 ) {
shoe2 = piedi[dati.first];
piedi.erase(piedi.begin()+dati.first);
}
}
if(!oddIteration) {
auto dati = findNearest(piedi,shoe1);
if(dati.first > -1 ) {
shoe1 = piedi[dati.first];
piedi.erase(piedi.begin()+dati.first);
}
}
}
if(shoe2.x < shoe1.x)
std::swap(shoe2,shoe1);
}
void QwopAnalyzer::calculateVariables()
{
terra = shoe1.y > (int)screenHeight - shoeHeightThresh || shoe2.y > screenHeight-shoeHeightThresh;
{
float testaCorpo = sqrt(distance(head,body));
Point bacino1((body.x-head.x) * 120/testaCorpo +head.x,(body.y-head.y) * 120/testaCorpo+head.y);
float testaRiga = head.x-280.0;
float b=acos(testaRiga/87.0);
Point bacino2(head.x-1.4*testaRiga,head.y +fabs(1.4*testaRiga * tan(b)));
double angolino = asin(8.0/sqrt(distance(head,sign)));
double angolone = atan2(sign.x-head.x,sign.y-head.y) - angolino;
Point bacino3(head.x+distTestaBacino*sin(angolone),head.y+distTestaBacino*cos(angolone));
//circle(screen,bacino3,10,Scalar(0,100,255),7);
float distUnoDue = distance(bacino1,bacino2);
float distDueTre = distance(bacino2,bacino3);
float distUnoTre = distance(bacino1,bacino3);
if(distUnoTre < distUnoDue && distUnoTre < distDueTre ) {
//esclude 2
bacino = Point((bacino1.x + bacino3.x)/2,(bacino1.y + bacino3.y)/2);
} else if(distDueTre < distUnoDue && distDueTre < distUnoTre) {
//esclude 1
bacino = Point((bacino2.x + bacino3.x)/2,(bacino2.y+bacino3.y)/2);
} else {
//esclude 3
bacino = Point((bacino1.x+bacino2.x)/2,(bacino1.y+bacino2.y)/2);
}
}
//
float angoloCOM = atan2(bacino.x-head.x,bacino.y-head.y);
COM = Point(head.x + 87.5 * sin(angoloCOM),head.y + 87.5*cos(angoloCOM));
{
//angoli
float angoloVecchio = angolo, omegaVecchio = omega;
float angoloPrevisto = angoloVecchio + elapsed*omegaAvg;
angolo = -angoloCOM;
omega = (angolo - angoloVecchio)/(elapsed);
if(decisionMaker.getStep() > 0 && fabs(angolo -angoloPrevisto>0.5))
;//angolo = angoloPrevisto;
if(fabs(omega-omegaVecchio) > 5.0)
omega = omegaVecchio;
angoloAvg = 0.8*angoloVecchio + 0.2 * angolo;
omegaAvg = 0.8*omegaVecchio + 0.2*omega;
}
if(distance(shoe2,bacino)> piedeLontanoThresh)
piedeLontano = true;
else
piedeLontano = false;
altezza = screenHeight-COM.y;
}
void QwopAnalyzer::showDebugScreen(cv::Mat & screen)
{
if(terra)
putText(screen,"terra",Point(250,20),FONT_HERSHEY_COMPLEX_SMALL,1,Scalar::all(255));
{
std::stringstream s;
s<<"angolo: "<<std::fixed<<std::showpos<<angoloAvg;
putText(screen,s.str().c_str(),Point(0,20),FONT_HERSHEY_COMPLEX_SMALL,1,Scalar::all(255));
}
{
std::stringstream s2;
s2<<"omega: "<<std::showpos<<std::fixed<<omegaAvg;
putText(screen,s2.str().c_str(),Point(0,40),FONT_HERSHEY_COMPLEX_SMALL,1,Scalar::all(255));
}
{
std::stringstream s;
s<<"altezza: "<<altezza;
putText(screen,s.str().c_str(),Point(0,60),FONT_HERSHEY_COMPLEX_SMALL,1,Scalar::all(255));
}
circle(screen,head,5,Scalar(255,50,50),7);
circle(screen,body,5,Scalar(50,255,50),7);
/*
*/
circle(screen,COM,3,Scalar(255,0,255),7);
circle(screen,bacino,10,Scalar(0,0,255),7);
circle(screen,shoe1,5,Scalar(50,200,150),7);
circle(screen,shoe2,5,Scalar(0,200,200),7);
//circle(screen,shoe2,80,Scalar(10,255,255),1);
circle(screen,sign,8,155,7);
line( screen,bacino,shoe1,Scalar::all(255),2);
line( screen,shoe2,shoe1,Scalar::all(255),2);
if(piedeLontano)
line( screen,shoe2,bacino,cv::Scalar(100,255,100),2);
else
line( screen,bacino,shoe2,Scalar::all(255),2);
Point puntino(bacino.x + sin(angolo)*100.0,bacino.y-cos(angolo)*100.0);
line( screen,bacino,puntino,Scalar::all(255),2);
//line( screen,Point(280,20),Point(280,200),Scalar::all(255),2);
imshow("QWOP",screen);
waitKey(5);
//std::cerr<<1.4*testaRiga/cos(b)<<std::endl;
//line( screen,head,testaCom,Scalar::all(255),2);
}
float QwopAnalyzer::secondsSinceLastUpdate()
{
Clock::time_point t0 = Clock::now();
milliseconds ms = std::chrono::duration_cast<milliseconds>(t0 - lastUpdate);
lastUpdate = t0;
return ms.count()/1000.0;
}
void QwopAnalyzer::processData(const std::vector<std::pair<char,cv::Point> > &data)
{
elapsed = secondsSinceLastUpdate();
auto testa = extractPoints(data,'H');
auto corpo = extractPoints(data,'B');
auto piedi = extractPoints(data,'S');
//Mat screen = Mat::zeros( Size(500,screenHeight), CV_8UC1 );
if(testa.size() > 0)
head = testa[0];
if(corpo.size())
body = corpo[0];
processFeet(piedi);
calculateVariables();
oddIteration = !oddIteration;
}
void QwopAnalyzer::reset()
{
if(!running) {
running = true;
} else
decisionMaker.endTurn();
omega = 0.2;
angolo = 0;
decisionMaker.newTurn();
}
bool QwopAnalyzer::needStep()
{
DecisionData data;
data.terra = terra;
data.angolo = angoloAvg;
data.omega = omegaAvg;
data.piedeLontano = piedeLontano;
data.elapsed = elapsed;
data.altezza = COM.y;
if(decisionMaker.makeDecision(data))
return true;
return false;
}
std::vector<cv::Point> QwopAnalyzer::extractPoints(const std::vector<std::pair<char,cv::Point> > &data,char type)
{
std::vector<cv::Point> result;
for(unsigned int i = 0; i<data.size(); i++) {
if(data[i].first == type)
result.push_back(data[i].second);
}
return result;
}
QwopAnalyzer::QwopAnalyzer()
{
lastUpdate = Clock::now();
screenHeight = 400;
elapsed = 0.1;
risultati.resize(2);
running = false;
}