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