Main Page | Class Hierarchy | Class List | Directories | File List | Class Members

Histogram.cpp

00001 
00005 #include <iostream>
00006 #include <string>
00007 #include <glut.h>
00008 #include <glui.h>
00009 #include <glu.h>
00010 #include <math.h>
00011 
00012 #include "LifeVariables.h"
00013 #include "Histogram.h"
00014 #include "TxFunction.h"
00015 
00016 using namespace std;
00017 
00018 Histogram::Histogram(): GuiComponent(0, HISTOGRAM_VALUE_RANGE,0,1,0,0,1,1) {
00019     this->txFunction = NULL;
00020     this->initialised = false;
00021     this->grabbedPoint = NULL;
00022     this->state = ENABLED;    
00023 }
00024 
00025 
00026 void Histogram::update(unsigned short *data, unsigned int &size) {
00027     this->hide();
00028     if(this->txFunction != NULL) {
00029         delete this->txFunction;
00030     }
00031     
00032     // loop counter
00033     int i = 0;
00034     int maxValue = 0;
00035 
00036     // initialise the array with zeros
00037     for(i=1; i < HISTOGRAM_VALUE_RANGE; i++) {
00038         occurrenceArray[i] = 1;
00039     }
00040 
00041     // count the densities and store the occurrence
00042     for(i=1; i<size; i++) {
00043         occurrenceArray[data[i]]+=1;
00044         
00045         if(maxValue < data[i]) {
00046             maxValue = data[i];
00047         }
00048     }
00049     this->initialised = true;
00050 
00051     // update the top plane
00052     this->top = maxValue;
00053     this->oneAlphaPercent = 1.0f/this->top;
00054     this->txFunction = new TxFunction(0,0,HISTOGRAM_VALUE_RANGE_FLOAT,this->top);
00055     
00056     cout << top << " " << oneAlphaPercent << endl;
00057 
00058     this->enable();
00059 }
00060 
00061 
00062 void Histogram::onLeftMouseClick(int x, int y) {
00063     if((this->state == ENABLED)&&(this->hit(x,y))) {
00064         // calculate the mouse position in the world coordinates of the histogram   
00065         this->getLocalWorldCoordinates(x, y);
00066         // check if the point is already in, if not add it
00067         grabbedPoint = txFunction->hitTxPoint(x,y);
00068         if (grabbedPoint == NULL) {          
00069             txFunction->addControlPoint(x,y,1,1,1,(float)y*(float)this->oneAlphaPercent);
00070             grabbedPoint = txFunction->hitTxPoint(x,y);
00071         }
00072     }
00073 }
00074 
00075 
00076 bool Histogram::onRightMouseClick(int x, int y) {
00077     if((this->state == ENABLED)&&(this->hit(x,y))) {
00078         // calculate the mouse position in the world coordinates of the histogram   
00079         this->getLocalWorldCoordinates(x, y);
00080 
00081         // check wether the point is already in, if so, move it
00082         // otherwise add it
00083         grabbedPoint = txFunction->hitTxPoint(x,y);
00084         if(grabbedPoint != NULL) {
00085             // change the render state to selected
00086             this->disable();
00087             return true;
00088         }
00089         else { 
00090             return false;
00091         }
00092     }
00093     else {
00094         return false;
00095     }
00096 }
00097 
00098 
00099 void Histogram::onMouseDrag(int x, int y) {
00100     if((this->state == ENABLED)&&(this->hit(x,y))) {
00101         // calculate the mouse position in the world coordinates of the histogram   
00102         this->getLocalWorldCoordinates(x, y);
00103 
00104         if(grabbedPoint!= NULL) {            
00105             //cout << "equals " << grabbedPoint->equals(txFunction->getLastTxPoint()) << endl;
00106             // start moving
00107             txFunction->moveControlPoint(grabbedPoint, x, y);
00108             grabbedPoint->rgba[3] = (float)y*(float)this->oneAlphaPercent;
00109         }
00110     }
00111 }
00112 
00113 
00114 void Histogram::render(int x, int y, int width, int height) {    
00115     if(this->state != HIDDEN) {
00116         int i = 0;        
00117         // store the current drawing color on the attribute stack
00118         glPushAttrib(GL_CURRENT_BIT);
00119         
00120         // setup orthographic projection at a specific point x,y 
00121         this->startCamera(x,y,width,height);
00122         
00123         // if the data had not been initalised, do not draw
00124         if(this->initialised == true) {
00125             
00126             // render the shape of the transfer function
00127             txFunction->render();
00128             
00129             i = 0;            
00130             glColor3f(0.2,0.2,0.2);            
00131             
00132             // draw the grid and the description
00133             int j = 0;
00134             
00135             while(i < HISTOGRAM_VALUE_RANGE) {
00136                 glBegin(GL_LINES);
00137                     glVertex2i(i, 0);
00138                     glVertex2i(i, this->top);                    
00139                 glEnd();               
00140                 i+=HISTOGRAM_VGRID_SIZE;                
00141                 glPushAttrib(GL_CURRENT_BIT);
00142                     glColor3f(0.4,0.5,0.4);                 
00143                     renderString(i,this->top/*-200*/,FONT,i);
00144                 glPopAttrib();
00145             }            
00146             
00147             // draw the vertical lines - the data
00148             glColor3f(0.9, 0.9 , 0.8);       
00149             for(i=0; i< HISTOGRAM_VALUE_RANGE; i++) {
00150                 glBegin(GL_LINES);
00151                     glVertex2i( i, 0);
00152                     glVertex2i( i, occurrenceArray[i]-1);
00153                 glEnd();                
00154             }
00155             
00156             // the background quad of the histogram
00157             glColor3f(0.4,0.5,0.4);        
00158             glBegin(GL_QUADS);
00159                 glVertex2i(0, 0);
00160                 glVertex2i(HISTOGRAM_VALUE_RANGE, 0);
00161                 glVertex2i(HISTOGRAM_VALUE_RANGE, this->top);
00162                 glVertex2i(0, this->top);
00163             glEnd();
00164             
00165         }
00166 
00167         // reset the old projection
00168         this->stopCamera();
00169 
00170         // restore the drawing color
00171         glPopAttrib();
00172     }
00173 }
00174 
00175 
00176 void Histogram::getColorByDensity(unsigned short &density, 
00177                                   float &red, float &green, float &blue, float &alpha) {
00178     // here might be a switch for different interpolation modes
00179 
00180     this->txFunction->getLinearInterpolatedColor(density, red, green, blue, alpha);
00181 }
00182 
00183 
00184 void Histogram::getVoxelColorByDensity(float density, 
00185                                        float &red, float &green, float &blue) {
00186     this->txFunction->getLinearInterpolatedVoxelColor(density, red, green, blue);
00187 }
00188 
00189 
00190 void Histogram::getLevoyVoxelColor(float density, 
00191                                    float &red, float &green, float &blue, float gradient) {
00192     this->txFunction->getLevoyVoxelColor(density, red, green, blue, gradient);
00193 }
00194 
00195 void Histogram::getSolidVoxelColorByDensity(float density, 
00196                                        float &red, float &green, float &blue) {
00197     this->txFunction->getSolidVoxelColor(density, red, green, blue);
00198 }
00199 
00200 
00201 
00202 bool Histogram::removeControlPoint() {
00203     if(this->grabbedPoint == NULL) {        
00204         return false;
00205     }
00206     else {        
00207         this->txFunction->removeControlPoint(grabbedPoint);
00208         return true;
00209     }
00210 }

Generated on Mon Dec 12 15:20:26 2005 for CCube by  doxygen 1.4.1