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)
12 : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
14 wxPanel *panel = this;
15 wxSizer *sizer = NULL;
18 wxButton *btnContourA = new wxButton( panel, -1, _T("Crown Extern"));
19 wxButton *btnContourB = new wxButton( panel, -1, _T("Crown Intern"));
20 wxButton *btnContourAB = new wxButton( panel, -1, _T("Crown Extern and Intern"));
22 //JSTG 21-03-08 --------------------------------------------------------------------
23 wxButton *btnSaveContour = new wxButton( panel, -1, _T("Save Contour"));
24 wxButton *btnSaveControlPoints = new wxButton( panel, -1, _T("Save Control Points"));
26 wxButton *btnEreaseLastContour = new wxButton( panel, -1, _T("Erease Last Contour"));
27 wxButton *btnEreaseLastCP = new wxButton( panel, -1, _T("Erease Last Control Points"));
28 //----------------------------------------------------------------------------------
30 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(10); //Original Value = 10
31 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
32 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
34 Connect(btnContourA->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourA );
35 Connect(btnContourB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourB );
36 Connect(btnContourAB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourAB );
38 //JSTG 21-03-08 ---------------------------------------------------------------------
39 Connect(btnSaveContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveContour );
40 Connect(btnSaveControlPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveControlPoints );
42 Connect(btnEreaseLastContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastContour );
43 Connect(btnEreaseLastCP->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastCP );
44 //-----------------------------------------------------------------------------------
46 sizer1->Add(btnContourA);
47 sizer1->Add(btnContourB);
48 sizer1->Add(btnContourAB);
50 //JSTG 21-03-08 --------------------
51 sizer1->Add(btnSaveContour);
52 sizer1->Add(btnSaveControlPoints);
54 sizer1->Add(btnEreaseLastContour);
55 sizer1->Add(btnEreaseLastCP);
56 //----------------------------------
58 panel -> SetSizer(sizer);
59 panel -> SetAutoLayout(true);
64 // ------------------------------------------------------------------------
66 wxMaracasCoutourTool::~wxMaracasCoutourTool()
70 // ------------------------------------------------------------------------
71 wxVtkBaseView *wxMaracasCoutourTool::GetWxVtkBaseView()
73 return this->wxvtkbaseview;
77 // ------------------------------------------------------------------------
78 void wxMaracasCoutourTool::Init(wxVtkBaseView *wxvtkbaseview, vtkImageData* imagedata)
80 this->imagedata = imagedata;
81 this->wxvtkbaseview = wxvtkbaseview;
83 InitVtkImagesResult();
85 vtkLookupTable *table = vtkLookupTable::New();
86 // table->SetAlphaRange(128,255);
87 table->SetRange(0, 255); // image intensity range
88 // table->SetValueRange(0.0, 1.0); // from black to white
89 table->SetValueRange(0.0, 255.0); // from black to white
91 table->SetTableValue (0, 1.0 , 1.0 , 1.0 , 0.1 );
92 table->SetTableValue (255, 1.0 , 1.0 , 0.0 , 0.1);
94 // table->SetTableValue (0, 0.0 , 0.0 , 1.0 , 0.35 );
95 // table->SetTableValue (255, 1.0 , 0.0 , 0.0 , 0.10);
97 table->SetSaturationRange(0.0, 0.0); // no color saturation
98 table->SetRampToLinear();
101 vtkImageMapToColors *mapperImage = vtkImageMapToColors::New();
102 mapperImage->SetLookupTable(table);
103 mapperImage->SetInput( imagedataMaskResult );
104 vtkImageActor *actorImage = vtkImageActor::New();
105 actorImage->SetInput( mapperImage->GetOutput() );
106 actorImage->SetOpacity(1);
108 wxvtkbaseview->GetRenderer()->AddActor(actorImage);
112 // ------------------------------------------------------------------------
113 void wxMaracasCoutourTool::ConfigureVTK()
116 this->imagedata->GetSpacing(spc);
119 _manContourControl_1 = new manualContourControler();
120 _mContourModel_1 = new manualContourModel();
121 _mViewContour_1 = new manualViewContour();
122 _mViewContour_1->SetModel( _mContourModel_1 );
123 _mViewContour_1->SetWxVtkBaseView( this->wxvtkbaseview );
124 _mViewContour_1->SetRange( 2 );
125 _mViewContour_1->SetZ( 1000 );
128 _mContourModel_1->SetNumberOfPointsSpline(100);
132 _mViewContour_1->SetSpacing(spc);
136 _mViewContour_1->SetColorNormalContour(0, 0, 1);
137 _mViewContour_1->SetColorEditContour(0.5, 0.5, 0.5);
140 _manContourControl_1->SetModelView( _mContourModel_1 , _mViewContour_1 );
141 ((vtkInteractorStyleBaseView*)this->wxvtkbaseview->GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _manContourControl_1 );
142 _manContourControl_1->CreateNewManualContour();
143 _manContourControl_1->SetActive(false);
144 _mViewContour_1->RefreshContour();
148 _manContourControl_2 = new manualContourControler();
149 _mContourModel_2 = new manualContourModel();
150 _mViewContour_2 = new manualViewContour();
151 _mViewContour_2->SetModel( _mContourModel_2 );
152 _mViewContour_2->SetWxVtkBaseView( this->wxvtkbaseview );
153 _mViewContour_2->SetRange( 2 );
154 _mViewContour_2->SetZ( 1000 );
157 _mViewContour_2->SetSpacing(spc);
160 _mViewContour_2->SetColorNormalContour(1, 0, 0);
161 _mViewContour_2->SetColorEditContour(0.5, 0.5, 0.5);
164 _manContourControl_2->SetModelView( _mContourModel_2 , _mViewContour_2 );
165 ((vtkInteractorStyleBaseView*)this->wxvtkbaseview->GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _manContourControl_2 );
166 _manContourControl_2->CreateNewManualContour();
167 _manContourControl_2->SetActive(false);
168 _mViewContour_2->RefreshContour();
173 _contprop = new ContourPropagation();
176 //JSTG 26-02-08 -------------------------------------------------------------------------------------------------------------
177 void wxMaracasCoutourTool::SetControlPoints( std::vector<double> *InVectorX,
178 std::vector<double> *InVectorY,
179 std::vector<double> *InVectorZ )
184 int i,size=InVectorX->size();
187 _mContourModel_1->DeleteAllPoints();
188 _mViewContour_1->DeleteContour();
192 lstSize = _mContourModel_1->AddPoint( (*InVectorX)[i] , (*InVectorY)[i] , (*InVectorZ)[i] );
193 _mViewContour_1->AddPoint();
195 //_manContourControl_1->CreateNewManualContour();
196 //_manContourControl_1->SetActive(true);
197 _mViewContour_1->CreateNewContour();
198 _mContourModel_1->UpdateSpline();
199 _mViewContour_1->RefreshContour();
200 //_mViewContour_1->Refresh ();
205 // JSTG 26-02-08 ------------------------------------------------------------------------------
206 void wxMaracasCoutourTool::GetSplinePoints( std::vector<double> *pLstContourX,
207 std::vector<double> *pLstContourY,
208 std::vector<double> *pLstContourZ )
212 _mContourModel_1->UpdateSpline();
214 for(i=0; i<_mContourModel_1->GetNumberOfPointsSpline(); i++)
216 _mContourModel_1->GetSpline_i_Point( i, &x, &y, &z );
217 pLstContourX->push_back(x);
218 pLstContourY->push_back(y);
219 //pLstContourY->push_back(z); //Fake Z -> For Visualization
220 pLstContourZ->push_back(_zz); //True Z -> For processing
223 //---------------------------------------------------------------------------------------------
225 //------------------------------------------------------------------------
226 void wxMaracasCoutourTool::OnContourA(wxCommandEvent& event)
228 _manContourControl_1 -> SetActive(true);
229 _manContourControl_2 -> SetActive(false);
231 //------------------------------------------------------------------------
232 void wxMaracasCoutourTool::OnContourB(wxCommandEvent& event)
234 _manContourControl_1 -> SetActive(false);
235 _manContourControl_2 -> SetActive(true);
237 //------------------------------------------------------------------------
238 void wxMaracasCoutourTool::OnContourAB(wxCommandEvent& event)
240 _manContourControl_1 -> SetActive(true);
241 _manContourControl_2 -> SetActive(true);
244 //------------------------------------------------------------------------
245 void wxMaracasCoutourTool::GetMinMaxPoint(int *minPoint,
247 manualViewBaseContour *manualviewbaseecontour,
248 manualContourModel *manualcontourmodel
252 //int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points // JPRx
254 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
255 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
256 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
257 //------------------------------------------------------------------------------------------------------
259 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
262 //double delta=( double ) ( np ) / ( double ) ( nps );
263 manualcontourmodel->UpdateSpline();
264 for (i=0; i<nps; i++)
266 //t= delta * (double)i;
267 //manualcontourmodel->GetSplinePoint(t,x,y,z);
268 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
269 if (x<minPoint[0]){ minPoint[0]=(int)x; }
270 if (y<minPoint[1]){ minPoint[1]=(int)y; }
271 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
272 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
274 //------------------------------------------------------------------------------------------------------
277 //------------------------------------------------------------------------
279 int wxMaracasCoutourTool::AnalisisContourInside(int x,
281 manualViewBaseContour *manualviewbaseecontour,
282 manualContourModel *manualcontourmodel
287 int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points
289 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
290 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
291 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
292 //double x1,y1,z1,x2,y2,z2,t;
293 double x1,y1,z1,x2,y2,z2;
294 double xx1, yy1,xx2, yy2;
295 //double delta=( double ) ( np ) / ( double ) ( nps );
296 manualcontourmodel->UpdateSpline();
297 //------------------------------------------------------------------------------------------------------
302 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
304 //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
305 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
306 for (i=1; i<=nps; i++)
309 //t= delta * (double)(i%nps);
310 //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
311 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
312 //------------------------------------------------------------------------------------------------------
313 //by triangle similarity
314 if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
316 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; }
317 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
318 if ( ((xx1<xx2)&&(x<(xx1+d))) || ((xx1>xx2)&&(x<(xx1-d))) ) { result++; }
326 //------------------------------------------------------------------------
329 bool wxMaracasCoutourTool::isInside(int x, int y)
334 imagedataValueResult->GetExtent(ext);
336 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
338 int numberLeftA=AnalisisContourInside(x,y,_mViewContour_1, _mContourModel_1);
339 int numberLeftB=AnalisisContourInside(x,y,_mViewContour_2, _mContourModel_2);
340 if ( (numberLeftA+numberLeftB) % 2 ==1){ result = true; }
346 //------------------------------------------------------------------------
348 double wxMaracasCoutourTool::GetDataValue(int x, int y)
351 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
352 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
356 p = (unsigned short *)imagedata->GetScalarPointer(x,y,z);
360 //------------------------------------------------------------------------
362 void wxMaracasCoutourTool::PutVtkImageDataResultValue( int x, int y, double value )
364 unsigned short *pValue;
365 unsigned short *pMask;
366 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,0);
367 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,0);
369 *pValue = (unsigned short)value;
372 //------------------------------------------------------------------------
373 void wxMaracasCoutourTool::ResetImageResult()
375 unsigned short *pValue;
376 unsigned short *pMask;
377 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,0);
378 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,0);
381 imagedataValueResult->GetExtent(ext);
383 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
384 for(i=0; i<size; i++)
392 //------------------------------------------------------------------------
394 void wxMaracasCoutourTool::GetValuesInsideCrown(std::vector<double> *pLstValue,
395 std::vector<double> *pLstValuePosX,
396 std::vector<double> *pLstValuePosY,
397 std::vector<double> *pLstValuePosZ)
400 pLstValuePosX->clear();
401 pLstValuePosY->clear();
402 pLstValuePosZ->clear();
416 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_1, _mContourModel_1);
417 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_2, _mContourModel_2);
419 for (j=minPoint[1]; j<maxPoint[1]; j++)
421 for (i=minPoint[0]; i<maxPoint[0]; i++)
423 if (isInside(i,j)==true)
425 value = GetDataValue(i,j);
426 PutVtkImageDataResultValue(i,j, value );
427 pLstValue -> push_back( value );
428 pLstValuePosX -> push_back( i );
429 pLstValuePosY -> push_back( j );
430 pLstValuePosZ -> push_back( -1 );
435 imagedataValueResult->Modified();
436 imagedataMaskResult->Modified();
439 //------------------------------------------------------------------------
441 vtkImageData *wxMaracasCoutourTool::GetVtkImageValueResult()
443 return imagedataValueResult;
445 //------------------------------------------------------------------------
446 vtkImageData *wxMaracasCoutourTool::GetVtkImageMaskResult()
448 return imagedataMaskResult;
450 // ------------------------------------------------------------------------
451 void wxMaracasCoutourTool::InitVtkImagesResult()
458 imagedata->GetSpacing(spc);
459 imagedata->GetExtent(ext);
460 newDim[0]=ext[1]-ext[0]+1;
461 newDim[1]=ext[3]-ext[2]+1;
463 scalartype = imagedata->GetScalarType();
465 imagedataValueResult = vtkImageData::New();
466 // imagedataValueResult->SetScalarType(scalartype);
467 imagedataValueResult->SetScalarTypeToUnsignedShort();
468 imagedataValueResult->SetSpacing(spc);
469 imagedataValueResult->SetDimensions( newDim );
470 imagedataValueResult->AllocateScalars();
472 imagedataMaskResult = vtkImageData::New();
473 // imagedataMaskResult->SetScalarType(scalartype);
474 imagedataMaskResult->SetScalarTypeToUnsignedShort();
475 imagedataMaskResult->SetSpacing(spc);
476 imagedataMaskResult->SetDimensions( newDim );
477 imagedataMaskResult->AllocateScalars();
481 //JSTG 21-03-08 -------------------------------------------------------------------
482 void wxMaracasCoutourTool::OnSaveContour(wxCommandEvent& event)
484 //For write the actual time and date
489 tblock = localtime(&timer);
494 //WHERE CAN I MAKE THEM DELETE!!!
495 _mpoint = new manualPoint();
497 std::vector<double> tempX;
498 std::vector<double> tempY;
499 std::vector<double> tempZ;
504 int size = _mContourModel_1->GetNumberOfPointsSpline();
505 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
506 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
509 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","a");
510 //fprintf(fd,"Date %s", asctime(tblock) );
511 fprintf(fd,"\nPointsContour %d", size );
512 for(int i=0; i<size; i++)
514 _mContourModel_1->GetSpline_i_Point(i,&xx,&yy,&zz);
515 fprintf(fd,"\n%f %f %d",xx,yy,z); //z -> Contain the true value || zz -> Contain the cheating value for visualitation
523 _contprop->appendContour(&tempX,&tempY,&tempZ);
526 //JSTG 21-03-08 --------------------------------------------------------------------
527 void wxMaracasCoutourTool::OnSaveControlPoints(wxCommandEvent& event)
529 //For write the actual time and date
534 tblock = localtime(&timer);
537 _mpoint = new manualPoint();
538 int size = _mContourModel_1->GetSizeLstPoints();
539 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
540 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
542 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","a");
543 //fprintf(fd,"Date %s", asctime(tblock) );
544 fprintf(fd,"\nNumberOfControlPoints %d", size );
545 for(int i=0; i<size; i++)
547 _mpoint = _mContourModel_1->GetManualPoint(i);
548 fprintf(fd,"\n%f %f %d",_mpoint->GetX(),_mpoint->GetY(),z);
552 //----------------------------------------------------------------------------------
553 void wxMaracasCoutourTool::EreaseLastContour(wxCommandEvent& event)
570 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","r");
575 // //fscanf(fd," %s %d",&firstline,&size); // JPRx
576 fscanf(fd," %s %d",firstline,&size);
578 _SizesC.push_back(size);
579 for(j=0; j<size; j++)
581 fscanf(fd,"%lf %lf %d",&x,&y,&z);
582 _tempCX.push_back(x);
583 _tempCY.push_back(y);
584 _tempCZ.push_back(z);
590 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
594 for(i=0; i<size; i++)
602 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","w");
606 for(i=0; i<(int)(_SizesC.size()); i++)
608 fprintf(fd,"\nNumberOfPoints %d", _SizesC[i] );
609 for(j=0; j<_SizesC[i]; j++)
611 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
618 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
623 //----------------------------------------------------------------------------------
624 void wxMaracasCoutourTool::EreaseLastCP(wxCommandEvent& event)
641 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","r");
646 //fscanf(fd,""); // ?? JPRx
647 //fscanf(fd," %s %d",&firstline,&size);// JPRx
648 fscanf(fd," %s %d",firstline,&size);
649 _SizesC.push_back(size);
650 for(j=0; j<size; j++)
652 fscanf(fd,"%lf %lf %d",&x,&y,&z);
653 _tempCX.push_back(x);
654 _tempCY.push_back(y);
655 _tempCZ.push_back(z);
661 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
665 for(i=0; i<size; i++)
673 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","w");
677 for(i=0; i<(int)(_SizesC.size()); i++)
679 fprintf(fd,"\nNumberOfControlPoints %d", _SizesC[i] );
680 for(j=0; j<_SizesC[i]; j++)
682 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
689 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
693 //----------------------------------------------------------------------------------