GLColorPlot.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 "GLColorPlot.h"
00032 #include "GLTools.h"
00033 
00034 GLColorPlot::GLColorPlot(pGLSubWindow parentWindow)
00035 : GLSubWindow(parentWindow){
00036   m_Buffer    = NULL;
00037   Clear();
00038 }
00039 
00040 GLColorPlot::~GLColorPlot(){
00041   Clear();
00042 }
00043 
00044 void GLColorPlot::Clear(){
00045   m_Values    = NULL;
00046   m_SizeX     = 0;
00047   m_SizeY     = 0;
00048   m_Offset    = 0;
00049   SetColorMap(1);
00050 
00051   m_AxesAuto  = true;
00052 
00053   m_MinC      = -1;  
00054   m_MaxC      =  1;
00055 
00056 
00057   if(m_Buffer!=NULL)
00058     delete [] m_Buffer;
00059   m_Buffer    = NULL;
00060 }
00061 
00062 void GLColorPlot::SetColorMap(int no){
00063   int i;
00064   switch(no){
00065   case 1:
00066     for(i=0*COLORMAP_SIZE/8;i<1*COLORMAP_SIZE/8;i++){
00067       m_Colormap[i].r = 0.0;
00068       m_Colormap[i].g = 0.0;
00069       m_Colormap[i].b =       ((float)(i + 1*COLORMAP_SIZE/8))/((float)COLORMAP_SIZE/4);
00070     }
00071     for(i=1*COLORMAP_SIZE/8;i<3*COLORMAP_SIZE/8;i++){
00072       m_Colormap[i].r = 0.0;
00073       m_Colormap[i].g =       ((float)(i - 1*COLORMAP_SIZE/8))/((float)COLORMAP_SIZE/4);
00074       m_Colormap[i].b = 1.0;
00075     }
00076     for(i=3*COLORMAP_SIZE/8;i<5*COLORMAP_SIZE/8;i++){
00077       m_Colormap[i].r =       ((float)(i - 3*COLORMAP_SIZE/8))/((float)COLORMAP_SIZE/4);
00078       m_Colormap[i].g = 1.0;
00079       m_Colormap[i].b = 1.0 - ((float)(i - 3*COLORMAP_SIZE/8))/((float)COLORMAP_SIZE/4);
00080     }
00081     for(i=5*COLORMAP_SIZE/8;i<7*COLORMAP_SIZE/8;i++){
00082       m_Colormap[i].r = 1.0;
00083       m_Colormap[i].g = 1.0 - ((float)(i - 5*COLORMAP_SIZE/8))/((float)COLORMAP_SIZE/4);
00084       m_Colormap[i].b = 0.0;
00085     }
00086     for(i=7*COLORMAP_SIZE/8;i<8*COLORMAP_SIZE/8;i++){
00087       m_Colormap[i].r = 1.0 - ((float)(i - 7*COLORMAP_SIZE/8))/((float)COLORMAP_SIZE/4);
00088       m_Colormap[i].g = 0.0;
00089       m_Colormap[i].b = 0.0;
00090     }
00091 
00092     break;
00093 
00094   case 0:
00095   default:
00096     for(i=0;i<COLORMAP_SIZE;i++){
00097       m_Colormap[i].r = ((float)i)/((float)COLORMAP_SIZE-1);
00098       m_Colormap[i].g = m_Colormap[i].r;
00099       m_Colormap[i].b = m_Colormap[i].r;
00100     }
00101     break;
00102   }
00103 }
00104 
00105 void GLColorPlot::SetColorPlot(float* Val, int sizeX, int sizeY){
00106   if((sizeX<=0)||(sizeY<=0))
00107     return;
00108 
00109   if(Val==NULL)
00110     return;
00111 
00112   if(m_Buffer!=NULL)
00113     delete [] m_Buffer;
00114 
00115   m_Values  = Val;
00116   m_SizeX   = sizeX;
00117   m_SizeY   = sizeY;
00118   m_Buffer  = new float[sizeX*sizeY*4];
00119 }
00120 
00121 void GLColorPlot::Render(){
00122 
00123   int i;
00124 
00125   // Finding axes limits;
00126   float minC,maxC;
00127 
00128   if(m_AxesAuto){
00129     if(m_Values==NULL)
00130       return;
00131 
00132     minC = m_Values[0];
00133     maxC = m_Values[0];
00134   
00135     float *val = m_Values;
00136     const int valSize = m_SizeX*m_SizeY;
00137     for(i=0;i<valSize;i++){
00138       if((*val)<minC) minC = (*val);
00139       if((*val)>maxC) maxC = (*val);
00140       val++;
00141     }
00142   }else{
00143     minC = m_MinC; maxC = m_MaxC;
00144   }
00145   //cout <<m_Title<<": "<< minC << " " << maxC<<endl;
00146   float *val = m_Values;
00147   float *buf = m_Buffer;
00148   const int valSize = m_SizeX*m_SizeY;
00149   float minMax = maxC-minC;
00150   float nVal;
00151   int   nIndex;
00152   const int offSize = m_Offset*m_SizeX;
00153   val += offSize;
00154   for(i=0;i<valSize-offSize;i++){
00155     nVal = ((*(val++)) - minC) / (minMax);
00156     if(nVal>1.0) nVal = 1.0;
00157     else if(nVal<0.0) nVal = 0.0;
00158     
00159     nIndex = (int)(floor(nVal * (COLORMAP_SIZE-1)));
00160      *(buf++) = m_Colormap[nIndex].r;
00161      *(buf++) = m_Colormap[nIndex].g;
00162      *(buf++) = m_Colormap[nIndex].b;
00163      *(buf++) = 0.6;
00164   }
00165   val = m_Values;
00166   for(i=0;i<offSize;i++){
00167     nVal = ((*(val++)) - minC) / (minMax);
00168     if(nVal>1.0) nVal = 1.0;
00169     else if(nVal<0.0) nVal = 0.0;
00170     
00171     nIndex = (int)(floor(nVal * (COLORMAP_SIZE-1)));
00172      *(buf++) = m_Colormap[nIndex].r;
00173      *(buf++) = m_Colormap[nIndex].g;
00174      *(buf++) = m_Colormap[nIndex].b;
00175      *(buf++) = 0.6;
00176   }
00177 
00178         glDisable(GL_DEPTH_TEST);
00179 
00180   glMatrixMode(GL_MODELVIEW);
00181         glPushMatrix();
00182   glRasterPos2i(2,m_ClientRect.m_Height-2);
00183   glPixelZoom(((float)m_ClientRect.m_Width-4)/m_SizeX,((float)m_ClientRect.m_Height-2)/m_SizeY);
00184   glDrawPixels(m_SizeX,m_SizeY,GL_RGBA,GL_FLOAT,m_Buffer);
00185   
00186   glMatrixMode(GL_MODELVIEW);
00187         glPopMatrix();
00188 
00189         glEnable(GL_DEPTH_TEST);
00190 
00191 }
00192 void GLColorPlot::SetOffset(int off){
00193   if((off <= m_SizeY)&(off>=0))
00194     m_Offset = off;
00195 }
00196 
00197 
00198 void  GLColorPlot::SetAxes(float minC, float maxC){
00199   m_MinC = minC; m_MaxC = maxC;
00200   m_AxesAuto = false;  
00201 }
00202 
00203 void GLColorPlot::ClearAxes(){
00204   m_AxesAuto = true;
00205 }
00206 
00207 void  GLColorPlot::OnNormalKey(char key){  
00208 }
00209 
00210 void  GLColorPlot::OnSpecialKey(int key){
00211 }
00212 
00213 void  GLColorPlot::Resize(int w, int h){
00214 }
 All Data Structures Functions Variables

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