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 
00347         SetInteractorStyleImage( vtkInteractorStyleBaseView2D::New() );
00348 
00349         _interactorstyleplane2D = new vtkInteractorStylePlane2D();
00350         ((vtkInteractorStyleBaseView*)GetInteractorStyleBaseView())->AddInteractorStyleMaracas( _interactorstyleplane2D );
00351 
00352         vtkCamera *camera =_imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->GetActiveCamera();
00353         
00354         camera->SetViewUp               (       0                       ,       1                               ,       0       );
00355         camera->SetPosition             ((0+_sizeIma)/2 , (0+_sizeIma)/2        , 10000 ); 
00356         
00357         camera->SetFocalPoint   ((0+_sizeIma)/2 , (0+_sizeIma)/2        ,       0       ); 
00358         camera->SetClippingRange(0.01, 100000);
00359         camera->ComputeViewPlaneNormal();
00360         camera->SetParallelScale( _sizeIma/3.0 );
00361 
00362         // text information over the graphic window
00363         _vtkInfoTextImage                                               = new vtkInfoTextImage();
00364         _vtkInfoTextImageInteractorPlane2D              = new vtkInfoTextImageInteractorPlane2D();
00365         _vtkInfoTextImage->SetWxVtk2DBaseView(this);
00366         _vtkInfoTextImage->SetMarImageData(  GetVtkmprbasedata()->GetMarImageData() );
00367         _vtkInfoTextImageInteractorPlane2D->SetModelVtkInfoTextImage(_vtkInfoTextImage);        
00368         _vtkInfoTextImage->Configure();
00369         ((vtkInteractorStyleBaseView*)this->GetInteractorStyleBaseView())->AddInteractorStyleMaracas(_vtkInfoTextImageInteractorPlane2D);
00370 
00371 
00372 }
00373 
00374 //-------------------------------------------------------------------
00375 void vtkPlane2DView::HorizontalLine()
00376 {
00377 // Axe Horizontal
00378         _pts = vtkPoints::New();
00379         _pts->SetNumberOfPoints(2);
00380         _pts->SetPoint(0, -1000 , -1000 , -1000 );
00381         _pts->SetPoint(1,  1000 ,  1000 ,  1000 );
00382         vtkCellArray *lines = vtkCellArray::New();
00383         lines->InsertNextCell(2);
00384         lines->InsertCellPoint(0);
00385         lines->InsertCellPoint(1);
00386         _pd = vtkPolyData::New();
00387     _pd->SetPoints( _pts );
00388     _pd->SetLines( lines );
00389         lines->Delete();  //do not delete lines ??
00390         _lineActor                                      =       vtkActor::New();
00391     _lineMapper                                 =       vtkPolyDataMapper::New();
00392         _lineMapper->SetInput(_pd);
00393         _lineMapper->ImmediateModeRenderingOn();
00394         _lineActor->SetMapper(_lineMapper);
00395         _lineActor->GetProperty()->BackfaceCullingOn();
00396         _lineActor->GetProperty()->SetDiffuseColor(0,0,1);
00397         _lineActor->GetProperty()->SetLineWidth(2);
00398         _lineActor->GetProperty()->SetOpacity(0);
00399     _imageViewer2XYZ->GetVtkImageViewer2()->GetRenderer()->AddActor( _lineActor );
00400 }
00401 
00402 //-------------------------------------------------------------------
00403 void vtkPlane2DView::RotationStart()
00404 {
00405         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
00406         vtkTransform    *transform       = mprbasedata->GetTransformOrientation();
00407         _transform1->SetMatrix( transform->GetMatrix() );
00408 }
00409 //-------------------------------------------------------------------
00410 void vtkPlane2DView::RotationDrag(double vx, double vy, bool ok_v, bool ok_ang)
00411 { 
00412         if (ok_ang==false)
00413         {
00414                 _ang =sqrt( vx*vx + vy*vy ) / 1.5;
00415         }
00416 
00417         if (ok_v==false){
00418                 _vxb=-vy;
00419                 _vyb=vx;
00420         }
00421         _transform2->Identity();
00422         _transform2->RotateWXYZ(_ang,0,_vxb,_vyb);
00423 
00424         vtkMPRBaseData  *mprbasedata = (vtkMPRBaseData*)this->GetVtkBaseData();
00425         vtkTransform *transform          =      vtkTransform::New();
00426         transform->Identity();
00427         transform->Concatenate(_transform1);
00428         transform->Concatenate(_transform2);
00429         mprbasedata->InitTransformOrientation(transform);
00430         transform->Delete();
00431 
00432         // Refresh Horizontal Line
00433         _pts->SetPoint( 0 , (_sizeIma/2) - _vxb*2       , (_sizeIma/2) - _vyb*2 , 1 );
00434         _pts->SetPoint( 1 , (_sizeIma/2) + _vxb*2       , (_sizeIma/2) + _vyb*2 , 1 );
00435 //      RefreshCircleLine();
00436 }
00437 
00438 //-------------------------------------------------------------------
00439 void vtkPlane2DView::Refresh(  )
00440 {
00441         ExtractPlane();
00442         wxVtkBaseView::Refresh();
00443 }
00444 //-------------------------------------------------------------------
00445 void vtkPlane2DView::SetImgSize( int imgSize )
00446 {
00447         _sizeIma = imgSize;
00448 }
00449 //-------------------------------------------------------------------
00450 int vtkPlane2DView::GetImgSize()
00451 {
00452         return _sizeIma;
00453 }
00454 //-------------------------------------------------------------------
00455 int     vtkPlane2DView::GetActualSlice()  // virtual 
00456 {
00457         _cx = GetVtkmprbasedata()->GetX();
00458         _cy = GetVtkmprbasedata()->GetY();
00459         _cz = GetVtkmprbasedata()->GetZ();
00460         return 0;
00461 }
00462 
00463 //-------------------------------------------------------------------
00464 
00465 bool vtkPlane2DView::GetMipVisualization()
00466 {
00467         return _mip_visualization;
00468 }
00469 
00470 //-------------------------------------------------------------------
00471 int vtkPlane2DView::GetMipWidth()
00472 {
00473         return _mip_width;
00474 }
00475 
00476 //-------------------------------------------------------------------
00477 void vtkPlane2DView::SetActualSlice(int slice)  // Virtual
00478 {
00479         double dir=(double)slice/3.0;
00480         GetVtkmprbasedata()->SetX( _cx + (_n[0]*dir) );
00481         GetVtkmprbasedata()->SetY( _cy + (_n[1]*dir) );
00482         GetVtkmprbasedata()->SetZ( _cz + (_n[2]*dir) );
00483 }
00484 //---------------------------------------------------------------------------
00485 vtkInteractorStylePlane2D       *vtkPlane2DView::GetInteractorstyleplane2D()
00486 {
00487         return this->_interactorstyleplane2D;
00488 }
00489 //---------------------------------------------------------------------------
00490 void vtkPlane2DView::SetActive(bool active)
00491 {
00492         _active = active;
00493 }
00494 
00495 //---------------------------------------------------------------------------
00496 
00497 void vtkPlane2DView::SetMipVisualization(bool ok)
00498 {
00499         _mip_visualization=ok;
00500 }
00501 
00502 //---------------------------------------------------------------------------
00503 
00504 void vtkPlane2DView::SetMipWidth(int value)
00505 {
00506         _mip_width=value;
00507 }
00508 
00509 //---------------------------------------------------------------------------
00510 void vtkPlane2DView::SetVisibleLine(bool ok)
00511 {
00512         double opacity;
00513         if (ok==true)
00514         {
00515                 opacity=1;
00516         } else {
00517                 opacity=0;
00518         }
00519         _lineActor->GetProperty()->SetOpacity(opacity);
00520 }
00521 // ----------------------------------------------------------------------------
00522 void vtkPlane2DView::TransfromCoordViewWorld2(double &X, double &Y, double &Z)
00523 {
00524         double spc[3];
00525         GetVtkmprbasedata()->GetImageData()->GetSpacing(spc);
00526 
00527         double xx = X;
00528         double yy = Y;
00529         double zz = 0;
00530 
00531         TransFromCoordScreenToWorld(xx,yy,zz);
00532 
00533 
00534         vtkTransform *transf1 = vtkTransform::New();
00535         transf1->Identity();
00536         vtkTransform *transf2 = GetVtkmprbasedata()->GetTransformOrientation();
00537         transf1->Concatenate(transf2->GetMatrix());
00538         double in[4], out[4];
00539         double center = GetImgSize() / 2;
00540         in[0] = 0;
00541         in[1] = xx - center;
00542         in[2] = yy - center;
00543         in[3] = 0;
00544 
00545         transf1->MultiplyPoint(in,out);
00546         transf1->Delete();
00547 
00548         X = out[0] + GetVtkmprbasedata()->GetX() ;
00549         Y = out[1] + GetVtkmprbasedata()->GetY() ;
00550         Z = out[2] + GetVtkmprbasedata()->GetZ() ;
00551 
00552 }
00553 

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1