00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "wxImageViewerWidget.h"
00019
00020 #include "vtkRenderer.h"
00021 #include "vtkRenderWindowInteractor.h"
00022 #include "vtkCommand.h"
00023 #include "vtkPolyData.h"
00024 #include "vtkCellArray.h"
00025 #include "vtkPolyDataMapper.h"
00026 #include "vtkInteractorObserver.h"
00027 #include "vtkInteractorStyleImage.h"
00028
00029 #include <vtkCamera.h>
00030 #include <vtkPolyLine.h>
00031 #include <vtkDataSetMapper.h>
00032 #include <vtkUnstructuredGrid.h>
00033
00034 #include <wx/tglbtn.h>
00035
00036
00037 class marZoomROIObserver : public vtkCommand{
00038 public:
00039 virtual char const *GetClassName() const { return "marZoomROIObserver";}
00040 static marZoomROIObserver *New(){
00041 marZoomROIObserver * result;
00042 result = new marZoomROIObserver;
00043 return result;
00044 }
00045
00046
00047 marZoomROIObserver() { this->ImageViewerWdg = NULL; }
00048 virtual void Execute(vtkObject *wdg, unsigned long event, void* calldata) {
00049 if ( this->ImageViewerWdg ) {
00050 this->ImageViewerWdg->ExecuteEvent(wdg, event, calldata);
00051 }
00052 }
00053 wxImageViewerWidget *ImageViewerWdg;
00054 };
00062
00063
00064
00065
00066 wxVtk2DView_TMP::wxVtk2DView_TMP(vtkImageViewer2 *imageviewer2)
00067 {
00068 _imageviewer2 = imageviewer2;
00069 }
00070
00071 wxVtk2DView_TMP::~wxVtk2DView_TMP()
00072 {
00073 }
00074
00075 vtkRenderer* wxVtk2DView_TMP::GetRenderer()
00076 {
00077 return _imageviewer2->GetRenderer();
00078 }
00079
00080 vtkRenderWindow* wxVtk2DView_TMP::GetRenWin()
00081 {
00082 return _imageviewer2->GetRenderWindow();
00083 }
00084
00085 void wxVtk2DView_TMP::Configure()
00086 {
00087 vtkInteractorStyleBaseView *interactorstylebaseview;
00088 interactorstylebaseview = vtkInteractorStyleBaseView2D::New();
00089
00090 SetInteractorStyleBaseView(interactorstylebaseview);
00091 wxVTKRenderWindowInteractor *iren = GetWxVTKRenderWindowInteractor();
00092 interactorstylebaseview->SetInteractor ( iren );
00093 iren->SetInteractorStyle(interactorstylebaseview);
00094 interactorstylebaseview->SetwxVtkBaseView(this);
00095 }
00096
00097 void wxVtk2DView_TMP::SetWxVTKRenderWindowInteractor( wxVTKRenderWindowInteractor *wxVTKiren)
00098 {
00099 _wxVTKiren = wxVTKiren;
00100 }
00101
00102 wxVTKRenderWindowInteractor *wxVtk2DView_TMP::GetWxVTKRenderWindowInteractor()
00103 {
00104 return _wxVTKiren;
00105 }
00106
00107
00108
00109
00110
00111 wxImageViewerWidget::wxImageViewerWidget(wxWindow* parent,
00112 wxWindowID id,
00113 const wxPoint& pos,
00114 const wxSize& size,
00115 long style,
00116 const wxString& name) :
00117 wxPanel( parent, id, pos, size, style, name )
00118 {
00119 _manContControl = NULL;
00120 _mContourModel = NULL;
00121 _mViewContour = NULL;
00122 _Observer = NULL;
00123
00124 _state=1;
00125
00126 _wxVTKiren = new wxVTKRenderWindowInteractorEditContour( this, -1 );
00127 _imageViewer = vtkImageViewer2::New();
00128
00129 wxBoxSizer* sizer_1 = new wxBoxSizer(wxVERTICAL);
00130 sizer_1->Add(_wxVTKiren, 1, wxGROW, 0);
00131 SetAutoLayout(true);
00132 SetSizer(sizer_1);
00133 Layout();
00134
00135 _intVtkPanWid = NULL;
00136
00137
00138 }
00139
00140
00141 wxImageViewerWidget::~wxImageViewerWidget(){
00142
00143
00144 if ( _Observer !=NULL ) { _Observer->Delete(); }
00145
00148
00149 if ( _imageViewer !=NULL ) { _imageViewer->Delete(); }
00150
00151
00152 if ( _wxVTKiren !=NULL ) { _wxVTKiren->Delete(); }
00153
00154
00155 if ( _manContControl !=NULL ) { delete _manContControl; }
00156 if ( _mContourModel !=NULL ) { delete _mContourModel; }
00157 if ( _mViewContour !=NULL ) { delete _mViewContour; }
00158 }
00159
00160
00161
00162 void wxImageViewerWidget::ConstructVTK(vtkImageData *imageData)
00163 {
00164 _imageViewer->SetInput ( imageData );
00165 _imageViewer->SetupInteractor ( _wxVTKiren );
00166 vtkCamera *camera =_imageViewer->GetRenderer()->GetActiveCamera();
00167 camera->Zoom(2.5);
00168 _imageViewer->GetRenderer()->ResetCamera ();
00169
00170
00171
00172
00173
00174
00175
00176 imageData->UpdateInformation();
00177 imageData->SetUpdateExtent( imageData->GetWholeExtent());
00178 imageData->Update();
00179 double *range = imageData->GetScalarRange();
00180
00181
00182
00183 _Observer = marZoomROIObserver::New();
00184 _Observer->ImageViewerWdg = this;
00185
00186 SetState(1);
00187 AddObserver_1();
00188 _wxVTKiren->SetObserver(_Observer);
00189
00196
00197
00198
00199 _manContControl = new manualContourControler();
00200 _mContourModel = new manualContourModel();
00201 _mViewContour = new manualViewContour();
00202
00203 _manContControl->SetZ(100);
00204 _mViewContour->SetModel(_mContourModel);
00205 wxVtk2DView_TMP *wxvtkbaseview_tmp =new wxVtk2DView_TMP(_imageViewer);
00206 _mViewContour->SetWxVtkBaseView( wxvtkbaseview_tmp );
00207 _mViewContour->SetRange( 2 );
00208 _manContControl->SetActive(false);
00209 _manContControl->SetModelView(_mContourModel,_mViewContour);
00210 wxvtkbaseview_tmp->SetWxVTKRenderWindowInteractor(_wxVTKiren);
00211 wxvtkbaseview_tmp->Configure();
00212 wxvtkbaseview_tmp->GetInteractorStyleBaseView()->AddInteractorStyleMaracas( _manContControl );
00213
00214 _interactorStyle2DMaracas = vtkInteractorStyle2DMaracas::New();
00215 _interactorStyle2DMaracas->SetInteractor ( _wxVTKiren );
00216 _wxVTKiren->SetInteractorStyle(_interactorStyle2DMaracas);
00217 vtkMaracasImageViewer2Callback *cbk = vtkMaracasImageViewer2Callback::New();
00218 cbk->IV = _imageViewer;
00219 _interactorStyle2DMaracas->AddObserver( vtkCommand::WindowLevelEvent, cbk);
00220 _interactorStyle2DMaracas->AddObserver( vtkCommand::StartWindowLevelEvent, cbk);
00221 _interactorStyle2DMaracas->AddObserver( vtkCommand::ResetWindowLevelEvent, cbk);
00222 cbk->Delete();
00223
00224 }
00225
00226
00227 void wxImageViewerWidget::SetState(int state){
00228 _state=state;
00229 _wxVTKiren->SetState(state);
00230 }
00231
00232 void wxImageViewerWidget::SetStateManualContour(int state){
00233 _manContControl->SetState(state);
00234 }
00235
00236 void wxImageViewerWidget::CreateNewManualContour(){
00237 _manContControl->CreateNewManualContour();
00238 _manContControl->SetActive(true);
00239 _mViewContour->Refresh();
00240 Refresh();
00241 }
00242
00243 void wxImageViewerWidget::EraseManualContour(){
00244 _manContControl->DeleteContour();
00245 }
00246
00247
00248 void wxImageViewerWidget::AddObserver_1(){
00249
00250
00251
00252 _wxVTKiren->GetInteractorStyle()->AddObserver( vtkCommand::MiddleButtonPressEvent , _Observer);
00253 _wxVTKiren->AddObserver(vtkCommand::MouseMoveEvent, _Observer);
00254
00255 _wxVTKiren->GetInteractorStyle()->AddObserver( vtkCommand::MiddleButtonReleaseEvent , _Observer);
00256
00257
00258 _wxVTKiren->AddObserver( wxEVT_MOUSEWHEEL+10000 , _Observer);
00259
00260
00261 _wxVTKiren->AddObserver( wxEVT_MOUSEWHEEL+10001 , _Observer);
00262
00263 }
00264
00265
00266
00267
00268 void wxImageViewerWidget::AddObserver_2(){
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 }
00280
00281
00282
00283 void wxImageViewerWidget::ExecuteEvent(vtkObject *wdg, unsigned long event, void* calldata){
00284 if (_state==1) { ExecuteEvent1(wdg,event,calldata); }
00285 if (_state==2) { ExecuteEvent2(wdg,event,calldata); }
00286 }
00287
00288
00289
00290 void wxImageViewerWidget::ExecuteEvent1(vtkObject *wdg, unsigned long event, void* calldata){
00291
00292 if ( event == vtkCommand::MiddleButtonPressEvent ){
00293 }else if ( event == vtkCommand::MouseMoveEvent ){
00294 }
00295
00296 if ( event == vtkCommand::MiddleButtonReleaseEvent ){
00297
00298 } else if ( event == wxEVT_MOUSEWHEEL+10000 ){
00299 wxMouseEvent mouseEvent;
00300 mouseEvent.m_wheelRotation=130;
00301 if (_intVtkPanWid!=NULL) { _intVtkPanWid->CallBackOnMouseWheel(mouseEvent);}
00302
00303 }else if ( event == wxEVT_MOUSEWHEEL+10001 ){
00304 wxMouseEvent mouseEvent;
00305 mouseEvent.m_wheelRotation=-130;
00306 if (_intVtkPanWid!=NULL) { _intVtkPanWid->CallBackOnMouseWheel(mouseEvent);}
00307 }
00308 }
00309
00310
00311
00312 void wxImageViewerWidget::ExecuteEvent2(vtkObject *wdg, unsigned long event, void* calldata){
00313 int X,Y;
00314 _wxVTKiren->GetEventPosition(X, Y);
00315 if (event==wxEVT_LEFT_DOWN ){
00316 _manContControl->MouseClickLeft(X,Y);
00317 }
00318
00319 if (event==wxEVT_RIGHT_DOWN ){
00320 _manContControl->MouseClickRight(X,Y);
00321 }
00322
00323 if (event==wxEVT_LEFT_UP){
00324 _manContControl->MouseReleaseLeft(X,Y);
00325 }
00326
00327 if (event==wxEVT_LEFT_DCLICK){
00328 _manContControl->MouseDLeft(X,Y);
00329 }
00330
00331 if (event==wxEVT_MOTION){
00332 _manContControl->MouseMove(X,Y);
00333 }
00334
00335 if (event==WXK_CLEAR){
00336 _manContControl->DeleteActualMousePoint(X,Y);
00337 }
00338 }
00339
00340
00341
00342 void wxImageViewerWidget::SetImage(vtkImageData* imagedata){
00343 _imageViewer->SetInput(imagedata);
00344 _imageViewer->Render( );
00345 }
00346
00347
00348 int wxImageViewerWidget::GetNumberOfPointsSplineManualContour(){
00349 return _manContControl->GetNumberOfPointsSplineManualContour();
00350 }
00351
00352 double* wxImageViewerWidget::GetVectorPointsXManualContour(){
00353 return _manContControl->GetVectorPointsXManualContour();
00354 }
00355
00356 double* wxImageViewerWidget::GetVectorPointsYManualContour(){
00357 return _manContControl->GetVectorPointsYManualContour();
00358 }
00359
00360 void wxImageViewerWidget::SetZSlice(int z){
00361 #if (VTK_MAJOR_VERSION >= 5)
00362 _imageViewer->SetSlice( z );
00363 #else
00364 _imageViewer->SetZSlice( z );
00365 #endif
00366
00367 }
00368
00369 int wxImageViewerWidget::GetZSlice(){
00370 return _imageViewer->GetZSlice();
00371 }
00372
00373 void wxImageViewerWidget::Render(){
00374 _imageViewer->Render();
00375 }
00376
00377
00378
00379
00380
00381
00382
00383 wxImageViewerWidgetRoi::wxImageViewerWidgetRoi(wxWindow* parent,
00384 wxWindowID id,
00385 const wxPoint& pos,
00386 const wxSize& size,
00387 long style,
00388 const wxString& name) :
00389 wxImageViewerWidget( parent, id, pos, size, style, name )
00390 {
00391 _bboxEnabled = false;
00392 _sliceEnabled = false;
00393 _RoiSelected = false;
00394 }
00395
00396 wxImageViewerWidgetRoi::~wxImageViewerWidgetRoi()
00397 {
00398 _pts ->Delete();
00399 _bboxActor ->Delete();
00400 _bboxMapper ->Delete();
00401 _pd ->Delete();
00402 }
00403
00404 void wxImageViewerWidgetRoi::GetROI( int extent[6] )
00405 {
00406
00407 double P[3];
00408 double Q[3];
00409 _pts->GetPoint(0, P);
00410 _pts->GetPoint(2, Q);
00411 if (P[0]<Q[0]) { extent[0]=(int)(P[0]); extent[1]=(int)(Q[0]); } else { extent[0]=(int)(Q[0]); extent[1]=(int)(P[0]); }
00412 if (P[1]<Q[1]) { extent[2]=(int)(P[1]); extent[3]=(int)(Q[1]); } else { extent[2]=(int)(Q[1]); extent[3]=(int)(P[1]); }
00413
00414 int border[6];
00415 _imageViewer->GetInput()->GetExtent( border );
00416
00417 if (extent[0] < border[0]) extent[0]=border[0];
00418 if (extent[1] > border[1]) extent[1]=border[1];
00419 if (extent[2] < border[2]) extent[2]=border[2];
00420 if (extent[3] > border[3]) extent[3]=border[3];
00421 extent[4] = border[4];
00422 extent[5] = border[5];
00423
00424
00425 double spacing[3];
00426 _imageViewer->GetInput()->GetSpacing( spacing );
00427 extent[0] = (int)( extent[0]/spacing[0] );
00428 extent[1] = (int)( extent[1]/spacing[0] );
00429 extent[2] = (int)( extent[2]/spacing[1] );
00430 extent[3] = (int)( extent[3]/spacing[1] );
00431
00432
00433
00434
00435
00436
00437
00438 if(extent[1]<=extent[0] || extent[3]<=extent[2]){
00439 extent[0] = border[0];
00440 extent[1] = border[1];
00441 extent[2] = border[2];
00442 extent[3] = border[3];
00443 }
00444 }
00445
00446 void wxImageViewerWidgetRoi::TransfromeCoordViewWorld(int &X, int &Y, int &Z)
00447 {
00448 _imageViewer->GetRenderer()->SetDisplayPoint(X, Y, Z);
00449 _imageViewer->GetRenderer()->DisplayToWorld();
00450 double fP[4];
00451 _imageViewer->GetRenderer()->GetWorldPoint( fP );
00452 if ( fP[3] ){
00453 fP[0] /= fP[3];
00454 fP[1] /= fP[3];
00455 fP[2] /= fP[3];
00456 }
00457 X=(int)(fP[0]);
00458 Y=(int)(fP[1]);
00459 Z=(int)(fP[2]);
00460 }
00461
00462 void wxImageViewerWidgetRoi::ExecuteEvent(vtkObject *wdg, unsigned long event, void* calldata)
00463 {
00464 int X, Y, Z=0;
00465 int XX,YY,ZZ;
00466 int slice = _imageViewer->GetZSlice();
00467 int min,max;
00468 min = _imageViewer->GetWholeZMin();
00469 max = _imageViewer->GetWholeZMax();
00470
00471
00472 if ( event == vtkCommand::LeftButtonPressEvent ) {
00473
00474
00475
00476
00477 {
00478
00479 _wxVTKiren->GetEventPosition( X, Y );
00480 XX=X; YY=Y; ZZ=Z;
00481 TransfromeCoordViewWorld(XX,YY,ZZ);
00482 _pts->SetPoint(0, XX, YY, ZZ);
00483 _pts->SetPoint(1, XX, YY, ZZ);
00484 _pts->SetPoint(2, XX, YY, ZZ);
00485 _pts->SetPoint(3, XX, YY, ZZ);
00486
00487 _bboxEnabled = true;
00488 _bboxActor->VisibilityOn();
00489
00490 _imageViewer->Render();
00491 }
00492 } else if ( event == vtkCommand::MouseMoveEvent ){
00493 if ( _bboxEnabled ){
00494 _RoiSelected=true;
00495 double p0[3];
00496 _pts->GetPoint(0, p0);
00497 _wxVTKiren->GetEventPosition(X, Y);
00498 XX=X; YY=Y; ZZ=Z;
00499 TransfromeCoordViewWorld(XX,YY,ZZ);
00500 _pts->SetPoint(1, XX , p0[1] , p0[2]);
00501 _pts->SetPoint(2, XX , YY , p0[2]);
00502 _pts->SetPoint(3, p0[0] , YY , p0[2]);
00503 _imageViewer->Render();
00504 }
00505 }
00506 if ( event == vtkCommand::LeftButtonReleaseEvent ){
00507 _bboxEnabled = false;
00508
00509 } else if ( event == wxEVT_MOUSEWHEEL+10000 ){
00510
00511
00512
00513
00514
00515
00516 if( _wxVTKiren->GetControlKey () ){
00517 slice += 10;
00518 } else if( _wxVTKiren->GetShiftKey () ) {
00519 slice++;
00520 } else {
00521
00522 slice += 5;
00523 }
00524
00525 slice = slice > min ? slice : min;
00526 slice = slice < max ? slice : max;
00527
00528 #if (VTK_MAJOR_VERSION >= 5)
00529 _imageViewer->SetSlice( slice );
00530 #else
00531 _imageViewer->SetZSlice( slice );
00532 #endif
00533
00534
00535
00536
00537
00538 wxMouseEvent mouseEvent;
00539 mouseEvent.m_wheelRotation=130;
00540 if (_intVtkPanWid!=NULL) {
00541 _intVtkPanWid->CallBackOnMouseWheel(mouseEvent);
00542 }
00543 _imageViewer->Render();
00544
00545
00546 } else if ( event == wxEVT_MOUSEWHEEL+10001 ){
00547
00548
00549 if( _wxVTKiren->GetControlKey () ){
00550 slice -= 10;
00551 } else if( _wxVTKiren->GetShiftKey () ) {
00552 slice--;
00553 } else {
00554
00555 slice -= 5;
00556 slice = (slice /5) * 5;
00557 }
00558
00559 slice = slice > min ? slice : min;
00560 slice = slice < max ? slice : max;
00561
00562
00563 #if (VTK_MAJOR_VERSION >= 5)
00564 _imageViewer->SetSlice( slice );
00565 #else
00566 _imageViewer->SetZSlice( slice );
00567 #endif
00568
00569
00570
00571
00572
00573 wxMouseEvent mouseEvent;
00574 mouseEvent.m_wheelRotation=-130;
00575 if (_intVtkPanWid!=NULL) {
00576 _intVtkPanWid->CallBackOnMouseWheel(mouseEvent);
00577 }
00578 _imageViewer->Render();
00579
00580 }
00581
00582 }
00583
00584 void wxImageViewerWidgetRoi::ConstructVTK(vtkImageData *imageData){
00585
00586 wxImageViewerWidget::ConstructVTK(imageData);
00587 _interactorStyle2DMaracas->SetObserver(_Observer);
00588
00589
00590 _pts = vtkPoints::New();
00591 _pts->SetNumberOfPoints(4);
00592 _pts->SetPoint(0, -1000 , -1000 , -1000 );
00593 _pts->SetPoint(1, 1000 , -1000 , 0 );
00594 _pts->SetPoint(2, 1000 , 1000 , 1000 );
00595 _pts->SetPoint(3, -1000 , 1000 , 0 );
00596
00597
00598 vtkCellArray *lines = vtkCellArray::New();
00599 lines->InsertNextCell(5);
00600 lines->InsertCellPoint(0);
00601 lines->InsertCellPoint(1);
00602 lines->InsertCellPoint(2);
00603 lines->InsertCellPoint(3);
00604 lines->InsertCellPoint(0);
00605
00606 _pd = vtkPolyData::New();
00607 _pd->SetPoints( _pts );
00608 _pd->SetLines( lines );
00609 lines->Delete();
00610
00611
00612 _bboxActor = vtkActor::New();
00613 _bboxMapper = vtkPolyDataMapper::New();
00614
00615 _bboxMapper->SetInput(_pd);
00616 _bboxMapper->ImmediateModeRenderingOn();
00617 _bboxActor->SetMapper(_bboxMapper);
00618 _bboxActor->GetProperty()->BackfaceCullingOn();
00619 _bboxActor->GetProperty()->SetDiffuseColor(1,0,0);
00620 _bboxActor->GetProperty()->SetLineWidth(2);
00621
00622 _imageViewer->GetRenderer()->AddActor( _bboxActor );
00623
00624
00625 }
00626
00627
00628 bool wxImageViewerWidgetRoi::GetRoiSelected(){
00629 return _RoiSelected;
00630 }