GCOP  1.0
body3dview.h
Go to the documentation of this file.
00001 #ifndef GCOP_BODY3DVIEW_H
00002 #define GCOP_BODY3DVIEW_H
00003 
00004 #include "body3d.h"
00005 #include "systemview.h"
00006 #include "GL/glut.h"
00007 #include <assert.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <iostream>
00011 //#include "viewer.h"
00012 #include "so3.h"
00013 
00014 
00015 namespace gcop {
00016 
00017 #include "utils.h"
00018 
00019   using namespace Eigen;
00020   
00021   template <int c = 6>
00022     class Body3dView : public SystemView<pair<Matrix3d, Vector9d>, Matrix<double, c, 1> > {
00023 
00024     typedef Matrix<double, c, 1> Vectorcd;
00025 
00026   public:
00027     
00033     Body3dView(const Body3d<c> &sys);
00034     
00035     
00041     Body3dView(const Body3d<c> &sys,
00042                vector<pair<Matrix3d, Vector9d> > *xs,
00043                vector<Vectorcd> *us = 0);
00044     
00045     virtual ~Body3dView();
00046     
00047   
00048     virtual void Render(const pair<Matrix3d, Vector9d> *x,
00049                         const Vectorcd *u = 0);
00050     
00051     void Render(const vector<pair<Matrix3d, Vector9d> > *xs, 
00052                 const vector<Vectorcd> *us = 0, 
00053                 bool rs = true,
00054                 int is = -1, int ie = -1,
00055                 int dis = 1, int dit = 1,
00056                 bool dl = false);
00057     
00058     const Body3d<c> &sys;
00059     GLUquadricObj *qobj;
00060     double dirSize;
00061     
00062     static void Transform(const Matrix3d &R, const Vector3d &p);
00063   };
00064 }
00065 
00066 
00067 using namespace gcop;
00068 using namespace Eigen;
00069 
00070 template<int c>
00071 Body3dView<c>::Body3dView(const Body3d<c> &sys) : 
00072   SystemView<pair<Matrix3d, Vector9d>, Matrix<double, c, 1> >("Body3d"), sys(sys)
00073 {
00074   this->rgba[0] = .5;
00075   this->rgba[1] = .5;
00076   this->rgba[2] = .5;
00077   this->rgba[3] = 0;
00078   this->lineWidth = 2;
00079   qobj = gluNewQuadric();
00080   dirSize = -1;
00081 }
00082 
00083 template<int c>
00084 Body3dView<c>::Body3dView(const Body3d<c> &sys,
00085                           vector< pair<Matrix3d, Vector9d> > *xs,
00086                           vector<Vectorcd> *us) : 
00087 SystemView<pair<Matrix3d, Vector9d>, Matrix<double, c, 1> >("Body3d", xs, us), sys(sys)
00088 {
00089   this->rgba[0] = 0.5;
00090   this->rgba[1] = 0.5;
00091   this->rgba[2] = 0.5;
00092   this->rgba[3] = 0;
00093   this->lineWidth = 2;
00094   qobj = gluNewQuadric();
00095 }
00096 
00097 
00098 template<int c>
00099 Body3dView<c>::~Body3dView()
00100 {
00101   free(qobj);
00102 }
00103 
00104 template<int c>
00105 void Body3dView<c>::Render(const pair<Matrix3d, Vector9d> *x,
00106                            const Vectorcd *u)
00107 {
00108   //   glColor4f(1,0.5,0.5,0.5);
00109   
00110   glPushMatrix();
00111   Transform(x->first, x->second.head<3>());
00112   glScaled(sys.ds[0], sys.ds[1], sys.ds[2]); 
00113   glutSolidCube(1);
00114   glPopMatrix();
00115 }
00116 
00117 
00118 template<int c>
00119 void Body3dView<c>::Render(const vector<pair<Matrix3d, Vector9d> > *xs, 
00120                            const vector<Vectorcd > *us, 
00121                            bool rs, 
00122                            int is, int ie,
00123                            int dis, int dit,
00124                            bool dl)
00125 {
00126   // Viewer::SetColor(this->rgba[0], this->rgba[1], this->rgba[2], this->rgba[3]);
00127   glColor3f(this->rgba[0], this->rgba[1], this->rgba[2]);
00128 
00129   // set defaults
00130   if (is == -1)
00131     is = 0;
00132   if (ie == -1)
00133     ie = xs->size()-1;
00134 
00135   assert(is >= 0 && is <= xs->size()-1 && ie >= 0 && ie <= xs->size()-1);
00136   assert(is <= ie);
00137 
00138   glDisable(GL_LIGHTING);
00139   glLineWidth(this->lineWidth);
00140   glBegin(GL_LINE_STRIP);
00141   for (int i = is; i <= ie; i+=dit) {
00142     const pair<Matrix3d, Vector9d> &x = (*xs)[i];
00143     glVertex3d(x.second[0], x.second[1], x.second[2]);
00144   }
00145   glEnd();
00146   glLineWidth(1);
00147   glEnable(GL_LIGHTING);
00148   
00149   if (rs) {
00150     for (int i = 0; i < xs->size(); i+=dis) {
00151       Render(&(*xs)[i]);
00152     }
00153   }
00154 
00155   if (dl)
00156     Render(&xs->back());
00157 }
00158 
00159 template<int c>
00160 void Body3dView<c>::Transform(const Matrix3d &R, const Vector3d &p)
00161 {
00162   const SO3 &so3 = SO3::Instance();
00163   glTranslated(p[0], p[1], p[2]);
00164   
00165   Vector3d e;
00166   so3.log(e, R);
00167   double n = e.norm();
00168   if (n > so3.tol) {
00169     e = e/n;
00170     glRotated(RAD2DEG(n), e[0], e[1], e[2]);
00171   }
00172 }
00173 
00174 
00175 #endif