-
Notifications
You must be signed in to change notification settings - Fork 2
/
experimenttrial.cpp
297 lines (220 loc) · 7.71 KB
/
experimenttrial.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
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
#include "experimenttrial.h"
#include "locationawarewidget.h"
#include "MultiWidgets/ImageWidget.hpp"
#include "MultiWidgets/StayInsideParentOperator.hpp"
#include <QtCore/qmath.h>
#include <QDebug>
#include <QDateTime>
#include <Resonant/DSPNetwork.hpp>
#include <Resonant/ModuleSamplePlayer.hpp>
#include "logthread.h"
#include "roundwidget.h"
#include "settings.h"
#include <sstream>
ExperimentTrial::ExperimentTrial(int id, RotationDirection direction, int distance, int size, int angle, int x1, int y1)
: logger(0),
first(0),
second(0)
{
this->id = id;
this->direction = direction;
this->distance = distance;
this->size = size;
this->angle = angle / 180.0 * M_PI;
this->target1 = Nimble::Vector2(x1,y1);
this->target2 = this->target1 + Nimble::Vector2(distance, 0).rotate(angle);
this->createUI();
this->setFixed(true);
this->setAllowRotation(false);
this->setInputTransparent(true);
this->setColor(0, 0, 0, 1);
this->setSize(1920, 1080);
// Deprecated, draw arrows from target circles instead
#if 0
// rotation direction
MultiWidgets::ImageWidget * rotation = new MultiWidgets::ImageWidget();
rotation->setFixed(true);
rotation->setSize(100, 100);
rotation->setColor(1,1,1,1);
if( this->direction == ExperimentTrial::Clockwise ) {
rotation->load("clockwise.png");
} else {
rotation->load("counterclockwise.png");
}
int max = distance > 200 ? distance : 200;
rotation->setLocation( target1.x - max, target1.y - max);
this->addChild( rotation );
#endif
// add logs
}
void ExperimentTrial::input(MultiWidgets::GrabManager & gm, float dt) {
MultiWidgets::Widget::input(gm, dt);
std::stringstream ss;
ss << "- time: ";
ss << QTime().currentTime().toString("hh:mm:ss:zzz").toStdString() + "\n";
ss << " fingers:\n";
int count = 0;
for(ChildIterator it=childBegin(), end = childEnd(); it != end; ++it) {
MultiWidgets::Widget::FingerIds::iterator start = it->grabFingerBegin();
MultiWidgets::Widget::FingerIds::iterator last = it->grabFingerEnd();
for( MultiWidgets::Widget::FingerIds::iterator finger = start; finger != last; finger++ ) {
MultiTouch::Finger f = gm.findFinger(*finger);
if(f.isNull())
continue;
ss << " - id: " << f.id() << "\n";
ss << " position: " << f.tipLocation() << "\n";
++count;
}
}
ss << " circles:\n";
ss << " - " << this->first->location() << "\n";
ss << " - " << this->second->location() << "\n";
bool lost1 = first->isContactLost();
bool lost2 = second->isContactLost();
if(count == 0 && !lost1 && !lost2) {
return;
}
if(lost1 || lost2) {
ss << " contact_lost:\n";
ss << " - circle1: " << lost1 << "\n";
ss << " - circle2: " << lost2 << "\n";
}
this->logger->append(ss.str());
}
void ExperimentTrial::createUI()
{
bool flip = target1.y > target2.y;
size_t targets[] = {0, 1};
if(flip && direction == Clockwise) {targets[0]= 1; targets[1]= 0;}
Nimble::Vector2 v = 0.5f*(target2-target1);
Nimble::Vector2 off = (v + v.perpendicular());
Nimble::Vector2 realTargets[] = { target1+off, target2-off };
this->first = createMovable( target1, realTargets[0]);
this->second = createMovable( target2, realTargets[1]);
first->setTarget(realTargets[targets[0]]);
second->setTarget(realTargets[targets[1]]);
LocationAwareWidget * higher = first;
LocationAwareWidget * lower = second;
if(flip) {
higher = second;
lower = first;
}
higher->setDefaultColor(1,0,0);
lower->setDefaultColor(0.3f, 0.3f, 1.0f);
MultiWidgets::Widget * box = new RoundWidget();
box->setColor(1,1,1,1);
box->setSize(0.5f*(higher->size()));
box->setFixed(true);
box->setInputTransparent(true);
box->setCenterLocation( 0.5f*higher->size() );
higher->addChild(box);
}
LocationAwareWidget * ExperimentTrial::createMovable(Nimble::Vector2 pos, Nimble::Vector2 target)
{
LocationAwareWidget * a = new LocationAwareWidget();
a->setHeight( size );
a->setWidth( size );
a->addOperator(new MultiWidgets::StayInsideParentOperator);
a->setCenterLocation( pos );
if( DEBUG ) {
a->setColor(1,0,0,50);
} else {
a->setColor(0,0,0,0);
}
a->setVelocity(0,0);
a->setInputFlags(MultiWidgets::Widget::INPUT_MOTION_XY);
a->eventAddListener("target_reached", "check_targets", this);
this->addChild(a);
MultiWidgets::Widget * as = new RoundWidget();
as->setWidth(this->size-10);
as->setHeight(this->size-10);
as->setColor(0.5, 0.5, 0, 1);
as->setCenterLocation( target );
as->setAllowRotation(false);
as->setFixed(true);
this->addChild(as);
return a;
}
bool ExperimentTrial::isFinished()
{
return !(this->logger && this->logger->running);
}
void ExperimentTrial::finish()
{
if(this->logger) {
this->logger->exit();
this->logger->wait(1000); // 1 sec
}
this->hide();
}
void ExperimentTrial::renderContent(Luminous::RenderContext & r)
{
// draw arrows from target{1,2} to indicate direction to rotate
Nimble::Vector2 v[] = { target1, target2 };
Nimble::Vector2 center = 0.5f*(v[0]+v[1]);
Radiant::Color c(1.0f, 0.5f, 0.3f, 0.9f);
float sz = size * 1.5f;
float w = 2.0f;
for(size_t i=0; i < 2; ++i) {
Nimble::Vector2 t = (v[i]-center).perpendicular().normalize(sz);
if(direction == Counterclockwise || target1.y <= target2.y)
t *= -1;
Nimble::Vector2 p = (center-v[i]).normalize(sz/5);
Nimble::Vector2 cps[] = {
v[i],
v[i]+0.5f*t - p,
v[i]+t,
v[i]+t+p
};
Nimble::Vector2 tangent = (cps[3] - cps[2]).normalize(sz/8);
Nimble::Vector2 dir1 = tangent;
dir1.rotate(Nimble::Math::PI * (4/5.0f));
Nimble::Vector2 dir2 = tangent;
dir2.rotate(Nimble::Math::PI * (-4/5.0f));
r.drawCurve(cps, w, c.data());
Nimble::Vector2 tip[] = {
cps[3]+tangent,
cps[3]+tangent+dir1,
cps[3]+tangent+dir2,
cps[3]+tangent
};
r.drawPolyLine(tip, 4, w, c.data());
}
}
void ExperimentTrial::processMessage(const char *id, Radiant::BinaryData &data)
{
if( strcmp( id , "check_targets") == 0 ) {
if( this->first->isTargetReached() && this->second->isTargetReached() ) {
this->logger->append("Targets reached\n");
// close logs
finish();
Resonant::ModuleSamplePlayer * player = Resonant::DSPNetwork::instance()->samplePlayer();
player->playSample("task_done.wav",
1.0f,
1.0f,
0,
0,
false);
eventSend("next_trial");
}
}
}
void ExperimentTrial::setApplication(MultiWidgets::GrabManager *application)
{
this->application = application;
// start logging
QString path = "./logs/log-" + QDateTime::currentDateTime().toString("yyyyMMddhhmm") + "-" + QString::fromStdString(filename) + "-" + QString::number( this->id ) + "-";
// start checking if connection is lost
//QFile * file1 = new QFile( path + "connection" );
// start generic log
QFile * file = new QFile( path + "log" );
this->logger = new LogThread(this, this->application, file );
this->logger->start();
QString header( QString::number( this->id ) + " >> x " + QString::number( target1.x ) + " >> y " + QString::number( target1.y ) + " >> d " + QString::number( this->distance ) + " >> size " + QString::number( this->size ) + " >> angle " + QString::number( this->angle ) + "\n");
this->logger->append( header.toStdString() );
}
ExperimentTrial::~ExperimentTrial()
{
// remove threads
delete this->logger;
}