primitives.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 //
00004 // This file is part of g2o.
00005 // 
00006 // g2o is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // g2o is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with g2o.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #include "primitives.h"
00020 
00021 #include "qglviewer.h"
00022 #include <cstdlib>
00023 #include <cmath>
00024 
00025 namespace g2o {
00026 
00030 class GLUWrapper
00031 {
00032   public:
00033     static GLUquadricObj* getQuadradic()
00034     {
00035       static GLUWrapper inst;
00036       return inst._quadratic;
00037     }
00038   protected:
00039     GLUWrapper()
00040     {
00041       //std::cerr << __PRETTY_FUNCTION__ << std::endl;
00042       _quadratic = gluNewQuadric();              // Create A Pointer To The Quadric Object ( NEW )
00043       gluQuadricNormals(_quadratic, GLU_SMOOTH); // Create Smooth Normals ( NEW )
00044     }
00045     ~GLUWrapper()
00046     {
00047       //std::cerr << __PRETTY_FUNCTION__ << std::endl;
00048       gluDeleteQuadric(_quadratic);
00049     }
00050     GLUquadricObj* _quadratic;;
00051 };
00052 
00053 void drawAxis(float length)
00054 {
00055   QGLViewer::drawAxis(length);
00056 }
00057 
00058 void drawArrow(float length, float radius, int nbSubdivisions)
00059 {
00060   QGLViewer::drawArrow(length, radius, nbSubdivisions);
00061 }
00062 
00063 void drawGrid(float size, int nbSubdivisions)
00064 {
00065   QGLViewer::drawGrid(size, nbSubdivisions);
00066 }
00067 
00068 void drawArrow2D(float len, float head_width, float head_len)
00069 {
00070   glBegin(GL_LINES);
00071   glVertex2f(0, 0);
00072   glVertex2f(len, 0);
00073   glEnd();
00074 
00075   glNormal3f(0,0,1);
00076   glBegin(GL_TRIANGLES);
00077   glVertex2f(len, 0);
00078   glVertex2f(len - head_len,  0.5*head_width);
00079   glVertex2f(len - head_len, -0.5*head_width);
00080   glEnd();
00081 }
00082 
00083 void drawPoseBox()
00084 {
00085   glPushMatrix();
00086   glScalef(0.5,1,1);
00087   glPushMatrix();
00088   glScalef(1,0.25,0.5);
00089   glTranslatef(-0.5,0.5,0);
00090   glColor3f(1.0, 0.3, 0.3);
00091   drawBox(1, 1, 1);
00092   glPopMatrix();
00093 
00094   glPushMatrix();
00095   glScalef(1,0.25,0.5);
00096   glTranslatef(-0.5,-0.5,0);
00097   glColor3f(1.0, 0.1, 0.1);
00098   drawBox(1, 1, 1);
00099   glPopMatrix();
00100 
00101   glPushMatrix();
00102   glScalef(1,0.25,0.5);
00103   glTranslatef(+0.5,0.5,0);
00104   glColor3f(0.3, 0.3, 1.0);
00105   drawBox(1, 1, 1);
00106   glPopMatrix();
00107 
00108   glPushMatrix();
00109   glScalef(1,0.25,0.5);
00110   glTranslatef(+0.5,-0.5,0);
00111   glColor3f(0.1, 0.1, 1.);
00112   drawBox(1, 1, 1);
00113   glPopMatrix();
00114   glPopMatrix();
00115 }
00116 
00117 void drawBox(GLfloat l, GLfloat w, GLfloat h)
00118 {
00119   GLfloat sx = l*0.5f;
00120   GLfloat sy = w*0.5f;
00121   GLfloat sz = h*0.5f;
00122 
00123   glBegin(GL_QUADS);
00124   // bottom
00125   glNormal3f( 0.0f, 0.0f,-1.0f);
00126   glVertex3f(-sx, -sy, -sz);
00127   glVertex3f(-sx, sy, -sz);
00128   glVertex3f(sx, sy, -sz);
00129   glVertex3f(sx, -sy, -sz);
00130   // top
00131   glNormal3f( 0.0f, 0.0f,1.0f);
00132   glVertex3f(-sx, -sy, sz);
00133   glVertex3f(-sx, sy, sz);
00134   glVertex3f(sx, sy, sz);
00135   glVertex3f(sx, -sy, sz);
00136   // back
00137   glNormal3f(-1.0f, 0.0f, 0.0f);
00138   glVertex3f(-sx, -sy, -sz);
00139   glVertex3f(-sx, sy, -sz);
00140   glVertex3f(-sx, sy, sz);
00141   glVertex3f(-sx, -sy, sz);
00142   // front
00143   glNormal3f( 1.0f, 0.0f, 0.0f);
00144   glVertex3f(sx, -sy, -sz);
00145   glVertex3f(sx, sy, -sz);
00146   glVertex3f(sx, sy, sz);
00147   glVertex3f(sx, -sy, sz);
00148   // left
00149   glNormal3f( 0.0f, -1.0f, 0.0f);
00150   glVertex3f(-sx, -sy, -sz);
00151   glVertex3f(sx, -sy, -sz);
00152   glVertex3f(sx, -sy, sz);
00153   glVertex3f(-sx, -sy, sz);
00154   //right
00155   glNormal3f( 0.0f, 1.0f, 0.0f);
00156   glVertex3f(-sx, sy, -sz);
00157   glVertex3f(sx, sy, -sz);
00158   glVertex3f(sx, sy, sz);
00159   glVertex3f(-sx, sy, sz);
00160   glEnd();
00161 }
00162 
00163 void drawPlane(GLfloat l, GLfloat w)
00164 {
00165   GLfloat sx = l*0.5f;
00166   GLfloat sy = w*0.5f;
00167 
00168   glBegin(GL_QUADS);
00169   glNormal3f( 0.0f, 0.0f, 1.0f);
00170   glVertex3f(-sx, -sy, 0.f);
00171   glVertex3f(-sx, sy, 0.f);
00172   glVertex3f(sx, sy, 0.f);
00173   glVertex3f(sx, -sy, 0.f);
00174   glEnd();
00175 }
00176 
00177 void drawSphere(GLfloat radius)
00178 {
00179   gluSphere(GLUWrapper::getQuadradic(), radius, 32, 32);
00180 }
00181 
00182 void drawEllipsoid(GLfloat r1, GLfloat r2, GLfloat r3)
00183 {
00184   GLboolean hasNormalization = glIsEnabled(GL_NORMALIZE);
00185   if (!hasNormalization)
00186     glEnable(GL_NORMALIZE);
00187   glPushMatrix();
00188   glScalef(r1, r2, r3);
00189   gluSphere(GLUWrapper::getQuadradic(), 1.0, 32, 32);
00190   glPopMatrix();
00191   if (!hasNormalization)
00192     glDisable(GL_NORMALIZE);
00193 }
00194 
00195 void drawCone(GLfloat radius, GLfloat height)
00196 {
00197   glPushMatrix();
00198   glRotatef(-90.f, 1.f, 0.f, 0.f);
00199   glTranslatef(0, 0, - height/2.0);
00200   gluCylinder(GLUWrapper::getQuadradic(), radius, 0.f, height, 32, 1);
00201   gluDisk(GLUWrapper::getQuadradic(), 0, radius, 32, 1);
00202   glPopMatrix();
00203 }
00204 
00205 void drawCylinder(GLfloat radius, GLfloat height)
00206 {
00207   glPushMatrix();
00208   glRotatef(-90, 1.f, 0.f, 0.f);
00209   glTranslatef(0, 0, + height/2.0);
00210   gluDisk(GLUWrapper::getQuadradic(), 0, radius, 32, 1);
00211   glTranslatef(0, 0, - height);
00212   gluCylinder(GLUWrapper::getQuadradic(), radius, radius, height, 32, 1);
00213   glRotatef(180, 1.f, 0.f, 0.f);
00214   gluDisk(GLUWrapper::getQuadradic(), 0, radius, 32, 1);
00215   glPopMatrix();
00216 }
00217 
00218 void drawDisk(GLfloat radius)
00219 {
00220   glRotatef(90, 0.f, 1.f, 0.f);
00221   gluDisk(GLUWrapper::getQuadradic(), 0, radius, 32, 1);
00222 }
00223 
00224 void drawPyramid(GLfloat length, GLfloat height)
00225 {
00226   glPushMatrix();
00227   glTranslatef(0, 0, - height/2.0);
00228   glRotatef(45, 0.f, 0.f, 1.f);
00229   gluCylinder(GLUWrapper::getQuadradic(), length, 0.f, height, 32, 1);
00230   gluDisk(GLUWrapper::getQuadradic(), 0, length, 32, 1);
00231   glPopMatrix();
00232 }
00233 
00234 void drawRangeRing(GLfloat range, GLfloat fov, GLfloat range_width)
00235 {
00236   glPushMatrix();
00237   glRotatef((fov/2.0f) - 90, 0.f, 0.f, 1.f);
00238   gluPartialDisk(GLUWrapper::getQuadradic(), range, range + range_width, 32, 1, 0.f, fov);
00239   glPopMatrix();
00240 }
00241 
00242 void drawSlice(GLfloat radius, GLfloat height, GLfloat fov, int slices_per_circle)
00243 {
00244   double fov_rad = fov/180.*M_PI; // convert to rad
00245   int num_slices = int(slices_per_circle * (fov_rad / (2*M_PI))) + 1;
00246   double angle_step = fov_rad / num_slices;
00247   double angle_step_half = angle_step * 0.5;
00248 
00249   GLfloat height_half = height * 0.5f;
00250   GLfloat lower_z = -height_half;
00251   GLfloat upper_z =  height_half;
00252 
00253   GLfloat last_x = std::cos(-fov_rad * 0.5) * radius;
00254   GLfloat last_y = std::sin(-fov_rad * 0.5) * radius;
00255 
00256   glPushMatrix();
00257   glBegin(GL_TRIANGLES);
00258   glNormal3f(std::sin(-fov_rad * 0.5), -std::cos(-fov_rad * 0.5), -0.f);
00259   glVertex3f(0.f, 0.f, upper_z);
00260   glVertex3f(0.f, 0.f, lower_z);
00261   glVertex3f(last_x, last_y, upper_z);
00262   glVertex3f(last_x, last_y, upper_z);
00263   glVertex3f(last_x, last_y, lower_z);
00264   glVertex3f(0.f, 0.f, lower_z);
00265 
00266   double start_angle = -0.5*fov_rad + angle_step;
00267   double angle       = start_angle;
00268   for (int i = 0; i < num_slices; ++i) {
00269     GLfloat x = std::cos(angle) * radius;
00270     GLfloat y = std::sin(angle) * radius;
00271     GLfloat front_normal_x = std::cos(angle + angle_step_half);
00272     GLfloat front_normal_y = std::sin(angle + angle_step_half);
00273 
00274     // lower triangle
00275     glNormal3f(0.f, 0.f, -1.f);
00276     glVertex3f(0.f, 0.f, lower_z);
00277     glVertex3f(x, y, lower_z);
00278     glVertex3f(last_x, last_y, lower_z);
00279     // upper
00280     glNormal3f(0.f, 0.f, 1.f);
00281     glVertex3f(0.f, 0.f, upper_z);
00282     glVertex3f(x, y, upper_z);
00283     glVertex3f(last_x, last_y, upper_z);
00284     //front rectangle (we use two triangles)
00285     glNormal3f(front_normal_x, front_normal_y, 0.f);
00286     glVertex3f(last_x, last_y, upper_z);
00287     glVertex3f(last_x, last_y, lower_z);
00288     glVertex3f(x, y, upper_z);
00289     glVertex3f(x, y, upper_z);
00290     glVertex3f(x, y, lower_z);
00291     glVertex3f(last_x, last_y, lower_z);
00292 
00293     last_x = x;
00294     last_y = y;
00295     angle += angle_step;
00296   }
00297 
00298   glNormal3f(-std::sin(fov_rad * 0.5), std::cos(fov_rad * 0.5), -0.f);
00299   glVertex3f(0.f, 0.f, upper_z);
00300   glVertex3f(0.f, 0.f, lower_z);
00301   glVertex3f(last_x, last_y, upper_z);
00302   glVertex3f(last_x, last_y, upper_z);
00303   glVertex3f(last_x, last_y, lower_z);
00304   glVertex3f(0.f, 0.f, lower_z);
00305 
00306   glEnd();
00307   glPopMatrix();
00308 }
00309 
00310 } // end namespace


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:09