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