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