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