iCub-main
visionobj.h
Go to the documentation of this file.
1 /*
2  * visionobj.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 VISIONOBJ_H
19 #define VISIONOBJ_H
20 
21 #include <qstring.h>
22 #include <yarp/os/Time.h>
23 
24 #ifdef __APPLE__
25 #include <OpenGL/glu.h>
26 #include <GLUT/glut.h> // Include GLUT, OpenGL, and GLU libraries
27 #else
28 #include <GL/glu.h>
29 #include <GL/glut.h> // Include GLUT, OpenGL, and GLU libraries
30 #endif
31 
32 #include <stdio.h> // Standard Input\Output C Library
33 #include <stdarg.h> // To use functions with variables arguments
34 #include <stdlib.h> // for malloc
35 
36 //GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_24;
37 
38 class GuiObj
39 {
40 public:
41  GuiObj(std::string& name,int r,int g,int b,double alpha)
42  {
43  mName=name;
44  mR=double(r)/255.0; mG=double(g)/255.0; mB=double(b)/255.0;
45  mAlpha=alpha;
46  }
47 
48  virtual ~GuiObj(){}
49  virtual void draw()=0;
50 
51  bool operator==(std::string &name)
52  {
53  return mName==name;
54  }
55 
56 protected:
57  void printw(float x,float y,float z,const char* text)
58  {
59  glDisable(GL_LIGHTING);
60  glRasterPos3f(x,y,z);
61  for (int i=0; text[i]!='\0'; ++i)
62  {
63  glutBitmapCharacter(GLUT_BITMAP_9_BY_15,text[i]);
64  }
65  glEnable(GL_LIGHTING);
66  }
67 
68  std::string mName;
69  double mR,mG,mB,mAlpha;
70 };
71 
72 class TrajectoryObj : public GuiObj
73 {
74 public:
75  TrajectoryObj(std::string name,std::string label,int bufflen,double persistence,int r,int g,int b,double alpha,GLfloat width,bool world)
76  : GuiObj(name,r,g,b,alpha)
77  {
78  mLabel=label;
79  mWidth=width;
80  mPersistence=persistence;
81 
82  bWorld=world;
83 
84  mBufflen=bufflen;
85 
86  mIndex=0;
87  mFull=false;
88 
89  mX=new double[mBufflen];
90  mY=new double[mBufflen];
91  mZ=new double[mBufflen];
92  mT=new double[mBufflen];
93  }
94 
95  void set(std::string& label,int bufflen,double persistence,int r,int g,int b,double alpha,double width)
96  {
97  mLabel=label;
98  mWidth=width;
99  mPersistence=persistence;
100  mR=double(r)/255.0; mG=double(g)/255.0; mB=double(b)/255.0;
101  mAlpha=alpha;
102 
103  if (mBufflen==bufflen) return;
104 
105  double *tX=new double[bufflen];
106  double *tY=new double[bufflen];
107  double *tZ=new double[bufflen];
108  double *tT=new double[bufflen];
109 
110  int n=bufflen<mBufflen?bufflen:mBufflen;
111 
112  for (int i=0; i<n; ++n)
113  {
114  tX[i]=mX[(mIndex+i)%mBufflen];
115  tY[i]=mY[(mIndex+i)%mBufflen];
116  tZ[i]=mZ[(mIndex+i)%mBufflen];
117  tT[i]=mT[(mIndex+i)%mBufflen];
118  }
119 
120  delete [] mX; mX=tX;
121  delete [] mY; mY=tY;
122  delete [] mZ; mZ=tZ;
123  delete [] mT; mT=tT;
124 
125  if (mFull)
126  {
127  if (bufflen>mBufflen) mFull=false;
128  }
129  else
130  {
131  if (bufflen<=mIndex) mFull=true;
132  }
133 
134  mIndex=0;
135 
136  mBufflen=bufflen;
137  }
138 
139  void update(double x,double y,double z)
140  {
141  mX[mIndex]=x;
142  mY[mIndex]=y;
143  mZ[mIndex]=z;
144  mT[mIndex]=yarp::os::Time::now();
145 
146  if (++mIndex==mBufflen)
147  {
148  mIndex=0;
149  mFull=true;
150  }
151  }
152 
153  void draw()
154  {
155  glColor4f(mR,mG,mB,1.0);
156  glLineWidth(mWidth);
157 
158  double now=yarp::os::Time::now();
159 
160  glBegin(GL_LINE_STRIP);
161  if (mFull)
162  {
163  for (int i=mIndex; i<mBufflen; ++i)
164  {
165  if (now-mT[i]<mPersistence) glVertex3d(mX[i],mY[i],mZ[i]);
166  }
167  }
168  for (int i=0; i<mIndex; ++i)
169  {
170  if (now-mT[i]<mPersistence) glVertex3d(mX[i],mY[i],mZ[i]);
171  }
172  glEnd();
173 
174  printw(mX[mIndex-1],mY[mIndex-1],mZ[mIndex-1]+50.0,mLabel.c_str());
175  }
176 
178  {
179  delete [] mX;
180  delete [] mY;
181  delete [] mZ;
182  delete [] mT;
183  }
184 
185  bool bWorld;
186 
187 protected:
188  int mIndex;
189  bool mFull;
190  int mBufflen;
191  double mPersistence;
192  double *mX;
193  double *mY;
194  double *mZ;
195  double *mT;
196  std::string mLabel;
197  GLfloat mWidth;
198 };
199 
200 class VisionObj : public GuiObj
201 {
202 public:
203  VisionObj(std::string name,
204  double dimx,double dimy,double dimz,
205  double posx,double posy,double posz,
206  double rotx,double roty,double rotz,
207  int r,int g,int b,double alpha,std::string& label)
208  : GuiObj(name,r,g,b,alpha)
209  {
210  mW=mH=0;
211  nTexID=0;
212  bTextured=false;
213  mTextureBuffer=NULL;
214 
215  set(dimx,dimy,dimz,posx,posy,posz,rotx,roty,rotz,r,g,b,alpha,label);
216  }
217 
219  {
220  if (bTextured) glDeleteTextures(1,&nTexID);
221  bTextured=false;
222  }
223 
224  void set(double dimx,double dimy,double dimz,
225  double posx,double posy,double posz,
226  double rotx,double roty,double rotz,
227  int r,int g,int b,double alpha,std::string& label)
228  {
229  mDimx=0.5*dimx; mDimy=0.5*dimy; mDimz=0.5*dimz;
230  mPosx=posx; mPosy=posy; mPosz=posz;
231  mRotx=rotx; mRoty=roty; mRotz=rotz;
232  mR=double(r)/255.0; mG=double(g)/255.0; mB=double(b)/255.0;
233  mAlpha=alpha;
234  optional_label=label;
235  }
236 
237  void draw()
238  {
239  if (mTextureBuffer!=NULL)
240  {
241  glEnable(GL_TEXTURE_2D);
242 
243  if (bTextured) glDeleteTextures(1,&nTexID);
244 
245  GLuint texture;
246  glGenTextures(1,&texture);
247  nTexID=texture;
248  bTextured=true;
249 
250  glBindTexture(GL_TEXTURE_2D,texture);
251  gluBuild2DMipmaps(GL_TEXTURE_2D,3,mW,mH,GL_RGB,GL_UNSIGNED_BYTE,mTextureBuffer);
252  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
253  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
254  glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
255 
256  delete [] mTextureBuffer;
257  mTextureBuffer=NULL;
258  glDisable(GL_TEXTURE_2D);
259  }
260 
261  glPushMatrix();
262  glTranslated(mPosx,mPosy,mPosz);
263  std::string text = mName + optional_label;
264  printw(0.0,0.0,1.2*mDimz,text.c_str());
265 
266  glRotated(mRotz,0.0,0.0,1.0);
267  glRotated(mRoty,0.0,1.0,0.0);
268  glRotated(mRotx,1.0,0.0,0.0);
269 
270  glColor4d(mR,mG,mB,mAlpha);
271 
272  if (mName == "ball")
273  {
274  if (bTextured)
275  {
276  glEnable(GL_TEXTURE_2D);
277  glBindTexture(GL_TEXTURE_2D,nTexID);
278  }
279 
280  glBegin(GL_QUADS);
281 
282  glutSolidSphere(mDimx, 20.0, 20.0);
283 
284  glEnd();
285 
286  if (bTextured)
287  {
288  glDisable(GL_TEXTURE_2D);
289  }
290  }
291  else if (bTextured)
292  {
293  glEnable(GL_TEXTURE_2D);
294  glBindTexture(GL_TEXTURE_2D,nTexID);
295 
296  glBegin(GL_QUADS);
297 
298  glTexCoord2f(1.0,0.0); glVertex3f( mDimx, mDimy, mDimz);
299  glTexCoord2f(0.0,0.0); glVertex3f( mDimx,-mDimy, mDimz);
300  glTexCoord2f(0.0,1.0); glVertex3f( mDimx,-mDimy,-mDimz);
301  glTexCoord2f(1.0,1.0); glVertex3f( mDimx, mDimy,-mDimz);
302 
303  glTexCoord2f(0.0,0.0); glVertex3f( mDimx, mDimy, mDimz);
304  glTexCoord2f(0.0,1.0); glVertex3f( mDimx, mDimy,-mDimz);
305  glTexCoord2f(1.0,1.0); glVertex3f(-mDimx, mDimy,-mDimz);
306  glTexCoord2f(1.0,0.0); glVertex3f(-mDimx, mDimy, mDimz);
307 
308  glTexCoord2f(1.0,0.0); glVertex3f( mDimx,-mDimy, mDimz);
309  glTexCoord2f(0.0,0.0); glVertex3f(-mDimx,-mDimy, mDimz);
310  glTexCoord2f(0.0,1.0); glVertex3f(-mDimx,-mDimy,-mDimz);
311  glTexCoord2f(1.0,1.0); glVertex3f( mDimx,-mDimy,-mDimz);
312 
313  glTexCoord2f(0.0,1.0); glVertex3f(-mDimx, mDimy,-mDimz);
314  glTexCoord2f(1.0,1.0); glVertex3f(-mDimx,-mDimy,-mDimz);
315  glTexCoord2f(1.0,0.0); glVertex3f(-mDimx,-mDimy, mDimz);
316  glTexCoord2f(0.0,0.0); glVertex3f(-mDimx, mDimy, mDimz);
317 
318  glVertex3f( mDimx, mDimy, mDimz);
319  glVertex3f(-mDimx, mDimy, mDimz);
320  glVertex3f(-mDimx,-mDimy, mDimz);
321  glVertex3f( mDimx,-mDimy, mDimz);
322 
323  glVertex3f( mDimx,-mDimy,-mDimz);
324  glVertex3f(-mDimx,-mDimy,-mDimz);
325  glVertex3f(-mDimx, mDimy,-mDimz);
326  glVertex3f( mDimx, mDimy,-mDimz);
327 
328  glEnd();
329 
330  glDisable(GL_TEXTURE_2D);
331  }
332  else
333  {
334  glBegin(GL_QUADS);
335  glVertex3f( mDimx, mDimy, mDimz);
336  glVertex3f( mDimx,-mDimy, mDimz);
337  glVertex3f( mDimx,-mDimy,-mDimz);
338  glVertex3f( mDimx, mDimy,-mDimz);
339 
340  glVertex3f( mDimx, mDimy, mDimz);
341  glVertex3f( mDimx, mDimy,-mDimz);
342  glVertex3f(-mDimx, mDimy,-mDimz);
343  glVertex3f(-mDimx, mDimy, mDimz);
344 
345  glVertex3f( mDimx, mDimy, mDimz);
346  glVertex3f(-mDimx, mDimy, mDimz);
347  glVertex3f(-mDimx,-mDimy, mDimz);
348  glVertex3f( mDimx,-mDimy, mDimz);
349 
350  glVertex3f( mDimx,-mDimy, mDimz);
351  glVertex3f(-mDimx,-mDimy, mDimz);
352  glVertex3f(-mDimx,-mDimy,-mDimz);
353  glVertex3f( mDimx,-mDimy,-mDimz);
354 
355  glVertex3f( mDimx,-mDimy,-mDimz);
356  glVertex3f(-mDimx,-mDimy,-mDimz);
357  glVertex3f(-mDimx, mDimy,-mDimz);
358  glVertex3f( mDimx, mDimy,-mDimz);
359 
360  glVertex3f(-mDimx, mDimy,-mDimz);
361  glVertex3f(-mDimx,-mDimy,-mDimz);
362  glVertex3f(-mDimx,-mDimy, mDimz);
363  glVertex3f(-mDimx, mDimy, mDimz);
364 
365  glEnd();
366  }
367 
368  glPopMatrix();
369  }
370 
371  int mW,mH;
372  bool bTextured;
373  unsigned char* mTextureBuffer;
374  std::string optional_label;
375 
376 protected:
377  GLuint nTexID;
378  double mDimx,mDimy,mDimz;
379  double mPosx,mPosy,mPosz;
380  double mRotx,mRoty,mRotz;
381 };
382 
383 #endif
bool operator==(std::string &name)
Definition: visionobj.h:51
virtual void draw()=0
virtual ~GuiObj()
Definition: visionobj.h:48
double mR
Definition: visionobj.h:69
double mG
Definition: visionobj.h:69
std::string mName
Definition: visionobj.h:68
GuiObj(std::string &name, int r, int g, int b, double alpha)
Definition: visionobj.h:41
void printw(float x, float y, float z, const char *text)
Definition: visionobj.h:57
double mB
Definition: visionobj.h:69
double mAlpha
Definition: visionobj.h:69
double * mT
Definition: visionobj.h:195
TrajectoryObj(std::string name, std::string label, int bufflen, double persistence, int r, int g, int b, double alpha, GLfloat width, bool world)
Definition: visionobj.h:75
void set(std::string &label, int bufflen, double persistence, int r, int g, int b, double alpha, double width)
Definition: visionobj.h:95
double * mZ
Definition: visionobj.h:194
void draw()
Definition: visionobj.h:153
std::string mLabel
Definition: visionobj.h:196
GLfloat mWidth
Definition: visionobj.h:197
double mPersistence
Definition: visionobj.h:191
void update(double x, double y, double z)
Definition: visionobj.h:139
double * mY
Definition: visionobj.h:193
double * mX
Definition: visionobj.h:192
double mPosx
Definition: visionobj.h:379
std::string optional_label
Definition: visionobj.h:374
VisionObj(std::string name, double dimx, double dimy, double dimz, double posx, double posy, double posz, double rotx, double roty, double rotz, int r, int g, int b, double alpha, std::string &label)
Definition: visionobj.h:203
double mRoty
Definition: visionobj.h:380
double mRotx
Definition: visionobj.h:380
double mDimz
Definition: visionobj.h:378
double mPosy
Definition: visionobj.h:379
double mRotz
Definition: visionobj.h:380
double mPosz
Definition: visionobj.h:379
double mDimx
Definition: visionobj.h:378
bool bTextured
Definition: visionobj.h:372
void draw()
Definition: visionobj.h:237
unsigned char * mTextureBuffer
Definition: visionobj.h:373
GLuint nTexID
Definition: visionobj.h:377
~VisionObj()
Definition: visionobj.h:218
double mDimy
Definition: visionobj.h:378
void set(double dimx, double dimy, double dimz, double posx, double posy, double posz, double rotx, double roty, double rotz, int r, int g, int b, double alpha, std::string &label)
Definition: visionobj.h:224
int n