1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 #include "ContourCrownWidget.h"
28 #include <vtkImageMapToColors.h>
29 #include <vtkImageActor.h>
31 #include "wxVtk2DBaseView.h"
32 #include "vtkLookupTable.h"
34 //----------------------------------------------------------------------
35 wxMaracasCoutourTool::wxMaracasCoutourTool(wxWindow *parent)
36 : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
38 wxPanel *panel = this;
39 wxSizer *sizer = NULL;
42 wxButton *btnContourA = new wxButton( panel, -1, _T("Crown Extern"));
43 wxButton *btnContourB = new wxButton( panel, -1, _T("Crown Intern"));
44 wxButton *btnContourAB = new wxButton( panel, -1, _T("Crown Extern and Intern"));
46 //JSTG 21-03-08 --------------------------------------------------------------------
47 wxButton *btnSaveContour = new wxButton( panel, -1, _T("Save Contour"));
48 wxButton *btnSaveControlPoints = new wxButton( panel, -1, _T("Save Control Points"));
50 wxButton *btnEreaseLastContour = new wxButton( panel, -1, _T("Erease Last Contour"));
51 wxButton *btnEreaseLastCP = new wxButton( panel, -1, _T("Erease Last Control Points"));
52 //----------------------------------------------------------------------------------
54 wxFlexGridSizer *sizer1 = new wxFlexGridSizer(10); //Original Value = 10
55 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
56 sizer1->Add(new wxStaticText(panel,-1,_T(" ")));
58 Connect(btnContourA->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourA );
59 Connect(btnContourB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourB );
60 Connect(btnContourAB->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnContourAB );
62 //JSTG 21-03-08 ---------------------------------------------------------------------
63 Connect(btnSaveContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveContour );
64 Connect(btnSaveControlPoints->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::OnSaveControlPoints );
66 Connect(btnEreaseLastContour->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastContour );
67 Connect(btnEreaseLastCP->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &wxMaracasCoutourTool::EreaseLastCP );
68 //-----------------------------------------------------------------------------------
70 sizer1->Add(btnContourA);
71 sizer1->Add(btnContourB);
72 sizer1->Add(btnContourAB);
74 //JSTG 21-03-08 --------------------
75 sizer1->Add(btnSaveContour);
76 sizer1->Add(btnSaveControlPoints);
78 sizer1->Add(btnEreaseLastContour);
79 sizer1->Add(btnEreaseLastCP);
80 //----------------------------------
82 panel -> SetSizer(sizer);
83 panel -> SetAutoLayout(true);
88 // ------------------------------------------------------------------------
90 wxMaracasCoutourTool::~wxMaracasCoutourTool()
94 // ------------------------------------------------------------------------
95 wxVtkBaseView *wxMaracasCoutourTool::GetWxVtkBaseView()
97 return this->wxvtkbaseview;
101 // ------------------------------------------------------------------------
102 void wxMaracasCoutourTool::Init(wxVtkBaseView *wxvtkbaseview, vtkImageData* imagedata)
104 this->imagedata = imagedata;
105 this->wxvtkbaseview = wxvtkbaseview;
107 InitVtkImagesResult();
109 vtkLookupTable *table = vtkLookupTable::New();
110 // table->SetAlphaRange(128,255);
111 table->SetRange(0, 255); // image intensity range
112 // table->SetValueRange(0.0, 1.0); // from black to white
113 table->SetValueRange(0.0, 255.0); // from black to white
115 table->SetTableValue (0, 1.0 , 1.0 , 1.0 , 0.1 );
116 table->SetTableValue (255, 1.0 , 1.0 , 0.0 , 0.1);
118 // table->SetTableValue (0, 0.0 , 0.0 , 1.0 , 0.35 );
119 // table->SetTableValue (255, 1.0 , 0.0 , 0.0 , 0.10);
121 table->SetSaturationRange(0.0, 0.0); // no color saturation
122 table->SetRampToLinear();
125 vtkImageMapToColors *mapperImage = vtkImageMapToColors::New();
126 vtkImageActor *actorImage = vtkImageActor::New();
128 mapperImage->SetLookupTable(table);
131 //EED 2017-01-01 Migration VTK7
132 #if VTK_MAJOR_VERSION <= 5
133 mapperImage->SetInput( imagedataMaskResult );
134 actorImage->SetInput( mapperImage->GetOutput() );
136 mapperImage->SetInputData( imagedataMaskResult );
137 actorImage->SetInputData( mapperImage->GetOutput() );
141 actorImage->SetOpacity(1);
143 wxvtkbaseview->GetRenderer()->AddActor(actorImage);
147 // ------------------------------------------------------------------------
148 void wxMaracasCoutourTool::ConfigureVTK()
151 this->imagedata->GetSpacing(spc);
154 _manContourControl_1 = new manualContourControler();
155 _mContourModel_1 = new manualContourModel();
156 _mViewContour_1 = new manualViewContour();
157 _mViewContour_1->SetModel( _mContourModel_1 );
158 _mViewContour_1->SetWxVtkBaseView( this->wxvtkbaseview );
159 _mViewContour_1->SetRange( 2 );
160 _mViewContour_1->SetZ( 1000 );
163 _mContourModel_1->SetNumberOfPointsSpline(100);
167 _mViewContour_1->SetSpacing(spc);
171 _mViewContour_1->SetColorNormalContour(0, 0, 1);
172 _mViewContour_1->SetColorEditContour(0.5, 0.5, 0.5);
175 _manContourControl_1->SetModelView( _mContourModel_1 , _mViewContour_1 );
176 ((vtkInteractorStyleBaseView*)this->wxvtkbaseview->GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _manContourControl_1 );
177 _manContourControl_1->CreateNewManualContour();
178 _manContourControl_1->SetActive(false);
179 _mViewContour_1->RefreshContour();
183 _manContourControl_2 = new manualContourControler();
184 _mContourModel_2 = new manualContourModel();
185 _mViewContour_2 = new manualViewContour();
186 _mViewContour_2->SetModel( _mContourModel_2 );
187 _mViewContour_2->SetWxVtkBaseView( this->wxvtkbaseview );
188 _mViewContour_2->SetRange( 2 );
189 _mViewContour_2->SetZ( 1000 );
192 _mViewContour_2->SetSpacing(spc);
195 _mViewContour_2->SetColorNormalContour(1, 0, 0);
196 _mViewContour_2->SetColorEditContour(0.5, 0.5, 0.5);
199 _manContourControl_2->SetModelView( _mContourModel_2 , _mViewContour_2 );
200 ((vtkInteractorStyleBaseView*)this->wxvtkbaseview->GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _manContourControl_2 );
201 _manContourControl_2->CreateNewManualContour();
202 _manContourControl_2->SetActive(false);
203 _mViewContour_2->RefreshContour();
208 _contprop = new ContourPropagation();
211 //JSTG 26-02-08 -------------------------------------------------------------------------------------------------------------
212 void wxMaracasCoutourTool::SetControlPoints( std::vector<double> *InVectorX,
213 std::vector<double> *InVectorY,
214 std::vector<double> *InVectorZ )
219 int i,size=InVectorX->size();
222 _mContourModel_1->DeleteAllPoints();
223 _mViewContour_1->DeleteContour();
227 lstSize = _mContourModel_1->AddPoint( (*InVectorX)[i] , (*InVectorY)[i] , (*InVectorZ)[i] );
228 _mViewContour_1->AddPoint();
230 //_manContourControl_1->CreateNewManualContour();
231 //_manContourControl_1->SetActive(true);
232 _mViewContour_1->CreateNewContour();
233 _mContourModel_1->UpdateSpline();
234 _mViewContour_1->RefreshContour();
235 //_mViewContour_1->Refresh ();
240 // JSTG 26-02-08 ------------------------------------------------------------------------------
241 void wxMaracasCoutourTool::GetSplinePoints( std::vector<double> *pLstContourX,
242 std::vector<double> *pLstContourY,
243 std::vector<double> *pLstContourZ )
247 _mContourModel_1->UpdateSpline();
249 for(i=0; i<_mContourModel_1->GetNumberOfPointsSpline(); i++)
251 _mContourModel_1->GetSpline_i_Point( i, &x, &y, &z );
252 pLstContourX->push_back(x);
253 pLstContourY->push_back(y);
254 //pLstContourY->push_back(z); //Fake Z -> For Visualization
255 pLstContourZ->push_back(_zz); //True Z -> For processing
258 //---------------------------------------------------------------------------------------------
260 //------------------------------------------------------------------------
261 void wxMaracasCoutourTool::OnContourA(wxCommandEvent& event)
263 _manContourControl_1 -> SetActive(true);
264 _manContourControl_2 -> SetActive(false);
266 //------------------------------------------------------------------------
267 void wxMaracasCoutourTool::OnContourB(wxCommandEvent& event)
269 _manContourControl_1 -> SetActive(false);
270 _manContourControl_2 -> SetActive(true);
272 //------------------------------------------------------------------------
273 void wxMaracasCoutourTool::OnContourAB(wxCommandEvent& event)
275 _manContourControl_1 -> SetActive(true);
276 _manContourControl_2 -> SetActive(true);
279 //------------------------------------------------------------------------
280 void wxMaracasCoutourTool::GetMinMaxPoint(int *minPoint,
282 manualViewBaseContour *manualviewbaseecontour,
283 manualContourModel *manualcontourmodel
287 //int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points // JPRx
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 //------------------------------------------------------------------------------------------------------
294 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
297 //double delta=( double ) ( np ) / ( double ) ( nps );
298 manualcontourmodel->UpdateSpline();
299 for (i=0; i<nps; i++)
301 //t= delta * (double)i;
302 //manualcontourmodel->GetSplinePoint(t,x,y,z);
303 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
304 if (x<minPoint[0]){ minPoint[0]=(int)x; }
305 if (y<minPoint[1]){ minPoint[1]=(int)y; }
306 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
307 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
309 //------------------------------------------------------------------------------------------------------
312 //------------------------------------------------------------------------
314 int wxMaracasCoutourTool::AnalisisContourInside(int x,
316 manualViewBaseContour *manualviewbaseecontour,
317 manualContourModel *manualcontourmodel
322 int np = manualviewbaseecontour->GetNumberOfPoints( ); // number of control points
324 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
325 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
326 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
327 //double x1,y1,z1,x2,y2,z2,t;
328 double x1,y1,z1,x2,y2,z2;
329 double xx1, yy1,xx2, yy2;
330 //double delta=( double ) ( np ) / ( double ) ( nps );
331 manualcontourmodel->UpdateSpline();
332 //------------------------------------------------------------------------------------------------------
337 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
339 //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
340 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
341 for (i=1; i<=nps; i++)
344 //t= delta * (double)(i%nps);
345 //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
346 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
347 //------------------------------------------------------------------------------------------------------
348 //by triangle similarity
349 if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
351 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; }
352 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
353 if ( ((xx1<xx2)&&(x<(xx1+d))) || ((xx1>xx2)&&(x<(xx1-d))) ) { result++; }
361 //------------------------------------------------------------------------
364 bool wxMaracasCoutourTool::isInside(int x, int y)
369 imagedataValueResult->GetExtent(ext);
371 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
373 int numberLeftA=AnalisisContourInside(x,y,_mViewContour_1, _mContourModel_1);
374 int numberLeftB=AnalisisContourInside(x,y,_mViewContour_2, _mContourModel_2);
375 if ( (numberLeftA+numberLeftB) % 2 ==1){ result = true; }
381 //------------------------------------------------------------------------
383 double wxMaracasCoutourTool::GetDataValue(int x, int y)
386 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
387 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
391 p = (unsigned short *)imagedata->GetScalarPointer(x,y,z);
395 //------------------------------------------------------------------------
397 void wxMaracasCoutourTool::PutVtkImageDataResultValue( int x, int y, double value )
399 unsigned short *pValue;
400 unsigned short *pMask;
401 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,0);
402 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,0);
404 *pValue = (unsigned short)value;
407 //------------------------------------------------------------------------
408 void wxMaracasCoutourTool::ResetImageResult()
410 unsigned short *pValue;
411 unsigned short *pMask;
412 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,0);
413 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,0);
416 imagedataValueResult->GetExtent(ext);
418 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
419 for(i=0; i<size; i++)
427 //------------------------------------------------------------------------
429 void wxMaracasCoutourTool::GetValuesInsideCrown(std::vector<double> *pLstValue,
430 std::vector<double> *pLstValuePosX,
431 std::vector<double> *pLstValuePosY,
432 std::vector<double> *pLstValuePosZ)
435 pLstValuePosX->clear();
436 pLstValuePosY->clear();
437 pLstValuePosZ->clear();
451 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_1, _mContourModel_1);
452 GetMinMaxPoint(minPoint,maxPoint, _mViewContour_2, _mContourModel_2);
454 for (j=minPoint[1]; j<maxPoint[1]; j++)
456 for (i=minPoint[0]; i<maxPoint[0]; i++)
458 if (isInside(i,j)==true)
460 value = GetDataValue(i,j);
461 PutVtkImageDataResultValue(i,j, value );
462 pLstValue -> push_back( value );
463 pLstValuePosX -> push_back( i );
464 pLstValuePosY -> push_back( j );
465 pLstValuePosZ -> push_back( -1 );
470 imagedataValueResult->Modified();
471 imagedataMaskResult->Modified();
474 //------------------------------------------------------------------------
476 vtkImageData *wxMaracasCoutourTool::GetVtkImageValueResult()
478 return imagedataValueResult;
480 //------------------------------------------------------------------------
481 vtkImageData *wxMaracasCoutourTool::GetVtkImageMaskResult()
483 return imagedataMaskResult;
485 // ------------------------------------------------------------------------
486 void wxMaracasCoutourTool::InitVtkImagesResult()
493 imagedata->GetSpacing(spc);
494 imagedata->GetExtent(ext);
495 newDim[0]=ext[1]-ext[0]+1;
496 newDim[1]=ext[3]-ext[2]+1;
498 scalartype = imagedata->GetScalarType();
500 imagedataValueResult = vtkImageData::New();
501 // imagedataValueResult->SetScalarType(scalartype);
502 imagedataValueResult->SetSpacing(spc);
503 imagedataValueResult->SetDimensions( newDim );
505 //EED 2017-01-01 Migration VTK7
506 #if VTK_MAJOR_VERSION <= 5
507 imagedataValueResult->SetScalarTypeToUnsignedShort();
508 imagedataValueResult->AllocateScalars();
510 imagedataValueResult->AllocateScalars(VTK_UNSIGNED_SHORT,1);
514 imagedataMaskResult = vtkImageData::New();
515 // imagedataMaskResult->SetScalarType(scalartype);
516 imagedataMaskResult->SetSpacing(spc);
517 imagedataMaskResult->SetDimensions( newDim );
519 //EED 2017-01-01 Migration VTK7
520 #if VTK_MAJOR_VERSION <= 5
521 imagedataMaskResult->SetScalarTypeToUnsignedShort();
522 imagedataMaskResult->AllocateScalars();
524 imagedataMaskResult->AllocateScalars(VTK_UNSIGNED_SHORT,1);
530 //JSTG 21-03-08 -------------------------------------------------------------------
531 void wxMaracasCoutourTool::OnSaveContour(wxCommandEvent& event)
533 //For write the actual time and date
538 tblock = localtime(&timer);
543 //WHERE CAN I MAKE THEM DELETE!!!
544 _mpoint = new manualPoint();
546 std::vector<double> tempX;
547 std::vector<double> tempY;
548 std::vector<double> tempZ;
553 int size = _mContourModel_1->GetNumberOfPointsSpline();
554 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
555 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
558 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","a");
559 //fprintf(fd,"Date %s", asctime(tblock) );
560 fprintf(fd,"\nPointsContour %d", size );
561 for(int i=0; i<size; i++)
563 _mContourModel_1->GetSpline_i_Point(i,&xx,&yy,&zz);
564 fprintf(fd,"\n%f %f %d",xx,yy,z); //z -> Contain the true value || zz -> Contain the cheating value for visualitation
572 _contprop->appendContour(&tempX,&tempY,&tempZ);
575 //JSTG 21-03-08 --------------------------------------------------------------------
576 void wxMaracasCoutourTool::OnSaveControlPoints(wxCommandEvent& event)
578 //For write the actual time and date
583 tblock = localtime(&timer);
586 _mpoint = new manualPoint();
587 int size = _mContourModel_1->GetSizeLstPoints();
588 wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
589 int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
591 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","a");
592 //fprintf(fd,"Date %s", asctime(tblock) );
593 fprintf(fd,"\nNumberOfControlPoints %d", size );
594 for(int i=0; i<size; i++)
596 _mpoint = _mContourModel_1->GetManualPoint(i);
597 fprintf(fd,"\n%f %f %d",_mpoint->GetX(),_mpoint->GetY(),z);
601 //----------------------------------------------------------------------------------
602 void wxMaracasCoutourTool::EreaseLastContour(wxCommandEvent& event)
619 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","r");
624 // //fscanf(fd," %s %d",&firstline,&size); // JPRx
625 fscanf(fd," %s %d",firstline,&size);
627 _SizesC.push_back(size);
628 for(j=0; j<size; j++)
630 fscanf(fd,"%lf %lf %d",&x,&y,&z);
631 _tempCX.push_back(x);
632 _tempCY.push_back(y);
633 _tempCZ.push_back(z);
639 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
643 for(i=0; i<size; i++)
651 fd = fopen("C:/bbtk_JS/data/SavedContours.txt","w");
655 for(i=0; i<(int)(_SizesC.size()); i++)
657 fprintf(fd,"\nNumberOfPoints %d", _SizesC[i] );
658 for(j=0; j<_SizesC[i]; j++)
660 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
667 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
672 //----------------------------------------------------------------------------------
673 void wxMaracasCoutourTool::EreaseLastCP(wxCommandEvent& event)
690 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","r");
695 //fscanf(fd,""); // ?? JPRx
696 //fscanf(fd," %s %d",&firstline,&size);// JPRx
697 fscanf(fd," %s %d",firstline,&size);
698 _SizesC.push_back(size);
699 for(j=0; j<size; j++)
701 fscanf(fd,"%lf %lf %d",&x,&y,&z);
702 _tempCX.push_back(x);
703 _tempCY.push_back(y);
704 _tempCZ.push_back(z);
710 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
714 for(i=0; i<size; i++)
722 fd = fopen("C:/bbtk_JS/data/SavedManualPoints.txt","w");
726 for(i=0; i<(int)(_SizesC.size()); i++)
728 fprintf(fd,"\nNumberOfControlPoints %d", _SizesC[i] );
729 for(j=0; j<_SizesC[i]; j++)
731 fprintf(fd,"\n%f %f %d",_tempCX[j+cont],_tempCY[j+cont],_tempCZ[j+cont]);
738 printf("\nJSTG - wxMaracasCoutourTool::EreaseLastContour - FILE NOT FOUND");
742 //----------------------------------------------------------------------------------