2 #include "ContourCrownWidget.h"
4 #include <vtkImageMapToColors.h>
5 #include <vtkImageActor.h>
9 //----------------------------------------------------------------------
10 wxMaracasCoutourTool::wxMaracasCoutourTool(wxWindow *parent, wxVtkBaseView *wxvtkbaseview, vtkImageData* imagedata)
11 : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
13 //EED this->mbbtkContourCrown = contourcrown;
14 this->imagedata = imagedata;
15 this->wxvtkbaseview = wxvtkbaseview;
16 wxPanel *panel = this;
17 wxSizer *sizer = NULL;
19 InitVtkImagesResult();
21 vtkLookupTable *table = vtkLookupTable::New();
22 // table->SetAlphaRange(128,255);
23 table->SetRange(0, 255); // image intensity range
24 // table->SetValueRange(0.0, 1.0); // from black to white
25 table->SetValueRange(0.0, 255.0); // from black to white
27 table->SetTableValue (0, 1.0 , 1.0 , 1.0 , 0.1 );
28 table->SetTableValue (255, 1.0 , 1.0 , 0.0 , 0.1);
30 // table->SetTableValue (0, 0.0 , 0.0 , 1.0 , 0.35 );
31 // table->SetTableValue (255, 1.0 , 0.0 , 0.0 , 0.10);
33 table->SetSaturationRange(0.0, 0.0); // no color saturation
34 table->SetRampToLinear();
37 vtkImageMapToColors *mapperImage = vtkImageMapToColors::New();
38 mapperImage->SetLookupTable(table);
39 mapperImage->SetInput( imagedataMaskResult );
40 vtkImageActor *actorImage = vtkImageActor::New();
41 actorImage->SetInput( mapperImage->GetOutput() );
42 actorImage->SetOpacity(1);
44 wxvtkbaseview->GetRenderer()->AddActor(actorImage);
47 wxButton *btnContourA = new wxButton( panel, -1, _T("Crown Extern"));
48 wxButton *btnContourB = new wxButton( panel, -1, _T("Crown Intern"));
49 wxButton *btnContourAB = new wxButton( panel, -1, _T("Crown Extern and Intern"));
51 //JSTG 21-03-08 --------------------------------------------------------------------
52 wxButton *btnSaveContour = new wxButton( panel, -1, _T("Save Contour"));
53 wxButton *btnSaveControlPoints = new wxButton( panel, -1, _T("Save Control Points"));
55 wxButton *btnEreaseLastContour = new wxButton( panel, -1, _T("Erease Last Contour"));
56 wxButton *btnEreaseLastCP = new wxButton( panel, -1, _T("Erease Last Control Points"));
57 //----------------------------------------------------------------------------------
59 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(10); //Original Value = 10
60 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
61 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
63 Connect(btnContourA->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourA );
64 Connect(btnContourB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourB );
65 Connect(btnContourAB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourAB );
67 //JSTG 21-03-08 ---------------------------------------------------------------------
68 Connect(btnSaveContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveContour );
69 Connect(btnSaveControlPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveControlPoints );
71 Connect(btnEreaseLastContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastContour );
72 Connect(btnEreaseLastCP->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastCP );
73 //-----------------------------------------------------------------------------------
75 sizer1->Add(btnContourA);
76 sizer1->Add(btnContourB);
77 sizer1->Add(btnContourAB);
79 //JSTG 21-03-08 --------------------
80 sizer1->Add(btnSaveContour);
81 sizer1->Add(btnSaveControlPoints);
83 sizer1->Add(btnEreaseLastContour);
84 sizer1->Add(btnEreaseLastCP);
85 //----------------------------------
87 panel -> SetSizer(sizer);
88 panel -> SetAutoLayout(true);
93 // ------------------------------------------------------------------------
95 wxMaracasCoutourTool::~wxMaracasCoutourTool()
99 // ------------------------------------------------------------------------
100 wxVtkBaseView *wxMaracasCoutourTool::GetWxVtkBaseView()
102 return this->wxvtkbaseview;
106 // ------------------------------------------------------------------------
108 void wxMaracasCoutourTool::ConfigureVTK()
111 this->imagedata->GetSpacing(spc);
114 _manContourControl_1 = new manualContourControler();
115 _mContourModel_1 = new manualContourModel();
116 _mViewContour_1 = new manualViewContour();
117 _mViewContour_1->SetModel( _mContourModel_1 );
118 _mViewContour_1->SetWxVtkBaseView( this->wxvtkbaseview );
119 _mViewContour_1->SetRange( 2 );
120 _mViewContour_1->SetZ( 1000 );
123 _mContourModel_1->SetNumberOfPointsSpline(100);
127 _mViewContour_1->SetSpacing(spc);
131 _mViewContour_1->SetColorNormalContour(0, 0, 1);
132 _mViewContour_1->SetColorEditContour(0.5, 0.5, 0.5);
135 _manContourControl_1->SetModelView( _mContourModel_1 , _mViewContour_1 );
136 this->wxvtkbaseview->GetInteractorStyleBaseView()->AddInteractorStyleMaracas( _manContourControl_1 );
137 _manContourControl_1->CreateNewManualContour();
138 _manContourControl_1->SetActive(false);
139 _mViewContour_1->RefreshContour();
143 _manContourControl_2 = new manualContourControler();
144 _mContourModel_2 = new manualContourModel();
145 _mViewContour_2 = new manualViewContour();
146 _mViewContour_2->SetModel( _mContourModel_2 );
147 _mViewContour_2->SetWxVtkBaseView( this->wxvtkbaseview );
148 _mViewContour_2->SetRange( 2 );
149 _mViewContour_2->SetZ( 1000 );
152 _mViewContour_2->SetSpacing(spc);
155 _mViewContour_2->SetColorNormalContour(1, 0, 0);
156 _mViewContour_2->SetColorEditContour(0.5, 0.5, 0.5);
159 _manContourControl_2->SetModelView( _mContourModel_2 , _mViewContour_2 );
160 this->wxvtkbaseview->GetInteractorStyleBaseView()->AddInteractorStyleMaracas( _manContourControl_2 );
161 _manContourControl_2->CreateNewManualContour();
162 _manContourControl_2->SetActive(false);
163 _mViewContour_2->RefreshContour();
168 _contprop = new ContourPropagation();
171 //JSTG 26-02-08 -------------------------------------------------------------------------------------------------------------
172 void wxMaracasCoutourTool::SetControlPoints( std::vector<double> *InVectorX,
173 std::vector<double> *InVectorY,
174 std::vector<double> *InVectorZ )
179 int i,size=InVectorX->size();
182 _mContourModel_1->DeleteAllPoints();
183 _mViewContour_1->DeleteContour();
187 lstSize = _mContourModel_1->AddPoint( (*InVectorX)[i] , (*InVectorY)[i] , (*InVectorZ)[i] );
188 _mViewContour_1->AddPoint();
190 //_manContourControl_1->CreateNewManualContour();
191 //_manContourControl_1->SetActive(true);
192 _mViewContour_1->CreateNewContour();
193 _mContourModel_1->UpdateSpline();
194 _mViewContour_1->RefreshContour();
195 //_mViewContour_1->Refresh ();
200 // JSTG 26-02-08 ------------------------------------------------------------------------------
201 void wxMaracasCoutourTool::GetSplinePoints( std::vector<double> *pLstContourX,
202 std::vector<double> *pLstContourY,
203 std::vector<double> *pLstContourZ )
207 _mContourModel_1->UpdateSpline();
209 for(i=0; i<_mContourModel_1->GetNumberOfPointsSpline(); i++)
211 _mContourModel_1->GetSpline_i_Point( i, &x, &y, &z );
212 pLstContourX->push_back(x);
213 pLstContourY->push_back(y);
214 //pLstContourY->push_back(z); //Fake Z -> For Visualization
215 pLstContourZ->push_back(_zz); //True Z -> For processing
218 //---------------------------------------------------------------------------------------------
220 //------------------------------------------------------------------------
221 void wxMaracasCoutourTool::OnContourA(wxCommandEvent& event)
223 _manContourControl_1 -> SetActive(true);
224 _manContourControl_2 -> SetActive(false);
226 //------------------------------------------------------------------------
227 void wxMaracasCoutourTool::OnContourB(wxCommandEvent& event)
229 _manContourControl_1 -> SetActive(false);
230 _manContourControl_2 -> SetActive(true);
232 //------------------------------------------------------------------------
233 void wxMaracasCoutourTool::OnContourAB(wxCommandEvent& event)
235 _manContourControl_1 -> SetActive(true);
236 _manContourControl_2 -> SetActive(true);
239 //------------------------------------------------------------------------
240 void wxMaracasCoutourTool::GetMinMaxPoint(int *minPoint,
242 manualViewBaseContour *manualviewbaseecontour,
243 manualContourModel *manualcontourmodel
247 int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points
249 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
250 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
251 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
252 //------------------------------------------------------------------------------------------------------
254 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
257 //double delta=( double ) ( np ) / ( double ) ( nps );
258 manualcontourmodel->UpdateSpline();
259 for (i=0; i<nps; i++)
261 //t= delta * (double)i;
262 //manualcontourmodel->GetSplinePoint(t,x,y,z);
263 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
264 if (x<minPoint[0]){ minPoint[0]=(int)x; }
265 if (y<minPoint[1]){ minPoint[1]=(int)y; }
266 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
267 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
269 //------------------------------------------------------------------------------------------------------
272 //------------------------------------------------------------------------
274 int wxMaracasCoutourTool::AnalisisContourInside(int x,
276 manualViewBaseContour *manualviewbaseecontour,
277 manualContourModel *manualcontourmodel
282 int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points
284 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
285 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
286 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
287 //double x1,y1,z1,x2,y2,z2,t;
288 double x1,y1,z1,x2,y2,z2;
289 double xx1, yy1,xx2, yy2;
290 //double delta=( double ) ( np ) / ( double ) ( nps );
291 manualcontourmodel->UpdateSpline();
292 //------------------------------------------------------------------------------------------------------
297 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
299 //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
300 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
301 for (i=1; i<=nps; i++)
304 //t= delta * (double)(i%nps);
305 //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
306 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
307 //------------------------------------------------------------------------------------------------------
308 //by triangle similarity
309 if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
311 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; }
312 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
313 if ( ((xx1<xx2)&&(x<(xx1+d))) || ((xx1>xx2)&&(x<(xx1-d))) ) { result++; }
321 //------------------------------------------------------------------------
324 bool wxMaracasCoutourTool::isInside(int x, int y)
329 imagedataValueResult->GetExtent(ext);
331 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
333 int numberLeftA=AnalisisContourInside(x,y,_mViewContour_1, _mContourModel_1);
334 int numberLeftB=AnalisisContourInside(x,y,_mViewContour_2, _mContourModel_2);
335 if ( (numberLeftA+numberLeftB) % 2 ==1){ result = true; }
341 //------------------------------------------------------------------------
343 double wxMaracasCoutourTool::GetDataValue(int x, int y)
346 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
347 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
351 p = (unsigned short *)imagedata->GetScalarPointer(x,y,z);
355 //------------------------------------------------------------------------
357 void wxMaracasCoutourTool::PutVtkImageDataResultValue( int x, int y, double value )
359 unsigned short *pValue;
360 unsigned short *pMask;
361 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,0);
362 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,0);
364 *pValue = (unsigned short)value;
367 //------------------------------------------------------------------------
368 void wxMaracasCoutourTool::ResetImageResult()
370 unsigned short *pValue;
371 unsigned short *pMask;
372 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,0);
373 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,0);
376 imagedataValueResult->GetExtent(ext);
378 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
379 for(i=0; i<size; i++)
387 //------------------------------------------------------------------------
389 void wxMaracasCoutourTool::GetValuesInsideCrown(std::vector<double> *pLstValue,
390 std::vector<double> *pLstValuePosX,
391 std::vector<double> *pLstValuePosY,
392 std::vector<double> *pLstValuePosZ)
395 pLstValuePosX->clear();
396 pLstValuePosY->clear();
397 pLstValuePosZ->clear();
411 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_1, _mContourModel_1);
412 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_2, _mContourModel_2);
414 for (j=minPoint[1]; j<maxPoint[1]; j++)
416 for (i=minPoint[0]; i<maxPoint[0]; i++)
418 if (isInside(i,j)==true)
420 value = GetDataValue(i,j);
421 PutVtkImageDataResultValue(i,j, value );
422 pLstValue -> push_back( value );
423 pLstValuePosX -> push_back( i );
424 pLstValuePosY -> push_back( j );
425 pLstValuePosZ -> push_back( -1 );
430 imagedataValueResult->Modified();
431 imagedataMaskResult->Modified();
434 //------------------------------------------------------------------------
436 vtkImageData *wxMaracasCoutourTool::GetVtkImageValueResult()
438 return imagedataValueResult;
440 //------------------------------------------------------------------------
441 vtkImageData *wxMaracasCoutourTool::GetVtkImageMaskResult()
443 return imagedataMaskResult;
445 // ------------------------------------------------------------------------
446 void wxMaracasCoutourTool::InitVtkImagesResult()
453 imagedata->GetSpacing(spc);
454 imagedata->GetExtent(ext);
455 newDim[0]=ext[1]-ext[0]+1;
456 newDim[1]=ext[3]-ext[2]+1;
458 scalartype = imagedata->GetScalarType();
460 imagedataValueResult = vtkImageData::New();
461 // imagedataValueResult->SetScalarType(scalartype);
462 imagedataValueResult->SetScalarTypeToUnsignedShort();
463 imagedataValueResult->SetSpacing(spc);
464 imagedataValueResult->SetDimensions( newDim );
465 imagedataValueResult->AllocateScalars();
467 imagedataMaskResult = vtkImageData::New();
468 // imagedataMaskResult->SetScalarType(scalartype);
469 imagedataMaskResult->SetScalarTypeToUnsignedShort();
470 imagedataMaskResult->SetSpacing(spc);
471 imagedataMaskResult->SetDimensions( newDim );
472 imagedataMaskResult->AllocateScalars();
476 //JSTG 21-03-08 -------------------------------------------------------------------
477 void wxMaracasCoutourTool::OnSaveContour(wxCommandEvent& event)
479 //For write the actual time and date
484 tblock = localtime(&timer);
489 //WHERE CAN I MAKE THEM DELETE!!!
490 _mpoint = new manualPoint();
492 std::vector<double> tempX;
493 std::vector<double> tempY;
494 std::vector<double> tempZ;
499 int size = _mContourModel_1->GetNumberOfPointsSpline();
500 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
501 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
504 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","a");
505 //fprintf(fd,"Date %s", asctime(tblock) );
506 fprintf(fd,"\nPointsContour %d", size );
507 for(int i=0; i<size; i++)
509 _mContourModel_1->GetSpline_i_Point(i,&xx,&yy,&zz);
510 fprintf(fd,"\n%f %f %d",xx,yy,z); //z -> Contain the true value || zz -> Contain the cheating value for visualitation
518 _contprop->appendContour(&tempX,&tempY,&tempZ);
521 //JSTG 21-03-08 --------------------------------------------------------------------
522 void wxMaracasCoutourTool::OnSaveControlPoints(wxCommandEvent& event)
524 //For write the actual time and date
529 tblock = localtime(&timer);
532 _mpoint = new manualPoint();
533 int size = _mContourModel_1->GetSizeLstPoints();
534 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
535 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
537 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","a");
538 //fprintf(fd,"Date %s", asctime(tblock) );
539 fprintf(fd,"\nNumberOfControlPoints %d", size );
540 for(int i=0; i<size; i++)
542 _mpoint = _mContourModel_1->GetManualPoint(i);
543 fprintf(fd,"\n%f %f %d",_mpoint->GetX(),_mpoint->GetY(),z);
547 //----------------------------------------------------------------------------------
548 void wxMaracasCoutourTool::EreaseLastContour(wxCommandEvent& event)
565 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","r");
571 fscanf(fd," %s %d",&firstline,&size);
572 _SizesC.push_back(size);
573 for(j=0; j<size; j++)
575 fscanf(fd,"%lf %lf %d",&x,&y,&z);
576 _tempCX.push_back(x);
577 _tempCY.push_back(y);
578 _tempCZ.push_back(z);
584 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
588 for(i=0; i<size; i++)
596 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","w");
600 for(i=0; i<_SizesC.size(); i++)
602 fprintf(fd,"\nNumberOfPoints %d", _SizesC[i] );
603 for(j=0; j<_SizesC[i]; j++)
605 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
612 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
617 //----------------------------------------------------------------------------------
618 void wxMaracasCoutourTool::EreaseLastCP(wxCommandEvent& event)
635 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","r");
641 fscanf(fd," %s %d",&firstline,&size);
642 _SizesC.push_back(size);
643 for(j=0; j<size; j++)
645 fscanf(fd,"%lf %lf %d",&x,&y,&z);
646 _tempCX.push_back(x);
647 _tempCY.push_back(y);
648 _tempCZ.push_back(z);
654 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
658 for(i=0; i<size; i++)
666 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","w");
670 for(i=0; i<_SizesC.size(); i++)
672 fprintf(fd,"\nNumberOfControlPoints %d", _SizesC[i] );
673 for(j=0; j<_SizesC[i]; j++)
675 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
682 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
686 //----------------------------------------------------------------------------------