GCOP  1.0
body2dview.h
Go to the documentation of this file.
00001 #ifndef GCOP_BODY2DVIEW_H
00002 #define GCOP_BODY2DVIEW_H
00003 
00004 #include "body2d.h"
00005 #include "systemview.h"
00006 #include "viewer.h"
00007 #include "se2.h"
00008 
00009 namespace gcop {
00010   using namespace Eigen;
00011   
00012   template<int c = 3>
00013     class Body2dView : public SystemView< Body2dState, Matrix<double, c, 1> > {
00014   public:
00015   
00016   typedef Matrix<double, c, 1> Vectorcd;
00017 
00022   Body2dView(const Body2d<c> &sys);
00023   
00029   Body2dView(const Body2d<c> &sys,
00030              vector<Body2dState > *xs = 0,
00031              vector<Vectorcd> *us = 0);
00032   
00033   virtual ~Body2dView();
00034     
00035 
00036     void Render(const pair<Matrix3d, Vector3d> *x,
00037                 const Vectorcd *u = 0);
00038    
00039     void Render(const vector<pair<Matrix3d, Vector3d> > *xs, 
00040                 const vector<Vectorcd> *us = 0,
00041                 bool rs = true,
00042                 int is = -1, int ie = -1,
00043                 int dis = 1, int dit = 1,
00044                 bool dl = false);
00045     
00046     const Body2d<c> &sys;
00047 
00048     GLUquadricObj *qobj;
00049 
00050     double forceScale; 
00051   };
00052 
00053   typedef Matrix<double, 6, 1> Vector6d;
00054   
00055   template<int c>
00056     Body2dView<c>::Body2dView(const Body2d<c> &sys) : 
00057     SystemView<Body2dState, Vectorcd>("Body2d"), sys(sys)
00058   {
00059     this->rgba[0] = .5;
00060     this->rgba[1] = .5;
00061     this->rgba[2] = .5;
00062     this->rgba[3] = 0.5;
00063     this->lineWidth = 2;
00064     qobj = gluNewQuadric();
00065     forceScale = 1;
00066   }
00067   
00068   template<int c>
00069     Body2dView<c>::Body2dView(const Body2d<c> &sys, 
00070                            vector< Body2dState > *xs,
00071                            vector<Vectorcd> *us) : 
00072     SystemView<Body2dState, Vectorcd>("Body2d", xs, us), sys(sys) 
00073   {
00074     this->rgba[0] = 0.5;
00075     this->rgba[1] = 0.5;
00076     this->rgba[2] = 0.5;
00077     this->rgba[3] = 0;
00078     this->lineWidth = 2;
00079     qobj = gluNewQuadric();
00080     forceScale = 1;
00081   }
00082   
00083   template<int c>
00084 Body2dView<c>::~Body2dView()
00085 {
00086   free(qobj);
00087 }
00088 
00089 
00090 static void RenderUuv(GLUquadricObj *qobj, double l, double c)
00091 {
00092 
00093   glPushMatrix();
00094 
00095   glRotated(90, 0,1,0);
00096   glTranslated(0,0,-l);
00097 
00098   // main body
00099   //  glColor3f(.9,.9,.9);
00100   gluCylinder(qobj, 0.6*c, 0.6*c, 2*l, 10, 10);
00101   gluDisk(qobj, c/6, c/2, 10, 10);
00102 
00103   // tip
00104   //  glColor3f(0,.4,.4);
00105   glPushMatrix();
00106   glTranslated(0,0,2*l);
00107   glutSolidSphere(0.58*c, 10, 10);
00108   glPopMatrix();
00109 
00110   //  glColor3f(.4,.4,.4);
00111   /*
00112   glPushMatrix();
00113   glTranslated(0,0,c/3);
00114   glScaled(c*.33, c*1.6, c*.6);
00115   glutSolidSphere(1, 10, 10);
00116   glPopMatrix();
00117   */
00118 
00119   double r = c;
00120 
00121     glColor3f(0,0,0);
00122     
00123     glPushMatrix();
00124     
00125     glTranslated(0, 0, -.7*c);
00126     glScaled(.7,.7,.7);
00127     // motor
00128     glPushMatrix();
00129     glTranslated(0,0,r);
00130     glScaled(.5,.5,1);
00131     glutSolidSphere(r, 10, 10);      
00132 
00133     //    glColor4f(0, 0, .5, .8);
00134     glPopMatrix();
00135 
00136     // motor stand
00137     glPushMatrix();
00138     glTranslated(-r,0,r);
00139     glScaled(1,.1,.1);
00140 
00141     glColor3f(.1,.1,.1);
00142     glutSolidSphere(.5*c, 10, 10);
00143 
00144     glPopMatrix();
00145 
00146     gluCylinder(qobj, r, r, r, 10, 10);
00147     
00148     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00149     glEnable(GL_BLEND);      
00150     
00151     glPushMatrix();    
00152     glColor4f(0, 0, .5,.3);
00153 
00154     //    gluCylinder(qobj, fabs(s.u[i])*.03, fabs(s.u[i])*.2, fabs(s.u[i]), 10, 10);
00155     //      std::cout << "PUT" << std::endl;
00156       //    }
00157     glPopMatrix();
00158     glDisable(GL_BLEND);      
00159 
00160     //  glPushMatrix();
00161  
00162     //    glColor(.3,.3,.3);
00163 
00164     glTranslated(0,0,l/16);
00165     
00166     //  glRotated(RAD2DEG(ar), 1, 0, 0);
00167     //  glRotated(RAD2DEG(ap), 0, 1, 0);
00168     
00169     //    int i0 = (int)round(rand()/(double)RAND_MAX);
00170     int i0=0;
00171     for (int j = 0; j < 4; ++j)
00172       gluPartialDisk(qobj, 0, r, 10, 10, i0*45 + 22.5+j*90, 22.5);
00173     //gluCylinder(tprop, rb, rb, .05, 10, 10);
00174     
00175     glPopMatrix();    
00176     
00177   
00178   glPopMatrix();
00179 }
00180 
00181 
00182  template<int c>
00183 void Body2dView<c>::Render(const Body2dState *x, const Vectorcd *u)
00184 {
00185   Vector3d q;
00186   SE2::Instance().g2q(q, x->first);
00187 
00188   glDisable(GL_LIGHTING);  
00189   //  Viewer::SetColor(rgba[0], rgba[1], rgba[2], rgba[3]);
00190   glPushMatrix();
00191   glTranslated(q[1], q[2], 0);
00192   glRotated(RAD2DEG(q[0]), 0,0,1);
00193   //  glScaled(sys.d[0], sys.d[1],  sys.d[1]/4);
00194   //  glutSolidSphere(1, 10, 10);
00195   //  glutSolidCube(1);
00196 
00197 
00198   glColor4d(this->rgba[0], this->rgba[1], this->rgba[2], this->rgba[3]);
00199   RenderUuv(qobj, 1,  .4);
00200   glColor4d(this->rgba[0], this->rgba[1], this->rgba[2], this->rgba[3]);
00201   if (this->renderForces && u) {
00202     Vector3d f(forceScale*(*u)[1], forceScale*(*u)[2], 0);
00203     Viewer::DrawArrow(f.data(), qobj);
00204   }
00205 
00206   glPopMatrix();
00207   glEnable(GL_LIGHTING);  
00208 
00209 }
00210 
00211 
00212   template<int c>
00213 void Body2dView<c>::Render(const vector<Body2dState> *xs,
00214                            const vector<Vectorcd> *us,
00215                            bool rs, 
00216                            int is, int ie,
00217                            int dis, int dit,
00218                            bool dl)
00219 {
00220   //  glColor4f(
00221 
00222   // set defaults
00223   if (is == -1)
00224     is = 0;
00225   if (ie == -1)
00226     ie = xs->size()-1;
00227 
00228   assert(is >= 0 && is <= xs->size()-1 && ie >= 0 && ie <= xs->size()-1);
00229   assert(is <= ie);
00230 
00231   glDisable(GL_LIGHTING);
00232   //  Viewer::SetColor(rgba[0], rgba[1], rgba[2], rgba[3]);
00233   glColor4d(this->rgba[0], this->rgba[1], this->rgba[2], this->rgba[3]);
00234   
00235   glLineWidth(this->lineWidth);
00236     glBegin(GL_LINE_STRIP);
00237   for (int i = is; i <= ie; i+=dit) {
00238     const Matrix3d &g = (*xs)[i].first;
00239     glVertex3d(g(0,2), g(1,2), 0);
00240   }
00241   glEnd();
00242   glLineWidth(1);
00243     
00244   if (rs) {
00245     for (int i = 0; i < xs->size(); i += dis) {
00246       if (us && i < us->size())
00247         Render(&(*xs)[i], &(*us)[i]);
00248       else
00249         Render(&(*xs)[i]);
00250     }
00251   }
00252   
00253 
00254   if (dl)
00255     Render(&xs->back());
00256   glEnable(GL_LIGHTING);
00257 
00258 }
00259 
00260 }
00261 
00262 #endif