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

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1