-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuio.cpp
157 lines (119 loc) · 5.5 KB
/
tuio.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
#include "tuio.h"
#include "common.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
Tuio::Tuio(int port, SharedData *shared_data)
: shared_data(shared_data)
{
osc_receiver = new UdpReceiver(port);
//osc_receiver = new TcpReceiver("127.0.0.1",3333);
//osc_receiver = new TcpReceiver("192.168.1.69",3333);
tuioClient = new TuioClient(osc_receiver);
tuioClient->addTuioListener(this);
tuioClient->connect();
if (!tuioClient->isConnected()) {
std::cerr<<"Error connecting! (port: "<<port<<')'<<std::endl;
std::exit(TUIO_ERROR_EXIT_CODE);
}
// current date/time based on current system
time_t now = time(0);
// convert now to string form
char* dt = ctime(&now);
// remove final '\n'
size_t end = strlen(dt);
dt[end-1] = '\0';
position_log = std::ofstream("positions log " + std::string(dt) + ".csv");
position_log << "ID, time, x, y, speed, orientation" << std::endl;
}
std::tuple<float, float> Tuio::getPositionFromId(const int id) {
std::list<TuioObject*> objectList = tuioClient->getTuioObjects();
tuioClient->lockObjectList();
for (std::list<TuioObject*>::iterator iter = objectList.begin(); iter!=objectList.end(); iter++) {
TuioObject *tuioObject = (*iter);
if (tuioObject->getSymbolID() == id) {
float x,y;
/*
std::list<TuioPoint> path = tuioObject->getPath();
TuioPoint last_point = path.back();
x = last_point.getX();
y = last_point.getY();
*/
TuioPoint position = tuioObject->getPosition();
x = position.getX();
y = position.getY();
tuioClient->unlockObjectList();
return std::make_tuple(x, y);
}
}
tuioClient->unlockObjectList();
throw IDNotFound(id);
}
void Tuio::insetTuioObjectInData(TuioObject *tobj) {
RobotPath *path;
try {
path = shared_data->get(tobj->getSymbolID());
} catch (std::out_of_range &e) {
return;
}
path->insertElem(tobj->getTuioTime().getTotalMilliseconds(), tobj->getX(), tobj->getY());
// ID
position_log << tobj->getSymbolID() << ", ";
// time
position_log << tobj->getTuioTime().getTotalMilliseconds() << ", ";
// x
position_log << tobj->getX() << ", ";
// y
position_log << tobj->getY() << ", ";
// speed x
position_log << tobj->getMotionSpeed() << ", ";
// orientation
position_log << tobj->getAngleDegrees() << std::endl;
}
void Tuio::addTuioObject(TuioObject *tobj) {
if (verbose)
std::cout << "add obj " << tobj->getSymbolID() << " (" << tobj->getSessionID() << "/"<< tobj->getTuioSourceID() << ") "<< tobj->getX() << " " << tobj->getY() << " " << tobj->getAngle() << std::endl;
insetTuioObjectInData(tobj);
}
void Tuio::updateTuioObject(TuioObject *tobj) {
if (verbose)
std::cout << "set obj " << tobj->getSymbolID() << " (" << tobj->getSessionID() << "/"<< tobj->getTuioSourceID() << ") "<< tobj->getX() << " " << tobj->getY() << " " << tobj->getAngle()
<< " " << tobj->getMotionSpeed() << " " << tobj->getRotationSpeed() << " " << tobj->getMotionAccel() << " " << tobj->getRotationAccel() << std::endl;
insetTuioObjectInData(tobj);
}
void Tuio::removeTuioObject(TuioObject *tobj) {
if (verbose)
std::cout << "del obj " << tobj->getSymbolID() << " (" << tobj->getSessionID() << "/"<< tobj->getTuioSourceID() << ")" << std::endl;
}
void Tuio::addTuioCursor(TuioCursor *tcur) {
if (verbose)
std::cout << "add cur " << tcur->getCursorID() << " (" << tcur->getSessionID() << "/"<< tcur->getTuioSourceID() << ") " << tcur->getX() << " " << tcur->getY() << std::endl;
}
void Tuio::updateTuioCursor(TuioCursor *tcur) {
if (verbose)
std::cout << "set cur " << tcur->getCursorID() << " (" << tcur->getSessionID() << "/"<< tcur->getTuioSourceID() << ") " << tcur->getX() << " " << tcur->getY()
<< " " << tcur->getMotionSpeed() << " " << tcur->getMotionAccel() << " " << std::endl;
}
void Tuio::removeTuioCursor(TuioCursor *tcur) {
if (verbose)
std::cout << "del cur " << tcur->getCursorID() << " (" << tcur->getSessionID() << "/"<< tcur->getTuioSourceID() << ")" << std::endl;
}
void Tuio::addTuioBlob(TuioBlob *tblb) {
if (verbose)
std::cout << "add blb " << tblb->getBlobID() << " (" << tblb->getSessionID() << "/"<< tblb->getTuioSourceID()<< ") "<< tblb->getX() << " " << tblb->getY() << " " << tblb->getAngle() << " " << tblb->getWidth() << " " << tblb->getHeight() << " " << tblb->getArea() << std::endl;
}
void Tuio::updateTuioBlob(TuioBlob *tblb) {
if (verbose)
std::cout << "set blb " << tblb->getBlobID() << " (" << tblb->getSessionID() << "/"<< tblb->getTuioSourceID() << ") "<< tblb->getX() << " " << tblb->getY() << " " << tblb->getAngle() << " "<< tblb->getWidth() << " " << tblb->getHeight() << " " << tblb->getArea()
<< " " << tblb->getMotionSpeed() << " " << tblb->getRotationSpeed() << " " << tblb->getMotionAccel() << " " << tblb->getRotationAccel() << std::endl;
}
void Tuio::removeTuioBlob(TuioBlob *tblb) {
if (verbose)
std::cout << "del blb " << tblb->getBlobID() << " (" << tblb->getSessionID() << "/"<< tblb->getTuioSourceID() << ")" << std::endl;
}
void Tuio::refresh(TUIO::TuioTime frameTime)
{
if (verbose)
std::cout << "REFRESH? " << frameTime.getTotalMilliseconds() << std::endl;
position_log.flush();
}