iCub-main
Loading...
Searching...
No Matches
objectsthread.h
Go to the documentation of this file.
1/*
2 * objectsthread.h
3 */
4
5/*
6 * Copyright (C) 2009 RobotCub Consortium
7 * Author: Alessandro Scalzo alessandro.scalzo@iit.it
8 * CopyPolicy: Released under the terms of the GNU GPL v2.0.
9 *
10 * Based on:
11 *
12 * Qavimator
13 * Copyright (C) 2006 by Zi Ree *
14 * Zi Ree @ SecondLife *
15 * Released under the terms of the GNU GPL v2.0.
16 */
17
18#ifndef OBJECTSTHREAD_H
19#define OBJECTSTHREAD_H
20
21#include <string>
22#include <qstring.h>
23#include <qthread.h>
24#include <vector>
25#include <yarp/os/Log.h>
26#include <yarp/sig/Vector.h>
28
29#include "bvhnoderoot.h"
30
31#ifdef __APPLE__
32#include <OpenGL/glu.h>
33#else
34#include <GL/glu.h>
35#endif
36
37#include <string.h>
38extern std::string GUI_NAME;
39
41{
42public:
43 ObjectsManager(const char *objPortName,const char *texPortName,const char *forcePortName)
44 {
45 mObjPort.open((GUI_NAME+objPortName).c_str());
46 mTexPort.open((GUI_NAME+texPortName).c_str());
47 mForcePort.open((GUI_NAME+forcePortName).c_str());
48
49 mObjPort.setStrict();
50 mTexPort.setStrict();
51 mForcePort.setStrict();
52
53 /*
54 mPx=mPy=mPz=0.0;
55 mRx=mRy=mRz=0.0;
56 for (int i=0; i<3; ++i)
57 for (int j=0; j<3; ++j)
58 R[i][j]=(double)(i==j);
59 */
60 }
61
63 {
64 mObjPort.interrupt();
65 mObjPort.close();
66
67 mTexPort.interrupt();
68 mTexPort.close();
69
70 mForcePort.interrupt();
71 mForcePort.close();
72
73 for (int i=0; i<(int)mObjectsRoot.size(); ++i)
74 {
75 if (mObjectsRoot[i]) delete mObjectsRoot[i];
76 }
77
78 for (int i=0; i<(int)mObjectsWorld.size(); ++i)
79 {
80 if (mObjectsWorld[i]) delete mObjectsWorld[i];
81 }
82
83 for (int i=0; i<(int)mTrajectoriesRoot.size(); ++i)
84 {
85 if (mTrajectoriesRoot[i]) delete mTrajectoriesRoot[i];
86 }
87
88 for (int i=0; i<(int)mTrajectoriesWorld.size(); ++i)
89 {
90 if (mTrajectoriesWorld[i]) delete mTrajectoriesWorld[i];
91 }
92 }
93
95 {
96 mAB=ab;
97 }
98
99 inline void manage(yarp::os::Bottle *msg);
100 inline void manage(yarp::sig::VectorOf<unsigned char> *img);
101 inline void manage(iCub::skinDynLib::skinContactList &forces);
102
103 /*
104 void readEncoders(double *enc)
105 {
106 static const double DEG2RAD=M_PI/180.0;
107
108 mPx=enc[3];
109 mPy=enc[4];
110 mPz=enc[5];
111
112 mRz=enc[0];
113 mRy=enc[1];
114 mRx=enc[2];
115
116 double Rz=DEG2RAD*mRz;
117 double Ry=DEG2RAD*mRy;
118 double Rx=DEG2RAD*mRx;
119
120 double cA=cos(Rz),sA=sin(Rz);
121 double cB=cos(Ry),sB=sin(Ry);
122 double cY=cos(Rx),sY=sin(Rx);
123
124 R[0][0]=cA*cB; R[0][1]=cA*sB*sY-sA*cY; R[0][2]=cA*sB*cY+sA*sY;
125 R[1][0]=sA*cB; R[1][1]=sA*sB*sY+cA*cY; R[1][2]=sA*sB*cY-cA*sY;
126 R[2][0]= -sB; R[2][1]= cB*sY; R[2][2]= cB*cY;
127 }
128 */
129
130 void update()
131 {
132 // objects
133 for (yarp::os::Bottle *botObj; botObj=mObjPort.read(false);)
134 {
135 manage(botObj);
136 }
137
138 // textures
139 for (yarp::sig::VectorOf<unsigned char> *imgTex; imgTex=mTexPort.read(false);)
140 {
141 manage(imgTex);
142 }
143
144 for (iCub::skinDynLib::skinContactList *forces; forces=mForcePort.read(false);)
145 {
146 manage(*forces);
147 }
148 }
149
151 {
152 for (int i=0; i<(int)mObjectsRoot.size(); ++i)
153 {
154 if (mObjectsRoot[i]) mObjectsRoot[i]->draw();
155 }
156
157 for (int i=0; i<(int)mTrajectoriesRoot.size(); ++i)
158 {
159 if(mTrajectoriesRoot[i]) mTrajectoriesRoot[i]->draw();
160 }
161 }
162
164 {
165 for (int i=0; i<(int)mObjectsWorld.size(); ++i)
166 {
167 if (mObjectsWorld[i]) mObjectsWorld[i]->draw();
168 }
169
170 for (int i=0; i<(int)mTrajectoriesWorld.size(); ++i)
171 {
172 if(mTrajectoriesWorld[i]) mTrajectoriesWorld[i]->draw();
173 }
174 }
175
176protected:
177 std::vector<VisionObj*> mObjectsRoot;
178 std::vector<VisionObj*> mObjectsWorld;
179 std::vector<TrajectoryObj*> mTrajectoriesRoot;
180 std::vector<TrajectoryObj*> mTrajectoriesWorld;
181
182 //double mPx,mPy,mPz;
183 //double mRx,mRy,mRz;
184 //double R[3][3];
185
187
188 yarp::os::BufferedPort<yarp::os::Bottle> mObjPort;
189 yarp::os::BufferedPort<yarp::sig::VectorOf<unsigned char> > mTexPort;
190 yarp::os::BufferedPort<iCub::skinDynLib::skinContactList> mForcePort;
191};
192
193void ObjectsManager::manage(yarp::os::Bottle *msg)
194{
195 std::string cmd=msg->get(0).asString();
196
197 if (cmd=="reset")
198 {
199 for (int i=0; i<(int)mObjectsRoot.size(); ++i) delete mObjectsRoot[i];
200 mObjectsRoot.clear();
201
202 for (int i=0; i<(int)mObjectsWorld.size(); ++i) delete mObjectsWorld[i];
203 mObjectsWorld.clear();
204
205 for (int i=0; i<(int)mTrajectoriesRoot.size(); ++i) delete mTrajectoriesRoot[i];
206 mTrajectoriesRoot.clear();
207
208 for (int i=0; i<(int)mTrajectoriesWorld.size(); ++i) delete mTrajectoriesWorld[i];
209 mTrajectoriesWorld.clear();
210
211 return;
212 }
213
214 if (cmd=="delete")
215 {
216 std::string name(msg->get(1).asString().c_str());
217
218 int size=(int)mObjectsRoot.size();
219 for (int i=0; i<size; ++i)
220 {
221 if (mObjectsRoot[i] && *mObjectsRoot[i]==name)
222 {
223 --size;
224 delete mObjectsRoot[i];
225 for (int j=i; j<size; ++j) mObjectsRoot[j]=mObjectsRoot[j+1];
226 mObjectsRoot.resize(size);
227 return;
228 }
229 }
230
231 size=(int)mObjectsWorld.size();
232 for (int i=0; i<(int)mObjectsWorld.size(); ++i)
233 {
234 if (mObjectsWorld[i] && *mObjectsWorld[i]==name)
235 {
236 --size;
237 delete mObjectsWorld[i];
238 for (int j=i; j<size; ++j) mObjectsWorld[j]=mObjectsWorld[j+1];
239 mObjectsWorld.resize(size);
240 return;
241 }
242 }
243
244 size=(int)mTrajectoriesRoot.size();
245 for (int i=0; i<(int)mTrajectoriesRoot.size(); ++i)
246 {
247 if (mTrajectoriesRoot[i] && *mTrajectoriesRoot[i]==name)
248 {
249 --size;
250 delete mTrajectoriesRoot[i];
251 for (int j=i; j<size; ++j) mTrajectoriesRoot[j]=mTrajectoriesRoot[j+1];
252 mTrajectoriesRoot.resize(size);
253 return;
254 }
255 }
256
257 size=(int)mTrajectoriesWorld.size();
258 for (int i=0; i<(int)mTrajectoriesWorld.size(); ++i)
259 {
260 if (mTrajectoriesWorld[i] && *mTrajectoriesWorld[i]==name)
261 {
262 --size;
263 delete mTrajectoriesWorld[i];
264 for (int j=i; j<size; ++j) mTrajectoriesWorld[j]=mTrajectoriesWorld[j+1];
265 mTrajectoriesWorld.resize(size);
266 return;
267 }
268 }
269
270 return;
271 }
272
273 if (cmd=="object" || cmd=="object_with_label")
274 {
275 int idd=1;
276
277 std::string name(msg->get(idd++).asString().c_str());
278 std::string label(cmd=="object_with_label"?msg->get(idd++).asString().c_str():"");
279
280 double dx=msg->get(idd++).asFloat64();
281 double dy=msg->get(idd++).asFloat64();
282 double dz=msg->get(idd++).asFloat64();
283
284 double px=msg->get(idd++).asFloat64();
285 double py=msg->get(idd++).asFloat64();
286 double pz=msg->get(idd++).asFloat64();
287
288 double rx=msg->get(idd++).asFloat64();
289 double ry=msg->get(idd++).asFloat64();
290 double rz=msg->get(idd++).asFloat64();
291
292 int r=msg->get(idd++).asInt32();
293 int g=msg->get(idd++).asInt32();
294 int b=msg->get(idd++).asInt32();
295
296 double alpha=msg->get(idd++).asFloat64();
297
298 for (int i=0; i<(int)mObjectsRoot.size(); ++i)
299 {
300 if (mObjectsRoot[i] && *mObjectsRoot[i]==name)
301 {
302 mObjectsRoot[i]->set(dx,dy,dz,px,py,pz,rx,ry,rz,r,g,b,alpha,label);
303 return;
304 }
305 }
306
307 for (int i=0; i<(int)mObjectsWorld.size(); ++i)
308 {
309 if (mObjectsWorld[i] && *mObjectsWorld[i]==name)
310 {
311 mObjectsWorld[i]->set(dx,dy,dz,px,py,pz,rx,ry,rz,r,g,b,alpha,label);
312 return;
313 }
314 }
315
316 bool bWorld=(msg->size()>idd && msg->get(idd).asString()=="WORLD");
317
318 if (bWorld)
319 {
320 mObjectsWorld.push_back(new VisionObj(name,dx,dy,dz,px,py,pz,rx,ry,rz,r,g,b,alpha,label));
321 }
322 else
323 {
324 mObjectsRoot.push_back(new VisionObj(name,dx,dy,dz,px,py,pz,rx,ry,rz,r,g,b,alpha,label));
325 }
326
327 return;
328 }
329
330 if (cmd=="trajectory")
331 {
332 std::string name(msg->get(1).asString().c_str());
333 std::string label(msg->get(2).asString().c_str());
334
335 int bufflen=msg->get(3).asInt32();
336 double persistence=msg->get(4).asFloat64();
337
338 int R=msg->get(5).asInt32();
339 int G=msg->get(6).asInt32();
340 int B=msg->get(7).asInt32();
341
342 double alpha=msg->get(8).asFloat64();
343 GLfloat width=(GLfloat)msg->get(9).asFloat64();
344
345 for (int i=0; i<(int)mTrajectoriesRoot.size(); ++i)
346 {
347 if (mTrajectoriesRoot[i] && *mTrajectoriesRoot[i]==name)
348 {
349 mTrajectoriesRoot[i]->set(label,bufflen,persistence,R,G,B,alpha,width);
350 return;
351 }
352 }
353
354 for (int i=0; i<(int)mTrajectoriesWorld.size(); ++i)
355 {
356 if (mTrajectoriesWorld[i] && *mTrajectoriesWorld[i]==name)
357 {
358 mTrajectoriesWorld[i]->set(label,bufflen,persistence,R,G,B,alpha,width);
359 return;
360 }
361 }
362
363 bool bWorld=(msg->size()>10 && msg->get(10).asString()=="WORLD");
364
365 if (bWorld)
366 {
367 mTrajectoriesWorld.push_back(new TrajectoryObj(name,label,bufflen,persistence,R,G,B,alpha,width,bWorld));
368 }
369 else
370 {
371 mTrajectoriesRoot.push_back(new TrajectoryObj(name,label,bufflen,persistence,R,G,B,alpha,width,bWorld));
372 }
373
374 return;
375 }
376
377 if (cmd=="addpoint")
378 {
379 std::string name(msg->get(1).asString().c_str());
380
381 double x=msg->get(2).asFloat64();
382 double y=msg->get(3).asFloat64();
383 double z=msg->get(4).asFloat64();
384
385 for (int i=0; i<(int)mTrajectoriesRoot.size(); ++i)
386 {
387 if (mTrajectoriesRoot[i] && *mTrajectoriesRoot[i]==name)
388 {
389 mTrajectoriesRoot[i]->update(x,y,z);
390 return;
391 }
392 }
393
394 for (int i=0; i<(int)mTrajectoriesWorld.size(); ++i)
395 {
396 if (mTrajectoriesWorld[i] && *mTrajectoriesWorld[i]==name)
397 {
398 mTrajectoriesWorld[i]->update(x,y,z);
399 return;
400 }
401 }
402
403 return;
404 }
405}
406
407void ObjectsManager::manage(yarp::sig::VectorOf<unsigned char> *img)
408{
409 yarp::sig::VectorOf<unsigned char> &texture=*img;
410
411 int xdim=texture[0];
412 int ydim=texture[1];
413 std::string objName;
414
415 int c;
416 for (c=2; texture[c]; ++c)
417 {
418 objName+=texture[c];
419 }
420
421 int xdim2,ydim2;
422 for (xdim2=1; xdim2<xdim; xdim2*=2);
423 for (ydim2=1; ydim2<ydim; ydim2*=2);
424
425 unsigned char *buffer=new unsigned char[3*xdim2*ydim2];
426 int index=0;
427
428 for (int y=0; y<ydim2; ++y)
429 {
430 double v=double(y)/double(ydim2);
431 double oy=v*double(ydim);
432
433 int y0=int(oy);
434 double b1=oy-double(y0);
435 double b0=1.0-b1;
436
437 for (int x=0; x<xdim2; ++x)
438 {
439 double u=double(x)/double(xdim2);
440 double ox=u*double(xdim);
441
442 int x0=int(ox);
443 double a1=ox-double(x0);
444 double a0=1.0-a1;
445
446 int p00=c+x0+y0*xdim;
447 int p10=p00+1;
448
449 if (x0==xdim-1) p10=p00;
450
451 int p01=p00+xdim;
452 int p11=p10+xdim;
453
454 if (y0==ydim-1)
455 {
456 p01=p00;
457 p11=p10;
458 }
459
460 double grey=a0*b0*double(texture[p00])+a0*b1*double(texture[p01])+a1*b0*double(texture[p10])+a1*b1*double(texture[p11]);
461 if (grey<0.0) grey=0.0; else if (grey>255.0) grey=255.0;
462
463 unsigned char G=(unsigned char)grey;
464
465 buffer[index++]=G;
466 buffer[index++]=G;
467 buffer[index++]=G;
468 }
469 }
470
471 for (int i=0; i<(int)mObjectsRoot.size(); ++i)
472 {
473 if (mObjectsRoot[i] && *mObjectsRoot[i]==objName)
474 {
475 mObjectsRoot[i]->mW=xdim2;
476 mObjectsRoot[i]->mH=ydim2;
477 mObjectsRoot[i]->mTextureBuffer=buffer;
478 return;
479 }
480 }
481
482 for (int i=0; i<(int)mObjectsWorld.size(); ++i)
483 {
484 if (mObjectsWorld[i] && *mObjectsWorld[i]==objName)
485 {
486 mObjectsWorld[i]->mW=xdim2;
487 mObjectsWorld[i]->mH=ydim2;
488 mObjectsWorld[i]->mTextureBuffer=buffer;
489 return;
490 }
491 }
492}
493
495{
496 for (int p=0; p<8; ++p)
497 {
498 for (int l=0; l<8; ++l)
499 {
500 if (mAB[p][l]) mAB[p][l]->clearArrows();
501 }
502 }
503
504 for (int i=0; i<(int)forces.size(); ++i)
505 {
506 yarp::sig::Vector P=forces[i].getCoP();
507
508 double f=forces[i].getForceModule();
509 yarp::sig::Vector F=forces[i].getForceDirection();
510 yarp::sig::Vector M=forces[i].getMoment();
511
512 int p=forces[i].getBodyPart();
513 int l=forces[i].getLinkNumber();
514
515 if (p >= 0 && p < 8 && l >= 0 && l < 8)
516 {
517 if (mAB[p][l])
518 {
519 mAB[p][l]->addArrow(new ForceArrow(P[0],P[1],P[2],f,F[0],F[1],F[2],M[0],M[1],M[2]));
520 }
521 }
522 else
523 {
524 yError("iCubGui: unexpected body part %d or link index %d contained in received skinContactList.", p, l);
525 }
526
527 }
528}
529
530#endif
void addArrow(ForceArrow *pArrow)
Definition bvhnode.h:239
void clearArrows()
Definition bvhnode.h:244
yarp::os::BufferedPort< yarp::os::Bottle > mObjPort
void setAddressBook(BVHNode ***ab)
void drawWorldObjects()
void manage(yarp::os::Bottle *msg)
std::vector< TrajectoryObj * > mTrajectoriesWorld
yarp::os::BufferedPort< iCub::skinDynLib::skinContactList > mForcePort
ObjectsManager(const char *objPortName, const char *texPortName, const char *forcePortName)
std::vector< VisionObj * > mObjectsRoot
std::vector< TrajectoryObj * > mTrajectoriesRoot
yarp::os::BufferedPort< yarp::sig::VectorOf< unsigned char > > mTexPort
BVHNode *** mAB
std::vector< VisionObj * > mObjectsWorld
Class representing a list of external contacts acting on the iCub' skin.
virtual bool read(yarp::os::ConnectionReader &connection)
cmd
Definition dataTypes.h:30
std::string GUI_NAME