vtk3DSurfaceWidget.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   wxMaracas
00004   Module:    $RCSfile: vtk3DSurfaceWidget.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2009/05/14 13:54:57 $
00007   Version:   $Revision: 1.1 $
00008 
00009   Copyright: (c) 2002, 2003
00010   License:
00011   
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notice for more information.
00015 
00016 =========================================================================*/
00017 #include "vtk3DSurfaceWidget.h"
00018 #include "../marDictionary.h"
00019 #include <vtkInteractorStyleSwitch.h>
00020 #include <vtkPointPicker.h>
00021 #include <vtkPointData.h>
00022 #include <vtkCellLocator.h>
00023 #include <vtkCellArray.h>
00024 #include <vtkStripper.h>
00025 #include <vtkCamera.h>
00026 #include "matrix.h"
00027 
00028 
00029 #include "UtilVtk3DGeometriSelection.h"
00030 
00031 
00032 
00042 //----------------------------------------------------------------------------
00043 BEGIN_EVENT_TABLE( vtk3DSurfaceWidget, wxVTKRenderWindowInteractor )
00044     EVT_LEFT_DCLICK( vtk3DSurfaceWidget::OnLeftDClick )
00045         EVT_MOUSEWHEEL( vtk3DSurfaceWidget::OnMouseWheel )
00046 END_EVENT_TABLE( );
00047 
00048 //----------------------------------------------------------------------------
00049 vtk3DSurfaceWidget::vtk3DSurfaceWidget( wxWindow* parent, wxWindowID id,
00050                                        const wxPoint& pos, const wxSize& size,
00051                                        long style, const wxString& name) 
00052                                        : wxVTKRenderWindowInteractor( parent, id, pos, size, style, name )
00053 {
00054   //make current interactor style be trackbal camera, asked by radiologists.
00055 
00056         /*
00057   vtkInteractorStyleSwitch *iss = dynamic_cast<vtkInteractorStyleSwitch*>(this->GetInteractorStyle());
00058   iss->SetCurrentStyleToTrackballCamera();
00059   */
00060 
00061 
00062   vtkInteractorStyle3DMaracas *interactorStyle3DMaracas = vtkInteractorStyle3DMaracas::New(); 
00063 //  interactorStyle3DMaracas->SetInteractor (this);
00064   this->SetInteractorStyle(interactorStyle3DMaracas);
00065 
00066 
00067   _pRenderer                    = vtkRenderer::New( );
00068   InitialSphere                 = 0;
00069   _centralLine                  = NULL;
00070   _centralLineMapper    = NULL;
00071   _centralLineActor             = NULL;
00072   _axesMapper                   = NULL;
00073   _axesActor                    = NULL;
00074   for(int i=0; i<4; i++) {
00075     _spheres[ i ]               = NULL;
00076     _spheresMapper[ i ] = NULL;
00077     _spheresActor[ i ]  = NULL;
00078   }
00079   _axesActor                    = NULL;
00080   _axesMapper                   = NULL;
00081   _surfActor                    = NULL;
00082   _surfMapper                   = NULL;
00083   _intVtkPanWid                 = NULL;
00084   _outActor                             = NULL;
00085   _outMapper                    = NULL;
00086   _outLine                              = NULL;
00087   _mCubes                               = NULL;
00088 }
00089 //----------------------------------------------------------------------------
00090 vtk3DSurfaceWidget::~vtk3DSurfaceWidget(){
00091 
00092   //good luck:
00093   if( _outActor )           _outActor                   ->      Delete( );
00094   if( _outMapper )          _outMapper                  ->      Delete( );
00095   if( _outLine )            _outLine                    ->      Delete( );
00096   if( _surfActor )          _surfActor                  ->      Delete( );
00097   if( _surfMapper )         _surfMapper                 ->      Delete( );
00098   if( _mCubes )             _mCubes                             ->      Delete( );
00099   if( _centralLine )        _centralLine                ->      Delete( );
00100   if( _centralLineMapper )  _centralLineMapper  ->      Delete( );
00101   if( _centralLineActor )   _centralLineActor   ->      Delete( );
00102   if( _axesActor )          _axesActor                  ->      Delete();
00103   if( _axesMapper )         _axesMapper                 ->      Delete();
00104   for(int i=0; i<4; i++)
00105   {
00106     if( _spheres[ i ] )       _spheres[ i ]                     ->Delete( );
00107     if( _spheresMapper[ i ] ) _spheresMapper[ i ]       ->Delete( );
00108     if( _spheresActor[ i ] )  _spheresActor[ i ]        ->Delete( );
00109   }
00110   if( _pRenderer )          _pRenderer                  ->  Delete( );
00111 }
00112 
00113 
00114 
00115 //----------------------------------------------------------------------------
00116 // virtual
00117 void vtk3DSurfaceWidget::OnMouseWheel( wxMouseEvent& event ){ 
00118         if (_intVtkPanWid!=NULL) { _intVtkPanWid->CallBackOnMouseWheel(event);}
00119 }
00120 //----------------------------------------------------------------------------
00121 void vtk3DSurfaceWidget::OnLeftDClick( wxMouseEvent& event ){
00122         double pp[ 3 ], cp[ 3 ];
00123         int eventrwi[2];
00124         vtkPointPicker* picker = vtkPointPicker::New( );        
00125         this->GetEventPosition( eventrwi );
00126         picker->Pick( eventrwi[0], eventrwi[1], 0.0, _pRenderer );
00127         _pRenderer->GetActiveCamera( )->GetPosition( cp );
00128         picker->GetPickPosition( pp );
00129 
00130         picker->Delete( );
00131         this->SetLast3DClickPoint( pp, cp );
00132         if (_intVtkPanWid!=NULL) { _intVtkPanWid->CallBackOnLeftDClick(event); }
00133 }
00134 //----------------------------------------------------------------------------
00135 void vtk3DSurfaceWidget::GetLast3DClickPoint( double *pp, double *cp ){
00136         pp[0]=_lastPickPoint[0];
00137         pp[1]=_lastPickPoint[1];
00138         pp[2]=_lastPickPoint[2];
00139         cp[0]=_lastCameraPos[0];
00140         cp[1]=_lastCameraPos[1];
00141         cp[2]=_lastCameraPos[2];
00142 }
00143 //----------------------------------------------------------------------------
00144 void vtk3DSurfaceWidget::SetLast3DClickPoint( double *pp, double *cp ){
00145         _lastPickPoint[0]=pp[0];
00146         _lastPickPoint[1]=pp[1];
00147         _lastPickPoint[2]=pp[2];
00148         _lastCameraPos[0]=cp[0];
00149         _lastCameraPos[1]=cp[1];
00150         _lastCameraPos[2]=cp[2];
00151 }
00152 //----------------------------------------------------------------------------
00153 void vtk3DSurfaceWidget::Render( ){
00154         _pRenderWindow->Render();
00155 }
00156 //----------------------------------------------------------------------------
00157 void vtk3DSurfaceWidget::InitCameraReset( ){
00158         _pRenderer->ResetCameraClippingRange();
00159         vtkCamera *cam=_pRenderer->GetActiveCamera();
00160         cam->Zoom(1.8);
00161 // EED 31 Mai 2007
00162 //      Render();
00163 }
00164 //----------------------------------------------------------------------------
00165 void vtk3DSurfaceWidget::SetSurfaceColor( float red, float green, float blue )
00166 {
00167   _surfActor->GetProperty()->SetColor(red, green, blue );
00168   _pRenderWindow->Render();
00169 }
00170 //----------------------------------------------------------------------------
00171 void vtk3DSurfaceWidget::SetSurfaceVisibility( bool visible )
00172 {
00173   _surfActor->SetVisibility( visible );
00174   _pRenderWindow->Render();
00175 }
00176 //----------------------------------------------------------------------------
00177 void vtk3DSurfaceWidget::SetSurfaceIsoValue( int isoval )
00178 {
00179   _mCubes->SetValue(0, isoval);
00180   _pRenderWindow->Render();
00181 }
00182 //----------------------------------------------------------------------------
00183 void vtk3DSurfaceWidget::SetSurfaceOpacity( int opaval )
00184 {
00185   //There is no way in Win32 to specify a slider different than 0->100
00186   _surfActor->GetProperty()->SetOpacity( (double)opaval/100.0 );
00187   _pRenderWindow->Render();
00188 }
00189 //----------------------------------------------------------------------------
00190 void vtk3DSurfaceWidget::GetSphereCenter( double center[3] )
00191 {
00192         _spheres[ 3 ]->GetCenter( center );
00193 }
00194 //----------------------------------------------------------------------------
00195 void vtk3DSurfaceWidget::SetAxis( vtkPolyData *axis )
00196 {
00197         _axesMapper = vtkPolyDataMapper::New( );
00198         _axesMapper->SetInput( axis );
00199         _axesMapper->Update();
00200     //axis->Delete();
00201 
00202         _axesActor = vtkActor::New( );
00203         _axesActor->SetMapper( _axesMapper );
00204         _axesActor->GetProperty()->SetColor( 1, 0, 0 );
00205         _axesActor->GetProperty()->SetLineWidth( 2.0 );
00206         _pRenderer->AddActor( _axesActor );
00207 
00208         //Generate a new vtkwindow ???
00209 //EED 31 Mai 2007
00210 //      _pRenderWindow->Render( );
00211  }
00212 //----------------------------------------------------------------------------
00213 void vtk3DSurfaceWidget::RemoveAxis( )
00214 {
00215   if (_axesActor)
00216   {
00217       _pRenderer->RemoveActor(_axesActor);
00218       _axesActor->Delete();
00219       _axesActor = NULL;
00220       _pRenderWindow->Render( );
00221         }
00222 }
00223 //----------------------------------------------------------------------------
00224 void vtk3DSurfaceWidget::ShowMARACASData( marInterface* mar ){
00225     int whd[3];
00226 
00227     _mar = mar;
00228     _marImageData = _mar->_experiment->getDynData( )->getVolume( )->castVtk();
00229     _marImageData->GetDimensions( whd );
00230     _width  = whd[0];
00231     _height = whd[1];
00232     _depth  = whd[2];
00233 
00234     _marImageData->GetScalarRange( _range );
00235     
00236     //Outline v2:
00237     // An outline provides context around the data.
00238     _outLine    = vtkOutlineFilter::New( );
00239     _outLine->SetInput( _marImageData );
00240     _outMapper  = vtkPolyDataMapper::New( );
00241     _outMapper->SetInput( _outLine->GetOutput( ) );
00242     _outActor   = vtkActor::New( );
00243     _outActor->SetMapper( _outMapper );
00244     _outActor->GetProperty( )->SetColor( 0.7, 0.0, 0.9 );
00245     //_outActor->PickableOff( );
00246     _pRenderer->AddActor( _outActor );
00247 
00248     // Surface
00249     _mCubes = vtkMarchingCubes::New( );
00250     _surfMapper = vtkPolyDataMapper::New( );
00251     _surfActor = vtkActor::New( );
00252 
00253     _mCubes->SetInput( _marImageData );
00254     _mCubes->SetValue( 0, _range[1] / 4 );
00255 
00256     vtkStripper *stripper = vtkStripper::New();
00257     stripper->SetInput( _mCubes->GetOutput( ) );
00258 
00259          _surfMapper->SetInput( stripper->GetOutput() );
00260     _surfMapper->ScalarVisibilityOff( );
00261     stripper->Delete();
00262 
00263     _surfActor->SetMapper( _surfMapper );
00264     _surfActor->PickableOff( );
00265     _surfActor->GetProperty( )->SetColor( 0.9803, 0.9215, 0.8392 );
00266     _surfActor->GetProperty( )->SetOpacity( 0.5 );
00267     _pRenderer->AddActor( _surfActor );
00268 
00269     // 1. ParallelProjectionOn should be set after AddActor (otherwise call vtkRenderer::ResetCameraClippingRange
00270     // 2. ParallelProjectionOn is *necessary* for the vtkImplicitSelectionLoop
00271     // otherwise this give a cone instead of a cylinder cutting.
00272     _pRenderer->GetActiveCamera()->ParallelProjectionOn();
00273 
00274 }
00275 //----------------------------------------------------------------------------
00276 void vtk3DSurfaceWidget::ShowMARACASDataCT( marInterfaceCT* mar ){
00277     int whd[3];
00278 
00279 
00280     _marCT = mar;
00281     _marImageData = _marCT->getDynData()->getVolume()->castVtk();
00282     _marImageData->GetDimensions( whd );
00283     _width  = whd[0];
00284     _height = whd[1];
00285     _depth  = whd[2];
00286 
00287     _marImageData->GetScalarRange( _range );
00288     
00289         int a = (_marCT->GetExperiment())->getQuantStart();
00290     //Outline v2:
00291     // An outline provides context around the data.
00292     _outLine    = vtkOutlineFilter::New( );
00293     _outLine->SetInput( _marImageData );
00294     _outMapper  = vtkPolyDataMapper::New( );
00295     _outMapper->SetInput( _outLine->GetOutput( ) );
00296     _outActor   = vtkActor::New( );
00297     _outActor->SetMapper( _outMapper );
00298     _outActor->GetProperty( )->SetColor( 0.7, 0.0, 0.9 );
00299     //_outActor->PickableOff( );
00300     _pRenderer->AddActor( _outActor );
00301 
00302     // Surface
00303     _mCubes = vtkMarchingCubes::New( );
00304     _surfMapper = vtkPolyDataMapper::New( );
00305     _surfActor = vtkActor::New( );
00306 
00307     _mCubes->SetInput( _marImageData );
00308     _mCubes->SetValue( 0, _range[1] / 4 );
00309 
00310     vtkStripper *stripper = vtkStripper::New();
00311     stripper->SetInput( _mCubes->GetOutput( ) );
00312 
00313          _surfMapper->SetInput( stripper->GetOutput() );
00314     _surfMapper->ScalarVisibilityOff( );
00315     stripper->Delete();
00316 
00317     _surfActor->SetMapper( _surfMapper );
00318     _surfActor->PickableOff( );
00319     _surfActor->GetProperty( )->SetColor( 0.9803, 0.9215, 0.8392 );
00320     _surfActor->GetProperty( )->SetOpacity( 0.5 );
00321     _pRenderer->AddActor( _surfActor );
00322 
00323     // 1. ParallelProjectionOn should be set after AddActor (otherwise call vtkRenderer::ResetCameraClippingRange
00324     // 2. ParallelProjectionOn is *necessary* for the vtkImplicitSelectionLoop
00325     // otherwise this give a cone instead of a cylinder cutting.
00326     _pRenderer->GetActiveCamera()->ParallelProjectionOn();
00327 
00328 }
00329 //----------------------------------------------------------------------------
00330 void vtk3DSurfaceWidget::SetInitialPoint(){
00331         this->SetInitialPoint( _lastPickPoint, _lastCameraPos );
00332 }
00333 //----------------------------------------------------------------------------
00334 void vtk3DSurfaceWidget::SetInitialPoint( float* pickPoint, float* cameraPos ){
00335         marDictionary marDict;
00336         char ttmp[256];
00337 
00338     gtm::TVector< double > pO( 3 ), pF( 3 ), pp( 3 ), cp( 3 );
00339     gtm::TVector< double > xc( 3 );
00340     gtm::TVector< double > x1( 3 ), n1( 3 );
00341     gtm::TVector< double > x2( 3 ), n2( 3 );
00342     gtm::TVector< double > tmp( 3 );
00343     double fac;
00344     bool success                = true;
00345     int notSuccessType  = 0;
00346 
00347     if( _centralLineActor )
00348     {
00349         _pRenderer->RemoveActor( _centralLineActor );
00350         _centralLineActor->Delete( );
00351     } // fi
00352     if( _centralLineMapper ) _centralLineMapper->Delete( );
00353     if( _centralLine )       _centralLine->Delete( );
00354 
00355     _centralLine       = NULL;
00356     _centralLineMapper = NULL;
00357     _centralLineActor  = NULL;
00358 
00359     for(int i=0; i<4; i++)
00360     {
00361       if( _spheresActor[ i ] )
00362       {
00363         _pRenderer->RemoveActor( _spheresActor[ i ] );
00364         _spheresActor[ i ]->Delete( );
00365       } // fi
00366       if( _spheresMapper[ i ] ) _spheresMapper[ i ]->Delete( );
00367       if( _spheres[ i ] )       _spheres[ i ]->Delete( );
00368       _spheres[ i ] = NULL;
00369       _spheresMapper[ i ] = NULL;
00370       _spheresActor[ i ] = NULL;
00371     } //rof
00372 
00373     fac = GTM_MAX( _width, _height );
00374     fac = 2 * GTM_MAX( fac, _depth );
00375     pp( 0 ) = pickPoint[ 0 ]; pp( 1 ) = pickPoint[ 1 ]; pp( 2 ) = pickPoint[ 2 ];
00376     cp( 0 ) = cameraPos[ 0 ]; cp( 1 ) = cameraPos[ 1 ]; cp( 2 ) = cameraPos[ 2 ];
00377 
00378 
00379         UtilVtk3DGeometriSelection utilVtk3DGeometriSelection;
00380         utilVtk3DGeometriSelection.SetDimentions(_width,_height,_depth);
00381         utilVtk3DGeometriSelection.SetMarchingCube(_mCubes);
00382 
00383     if( utilVtk3DGeometriSelection.FindCubePointsFromPoints(
00384       pO.GetAnsiRef( ), pF.GetAnsiRef( ),
00385       pp.GetAnsiRef( ), cp.GetAnsiRef( )
00386     ) ) {
00387 
00388         if( utilVtk3DGeometriSelection.GetPointAndNormalIntersection(
00389             x1.GetAnsiRef( ), n1.GetAnsiRef( ),
00390             pO.GetAnsiRef( ), pF.GetAnsiRef( )
00391     ) ) {
00392 
00393             if( utilVtk3DGeometriSelection.GetPointAndNormalIntersection(
00394           x2.GetAnsiRef( ), n2.GetAnsiRef( ),
00395           ( x1 - n1 ).GetAnsiRef( ), ( x1 - ( n1 * fac ) ).GetAnsiRef( )
00396         ) ) {
00397 
00398                 xc = ( x2 + x1 ) * 0.5;
00399 
00400 
00401                 double dd=12; // EED   ****** ATENTION ****************
00402 
00403                 if (!(
00404                         (xc(0)<dd) || (xc(1)<dd) || (xc(2)<dd) || 
00405                         (fabs(xc(0)-_width)<dd) || (fabs(xc(1)-_height)<dd) || (fabs(xc(2)-_depth)<dd) 
00406                         )){
00407 
00408                 _mar->_experiment->setStartPoint( (int)xc( 0 ), (int)xc( 1 ), (int)xc( 2 ) );
00409         _mar->_experiment->getParameters( )->setROIStep( 2*( xc - x1 ).GetNorm( ) );
00410 
00411       for(int i=0; i<4; i++)
00412       {
00413         _spheres[ i ] = vtkSphereSource::New( );
00414         _spheresMapper[ i ] = vtkPolyDataMapper::New( );
00415         _spheresMapper[ i ]->SetInput( _spheres[ i ]->GetOutput( ) );
00416         _spheresActor[ i ] = vtkActor::New( );
00417         _spheresActor[ i ]->SetMapper( _spheresMapper[ i ] );
00418         _spheresActor[ i ]->PickableOff( );
00419         _pRenderer->AddActor( _spheresActor[ i ] );
00420       }
00421 
00422                 _spheres[ 0 ]->SetCenter( x1( 0 ), x1( 1 ), x1( 2 ) );
00423                 _spheres[ 1 ]->SetCenter( x2( 0 ), x2( 1 ), x2( 2 ) );
00424                 _spheres[ 2 ]->SetCenter( xc( 0 ), xc( 1 ), xc( 2 ) );
00425                 _spheres[ 3 ]->SetCenter( xc( 0 ), xc( 1 ), xc( 2 ) );
00426 
00427                 _spheres[ 0 ]->SetRadius( 0.5 );
00428                 _spheres[ 1 ]->SetRadius( 0.5 );
00429                 _spheres[ 2 ]->SetRadius( 0.5 );
00430                 _spheres[ 3 ]->SetRadius( ( xc - x1 ).GetNorm( ) );
00431 
00432                 _spheresActor[ 0 ]->GetProperty( )->SetColor( 1.0, 0.0, 0.0 );
00433                 _spheresActor[ 1 ]->GetProperty( )->SetColor( 0.0, 1.0, 0.0 );
00434                 _spheresActor[ 2 ]->GetProperty( )->SetColor( 0.0, 0.0, 1.0 );
00435                 _spheresActor[ 3 ]->GetProperty( )->SetColor( 1.0, 0.0, 0.0 );
00436                 _spheresActor[ 3 ]->GetProperty( )->SetOpacity( 0.3 );
00437 
00438                 vtkPoints* points = vtkPoints::New( );
00439                 points->InsertNextPoint( x1.GetAnsiRef( ) );
00440                 points->InsertNextPoint( x2.GetAnsiRef( ) );
00441 
00442                 vtkCellArray* array = vtkCellArray::New( );
00443                 array->InsertNextCell( 2 );
00444                 array->InsertCellPoint( 0 );
00445                 array->InsertCellPoint( 1 );
00446 
00447                 _centralLine = vtkPolyData::New( );
00448                 _centralLine->SetPoints( points );
00449                 _centralLine->SetLines( array );
00450       points->Delete();
00451       array->Delete();
00452 
00453                 _centralLineMapper = vtkPolyDataMapper::New( );
00454                 _centralLineMapper->SetInput( _centralLine );
00455 
00456                 _centralLineActor = vtkActor::New( );
00457                 _centralLineActor->SetMapper( _centralLineMapper );
00458                 _centralLineActor->GetProperty( )->SetColor( 1.0, 1.0, 1.0 );
00459                 _centralLineActor->PickableOff( );
00460                 _pRenderer->AddActor( _centralLineActor );
00461 
00462 
00463            } else  {success = false; notSuccessType=1; }
00464 
00465         } // fi
00466 
00467         } else success = false;
00468 
00469     } else success = false;
00470 
00471     // Show a message, if any.
00472     if (( !success ) && (notSuccessType==0)) {
00473                 strcpy( ttmp , marDict.GetString(905) ); strcat( ttmp , "\n \n" ); strcat( ttmp , marDict.GetString(910) );
00474         wxMessageBox( wxString(ttmp, wxConvUTF8) , // "Set an initial point.\n \n (Double click over the interest artery.)"
00475                                           _T("DxMM : MARACAS"), wxOK | wxCENTRE | wxICON_INFORMATION, this);
00476         }
00477     if ((!success ) && (notSuccessType==1)) {
00478                 strcpy( ttmp , marDict.GetString(915) ); strcat( ttmp , "\n \n" ); strcat( ttmp , marDict.GetString(920) );
00479         wxMessageBox( wxString(ttmp, wxConvUTF8), //"The initial point should be far \n of the limits of the volume."
00480                                           _T("DxMM : MARACAS"), wxOK | wxCENTRE | wxICON_INFORMATION, this);
00481         }
00482     // Finish
00483     _pRenderWindow->Render( );
00484     InitialSphere = success;
00485 
00486 }
00487 //----------------------------------------------------------------------------
00488 void vtk3DSurfaceWidget::ConfigureVTK( )
00489 {
00490   // get render window
00491   _pRenderWindow =  this->GetRenderWindow(  );
00492 
00493   // connect renderer and render window and configure render window
00494   _pRenderWindow->AddRenderer( _pRenderer );
00495 
00496   // configure renderer
00497   _pRenderer->SetBackground( 0.0, 0.0, 0.0 );
00498   //_pRenderer->SetBackground( 1, 1, 0);  //FIXME
00499   _pRenderer->GetActiveCamera( )->Zoom( 1.0 );
00500   _pRenderer->GetActiveCamera( )->SetClippingRange( 1, 1000 );
00501   _pRenderer->ResetCamera( );
00502 
00503 //EED 22Mai2007 
00504 //  AttachRenderWindow();
00505 
00506 }
00507 
00508 
00509 //----------------------------------------------------------------------------
00510 vtkRenderer* vtk3DSurfaceWidget::GetRenderer(){
00511         return _pRenderer;
00512 }
00513 //----------------------------------------------------------------------------
00514 
00515 

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1