]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramWidget.cxx
51a30e7354cf7e6b9c0b1237a89570c97b6a91e9
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / HistogramWidget.cxx
1 /*
2  This class plots image's histograms in a plotter
3 */
4
5  //---------------------
6  // Includes
7  //----------------------
8  #include "HistogramWidget.h"
9  #include  "vtkImageCast.h"
10  #include  <math.h>
11
12 // ----------------------------------------------------------------------------
13 // WX headers inclusion.
14 // For compilers that support precompilation, includes <wx/wx.h>.
15 // ----------------------------------------------------------------------------
16
17         #ifndef WX_PRECOMP
18         #include <wx/wx.h>
19
20         #endif
21 #include <wx/bitmap.h>
22 //----------------------------------------------------------------------------
23 // Class implementation
24 //----------------------------------------------------------------------------
25
26 IMPLEMENT_CLASS(HistogramWidget, wxWindow)
27 //----------------------------------------------------------------------------
28 // Event Table
29 //----------------------------------------------------------------------------
30
31 BEGIN_EVENT_TABLE(HistogramWidget, wxWindow)
32         EVT_SIZE  (HistogramWidget::OnSize)
33 END_EVENT_TABLE()
34  //---------------------
35  // Constructor
36  //----------------------
37 //wxScrolledWindow(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxHSCROLL | wxVSCROLL, const wxString& name = "scrolledWindow")
38 //wxWindow(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxString& name = wxPanelNameStr)
39 //pPlotterWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
40 /*
41 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag):wxWindow(parent,id,pos,size,flag)
42         {
43         }
44  */
45         HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag,vtkImageData* imageData,int type):
46         wxPanel(parent,id,wxDefaultPosition,wxDefaultSize)
47         {
48                  SetBackgroundColour(wxColour(255,255,255));
49                 //histogram
50                 histogram= new pHistogram(imageData);
51
52                 
53                 //plotter
54                 plotter=new pPlotter(this, 400,350);
55                 
56                 //is a plotter of histograms
57                 plotter->setType(2);
58                 //setting the popMenu
59                 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
60                 histogramSize=0;
61                 idTransferenceFunction=-1;
62                 idHistogram=-1;
63                 transferenceFunctionHasColor=true;
64                 transferenceFunctionHasPoints=true;
65                 this->type=type;
66                 
67                 this->SetAutoLayout(true);
68                 this->Refresh();
69         
70                 //drawing
71                 drawHistogram();
72                 drawTransferenceFunction();
73                 
74         }
75
76         HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id)
77                 : wxPanel(parent,id){
78
79
80                         SetBackgroundColour(wxColour(255,255,255));
81                         histogram = NULL;       
82                         
83                         
84                         //plotter
85                         plotter=new pPlotter(this, 400,350);
86                         
87                         //is a plotter of histograms
88                         plotter->setType(2);
89                         //setting the popMenu
90                         plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
91                         histogramSize=0;
92                         idTransferenceFunction=-1;
93                         idHistogram=-1;
94                         transferenceFunctionHasColor=true;
95                         transferenceFunctionHasPoints=true;
96                         this->type=type;;
97                         this->SetAutoLayout(true);
98                         this->Refresh();
99                 
100                                 
101         }
102
103         
104         
105         void HistogramWidget::initializeHistogram(vtkImageData* img){
106                 if(histogram ==NULL){
107                         histogram= new pHistogram(img);
108                 }               
109                 //draw
110                 drawHistogram();
111                 drawTransferenceFunction();
112         }
113
114         HistogramWidget::~HistogramWidget()
115         {
116                 delete histogram;
117                 delete plotter;
118
119         }
120         /*
121         Draw the histogram in the plotter
122         */
123         void HistogramWidget::drawHistogram()
124         {
125                 //int xValues[MAX],yValues[MAX],extent[6];
126                 double* xValues;
127                 double* yValues;
128                 
129                 vtkImageData* histogramImageData=histogram->getHistogram();
130                 
131                 //size
132                 histogramSize=histogram->getSize();
133
134                 //plotting
135                 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
136                 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
137                 
138                 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
139                 
140                 for(int i=0; i< histogramSize; i++)
141                 {
142                         xValues[i]=i;
143                         yValues[i]=log( (double) histogramPointer[i]);
144                 }
145
146                 
147                 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
148                 
149                 if (histogramFunction)
150                 {
151                         histogramFunction->setEditable(false);
152                         //smooth function
153                         histogramFunction->setType(2);
154                         //it is an histogram
155                         histogramFunction->setmType(2);
156                         idHistogram=plotter->addFunction(histogramFunction);
157                         //for setting range the vision
158                         plotter->addFunctionToMove(histogramFunction);
159                         wxPen mypen1(*wxBLUE, 1, wxSOLID );
160                         mypen1.SetWidth(2);
161                         histogramFunction->SetPen( mypen1 );
162                 }
163                         
164                         free(xValues);
165                         free(yValues);
166                 
167         }
168         /*
169         Draw the transference function in the plotter
170         one  for default
171         */
172         void HistogramWidget::drawTransferenceFunction()
173         {
174         
175                         double xValues[5],yValues[5];
176                         //xValues
177                         int maxValueGrey=histogram->getMaximumLevelOfGrey();
178                         xValues[0]=0;
179                         xValues[1]=maxValueGrey/16;
180                         xValues[2]=maxValueGrey/8;
181                         xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
182                         xValues[4]=maxValueGrey;
183                         
184                         //yValues
185                         yValues[0]=0;
186                         yValues[1]=25;
187                         yValues[2]=100;
188                         yValues[3]=25;
189                         yValues[4]=0;
190                         
191                         pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 ); 
192                 printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);             
193                         // Including and drawing the created function in the plotter
194                         if (tf)
195                         {
196                                 tf->setType(1);
197                                 //is the actual Function
198                                 tf->setActual(true);
199                                 //show points
200                                 tf->SetShowPoints(true);
201                                 idTransferenceFunction=plotter->addFunction( tf );
202                                 if(type==1)
203                                         plotter->addFunctionToMove(tf);
204                                 wxPen mypen(*wxBLACK,0.5, wxSOLID  );
205                                 mypen.SetWidth(2);
206                                 tf->SetPen( mypen );
207                         }
208         }
209
210         /*
211                 if the user resize the window   
212         */
213         void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
214         {
215                 int scrX,scrY;
216                 GetClientSize(&scrX,&scrY);
217                 plotter->SetSize(scrX,scrY);
218                 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
219                 if(actual!=NULL){
220                         actual->setScreens(scrX,scrY);
221                         actual->setScales();
222                 }
223                 
224         }
225         /*
226                 get number of points of the transference function
227         */
228         int HistogramWidget::getSizeTransferenceFunction()
229         {
230                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
231                 return tf->getSizePoints();
232
233         }
234         /*
235                 get a point of the transference function
236         */
237         void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
238         {
239                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
240                 if(tf)
241                 {
242                         wxNode* pnode=tf->GetPointAt(index);
243                         pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
244                         x=point->getRealX();
245                         y=point->getRealY();
246                 }
247         }
248         /*
249                 get a point of the Histogram
250                 given the grey value
251         */
252         int HistogramWidget::getHistogramPoint(int gValue)
253         {
254                 return histogram->getHistogramPoint(gValue);
255         }
256         /*
257                 get number of points of the barColor
258         */
259         int HistogramWidget::getSizeBarColor()
260         {
261                 return  plotter->getColorPointsSize();
262         }
263         /*
264                 get a  color int the bqr color
265         */
266         void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
267         {
268                 double tmp=x;
269                 plotter->getBarColorDataAt(index,tmp,red,green,blue);
270                 x=(int)tmp;
271         }
272         /*
273          Returns the  maximum value ot the histogram that is show to the user 
274         */
275         float HistogramWidget::getMaxShowedPorcentage()
276         {
277                 float  porcentageMaxX=plotter->getMaxShowedPorcentage();
278                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
279                 int min=histogramFunction->getMinX();
280                 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
281                 return min+ x;
282         }
283         /*
284          Returns the  minimum value ot the histogram that is show to the user 
285         */
286         float HistogramWidget::getMinShowedPorcentage()
287         {
288                 float  porcentageMinX=plotter->getMinShowedPorcentage();
289                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
290                 int min=histogramFunction->getMinX();
291                 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
292                 return min+ x;
293         }
294         /*
295          Returns the  minimum value ot the histogram that is show to the user 
296         */
297         float HistogramWidget::getActualShowedPorcentage()
298         {
299                 float  porcentageActualX=plotter->getMinShowedPorcentage();
300                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
301                 int min=histogramFunction->getMinX();
302                 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
303                 return min+ x;
304         }
305         //---------------------------------------
306         // setting data in transferences function
307         // and in bar color
308         //----------------------------------------
309         /*
310           Adds a point to the transference function
311         */
312         bool  HistogramWidget::addPointToTransferenceFunction(double x, double y)
313         {
314                 bool result=false;
315                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
316 printf("EED %p HistogramWidget::addPointToTransferenceFunction tp%p x%f y%f %d\n",this, tf, x ,y, idTransferenceFunction);
317                 if (tf!=NULL) { result=tf->AddPoint(x,y); }
318                 
319                 return result;
320         }
321         /*
322                 add a color point to the histogram
323                 @param x the level of grey to which the color is assigned
324                 @param red the level of red for the color
325                 @param green the level of red for the color
326                 @param blue the level of red for the color
327         */
328         bool  HistogramWidget::addColorPoint(double x,int red,int green, int blue)
329         {
330                 return plotter->addColorPoint(x,red,green,blue);
331         }
332          
333         //------------------------
334         //Erase data
335         //------------------------
336         /*
337          Erase all the points that are in the transference function
338         */
339
340         void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
341         {
342                 if(transferenceFunctionHasPoints)
343                 {
344                         // we have to erase the points
345                         pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
346                         if (tf!=NULL){
347                                 int numOfPoints=tf->getSizePoints();
348                                 int i=numOfPoints-1;//-2;
349                                 while(i>=0)
350                                 {
351                                         tf->deletePointAt(i);
352                                         i--;
353                                 } // while
354                         } // if tf
355                 } // if transferenceFunctionHasPoints
356                 
357         //we set for actual the histogram
358                 //plotter->setActual()
359         }
360         /*
361         Erase the  color points in the plotter
362         */
363         void HistogramWidget::eraseColorPoints()
364         {
365                 plotter->eraseColorPoints();
366         }
367         //------------------
368         //Updating plotter
369         //------------------
370         void HistogramWidget::updatePlotter()
371         {
372                 plotter->update();
373         }
374
375         //-------------------
376         // Getter and setters
377         //-------------------
378
379         void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
380         {
381                 transferenceFunctionHasPoints=hasPoints;
382         }
383
384         void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
385         {
386                 transferenceFunctionHasPoints=hasColorPoints;
387         }
388         int HistogramWidget::getHistogramSize()
389         {
390                 return histogramSize;
391         }
392         void HistogramWidget::setType(int type)
393         {
394                 this->type=type;
395         }
396
397 /**
398 **      Returns two vectors, the grey level of the point and its value, the value is between [0,1]
399 **/
400         void HistogramWidget::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value){
401                 plotter->GetValuesPointsFunction(greylevel,value,histogramSize);
402         }
403
404 /**
405 **      Returns two vectors, the grey level of the point and its value, the red, green
406 **      and blue value is between [0,1]
407 **/
408         void HistogramWidget::GetValuesColorPointsFunction(std::vector<double>& greylevel,
409                                                                 std::vector<double>& red,
410                                                                 std::vector<double>& green,
411                                                                 std::vector<double>& blue)
412         {
413                 plotter->GetValuesColorPointsFunction(greylevel,red,green,blue);
414         }