]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramWidget.cxx
#2490 creaMaracasVisu Feature New High - Histogram Interaction
[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         HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id)
101                 : wxPanel(parent,id){
102
103                         SetBackgroundColour(wxColour(255,255,255));
104                         histogram = NULL;       
105                         
106                         //plotter
107                         plotter=new pPlotter(this, 400,350);
108                         
109                         //is a plotter of histograms
110                         plotter->setType(2);
111                         //setting the popMenu
112                         plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
113                         histogramSize                                           = 0;
114                         idTransferenceFunction                  = -1;
115                         idHistogram                                                     = -1;
116                         transferenceFunctionHasColor    = true;
117                         transferenceFunctionHasPoints   = true;
118                         this->type                                                      = type;;
119                         this->SetAutoLayout(true);
120                         this->Refresh();
121         }
122
123         
124         
125         void HistogramWidget::initializeHistogram(vtkImageData* img){
126                 if(histogram ==NULL)
127                 {
128                         histogram= new pHistogram(img);
129                 }               
130                 //draw
131                 drawHistogram();
132                 drawTransferenceFunction();
133         }
134
135         HistogramWidget::~HistogramWidget()
136         {
137                 delete histogram;
138                 delete plotter;
139
140         }
141         /*
142         Draw the histogram in the plotter
143         */
144         void HistogramWidget::drawHistogram()
145         {
146                 //int xValues[MAX],yValues[MAX],extent[6];
147                 double* xValues;
148                 double* yValues;
149                 
150                 vtkImageData* histogramImageData=histogram->getHistogram();
151                 
152                 //size
153                 histogramSize=histogram->getSize();
154
155                 //plotting
156                 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
157                 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
158                 
159                 double* histogramPointer=(double*)histogramImageData->GetScalarPointer(0,0,0);
160                 int i;
161                 for(i=0; i< histogramSize; i++)
162                 {
163                         xValues[i]=i;
164                         if (histogramPointer[i]==0)
165                         {
166                                 yValues[i]=0;
167                         } else {
168                                 yValues[i]=log(histogramPointer[i])*10;
169                         } // histogramPointer 
170                 } // for i
171
172                 
173                 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
174                 
175                 if (histogramFunction)
176                 {
177                         histogramFunction->setEditable(false);
178                         //smooth function
179                         histogramFunction->setType(2);
180                         //it is an histogram
181                         histogramFunction->setmType(2);
182                         idHistogram=plotter->addFunction(histogramFunction);
183                         //for setting range the vision
184                         plotter->addFunctionToMove(histogramFunction);
185                         wxPen mypen1(*wxBLUE, 1, wxSOLID );
186                         mypen1.SetWidth(2);
187                         histogramFunction->SetPen( mypen1 );
188                 }
189                         
190                         free(xValues);
191                         free(yValues);
192                 
193         }
194         /*
195         Draw the transference function in the plotter
196         one  for default
197         */
198         void HistogramWidget::drawTransferenceFunction()
199         {
200                         double xValues[5],yValues[5];
201                         //xValues
202                         int maxValueGrey=histogram->getMaximumLevelOfGrey();
203                         xValues[0] = 0;
204                         xValues[1] = maxValueGrey/16;
205                         xValues[2] = maxValueGrey/8;
206                         xValues[3] = maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
207                         xValues[4] = maxValueGrey;
208                         
209                         //yValues
210                         yValues[0] = 0;
211                         yValues[1] = 25;
212                         yValues[2] = 100;
213                         yValues[3] = 25;
214                         yValues[4] = 0;
215                         
216                         pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 ); 
217                         printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);             
218                         // Including and drawing the created function in the plotter
219                         if (tf)
220                         {
221                                 tf->setType(1);
222                                 //is the actual Function
223                                 tf->setActual(true);
224                                 //show points
225                                 tf->SetShowPoints(true);
226                                 idTransferenceFunction=plotter->addFunction( tf );
227                                 if(type==1)
228                                 {
229                                         plotter->addFunctionToMove(tf);
230                                 }
231                                 wxPen mypen(*wxBLACK,0.5, wxSOLID  );
232                                 mypen.SetWidth(2);
233                                 tf->SetPen( mypen );
234                         } // if tf
235         }
236
237         /*
238                 if the user resize the window   
239         */
240         void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
241         {
242                 int scrX,scrY;
243                 GetClientSize(&scrX,&scrY);
244                 plotter->SetSize(scrX,scrY);
245                 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
246                 if(actual!=NULL)
247                 {
248                         actual->setScreens(scrX,scrY);
249                         actual->setScales();
250                 }
251                 
252         }
253         /*
254                 get number of points of the transference function
255         */
256         int HistogramWidget::getSizeTransferenceFunction()
257         {
258                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
259                 return tf->getSizePoints();
260
261         }
262         /*
263                 get a point of the transference function
264         */
265         void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
266         {
267                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
268                 if(tf!=NULL)
269                 {
270                         wxNode* pnode                           = tf->GetPointAt(index);
271                         pFunctionPoint* point   = (pFunctionPoint*)pnode->GetData();
272                         x                                                               = point->getRealX();
273                         y                                                               = point->getRealY();
274                 }
275         }
276         /*
277                 get a point of the Histogram
278                 given the grey value
279         */
280         int HistogramWidget::getHistogramPoint(int gValue)
281         {
282                 return histogram->getHistogramPoint(gValue);
283         }
284         /*
285                 get number of points of the barColor
286         */
287         int HistogramWidget::getSizeBarColor()
288         {
289                 return  plotter->getColorPointsSize();
290         }
291         /*
292                 get a  color int the bqr color
293         */
294         void HistogramWidget::getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
295         {
296                 double tmp      = x;
297                 plotter->getBarColorDataAt(index,tmp,red,green,blue);
298                 x                               = (int)tmp;
299         }
300         /*
301          Returns the  maximum value ot the histogram that is show to the user 
302         */
303         float HistogramWidget::getMaxShowedPorcentage()
304         {
305                 float  porcentageMaxX                                           = plotter->getMaxShowedPorcentage();
306                 pGraphicalFunction* histogramFunction   = plotter->getFunction(idHistogram);
307                 int min                                                                                 = histogramFunction->getMinX();
308                 float x                                                                                 = porcentageMaxX*(histogramFunction->getMaxX()-min);
309                 return min + x;
310         }
311         /*
312          Returns the  minimum value ot the histogram that is show to the user 
313         */
314         float HistogramWidget::getMinShowedPorcentage()
315         {
316                 float  porcentageMinX                                           = plotter->getMinShowedPorcentage();
317                 pGraphicalFunction* histogramFunction   = plotter->getFunction(idHistogram);
318                 int min                                                                                 = histogramFunction->getMinX();
319                 float x                                                                                 = porcentageMinX*(histogramFunction->getMaxX()-min);
320                 return min + x;
321         }
322         /*
323          Returns the  minimum value ot the histogram that is show to the user 
324         */
325         float HistogramWidget::getActualShowedPorcentage()
326         {
327                 float  porcentageActualX                                        = plotter->getMinShowedPorcentage();
328                 pGraphicalFunction* histogramFunction   = plotter->getFunction(idHistogram);
329                 int min                                                                                 = histogramFunction->getMinX();
330                 float x                                                                                 = porcentageActualX*(histogramFunction->getMaxX()-min);
331                 return min + x;
332         }
333         //---------------------------------------
334         // setting data in transferences function
335         // and in bar color
336         //----------------------------------------
337         /*
338           Adds a point to the transference function
339         */
340         bool  HistogramWidget::addPointToTransferenceFunction(double x, double y)
341         {
342                 bool result=false;
343                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
344 //printf("EED %p HistogramWidget::addPointToTransferenceFunction tp%p x%f y%f %d\n",this, tf, x ,y, idTransferenceFunction);
345                 if (tf!=NULL) 
346                 { 
347                         result=tf->AddPoint(x,y); 
348                 } // if tf
349                 
350                 return result;
351         }
352         /*
353                 add a color point to the histogram
354                 @param x the level of grey to which the color is assigned
355                 @param red the level of red for the color
356                 @param green the level of red for the color
357                 @param blue the level of red for the color
358         */
359         bool  HistogramWidget::addColorPoint(double x,int red,int green, int blue)
360         {
361                 return plotter->addColorPoint(x,red,green,blue);
362         }
363          
364         //------------------------
365         //Erase data
366         //------------------------
367         /*
368          Erase all the points that are in the transference function
369         */
370
371         void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
372         {
373                 if(transferenceFunctionHasPoints)
374                 {
375                         // we have to erase the points
376                         pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
377                         if (tf!=NULL){
378                                 int numOfPoints=tf->getSizePoints();
379                                 int i=numOfPoints-1;//-2;
380                                 while(i>=0)
381                                 {
382                                         tf->deletePointAt(i);
383                                         i--;
384                                 } // while
385                         } // if tf
386                 } // if transferenceFunctionHasPoints
387                 
388         //we set for actual the histogram
389                 //plotter->setActual()
390         }
391         /*
392         Erase the  color points in the plotter
393         */
394         void HistogramWidget::eraseColorPoints()
395         {
396                 plotter->eraseColorPoints();
397         }
398         //------------------
399         //Updating plotter
400         //------------------
401         void HistogramWidget::updatePlotter()
402         {
403                 plotter->update();
404         }
405
406         //-------------------
407         // Getter and setters
408         //-------------------
409
410         void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
411         {
412                 transferenceFunctionHasPoints = hasPoints;
413         }
414
415         void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
416         {
417                 transferenceFunctionHasPoints = hasColorPoints;
418         }
419
420         int HistogramWidget::getHistogramSize()
421         {
422                 return histogramSize;
423         }
424
425         void HistogramWidget::setType(int type)
426         {
427                 this->type = type;
428         }
429
430 /**
431 **      Returns two vectors, the grey level of the point and its value, the value is between [0,1]
432 **/
433         void HistogramWidget::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value)
434         {
435                 plotter->GetValuesPointsFunction(greylevel,value,histogramSize);
436         }
437
438 /**
439 **      Returns two vectors, the grey level of the point and its value, the red, green
440 **      and blue value is between [0,1]
441 **/
442         void HistogramWidget::GetValuesColorPointsFunction(std::vector<double>& greylevel,
443                                                                 std::vector<double>& red,
444                                                                 std::vector<double>& green,
445                                                                 std::vector<double>& blue)
446         {
447                 plotter->GetValuesColorPointsFunction(greylevel,red,green,blue);
448         }