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)
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( wxWindow *parent, wxWindowID id)
00071                 : wxPanel(parent,id){
00072 
00073 
00074                         SetBackgroundColour(wxColour(255,255,255));
00075                         histogram = NULL;       
00076                         
00077                         //plotter
00078                         plotter=new pPlotter(this, 400,350);
00079 
00080                         //is a plotter of histograms
00081                         plotter->setType(2);
00082                         //setting the popMenu
00083                         plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
00084                         histogramSize=0;
00085                         idTransferenceFunction=-1;
00086                         idHistogram=-1;
00087                         transferenceFunctionHasColor=true;
00088                         transferenceFunctionHasPoints=true;
00089                         this->type=type;;
00090                 
00091                                 
00092         }
00093 
00094         void HistogramWidget::initializeHistogram(vtkImageData* img){
00095                 if(histogram ==NULL){
00096                         histogram= new pHistogram(img);
00097                 }               
00098                 //draw
00099                 drawHistogram();
00100                 drawTransferenceFunction();
00101         }
00102 
00103         HistogramWidget::~HistogramWidget()
00104         {
00105                 delete histogram;
00106                 delete plotter;
00107 
00108         }
00109         /*
00110         Draw the histogram in the plotter
00111         */
00112         void HistogramWidget::drawHistogram()
00113         {
00114                 //int xValues[MAX],yValues[MAX],extent[6];
00115                 double* xValues;
00116                 double* yValues;
00117                 
00118                 vtkImageData* histogramImageData=histogram->getHistogram();
00119                 
00120                 //size
00121                 histogramSize=histogram->getSize();
00122 
00123                 //plotting
00124                 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
00125                 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
00126                 
00127                 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
00128                 
00129                 for(int i=0; i< histogramSize; i++)
00130                 {
00131                         xValues[i]=i;
00132                         yValues[i]=log( (double) histogramPointer[i]);
00133                 }
00134 
00135                 
00136                 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
00137                 
00138                 if (histogramFunction)
00139                 {
00140                         histogramFunction->setEditable(false);
00141                         //smooth function
00142                         histogramFunction->setType(2);
00143                         //it is an histogram
00144                         histogramFunction->setmType(2);
00145                         idHistogram=plotter->addFunction(histogramFunction);
00146                         //for setting range the vision
00147                         plotter->addFunctionToMove(histogramFunction);
00148                         wxPen mypen1(*wxBLUE, 1, wxSOLID );
00149                         mypen1.SetWidth(2);
00150                         histogramFunction->SetPen( mypen1 );
00151                 }
00152                         
00153                         free(xValues);
00154                         free(yValues);
00155                 
00156         }
00157         /*
00158         Draw the transference function in the plotter
00159         one  for default
00160         */
00161         void HistogramWidget::drawTransferenceFunction()
00162         {
00163         
00164                         double xValues[5],yValues[5];
00165                         //xValues
00166                         int maxValueGrey=histogram->getMaximumLevelOfGrey();
00167                         xValues[0]=0;
00168                         xValues[1]=maxValueGrey/16;
00169                         xValues[2]=maxValueGrey/8;
00170                         xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
00171                         xValues[4]=maxValueGrey;
00172                         
00173                         //yValues
00174                         yValues[0]=0;
00175                         yValues[1]=25;
00176                         yValues[2]=100;
00177                         yValues[3]=25;
00178                         yValues[4]=0;
00179                         
00180                         pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 ); 
00181                 printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);             
00182                         // Including and drawing the created function in the plotter
00183                         if (tf)
00184                         {
00185                                 tf->setType(1);
00186                                 //is the actual Function
00187                                 tf->setActual(true);
00188                                 //show points
00189                                 tf->SetShowPoints(true);
00190                                 idTransferenceFunction=plotter->addFunction( tf );
00191                                 if(type==1)
00192                                         plotter->addFunctionToMove(tf);
00193                                 wxPen mypen(*wxBLACK,0.5, wxSOLID  );
00194                                 mypen.SetWidth(2);
00195                                 tf->SetPen( mypen );
00196                         }
00197         }
00198 
00199         /*
00200                 if the user resize the window   
00201         */
00202         void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
00203         {
00204                 int scrX,scrY;
00205                 GetClientSize(&scrX,&scrY);
00206                 plotter->SetSize(scrX,scrY);
00207                 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
00208                 if(actual!=NULL){
00209                         actual->setScreens(scrX,scrY);
00210                         actual->setScales();
00211                 }
00212                 
00213         }
00214         /*
00215                 get number of points of the transference function
00216         */
00217         int HistogramWidget::getSizeTransferenceFunction()
00218         {
00219                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00220                 return tf->getSizePoints();
00221 
00222         }
00223         /*
00224                 get a point of the transference function
00225         */
00226         void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
00227         {
00228                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00229                 if(tf)
00230                 {
00231                         wxNode* pnode=tf->GetPointAt(index);
00232                         pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
00233                         x=point->getRealX();
00234                         y=point->getRealY();
00235                 }
00236         }
00237         /*
00238                 get a point of the Histogram
00239                 given the grey value
00240         */
00241         int HistogramWidget::getHistogramPoint(int gValue)
00242         {
00243                 return histogram->getHistogramPoint(gValue);
00244         }
00245         /*
00246                 get number of points of the barColor
00247         */
00248         int HistogramWidget::getSizeBarColor()
00249         {
00250                 return  plotter->getColorPointsSize();
00251         }
00252         /*
00253                 get a  color int the bqr color
00254         */
00255         void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
00256         {
00257                 double tmp=x;
00258                 plotter->getBarColorDataAt(index,tmp,red,green,blue);
00259                 x=(int)tmp;
00260         }
00261         /*
00262          Returns the  maximum value ot the histogram that is show to the user 
00263         */
00264         float HistogramWidget::getMaxShowedPorcentage()
00265         {
00266                 float  porcentageMaxX=plotter->getMaxShowedPorcentage();
00267                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00268                 int min=histogramFunction->getMinX();
00269                 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
00270                 return min+ x;
00271         }
00272         /*
00273          Returns the  minimum value ot the histogram that is show to the user 
00274         */
00275         float HistogramWidget::getMinShowedPorcentage()
00276         {
00277                 float  porcentageMinX=plotter->getMinShowedPorcentage();
00278                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00279                 int min=histogramFunction->getMinX();
00280                 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
00281                 return min+ x;
00282         }
00283         /*
00284          Returns the  minimum value ot the histogram that is show to the user 
00285         */
00286         float HistogramWidget::getActualShowedPorcentage()
00287         {
00288                 float  porcentageActualX=plotter->getMinShowedPorcentage();
00289                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00290                 int min=histogramFunction->getMinX();
00291                 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
00292                 return min+ x;
00293         }
00294         //---------------------------------------
00295         // setting data in transferences function
00296         // and in bar color
00297         //----------------------------------------
00298         /*
00299           Adds a point to the transference function
00300         */
00301         bool  HistogramWidget::addPointToTransferenceFunction(double x, double y)
00302         {
00303                 bool result=false;
00304                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00305 printf("EED %p HistogramWidget::addPointToTransferenceFunction tp%p x%f y%f %d\n",this, tf, x ,y, idTransferenceFunction);
00306                 if (tf!=NULL) { result=tf->AddPoint(x,y); }
00307                 
00308                 return result;
00309         }
00310         /*
00311                 add a color point to the histogram
00312                 @param x the level of grey to which the color is assigned
00313                 @param red the level of red for the color
00314                 @param green the level of red for the color
00315                 @param blue the level of red for the color
00316         */
00317         bool  HistogramWidget::addColorPoint(double x,int red,int green, int blue)
00318         {
00319                 return plotter->addColorPoint(x,red,green,blue);
00320         }
00321          
00322         //------------------------
00323         //Erase data
00324         //------------------------
00325         /*
00326          Erase all the points that are in the transference function
00327         */
00328 
00329         void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
00330         {
00331                 if(transferenceFunctionHasPoints)
00332                 {
00333                         // we have to erase the points
00334                         pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00335                         if (tf!=NULL){
00336                                 int numOfPoints=tf->getSizePoints();
00337                                 int i=numOfPoints-1;//-2;
00338                                 while(i>=0)
00339                                 {
00340                                         tf->deletePointAt(i);
00341                                         i--;
00342                                 } // while
00343                         } // if tf
00344                 } // if transferenceFunctionHasPoints
00345                 
00346         //we set for actual the histogram
00347                 //plotter->setActual()
00348         }
00349         /*
00350         Erase the  color points in the plotter
00351         */
00352         void HistogramWidget::eraseColorPoints()
00353         {
00354                 plotter->eraseColorPoints();
00355         }
00356         //------------------
00357         //Updating plotter
00358         //------------------
00359         void HistogramWidget::updatePlotter()
00360         {
00361                 plotter->update();
00362         }
00363 
00364         //-------------------
00365         // Getter and setters
00366         //-------------------
00367 
00368         void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
00369         {
00370                 transferenceFunctionHasPoints=hasPoints;
00371         }
00372 
00373         void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
00374         {
00375                 transferenceFunctionHasPoints=hasColorPoints;
00376         }
00377         int HistogramWidget::getHistogramSize()
00378         {
00379                 return histogramSize;
00380         }
00381         void HistogramWidget::setType(int type)
00382         {
00383                 this->type=type;
00384         }
00385 
00389         void HistogramWidget::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value){
00390                 plotter->GetValuesPointsFunction(greylevel,value,histogramSize);
00391         }
00392 
00397         void HistogramWidget::GetValuesColorPointsFunction(std::vector<double>& greylevel,
00398                                                                 std::vector<double>& red,
00399                                                                 std::vector<double>& green,
00400                                                                 std::vector<double>& blue)
00401         {
00402                 plotter->GetValuesColorPointsFunction(greylevel,red,green,blue);
00403         }

Generated on Wed Jul 29 16:35:29 2009 for creaMaracasVisu_lib by  doxygen 1.5.3