HistogramWidget.cxx

Go to the documentation of this file.
00001 /*
00002  This class plots image's histograms in a plotter
00003 */
00004 
00005  //---------------------
00006  // Includes
00007  //----------------------
00008  #include "HistogramWidget.h"
00009  #include  "vtkImageCast.h"
00010  #include  <math.h>
00011 // ----------------------------------------------------------------------------
00012 // WX headers inclusion.
00013 // For compilers that support precompilation, includes <wx/wx.h>.
00014 // ----------------------------------------------------------------------------
00015 
00016         #ifndef WX_PRECOMP
00017         #include <wx/wx.h>
00018         #endif
00019 
00020 //----------------------------------------------------------------------------
00021 // Class implementation
00022 //----------------------------------------------------------------------------
00023 
00024 IMPLEMENT_CLASS(HistogramWidget, wxWindow)
00025 //----------------------------------------------------------------------------
00026 // Event Table
00027 //----------------------------------------------------------------------------
00028 
00029 BEGIN_EVENT_TABLE(HistogramWidget, wxWindow)
00030         EVT_SIZE  (HistogramWidget::OnSize)
00031 END_EVENT_TABLE()
00032  //---------------------
00033  // Constructor
00034  //----------------------
00035 //wxScrolledWindow(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxHSCROLL | wxVSCROLL, const wxString& name = "scrolledWindow")
00036 //wxWindow(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxString& name = wxPanelNameStr)
00037 //pPlotterWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
00038 /*
00039 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag):wxWindow(parent,id,pos,size,flag)
00040         {
00041         }
00042  */
00043         HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag,vtkImageData* imageData,int type):
00044         wxPanel(parent,id,pos,size,flag)
00045         {
00046                  SetBackgroundColour(wxColour(255,255,255));
00047                 //histogram
00048                 histogram= new pHistogram(imageData);
00049                 
00050                 //plotter
00051                 plotter=new pPlotter(this, 400,350);
00052 
00053                 //is a plotter of histograms
00054                 plotter->setType(2);
00055                 //setting the popMenu
00056                 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
00057                 histogramSize=0;
00058                 idTransferenceFunction=-1;
00059                 idHistogram=-1;
00060                 transferenceFunctionHasColor=true;
00061                 transferenceFunctionHasPoints=true;
00062                 this->type=type;;
00063                 
00064                 //drawing
00065                 drawHistogram();
00066                 drawTransferenceFunction();
00067                 
00068         }
00069 
00070         HistogramWidget::~HistogramWidget()
00071         {
00072                 delete histogram;
00073                 delete plotter;
00074 
00075         }
00076         /*
00077         Draw the histogram in the plotter
00078         */
00079         void HistogramWidget::drawHistogram()
00080         {
00081                 //int xValues[MAX],yValues[MAX],extent[6];
00082                 double* xValues;
00083                 double* yValues;
00084                 
00085                 vtkImageData* histogramImageData=histogram->getHistogram();
00086                 
00087                 //size
00088                 histogramSize=histogram->getSize();
00089 
00090                 //plotting
00091                 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
00092                 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
00093                 
00094                 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
00095                 
00096                 for(int i=0; i< histogramSize; i++)
00097                 {
00098                         xValues[i]=i;
00099                         yValues[i]=log( (double) histogramPointer[i]);
00100                 }
00101 
00102                 
00103                 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
00104                 
00105                 if (histogramFunction)
00106                 {
00107                         histogramFunction->setEditable(false);
00108                         //smooth function
00109                         histogramFunction->setType(2);
00110                         //it is an histogram
00111                         histogramFunction->setmType(2);
00112                         idHistogram=plotter->addFunction(histogramFunction);
00113                         //for setting range the vision
00114                         plotter->addFunctionToMove(histogramFunction);
00115                         wxPen mypen1(*wxBLUE, 1, wxSOLID );
00116                         mypen1.SetWidth(2);
00117                         histogramFunction->SetPen( mypen1 );
00118                 }
00119                         
00120                         free(xValues);
00121                         free(yValues);
00122                 
00123         }
00124         /*
00125         Draw the transference function in the plotter
00126         one  for default
00127         */
00128         void HistogramWidget::drawTransferenceFunction()
00129         {
00130         
00131                         double xValues[5],yValues[5];
00132                         //xValues
00133                         int maxValueGrey=histogram->getMaximumLevelOfGrey();
00134                         xValues[0]=0;
00135                         xValues[1]=maxValueGrey/16;
00136                         xValues[2]=maxValueGrey/8;
00137                         xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
00138                         xValues[4]=maxValueGrey;
00139                         
00140                         //yValues
00141                         yValues[0]=0;
00142                         yValues[1]=25;
00143                         yValues[2]=100;
00144                         yValues[3]=25;
00145                         yValues[4]=0;
00146                         
00147                         pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 ); 
00148                 printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);             
00149                         // Including and drawing the created function in the plotter
00150                         if (tf)
00151                         {
00152                                 tf->setType(1);
00153                                 //is the actual Function
00154                                 tf->setActual(true);
00155                                 //show points
00156                                 tf->SetShowPoints(true);
00157                                 idTransferenceFunction=plotter->addFunction( tf );
00158                                 if(type==1)
00159                                         plotter->addFunctionToMove(tf);
00160                                 wxPen mypen(*wxBLACK,0.5, wxSOLID  );
00161                                 mypen.SetWidth(2);
00162                                 tf->SetPen( mypen );
00163                         }
00164         }
00165 
00166         /*
00167                 if the user resize the window   
00168         */
00169         void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
00170         {
00171                 int scrX,scrY;
00172                 GetClientSize(&scrX,&scrY);
00173                 plotter->SetSize(scrX,scrY);
00174                 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
00175                 if(actual!=NULL){
00176                         actual->setScreens(scrX,scrY);
00177                         actual->setScales();
00178                 }
00179                 
00180         }
00181         /*
00182                 get number of points of the transference function
00183         */
00184         int HistogramWidget::getSizeTransferenceFunction()
00185         {
00186                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00187                 return tf->getSizePoints();
00188 
00189         }
00190         /*
00191                 get a point of the transference function
00192         */
00193         void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
00194         {
00195                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00196                 if(tf)
00197                 {
00198                         wxNode* pnode=tf->GetPointAt(index);
00199                         pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
00200                         x=point->getRealX();
00201                         y=point->getRealY();
00202                 }
00203         }
00204         /*
00205                 get a point of the Histogram
00206                 given the grey value
00207         */
00208         int HistogramWidget::getHistogramPoint(int gValue)
00209         {
00210                 return histogram->getHistogramPoint(gValue);
00211         }
00212         /*
00213                 get number of points of the barColor
00214         */
00215         int HistogramWidget::getSizeBarColor()
00216         {
00217                 return  plotter->getColorPointsSize();
00218         }
00219         /*
00220                 get a  color int the bqr color
00221         */
00222         void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
00223         {
00224                 double tmp=x;
00225                 plotter->getBarColorDataAt(index,tmp,red,green,blue);
00226                 x=(int)tmp;
00227         }
00228         /*
00229          Returns the  maximum value ot the histogram that is show to the user 
00230         */
00231         float HistogramWidget::getMaxShowedPorcentage()
00232         {
00233                 float  porcentageMaxX=plotter->getMaxShowedPorcentage();
00234                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00235                 int min=histogramFunction->getMinX();
00236                 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
00237                 return min+ x;
00238         }
00239         /*
00240          Returns the  minimum value ot the histogram that is show to the user 
00241         */
00242         float HistogramWidget::getMinShowedPorcentage()
00243         {
00244                 float  porcentageMinX=plotter->getMinShowedPorcentage();
00245                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00246                 int min=histogramFunction->getMinX();
00247                 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
00248                 return min+ x;
00249         }
00250         /*
00251          Returns the  minimum value ot the histogram that is show to the user 
00252         */
00253         float HistogramWidget::getActualShowedPorcentage()
00254         {
00255                 float  porcentageActualX=plotter->getMinShowedPorcentage();
00256                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00257                 int min=histogramFunction->getMinX();
00258                 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
00259                 return min+ x;
00260         }
00261         //---------------------------------------
00262         // setting data in transferences function
00263         // and in bar color
00264         //----------------------------------------
00265         /*
00266           Adds a point to the transference function
00267         */
00268         bool  HistogramWidget::addPointToTransferenceFunction(double x, double y)
00269         {
00270                 bool result=false;
00271                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00272 printf("EED %p HistogramWidget::addPointToTransferenceFunction tp%p x%f y%f %d\n",this, tf, x ,y, idTransferenceFunction);
00273                 if (tf!=NULL) { result=tf->AddPoint(x,y); }
00274                 
00275                 return result;
00276         }
00277         /*
00278                 add a color point to the histogram
00279                 @param x the level of grey to which the color is assigned
00280                 @param red the level of red for the color
00281                 @param green the level of red for the color
00282                 @param blue the level of red for the color
00283         */
00284         bool  HistogramWidget::addColorPoint(double x,int red,int green, int blue)
00285         {
00286                 return plotter->addColorPoint(x,red,green,blue);
00287         }
00288          
00289         //------------------------
00290         //Erase data
00291         //------------------------
00292         /*
00293          Erase all the points that are in the transference function
00294         */
00295 
00296         void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
00297         {
00298                 if(transferenceFunctionHasPoints)
00299                 {
00300                         // we have to erase the points
00301                         pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00302                         if (tf!=NULL){
00303                                 int numOfPoints=tf->getSizePoints();
00304                                 int i=numOfPoints-1;//-2;
00305                                 while(i>=0)
00306                                 {
00307                                         tf->deletePointAt(i);
00308                                         i--;
00309                                 } // while
00310                         } // if tf
00311                 } // if transferenceFunctionHasPoints
00312                 
00313         //we set for actual the histogram
00314                 //plotter->setActual()
00315         }
00316         /*
00317         Erase the  color points in the plotter
00318         */
00319         void HistogramWidget::eraseColorPoints()
00320         {
00321                 plotter->eraseColorPoints();
00322         }
00323         //------------------
00324         //Updating plotter
00325         //------------------
00326         void HistogramWidget::updatePlotter()
00327         {
00328                 plotter->update();
00329         }
00330 
00331         //-------------------
00332         // Getter and setters
00333         //-------------------
00334 
00335         void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
00336         {
00337                 transferenceFunctionHasPoints=hasPoints;
00338         }
00339 
00340         void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
00341         {
00342                 transferenceFunctionHasPoints=hasColorPoints;
00343         }
00344         int HistogramWidget::getHistogramSize()
00345         {
00346                 return histogramSize;
00347         }
00348         void HistogramWidget::setType(int type)
00349         {
00350                 this->type=type;
00351         }
00352 
00353 

Generated on Fri Jun 12 00:08:33 2009 for creaMaracasVisu by  doxygen 1.5.7.1