2 #include "ContourCrownWidget.h"
4 #include <vtkImageMapToColors.h>
5 #include <vtkImageActor.h>
7 #include "wxVtk2DBaseView.h"
8 #include "vtkLookupTable.h"
10 //----------------------------------------------------------------------
11 wxMaracasCoutourTool::wxMaracasCoutourTool(wxWindow *parent, wxVtkBaseView *wxvtkbaseview, vtkImageData* imagedata)
12 : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
14 //EED this->mbbtkContourCrown = contourcrown;
15 this->imagedata = imagedata;
16 this->wxvtkbaseview = wxvtkbaseview;
17 wxPanel *panel = this;
18 wxSizer *sizer = NULL;
20 InitVtkImagesResult();
22 vtkLookupTable *table = vtkLookupTable::New();
23 // table->SetAlphaRange(128,255);
24 table->SetRange(0, 255); // image intensity range
25 // table->SetValueRange(0.0, 1.0); // from black to white
26 table->SetValueRange(0.0, 255.0); // from black to white
28 table->SetTableValue (0, 1.0 , 1.0 , 1.0 , 0.1 );
29 table->SetTableValue (255, 1.0 , 1.0 , 0.0 , 0.1);
31 // table->SetTableValue (0, 0.0 , 0.0 , 1.0 , 0.35 );
32 // table->SetTableValue (255, 1.0 , 0.0 , 0.0 , 0.10);
34 table->SetSaturationRange(0.0, 0.0); // no color saturation
35 table->SetRampToLinear();
38 vtkImageMapToColors *mapperImage = vtkImageMapToColors::New();
39 mapperImage->SetLookupTable(table);
40 mapperImage->SetInput( imagedataMaskResult );
41 vtkImageActor *actorImage = vtkImageActor::New();
42 actorImage->SetInput( mapperImage->GetOutput() );
43 actorImage->SetOpacity(1);
45 wxvtkbaseview->GetRenderer()->AddActor(actorImage);
48 wxButton *btnContourA = new wxButton( panel, -1, _T("Crown Extern"));
49 wxButton *btnContourB = new wxButton( panel, -1, _T("Crown Intern"));
50 wxButton *btnContourAB = new wxButton( panel, -1, _T("Crown Extern and Intern"));
52 //JSTG 21-03-08 --------------------------------------------------------------------
53 wxButton *btnSaveContour = new wxButton( panel, -1, _T("Save Contour"));
54 wxButton *btnSaveControlPoints = new wxButton( panel, -1, _T("Save Control Points"));
56 wxButton *btnEreaseLastContour = new wxButton( panel, -1, _T("Erease Last Contour"));
57 wxButton *btnEreaseLastCP = new wxButton( panel, -1, _T("Erease Last Control Points"));
58 //----------------------------------------------------------------------------------
60 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(10); //Original Value = 10
61 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
62 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
64 Connect(btnContourA->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourA );
65 Connect(btnContourB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourB );
66 Connect(btnContourAB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourAB );
68 //JSTG 21-03-08 ---------------------------------------------------------------------
69 Connect(btnSaveContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveContour );
70 Connect(btnSaveControlPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveControlPoints );
72 Connect(btnEreaseLastContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastContour );
73 Connect(btnEreaseLastCP->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastCP );
74 //-----------------------------------------------------------------------------------
76 sizer1->Add(btnContourA);
77 sizer1->Add(btnContourB);
78 sizer1->Add(btnContourAB);
80 //JSTG 21-03-08 --------------------
81 sizer1->Add(btnSaveContour);
82 sizer1->Add(btnSaveControlPoints);
84 sizer1->Add(btnEreaseLastContour);
85 sizer1->Add(btnEreaseLastCP);
86 //----------------------------------
88 panel -> SetSizer(sizer);
89 panel -> SetAutoLayout(true);
94 // ------------------------------------------------------------------------
96 wxMaracasCoutourTool::~wxMaracasCoutourTool()
100 // ------------------------------------------------------------------------
101 wxVtkBaseView *wxMaracasCoutourTool::GetWxVtkBaseView()
103 return this->wxvtkbaseview;
107 // ------------------------------------------------------------------------
109 void wxMaracasCoutourTool::ConfigureVTK()
112 this->imagedata->GetSpacing(spc);
115 _manContourControl_1 = new manualContourControler();
116 _mContourModel_1 = new manualContourModel();
117 _mViewContour_1 = new manualViewContour();
118 _mViewContour_1->SetModel( _mContourModel_1 );
119 _mViewContour_1->SetWxVtkBaseView( this->wxvtkbaseview );
120 _mViewContour_1->SetRange( 2 );
121 _mViewContour_1->SetZ( 1000 );
124 _mContourModel_1->SetNumberOfPointsSpline(100);
128 _mViewContour_1->SetSpacing(spc);
132 _mViewContour_1->SetColorNormalContour(0, 0, 1);
133 _mViewContour_1->SetColorEditContour(0.5, 0.5, 0.5);
136 _manContourControl_1->SetModelView( _mContourModel_1 , _mViewContour_1 );
137 ((vtkInteractorStyleBaseView*)this->wxvtkbaseview->GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _manContourControl_1 );
138 _manContourControl_1->CreateNewManualContour();
139 _manContourControl_1->SetActive(false);
140 _mViewContour_1->RefreshContour();
144 _manContourControl_2 = new manualContourControler();
145 _mContourModel_2 = new manualContourModel();
146 _mViewContour_2 = new manualViewContour();
147 _mViewContour_2->SetModel( _mContourModel_2 );
148 _mViewContour_2->SetWxVtkBaseView( this->wxvtkbaseview );
149 _mViewContour_2->SetRange( 2 );
150 _mViewContour_2->SetZ( 1000 );
153 _mViewContour_2->SetSpacing(spc);
156 _mViewContour_2->SetColorNormalContour(1, 0, 0);
157 _mViewContour_2->SetColorEditContour(0.5, 0.5, 0.5);
160 _manContourControl_2->SetModelView( _mContourModel_2 , _mViewContour_2 );
161 ((vtkInteractorStyleBaseView*)this->wxvtkbaseview->GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _manContourControl_2 );
162 _manContourControl_2->CreateNewManualContour();
163 _manContourControl_2->SetActive(false);
164 _mViewContour_2->RefreshContour();
169 _contprop = new ContourPropagation();
172 //JSTG 26-02-08 -------------------------------------------------------------------------------------------------------------
173 void wxMaracasCoutourTool::SetControlPoints( std::vector<double> *InVectorX,
174 std::vector<double> *InVectorY,
175 std::vector<double> *InVectorZ )
180 int i,size=InVectorX->size();
183 _mContourModel_1->DeleteAllPoints();
184 _mViewContour_1->DeleteContour();
188 lstSize = _mContourModel_1->AddPoint( (*InVectorX)[i] , (*InVectorY)[i] , (*InVectorZ)[i] );
189 _mViewContour_1->AddPoint();
191 //_manContourControl_1->CreateNewManualContour();
192 //_manContourControl_1->SetActive(true);
193 _mViewContour_1->CreateNewContour();
194 _mContourModel_1->UpdateSpline();
195 _mViewContour_1->RefreshContour();
196 //_mViewContour_1->Refresh ();
201 // JSTG 26-02-08 ------------------------------------------------------------------------------
202 void wxMaracasCoutourTool::GetSplinePoints( std::vector<double> *pLstContourX,
203 std::vector<double> *pLstContourY,
204 std::vector<double> *pLstContourZ )
208 _mContourModel_1->UpdateSpline();
210 for(i=0; i<_mContourModel_1->GetNumberOfPointsSpline(); i++)
212 _mContourModel_1->GetSpline_i_Point( i, &x, &y, &z );
213 pLstContourX->push_back(x);
214 pLstContourY->push_back(y);
215 //pLstContourY->push_back(z); //Fake Z -> For Visualization
216 pLstContourZ->push_back(_zz); //True Z -> For processing
219 //---------------------------------------------------------------------------------------------
221 //------------------------------------------------------------------------
222 void wxMaracasCoutourTool::OnContourA(wxCommandEvent& event)
224 _manContourControl_1 -> SetActive(true);
225 _manContourControl_2 -> SetActive(false);
227 //------------------------------------------------------------------------
228 void wxMaracasCoutourTool::OnContourB(wxCommandEvent& event)
230 _manContourControl_1 -> SetActive(false);
231 _manContourControl_2 -> SetActive(true);
233 //------------------------------------------------------------------------
234 void wxMaracasCoutourTool::OnContourAB(wxCommandEvent& event)
236 _manContourControl_1 -> SetActive(true);
237 _manContourControl_2 -> SetActive(true);
240 //------------------------------------------------------------------------
241 void wxMaracasCoutourTool::GetMinMaxPoint(int *minPoint,
243 manualViewBaseContour *manualviewbaseecontour,
244 manualContourModel *manualcontourmodel
248 //int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points // JPRx
250 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
251 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
252 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
253 //------------------------------------------------------------------------------------------------------
255 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
258 //double delta=( double ) ( np ) / ( double ) ( nps );
259 manualcontourmodel->UpdateSpline();
260 for (i=0; i<nps; i++)
262 //t= delta * (double)i;
263 //manualcontourmodel->GetSplinePoint(t,x,y,z);
264 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
265 if (x<minPoint[0]){ minPoint[0]=(int)x; }
266 if (y<minPoint[1]){ minPoint[1]=(int)y; }
267 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
268 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
270 //------------------------------------------------------------------------------------------------------
273 //------------------------------------------------------------------------
275 int wxMaracasCoutourTool::AnalisisContourInside(int x,
277 manualViewBaseContour *manualviewbaseecontour,
278 manualContourModel *manualcontourmodel
283 int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points
285 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
286 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
287 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
288 //double x1,y1,z1,x2,y2,z2,t;
289 double x1,y1,z1,x2,y2,z2;
290 double xx1, yy1,xx2, yy2;
291 //double delta=( double ) ( np ) / ( double ) ( nps );
292 manualcontourmodel->UpdateSpline();
293 //------------------------------------------------------------------------------------------------------
298 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
300 //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
301 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
302 for (i=1; i<=nps; i++)
305 //t= delta * (double)(i%nps);
306 //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
307 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
308 //------------------------------------------------------------------------------------------------------
309 //by triangle similarity
310 if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
312 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; }
313 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
314 if ( ((xx1<xx2)&&(x<(xx1+d))) || ((xx1>xx2)&&(x<(xx1-d))) ) { result++; }
322 //------------------------------------------------------------------------
325 bool wxMaracasCoutourTool::isInside(int x, int y)
330 imagedataValueResult->GetExtent(ext);
332 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
334 int numberLeftA=AnalisisContourInside(x,y,_mViewContour_1, _mContourModel_1);
335 int numberLeftB=AnalisisContourInside(x,y,_mViewContour_2, _mContourModel_2);
336 if ( (numberLeftA+numberLeftB) % 2 ==1){ result = true; }
342 //------------------------------------------------------------------------
344 double wxMaracasCoutourTool::GetDataValue(int x, int y)
347 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
348 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
352 p = (unsigned short *)imagedata->GetScalarPointer(x,y,z);
356 //------------------------------------------------------------------------
358 void wxMaracasCoutourTool::PutVtkImageDataResultValue( int x, int y, double value )
360 unsigned short *pValue;
361 unsigned short *pMask;
362 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,0);
363 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,0);
365 *pValue = (unsigned short)value;
368 //------------------------------------------------------------------------
369 void wxMaracasCoutourTool::ResetImageResult()
371 unsigned short *pValue;
372 unsigned short *pMask;
373 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,0);
374 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,0);
377 imagedataValueResult->GetExtent(ext);
379 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
380 for(i=0; i<size; i++)
388 //------------------------------------------------------------------------
390 void wxMaracasCoutourTool::GetValuesInsideCrown(std::vector<double> *pLstValue,
391 std::vector<double> *pLstValuePosX,
392 std::vector<double> *pLstValuePosY,
393 std::vector<double> *pLstValuePosZ)
396 pLstValuePosX->clear();
397 pLstValuePosY->clear();
398 pLstValuePosZ->clear();
412 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_1, _mContourModel_1);
413 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_2, _mContourModel_2);
415 for (j=minPoint[1]; j<maxPoint[1]; j++)
417 for (i=minPoint[0]; i<maxPoint[0]; i++)
419 if (isInside(i,j)==true)
421 value = GetDataValue(i,j);
422 PutVtkImageDataResultValue(i,j, value );
423 pLstValue -> push_back( value );
424 pLstValuePosX -> push_back( i );
425 pLstValuePosY -> push_back( j );
426 pLstValuePosZ -> push_back( -1 );
431 imagedataValueResult->Modified();
432 imagedataMaskResult->Modified();
435 //------------------------------------------------------------------------
437 vtkImageData *wxMaracasCoutourTool::GetVtkImageValueResult()
439 return imagedataValueResult;
441 //------------------------------------------------------------------------
442 vtkImageData *wxMaracasCoutourTool::GetVtkImageMaskResult()
444 return imagedataMaskResult;
446 // ------------------------------------------------------------------------
447 void wxMaracasCoutourTool::InitVtkImagesResult()
454 imagedata->GetSpacing(spc);
455 imagedata->GetExtent(ext);
456 newDim[0]=ext[1]-ext[0]+1;
457 newDim[1]=ext[3]-ext[2]+1;
459 scalartype = imagedata->GetScalarType();
461 imagedataValueResult = vtkImageData::New();
462 // imagedataValueResult->SetScalarType(scalartype);
463 imagedataValueResult->SetScalarTypeToUnsignedShort();
464 imagedataValueResult->SetSpacing(spc);
465 imagedataValueResult->SetDimensions( newDim );
466 imagedataValueResult->AllocateScalars();
468 imagedataMaskResult = vtkImageData::New();
469 // imagedataMaskResult->SetScalarType(scalartype);
470 imagedataMaskResult->SetScalarTypeToUnsignedShort();
471 imagedataMaskResult->SetSpacing(spc);
472 imagedataMaskResult->SetDimensions( newDim );
473 imagedataMaskResult->AllocateScalars();
477 //JSTG 21-03-08 -------------------------------------------------------------------
478 void wxMaracasCoutourTool::OnSaveContour(wxCommandEvent& event)
480 //For write the actual time and date
485 tblock = localtime(&timer);
490 //WHERE CAN I MAKE THEM DELETE!!!
491 _mpoint = new manualPoint();
493 std::vector<double> tempX;
494 std::vector<double> tempY;
495 std::vector<double> tempZ;
500 int size = _mContourModel_1->GetNumberOfPointsSpline();
501 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
502 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
505 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","a");
506 //fprintf(fd,"Date %s", asctime(tblock) );
507 fprintf(fd,"\nPointsContour %d", size );
508 for(int i=0; i<size; i++)
510 _mContourModel_1->GetSpline_i_Point(i,&xx,&yy,&zz);
511 fprintf(fd,"\n%f %f %d",xx,yy,z); //z -> Contain the true value || zz -> Contain the cheating value for visualitation
519 _contprop->appendContour(&tempX,&tempY,&tempZ);
522 //JSTG 21-03-08 --------------------------------------------------------------------
523 void wxMaracasCoutourTool::OnSaveControlPoints(wxCommandEvent& event)
525 //For write the actual time and date
530 tblock = localtime(&timer);
533 _mpoint = new manualPoint();
534 int size = _mContourModel_1->GetSizeLstPoints();
535 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
536 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
538 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","a");
539 //fprintf(fd,"Date %s", asctime(tblock) );
540 fprintf(fd,"\nNumberOfControlPoints %d", size );
541 for(int i=0; i<size; i++)
543 _mpoint = _mContourModel_1->GetManualPoint(i);
544 fprintf(fd,"\n%f %f %d",_mpoint->GetX(),_mpoint->GetY(),z);
548 //----------------------------------------------------------------------------------
549 void wxMaracasCoutourTool::EreaseLastContour(wxCommandEvent& event)
566 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","r");
571 // //fscanf(fd," %s %d",&firstline,&size); // JPRx
572 fscanf(fd," %s %d",firstline,&size);
574 _SizesC.push_back(size);
575 for(j=0; j<size; j++)
577 fscanf(fd,"%lf %lf %d",&x,&y,&z);
578 _tempCX.push_back(x);
579 _tempCY.push_back(y);
580 _tempCZ.push_back(z);
586 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
590 for(i=0; i<size; i++)
598 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","w");
602 for(i=0; i<(int)(_SizesC.size()); i++)
604 fprintf(fd,"\nNumberOfPoints %d", _SizesC[i] );
605 for(j=0; j<_SizesC[i]; j++)
607 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
614 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
619 //----------------------------------------------------------------------------------
620 void wxMaracasCoutourTool::EreaseLastCP(wxCommandEvent& event)
637 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","r");
642 //fscanf(fd,""); // ?? JPRx
643 //fscanf(fd," %s %d",&firstline,&size);// JPRx
644 fscanf(fd," %s %d",firstline,&size);
645 _SizesC.push_back(size);
646 for(j=0; j<size; j++)
648 fscanf(fd,"%lf %lf %d",&x,&y,&z);
649 _tempCX.push_back(x);
650 _tempCY.push_back(y);
651 _tempCZ.push_back(z);
657 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
661 for(i=0; i<size; i++)
669 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","w");
673 for(i=0; i<(int)(_SizesC.size()); i++)
675 fprintf(fd,"\nNumberOfControlPoints %d", _SizesC[i] );
676 for(j=0; j<_SizesC[i]; j++)
678 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
685 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
689 //----------------------------------------------------------------------------------