GL2DPlot.cpp

00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
00002 /* 
00003  * Copyright (C) 2008 Micha Hersch, EPFL
00004  * RobotCub Consortium, European Commission FP6 Project IST-004370
00005  * email:   micha.hersch@robotcub.org
00006  * website: www.robotcub.org
00007  * Permission is granted to copy, distribute, and/or modify this program
00008  * under the terms of the GNU General Public License, version 2 or any
00009  * later version published by the Free Software Foundation.
00010  *
00011  * A copy of the license can be found at
00012  * http://www.robotcub.org/icub/license/gpl.txt
00013  *
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00017  * Public License for more details
00018  */
00019 #pragma warning( disable : 4786)
00020 
00021 #include <GL/glut.h>
00022 #include <math.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <vector>
00026 #include <iostream>
00027 #include <string>
00028 using namespace std;
00029 
00030 
00031 #include "GL2DPlot.h"
00032 #include "GLTools.h"
00033 
00034 GL2DPlot::GL2DPlot(pGLSubWindow parentWindow)
00035 : GLSubWindow(parentWindow){
00036   Clear();
00037 }
00038 
00039 GL2DPlot::~GL2DPlot(){
00040   Clear();
00041 }
00042 
00043 void GL2DPlot::Clear(){
00044   m_AxesAuto = true;
00045   m_NbPlots = 0;
00046   m_PlotSize.clear();
00047   m_XValues.clear();
00048   m_YValues.clear();
00049   m_Color.clear();
00050   m_LineStyle.clear();
00051   m_MinX = -1;  m_MaxX =  1;
00052   m_MinY = -1;  m_MaxY =  1;
00053   m_Offset = 0;
00054 }
00055 
00056 void GL2DPlot::SetColor(float r, float g, float b){
00057   if(m_NbPlots>0){
00058     m_Color[m_NbPlots-1].r  = r;
00059     m_Color[m_NbPlots-1].g  = g;
00060     m_Color[m_NbPlots-1].b  = b;
00061   }
00062 }
00063 
00064 void GL2DPlot::SetColor(int no, float r, float g, float b){
00065   if((no<m_NbPlots)&&(no>=0)){
00066     m_Color[no].r  = r;
00067     m_Color[no].g  = g;
00068     m_Color[no].b  = b;
00069   }
00070 }
00071 void GL2DPlot::AddPlot(float* YVal, int size){
00072   AddPlot(NULL,YVal,size);
00073 }
00074 void GL2DPlot::AddPlot(float* XVal, float* YVal, int size){
00075   if((size<=0)||(YVal==NULL))
00076     return;
00077 
00078   m_PlotSize.push_back(size);
00079   m_XValues.push_back(XVal);
00080   m_YValues.push_back(YVal);
00081 
00082   RGBColor c;
00083   c.r = 1.0; c.g = 1.0; c.b = 1.0;
00084   m_Color.push_back(c);
00085 
00086 
00087   m_NbPlots++;
00088 }
00089 void GL2DPlot::SetOffset(int off){
00090   if((off>=0))
00091     m_Offset = off;
00092 }
00093 void GL2DPlot::Render(){
00094 
00095   int i,j;
00096 
00097   // Finding axes limits
00098   float minX,maxX,minY,maxY;
00099 
00100 //  float *xval, *yval;
00101 
00102   if(m_AxesAuto){
00103     if(m_NbPlots>0){
00104       if(m_XValues[0]!=NULL){
00105         minX = m_XValues[0][0];
00106         maxX = m_XValues[0][0];
00107       }else{
00108         minX = 0;
00109         maxX = m_PlotSize[0]-1;
00110       }
00111       minY = m_YValues[0][0];
00112       maxY = m_YValues[0][0];
00113     }else{
00114       return;
00115     }
00116   
00117     for(i=0;i<m_NbPlots;i++){
00118       for(j=0;j<m_PlotSize[i];j++){
00119         minY = (m_YValues[i][j]<minY?m_YValues[i][j]:minY);
00120         maxY = (m_YValues[i][j]>maxY?m_YValues[i][j]:maxY);
00121       }
00122     }
00123     for(i=0;i<m_NbPlots;i++){
00124       if(m_XValues[i]!=NULL){
00125         for(j=0;j<m_PlotSize[i];j++){
00126           minX = (m_XValues[i][j]<minX?m_XValues[i][j]:minX);
00127           maxX = (m_XValues[i][j]>maxX?m_XValues[i][j]:maxX);
00128         }
00129       }else{
00130         minX = (0            <minX ? 0            :minX);
00131         maxX = (m_PlotSize[i]>maxX ? m_PlotSize[i]:maxX);
00132       }
00133     }
00134 
00135   }else{
00136     minX = m_MinX; maxX = m_MaxX;
00137     minY = m_MinY; maxY = m_MaxY;
00138   }
00139 
00140         glDisable(GL_DEPTH_TEST);
00141 
00142   glMatrixMode(GL_MODELVIEW);
00143         glPushMatrix();
00144   glScalef(((float)m_ClientRect.m_Width)/2.0,((float)m_ClientRect.m_Height)/2.0,1.0);
00145   glTranslatef(1,1,0);
00146   glScalef(2.0/(maxX-minX),-2.0/(maxY-minY),1.0);
00147   glTranslatef(-(maxX+minX)/2.0,-(maxY+minY)/2.0,0.0);
00148   
00149 
00150   for(i=0;i<m_NbPlots;i++){
00151     if(m_XValues[i]!=NULL){
00152           glColor3fv((float*)&(m_Color[i]));
00153       glBegin(GL_LINE_STRIP);
00154       int j;
00155       const int pSize = m_PlotSize[i];
00156       float* xVal = m_XValues[i];
00157       float* yVal = m_YValues[i];
00158       for(j=0;j<pSize;j++){
00159         glVertex2f(*(xVal++),*(yVal++));
00160       }
00161       glEnd();
00162     }else{
00163           glColor3fv((float*)&(m_Color[i]));
00164       glBegin(GL_LINE_STRIP);
00165       int j;
00166       int off;
00167       if(m_Offset>m_PlotSize[i])
00168         off = 0;
00169       else
00170         off = m_Offset;
00171 
00172       const int pSize = m_PlotSize[i];      
00173       float* yVal = m_YValues[i]+off;
00174       for(j=0;j<pSize-off;j++){
00175         glVertex2f(j,*(yVal++));
00176       }
00177       yVal = m_YValues[i];
00178       for(j=pSize-off;j<pSize;j++){
00179         glVertex2f(j,*(yVal++));
00180       }
00181       glEnd();
00182     }
00183   }
00184   glMatrixMode(GL_MODELVIEW);
00185         glPopMatrix();
00186 
00187         glEnable(GL_DEPTH_TEST);
00188 
00189 }
00190 
00191 
00192 void  GL2DPlot::SetAxes(float minX, float minY, float maxX, float maxY){
00193   m_MinX = minX; m_MaxX = maxX;
00194   m_MinY = minY; m_MaxY = maxY;
00195   m_AxesAuto = false;  
00196 }
00197 
00198 void GL2DPlot::ClearAxes(){
00199   m_AxesAuto = true;
00200 }
00201 
00202 void  GL2DPlot::OnNormalKey(char key){  
00203 }
00204 
00205 void  GL2DPlot::OnSpecialKey(int key){
00206 }
00207 
00208 void  GL2DPlot::Resize(int w, int h){
00209 }
 All Data Structures Functions Variables

Generated on Wed Sep 22 16:51:25 2010 for Body_Schema_Learning by  doxygen 1.6.1