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,flag)
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()
00071 {
00072 delete histogram;
00073 delete plotter;
00074
00075 }
00076
00077
00078
00079 void HistogramWidget::drawHistogram()
00080 {
00081
00082 double* xValues;
00083 double* yValues;
00084
00085 vtkImageData* histogramImageData=histogram->getHistogram();
00086
00087
00088 histogramSize=histogram->getSize();
00089
00090
00091 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
00092 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
00093
00094 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
00095
00096 for(int i=0; i< histogramSize; i++)
00097 {
00098 xValues[i]=i;
00099 yValues[i]=log( (double) histogramPointer[i]);
00100 }
00101
00102
00103 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
00104
00105 if (histogramFunction)
00106 {
00107 histogramFunction->setEditable(false);
00108
00109 histogramFunction->setType(2);
00110
00111 histogramFunction->setmType(2);
00112 idHistogram=plotter->addFunction(histogramFunction);
00113
00114 plotter->addFunctionToMove(histogramFunction);
00115 wxPen mypen1(*wxBLUE, 1, wxSOLID );
00116 mypen1.SetWidth(2);
00117 histogramFunction->SetPen( mypen1 );
00118 }
00119
00120 free(xValues);
00121 free(yValues);
00122
00123 }
00124
00125
00126
00127
00128 void HistogramWidget::drawTransferenceFunction()
00129 {
00130
00131 double xValues[5],yValues[5];
00132
00133 int maxValueGrey=histogram->getMaximumLevelOfGrey();
00134 xValues[0]=0;
00135 xValues[1]=maxValueGrey/16;
00136 xValues[2]=maxValueGrey/8;
00137 xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
00138 xValues[4]=maxValueGrey;
00139
00140
00141 yValues[0]=0;
00142 yValues[1]=25;
00143 yValues[2]=100;
00144 yValues[3]=25;
00145 yValues[4]=0;
00146
00147 pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 );
00148 printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);
00149
00150 if (tf)
00151 {
00152 tf->setType(1);
00153
00154 tf->setActual(true);
00155
00156 tf->SetShowPoints(true);
00157 idTransferenceFunction=plotter->addFunction( tf );
00158 if(type==1)
00159 plotter->addFunctionToMove(tf);
00160 wxPen mypen(*wxBLACK,0.5, wxSOLID );
00161 mypen.SetWidth(2);
00162 tf->SetPen( mypen );
00163 }
00164 }
00165
00166
00167
00168
00169 void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
00170 {
00171 int scrX,scrY;
00172 GetClientSize(&scrX,&scrY);
00173 plotter->SetSize(scrX,scrY);
00174 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
00175 if(actual!=NULL){
00176 actual->setScreens(scrX,scrY);
00177 actual->setScales();
00178 }
00179
00180 }
00181
00182
00183
00184 int HistogramWidget::getSizeTransferenceFunction()
00185 {
00186 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00187 return tf->getSizePoints();
00188
00189 }
00190
00191
00192
00193 void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
00194 {
00195 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00196 if(tf)
00197 {
00198 wxNode* pnode=tf->GetPointAt(index);
00199 pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
00200 x=point->getRealX();
00201 y=point->getRealY();
00202 }
00203 }
00204
00205
00206
00207
00208 int HistogramWidget::getHistogramPoint(int gValue)
00209 {
00210 return histogram->getHistogramPoint(gValue);
00211 }
00212
00213
00214
00215 int HistogramWidget::getSizeBarColor()
00216 {
00217 return plotter->getColorPointsSize();
00218 }
00219
00220
00221
00222 void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
00223 {
00224 double tmp=x;
00225 plotter->getBarColorDataAt(index,tmp,red,green,blue);
00226 x=(int)tmp;
00227 }
00228
00229
00230
00231 float HistogramWidget::getMaxShowedPorcentage()
00232 {
00233 float porcentageMaxX=plotter->getMaxShowedPorcentage();
00234 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00235 int min=histogramFunction->getMinX();
00236 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
00237 return min+ x;
00238 }
00239
00240
00241
00242 float HistogramWidget::getMinShowedPorcentage()
00243 {
00244 float porcentageMinX=plotter->getMinShowedPorcentage();
00245 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00246 int min=histogramFunction->getMinX();
00247 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
00248 return min+ x;
00249 }
00250
00251
00252
00253 float HistogramWidget::getActualShowedPorcentage()
00254 {
00255 float porcentageActualX=plotter->getMinShowedPorcentage();
00256 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00257 int min=histogramFunction->getMinX();
00258 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
00259 return min+ x;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268 bool HistogramWidget::addPointToTransferenceFunction(double x, double y)
00269 {
00270 bool result=false;
00271 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00272 printf("EED %p HistogramWidget::addPointToTransferenceFunction tp%p x%f y%f %d\n",this, tf, x ,y, idTransferenceFunction);
00273 if (tf!=NULL) { result=tf->AddPoint(x,y); }
00274
00275 return result;
00276 }
00277
00278
00279
00280
00281
00282
00283
00284 bool HistogramWidget::addColorPoint(double x,int red,int green, int blue)
00285 {
00286 return plotter->addColorPoint(x,red,green,blue);
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296 void HistogramWidget::erasePointsTransferenceFunction()
00297 {
00298 if(transferenceFunctionHasPoints)
00299 {
00300
00301 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00302 if (tf!=NULL){
00303 int numOfPoints=tf->getSizePoints();
00304 int i=numOfPoints-1;
00305 while(i>=0)
00306 {
00307 tf->deletePointAt(i);
00308 i--;
00309 }
00310 }
00311 }
00312
00313
00314
00315 }
00316
00317
00318
00319 void HistogramWidget::eraseColorPoints()
00320 {
00321 plotter->eraseColorPoints();
00322 }
00323
00324
00325
00326 void HistogramWidget::updatePlotter()
00327 {
00328 plotter->update();
00329 }
00330
00331
00332
00333
00334
00335 void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
00336 {
00337 transferenceFunctionHasPoints=hasPoints;
00338 }
00339
00340 void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
00341 {
00342 transferenceFunctionHasPoints=hasColorPoints;
00343 }
00344 int HistogramWidget::getHistogramSize()
00345 {
00346 return histogramSize;
00347 }
00348 void HistogramWidget::setType(int type)
00349 {
00350 this->type=type;
00351 }
00352
00353