vtkPlane2DView.cxx

Go to the documentation of this file.
00001 #include "vtkPlane2DView.h"
00002 
00003 #include "vtkCellArray.h"
00004 
00005 #include "vtkInteractorStyleBaseView2D.h"
00006 #include "vtkPointData.h"
00007 
00008 
00009 vtkPlane2DView::vtkPlane2DView( wxWindow *parent)
00010 : wxVtk2DBaseView(parent)
00011 {
00012 
00013         _backX                                  =       -99999;
00014         _backY                                  =       -99999;
00015         _backZ                                  =       -99999;
00016 
00017         _backOrient[0]                  =       -99999;
00018         _backOrient[1]                  =       -99999;
00019         _backOrient[2]                  =       -99999;
00020         _backOrient[3]                  =       -99999;
00021 
00022         _active                                 =   true;
00023         _mip_visualization              =       true;
00024         _mip_width                              =       2;
00025 
00026     _transform1                         =       vtkTransform::New();
00027     _transform2                         =       vtkTransform::New();
00028         _transform1->Identity();
00029         _transform2->Identity();
00030 
00031         _sizeIma                                =       200;
00032 
00033         _pSource                                =       NULL;
00034         _3Dslices                               =       NULL;
00035         _stPoints                               =       NULL;
00036         _change                                 =       NULL;
00037         _imageResult                    =       NULL;
00038 
00039         // line horizontal
00040     _pts                        = NULL;
00041     _lineActor          = NULL;
00042         _lineMapper             = NULL;
00043         _pd                             = NULL;
00044 
00045         _interactorstyleplane2D =       false;
00046 }
00047 //-------------------------------------------------------------------
00048 vtkPlane2DView::~vtkPlane2DView()
00049 {
00050         ResetPlane();
00051 
00052         // Horizontal Line
00053     if (_pts            != NULL)        {       _pts                    -> Delete(); }
00054     if (_lineActor      != NULL)        {       _lineActor              -> Delete(); }
00055     if (_lineMapper     != NULL)        {       _lineMapper             -> Delete(); }
00056     if (_pd                     != NULL)        {       _pd                             -> Delete(); }
00057 
00058         _transform1             -> Delete();
00059         _transform2             -> Delete();
00060 }
00061 
00062 //-------------------------------------------------------------------
00063 void vtkPlane2DView::ResetBack()
00064 {
00065                 _backX=-1;
00066                 _backY=-1;
00067                 _backZ=-1;
00068                 _backOrient[0]=-1;
00069                 _backOrient[1]=-1;
00070                 _backOrient[2]=-1;
00071                 _backOrient[3]=-1;
00072 }
00073 
00074 //-------------------------------------------------------------------
00075 void vtkPlane2DView::ResetPlane(){
00076         if (_pSource    !=NULL) { _pSource      -> Delete();    }
00077         if (_3Dslices   !=NULL) { _3Dslices     -> Delete();    }
00078         if (_stPoints   !=NULL) { _stPoints     -> Delete();    }
00079         if (_change             !=NULL) { _change       -> Delete();    }
00080 }
00081 //-------------------------------------------------------------------
00082 vtkMPRBaseData *vtkPlane2DView::GetVtkmprbasedata()
00083 {
00084         return (vtkMPRBaseData*)GetVtkBaseData();
00085 }
00086 
00087 //-------------------------------------------------------------------
00088 void vtkPlane2DView::SetPSource(int sizeIma){
00089         int dimIma      = sizeIma; 
00090 
00091         double x = GetVtkmprbasedata()->GetX();
00092         double y = GetVtkmprbasedata()->GetY();
00093         double z = GetVtkmprbasedata()->GetZ();
00094 
00095         double spc[3];
00096         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
00097         imagedata->GetSpacing(spc);
00098         x=x*spc[0];
00099         y=y*spc[1];
00100         z=z*spc[2];
00101 
00102 
00103         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
00104         vtkTransform    *transform       = mprbasedata->GetTransformOrientation();
00105 
00106         double in[3];  // temp
00107         double pA[3];
00108         double pB[3];
00109         in[0]=1;                in[1]=0;                        in[2]=0;
00110         transform->TransformPoint(in,_n);
00111 
00112         in[0]=0;        in[1]=dimIma-1;         in[2] = 0;
00113         transform->TransformPoint(in,pA);
00114 
00115         in[0]=0;        in[1]=0;                in[2]=dimIma-1;
00116         transform->TransformPoint(in,pB);
00117 
00118     _pSource -> SetPoint1( pA   );
00119     _pSource -> SetPoint2( pB   );
00120 
00121         _pSource -> SetOrigin( 0        , 0             , 0             );
00122     _pSource -> SetResolution( sizeIma-1 , sizeIma -1 );
00123         _pSource -> Update();
00124         _pSource -> SetCenter( x, y, z );
00125     _pSource -> SetNormal( _n );
00126     _pSource -> Update( );
00127 
00128 // EED Borrame
00129 //      transform->Delete();
00130 
00131 }
00132 
00133 //-------------------------------------------------------------------
00134 void vtkPlane2DView::ExtractPlane() 
00135 {
00136 
00137         double x = GetVtkmprbasedata()->GetX();
00138         double y = GetVtkmprbasedata()->GetY();
00139         double z = GetVtkmprbasedata()->GetZ();
00140 
00141         double spc[3];
00142         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
00143         imagedata->GetSpacing(spc);
00144         x=x*spc[0];
00145         y=y*spc[1];
00146         z=z*spc[2];
00147 
00148         vtkTransform *transform = GetVtkmprbasedata()->GetTransformOrientation();
00149         double orientation[4];
00150         transform->GetOrientationWXYZ(orientation);
00151 
00152         bool okOrientation=true;
00153         if ((orientation[0]!=_backOrient[0]) || (orientation[1]!=_backOrient[1]) ||
00154                 (orientation[2]!=_backOrient[2]) || (orientation[3]!=_backOrient[3]))
00155         {
00156                 okOrientation=false;
00157         }
00158 
00159         bool okPosicion=true;
00160         if ( (x!=_backX) || (y!=_backY) || (z!=_backZ) )
00161         {
00162                 okPosicion=false;
00163         }
00164 
00165         if ((okPosicion==false) || (okOrientation==false) ) {
00166                 if (_active==true){
00167 
00168                         bool ok = false;
00169 
00170                         if ( _mip_visualization==true )
00171                         {
00172                                 if (_interactorstyleplane2D!=NULL)
00173                                 {
00174                                         if (_interactorstyleplane2D->GetStateRotate()==false )
00175                                         {
00176                                                 ok=true;
00177                                         }
00178                                 }
00179                         }
00180 //                      ok=true;
00181 
00182                         if (ok==true)
00183                         {
00184                                 Extract_MIP_PlaneVTK();
00185                         } else {
00186                                 Extract_One_PlaneVTK();
00187                         } // ok
00188 
00189                 } // active
00190                 _backX=x;
00191                 _backY=y;
00192                 _backZ=z;
00193                 _backOrient[0]=orientation[0];
00194                 _backOrient[1]=orientation[1];
00195                 _backOrient[2]=orientation[2];
00196                 _backOrient[3]=orientation[3];
00197         } //okPosition okOrientation
00198 }
00199 
00200 //-------------------------------------------------------------------
00201 
00202 void vtkPlane2DView::Extract_One_PlaneVTK()
00203 {
00204         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
00205         SetPSource(_sizeIma);
00206         _3Dslices -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
00207         _3Dslices -> SetSource( imagedata );
00208         _3Dslices -> Update( );
00209         _stPoints -> GetPointData( )->SetScalars(  _3Dslices->GetOutput()->GetPointData()->GetScalars()  );
00210         _stPoints -> SetDimensions( _sizeIma, _sizeIma, 1 );
00211         _stPoints -> SetScalarType( imagedata->GetScalarType() );
00212         _stPoints -> SetScalarTypeToShort();
00213         _stPoints -> Update();
00214 //      _change   -> SetInput( _stPoints );  
00215 //      _change   -> Update();    //important
00216         _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _stPoints );
00217 //      _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _change->GetOutput() );
00218         //      vtkImageActor *imageActor = _imageViewer2XYZ->GetVtkImageViewer2()->GetImageActor();
00219 }
00220 
00221 //-------------------------------------------------------------------
00222 
00223 void vtkPlane2DView::Extract_MIP_PlaneVTK( /*double heightDefinition*/ )
00224 {
00225 
00226         int mipWidth;
00227         double sp;
00228         int sizeWidth = (_mip_width*2)+1 ;
00229         int deltaPixel;
00230         int iWidth,itmp,tmpSizeWith;
00231 
00232         double spc[3];
00233         vtkImageData *imagedata = GetVtkmprbasedata()->GetImageData();
00234         SetPSource(_sizeIma);
00235         imagedata->GetSpacing(spc);
00236 
00237         bool heightDefinition=false;
00238         if (_mip_width<3)
00239         {
00240                 heightDefinition=true;
00241         }
00242 
00243         if (heightDefinition==true)
00244         {
00245                 mipWidth        =       _mip_width;
00246                 sp                      =       spc[0];
00247                 deltaPixel      =       1;
00248         } else {
00249                 mipWidth        =       2;
00250                 tmpSizeWith =   (mipWidth*2) + 1;
00251                 sp                      =       (spc[0]*sizeWidth)/tmpSizeWith;
00252                 sizeWidth       =       tmpSizeWith;
00253                 deltaPixel      =       4;
00254         }
00255 
00256 
00257 
00258         std::vector< vtkProbeFilter* > slicesLST;
00259 
00260         _pSource->Push( -mipWidth * sp );
00261         _pSource->Update();
00262 
00263         for ( iWidth=0 ; iWidth<sizeWidth ; iWidth++ )
00264         {       
00265                 vtkProbeFilter *slice = vtkProbeFilter::New();
00266                 slice -> SetInput( ( vtkDataSet* )_pSource->GetOutput( ) );
00267                 slice -> SetSource( imagedata );
00268                 slice -> Update( );
00269                 slicesLST.push_back( slice );
00270                 _pSource->Push( sp );
00271         }
00272         
00273         if (_imageResult ==NULL)
00274         {
00275                 _imageResult = vtkImageData::New();
00276                 _imageResult -> SetDimensions(_sizeIma,_sizeIma,1);
00277                 _imageResult -> SetSpacing(1,1,1);
00278                 _imageResult -> SetScalarType( imagedata->GetScalarType() );
00279                 _imageResult -> SetExtent(0,_sizeIma-1,0,_sizeIma-1,0,0);
00280                 _imageResult -> SetWholeExtent(0,_sizeIma-1,0,_sizeIma-1,0,0);
00281                 _imageResult -> AllocateScalars();
00282                 _imageResult -> Update();
00283         }
00284 
00285         unsigned short *pTemp;
00286         unsigned short *pResult;
00287         pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 ); 
00288 
00289         int iPixels , sizePixels = _sizeIma*_sizeIma;
00290         for(iPixels=0 ; iPixels<sizePixels ; iPixels=iPixels+deltaPixel)
00291         {
00292 
00293                 pTemp = (unsigned short*)slicesLST[0]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
00294 
00295                 pResult = (unsigned short*)_imageResult->GetScalarPointer( 0 , 0 , 0 ); 
00296                 pResult[iPixels] = pTemp[iPixels];
00297 
00298                 for (iWidth=1;iWidth<sizeWidth;iWidth++)
00299                 {
00300                         pTemp = (unsigned short*)slicesLST[iWidth]->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0);
00301 
00302                         if (pResult[iPixels]< pTemp[iPixels])
00303                         {
00304                                 pResult[iPixels] = pTemp[iPixels];
00305                         }
00306                 }
00307 
00308                 if (deltaPixel!=1)
00309                 {
00310                         for (itmp=1;itmp<deltaPixel;itmp++)
00311                         {
00312                                 pResult[iPixels+itmp] = pResult[iPixels];
00313                         }
00314                 }
00315 
00316         }
00317 
00318         for (iWidth=0;iWidth<sizeWidth;iWidth++)
00319         {       
00320                 slicesLST[iWidth]->Delete();
00321         }
00322 
00323         _imageResult->Modified();
00324         _imageViewer2XYZ->GetVtkImageViewer2()->SetInput ( _imageResult );
00325 
00326 }
00327 
00328 //-------------------------------------------------------------------
00329 void vtkPlane2DView::Configure( )
00330 {
00331         wxVtk2DBaseView::Configure(false);
00332 
00333         HorizontalLine();
00334 // Borrame
00335 //      CircleLine();
00336 
00337         _pSource         = vtkPlaneSource::New( );
00338     _3Dslices    = vtkProbeFilter::New( ) ;
00339         _stPoints        = vtkStructuredPoints::New( );
00340         _change          = vtkImageChangeInformation::New();
00341 
00342         wxVTKRenderWindowInteractor *iren               = GetWxVTKRenderWindowInteractor();
00343         ExtractPlane();
00344 //      SetActive(false);
00345         _imageViewer2XYZ -> GetVtkImageViewer2()        -> SetupInteractor ( iren );
00346 
00348         //RaC 04-2010 Look for the description of new changes in vtkInteractorStyleBaseView2D constructor.
00349         //
00350         // Previous version
00351         // SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
00352         //
00353         // Actual version
00354         vtkInteractorStyleBaseView2D *style2D = vtkInteractorStyleBaseView2D::New();
00355 
00356         manualInteractorWindowLevel *_manualinteractorwindowlevel= new manualInteractorWindowLevel();
00357         style2D->SetInteractorWindowLevel( _manualinteractorwindowlevel );
00358 
00359         vtkInteractorScrollZ *_vtkInteractorScrollZ = new vtkInteractorScrollZ();
00360         style2D->SetInteractorScrollZ(_vtkInteractorScrollZ);
00361 
00362         SetInteractorStyleImage( style2D );
00363 
00364         // RaC
00366 
00367         _interactorstyleplane2D = new vtkInteractorStylePlane2D();
00368         ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
00369 
00370         vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
00371         
00372         camera->SetViewUp               (       0                       ,       1                               ,       0       );
00373         camera->SetPosition             ((0+_sizeIma)/2 , (0+_sizeIma)/2        , 10000 ); 
00374         
00375         camera->SetFocalPoint   ((0+_sizeIma)/2 , (0+_sizeIma)/2        ,       0       ); 
00376         camera->SetClippingRange(0.01, 100000);
00377         camera->ComputeViewPlaneNormal();
00378         camera->SetParallelScale( _sizeIma/3.0 );
00379 
00380         // text information over the graphic window
00381         _vtkInfoTextImage                                               = new vtkInfoTextImage();
00382         _vtkInfoTextImageInteractorPlane2D              = new vtkInfoTextImageInteractorPlane2D();
00383         _vtkInfoTextImage->SetWxVtk2DBaseView(this);
00384         _vtkInfoTextImage->SetMarImageData(  GetVtkmprbasedata()->GetMarImageData() );
00385         _vtkInfoTextImageInteractorPlane2D->SetModelVtkInfoTextImage(_vtkInfoTextImage);        
00386         _vtkInfoTextImage->Configure();
00387         ((vtkInteractorStyleBaseView*)this->GetInteractorStyleBaseView())->AddInteractorStyleMaracas(_vtkInfoTextImageInteractorPlane2D);
00388 }
00389 
00390 //-------------------------------------------------------------------
00391 void vtkPlane2DView::HorizontalLine()
00392 {
00393 // Axe Horizontal
00394         _pts = vtkPoints::New();
00395         _pts->SetNumberOfPoints(2);
00396         _pts->SetPoint(0, -1000 , -1000 , -1000 );
00397         _pts->SetPoint(1,  1000 ,  1000 ,  1000 );
00398         vtkCellArray *lines = vtkCellArray::New();
00399         lines->InsertNextCell(2);
00400         lines->InsertCellPoint(0);
00401         lines->InsertCellPoint(1);
00402         _pd = vtkPolyData::New();
00403     _pd->SetPoints( _pts );
00404     _pd->SetLines( lines );
00405         lines->Delete();  //do not delete lines ??
00406         _lineActor                                      =       vtkActor::New();
00407     _lineMapper                                 =       vtkPolyDataMapper::New();
00408         _lineMapper->SetInput(_pd);
00409         _lineMapper->ImmediateModeRenderingOn();
00410         _lineActor->SetMapper(_lineMapper);
00411         _lineActor->GetProperty()->BackfaceCullingOn();
00412         _lineActor->GetProperty()->SetDiffuseColor(0,0,1);
00413         _lineActor->GetProperty()->SetLineWidth(2);
00414         _lineActor->GetProperty()->SetOpacity(0);
00415     _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineActor );
00416 }
00417 
00418 //-------------------------------------------------------------------
00419 void vtkPlane2DView::RotationStart()
00420 {
00421         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
00422         vtkTransform    *transform       = mprbasedata->GetTransformOrientation();
00423         _transform1->SetMatrix( transform->GetMatrix() );
00424 }
00425 //-------------------------------------------------------------------
00426 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
00427 { 
00428         if (ok_ang==false)
00429         {
00430                 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
00431         }
00432 
00433         if (ok_v==false){
00434                 _vxb=-vy;
00435                 _vyb=vx;
00436         }
00437         _transform2->Identity();
00438         _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
00439 
00440         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
00441         vtkTransform *transform          =      vtkTransform::New();
00442         transform->Identity();
00443         transform->Concatenate(_transform1);
00444         transform->Concatenate(_transform2);
00445         mprbasedata->InitTransformOrientation(transform);
00446         transform->Delete();
00447 
00448         // Refresh Horizontal Line
00449         _pts->SetPoint( 0 , (_sizeIma/2) - _vxb*2       , (_sizeIma/2) - _vyb*2 , 1 );
00450         _pts->SetPoint( 1 , (_sizeIma/2) + _vxb*2       , (_sizeIma/2) + _vyb*2 , 1 );
00451 //      RefreshCircleLine();
00452 }
00453 
00454 //-------------------------------------------------------------------
00455 void vtkPlane2DView::Refresh(  )
00456 {
00457         ExtractPlane();
00458         wxVtkBaseView::Refresh();
00459 }
00460 //-------------------------------------------------------------------
00461 void vtkPlane2DView::SetImgSize( int imgSize )
00462 {
00463         _sizeIma = imgSize;
00464 }
00465 //-------------------------------------------------------------------
00466 int vtkPlane2DView::GetImgSize()
00467 {
00468         return _sizeIma;
00469 }
00470 //-------------------------------------------------------------------
00471 int     vtkPlane2DView::GetActualSlice()  // virtual 
00472 {
00473         _cx = GetVtkmprbasedata()->GetX();
00474         _cy = GetVtkmprbasedata()->GetY();
00475         _cz = GetVtkmprbasedata()->GetZ();
00476         return 0;
00477 }
00478 
00479 //-------------------------------------------------------------------
00480 
00481 bool vtkPlane2DView::GetMipVisualization()
00482 {
00483         return _mip_visualization;
00484 }
00485 
00486 //-------------------------------------------------------------------
00487 int vtkPlane2DView::GetMipWidth()
00488 {
00489         return _mip_width;
00490 }
00491 
00492 //-------------------------------------------------------------------
00493 void vtkPlane2DView::SetActualSlice(int slice)  // Virtual
00494 {
00495         double dir=(double)slice/3.0;
00496         GetVtkmprbasedata()->SetX( _cx + (_n[0]*dir) );
00497         GetVtkmprbasedata()->SetY( _cy + (_n[1]*dir) );
00498         GetVtkmprbasedata()->SetZ( _cz + (_n[2]*dir) );
00499 }
00500 //---------------------------------------------------------------------------
00501 vtkInteractorStylePlane2D       *vtkPlane2DView::GetInteractorstyleplane2D()
00502 {
00503         return this->_interactorstyleplane2D;
00504 }
00505 //---------------------------------------------------------------------------
00506 void vtkPlane2DView::SetActive(bool active)
00507 {
00508         _active = active;
00509 }
00510 
00511 //---------------------------------------------------------------------------
00512 
00513 void vtkPlane2DView::SetMipVisualization(bool ok)
00514 {
00515         _mip_visualization=ok;
00516 }
00517 
00518 //---------------------------------------------------------------------------
00519 
00520 void vtkPlane2DView::SetMipWidth(int value)
00521 {
00522         _mip_width=value;
00523 }
00524 
00525 //---------------------------------------------------------------------------
00526 void vtkPlane2DView::SetVisibleLine(bool ok)
00527 {
00528         double opacity;
00529         if (ok==true)
00530         {
00531                 opacity=1;
00532         } else {
00533                 opacity=0;
00534         }
00535         _lineActor->GetProperty()->SetOpacity(opacity);
00536 }
00537 // ----------------------------------------------------------------------------
00538 void vtkPlane2DView::TransfromCoordViewWorld2(double &X, double &Y, double &Z)
00539 {
00540         double spc[3];
00541         GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
00542 
00543         double xx = X;
00544         double yy = Y;
00545         double zz = 0;
00546 
00547         TransFromCoordScreenToWorld(xx,yy,zz);
00548 
00549 
00550         vtkTransform *transf1 = vtkTransform::New();
00551         transf1->Identity();
00552         vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
00553         transf1->Concatenate(transf2->GetMatrix());
00554         double in[4], out[4];
00555         double center = GetImgSize() / 2;
00556         in[0] = 0;
00557         in[1] = xx - center;
00558         in[2] = yy - center;
00559         in[3] = 0;
00560 
00561         transf1->MultiplyPoint(in,out);
00562         transf1->Delete();
00563 
00564         X = out[0] + GetVtkmprbasedata()->GetX() ;
00565         Y = out[1] + GetVtkmprbasedata()->GetY() ;
00566         Z = out[2] + GetVtkmprbasedata()->GetZ() ;
00567 }
00568 

Generated on 20 Oct 2010 for creaMaracasVisu_lib by  doxygen 1.6.1