[vtkusers] mouse interaction in SDI application, vtkRenderwindowInteractor??

circass circass at gmail.com
Fri Feb 12 02:10:43 EST 2010


If you want to use only two dimensional image interaction you have to set the
interactorstyle to imagestyle. i think it is enough for that. 

but if you want to handle the mouse interactions and use annotations on it
you need that class. you can use it in the same way just change the types of
variables and use it.


madness78 wrote:
> 
> Hello, vtkusers!
> Can anyone help me?
> Below code is to display 2D raw image in SDI application.
> The code works well.
> The problem is about mouse interaction.
> Once the image displays, mouse interaction is already applied (in 3
> dimensional coordinate).
> I don’t want to have mouse interaction in 3 dimensional coordinate.
> I tried to change it to my manual interaction.
> But I don’t know how to use “pvtkRenderWindowInteractor” MFC SDI
> application.
> 
> //vtkRenderWindow *pvtkRenderWindow = vtkRenderWindow::New();
> //pvtkRenderWindow->AddRenderer(pVOXA_Ver10View->GetRenderer());
> //vtkRenderWindowInteractor *pvtkRenderWindowInteractor =
> vtkRenderWindowInteractor::New();
> //pvtkRenderWindowInteractor->SetRenderWindow(pvtkRenderWindow);
> 
> //pvtkRenderWindowInteractor->Initialize();
> //pvtkRenderWindowInteractor->Start();
> 
> If I insert the code above, new renderwindow appear with image.
> Can any one help me?
> 
> void CVOXA_Ver10Doc::TestSlice(void){
> 
>         POSITION pos = this->GetFirstViewPosition();
>         CVOXA_Ver10View *pVOXA_Ver10View = NULL;
>         unsigned short max(0);
>         
>         if (pos)
>         {
>                pVOXA_Ver10View = (CVOXA_Ver10View *)GetNextView(pos);
>         }
>         else  // return
>         {
>                
>                ASSERT(FALSE);
>                return;
>         }
>         int dims[2];
>         this->pvtkImageReader1 = vtkImageReader::New();
>         this->pvtkImageReader1->SetFileName("Image.slice");
>         this->pvtkImageReader1->SetDataExtent(0,255,0,255,0,0);
>         this->pvtkImageReader1->SetFileDimensionality(2);
>         this->pvtkImageReader1->SetDataOrigin(0,0,0);
>         this->pvtkImageReader1->SetDataScalarTypeToUnsignedChar();
>         this->pvtkImageReader1-> SetDataByteOrderToLittleEndian();
>         this->pvtkImageReader1->Update();
> 
> this->pvtkImageActor=vtkImageActor::New();
>        
> this->pvtkImageActor->SetInput(this->pvtkImageReader1->GetOutput());
>         
>         pVOXA_Ver10View->GetRenderer()->SetBackground(1,1,1);
>         pVOXA_Ver10View->GetRenderer()->AddActor(this->pvtkImageActor);
>         pVOXA_Ver10View->GetRenderer()->ResetCamera();
> 
>         //vtkRenderWindow *pvtkRenderWindow = vtkRenderWindow::New();
>        //pvtkRenderWindow->AddRenderer(pVOXA_Ver10View->GetRenderer());
> 
>        //vtkRenderWindowInteractor *pvtkRenderWindowInteractor =
> vtkRenderWindowInteractor::New();
>        //pvtkRenderWindowInteractor->SetRenderWindow(pvtkRenderWindow);
> 
>        //pvtkRenderWindowInteractor->Initialize();
>        //pvtkRenderWindowInteractor->Start();
> 
>         pvtkImageReader1->Delete();
>         pvtkImageActor->Delete();
>         pvtkImageReader1 = NULL;
>         UpdateAllViews(FALSE); 
> }
> 
> //////////////////////////////////////////////////////////////////////////////////////////////////////////////
> I found this code. It works well.
> I want to have mouse interaction like this.
> But I don’t still know, how to modify the code to use in my SDI
> application.
> 
> void CVOXA_Ver10Doc::TestJpeg(void)
> {       
>   
>   vtkJPEGReader *pvtkJPEGReader = vtkJPEGReader::New();
>   pvtkJPEGReader->SetFileName ("DSC_0190.JPG");
>   pvtkJPEGReader->Update();
> 
>   // Picker to pick pixels
>   vtkPropPicker *pvtkPropPicker = vtkPropPicker::New();
>   pvtkPropPicker->PickFromListOn();
>  
>   // Give the picker a prop to pick
>   vtkImageViewer2 *pvtkImageViewer2 = vtkImageViewer2::New();
>   pvtkPropPicker->AddPickList(pvtkImageViewer2->GetImageActor());
>  
>   // Visualize
>   vtkRenderWindowInteractor *pvtkRenderWindowInteractor
> =vtkRenderWindowInteractor::New();
>   pvtkImageViewer2->SetInputConnection(pvtkJPEGReader->GetOutputPort());
>   pvtkImageViewer2->SetupInteractor(pvtkRenderWindowInteractor);
>   pvtkImageViewer2->SetSize(600,600);
>   vtkRenderer* pvktRenderer = pvtkImageViewer2->GetRenderer();
> 
>   pvktRenderer->ResetCamera();
>   pvktRenderer->GradientBackgroundOn();
>   pvktRenderer->SetBackground(0,0,0);
>   pvktRenderer->SetBackground2(1,1,1);
>  
>  
> 
>   // Annotate the image with window/level and mouse over pixel information
>   vtkCornerAnnotation *pvtkCornerAnnotation = vtkCornerAnnotation::New();
>   pvtkCornerAnnotation->SetLinearFontScaleFactor( 2 );
>   pvtkCornerAnnotation->SetNonlinearFontScaleFactor( 1 );
>   pvtkCornerAnnotation->SetMaximumFontSize( 20 );
>   pvtkCornerAnnotation->SetText( 0, "Off Image" );
>   pvtkCornerAnnotation->SetText( 3, "<window>\n<level>" );
>   pvtkCornerAnnotation->GetTextProperty()->SetColor( 1,0,0);
>  
>   pvtkImageViewer2->GetRenderer()->AddViewProp(pvtkCornerAnnotation);
>  
>   // Callback listens to MouseMoveEvents invoked by the interactor's style
>   vtkImageInteractionCallback
> *Callback=vtkImageInteractionCallback::New();
>   Callback->SetViewer(pvtkImageViewer2);
>   Callback->SetAnnotation(pvtkCornerAnnotation );
>   Callback->SetPicker(pvtkPropPicker);
>  
>   // InteractorStyleImage allows for the following controls:
>   // 1) middle mouse + move = camera pan
>   // 2) left mouse + move = window/level
>   // 3) right mouse + move = camera zoom
>   // 4) middle mouse wheel scroll = zoom
>   // 5) 'r' = reset window/level
>   // 6) shift + 'r' = reset camera
>   vtkInteractorStyleImage* imageStyle =
> pvtkImageViewer2->GetInteractorStyle();
>   imageStyle->AddObserver( vtkCommand::MouseMoveEvent, Callback );
>  
>   pvtkRenderWindowInteractor->Initialize();
>   pvtkRenderWindowInteractor->Start();
>  
> }
> 
> class vtkImageInteractionCallback :            public vtkCommand
> {
> public:
>              //vtkImageInteractionCallback(void);
>              //~vtkImageInteractionCallback(void);
> 
>               static vtkImageInteractionCallback *New() 
>     {
>       return new vtkImageInteractionCallback; 
>     }
>  
>     vtkImageInteractionCallback() 
>     {
>       this->Viewer = 0;
>       this->Picker = 0;
>       this->Annotation = 0;
> 
>       this->PointData = vtkPointData::New();
>     }
>  
>     ~vtkImageInteractionCallback()
>     {
>       this->Viewer = 0;
>       this->Picker = 0;
>       this->Annotation = 0;
>       this->PointData->Delete();
>     }
>  
>     void SetPicker(vtkPropPicker *picker) 
>     {
>       this->Picker = picker; 
>     }
>  
>     void SetAnnotation(vtkCornerAnnotation *annotation) 
>     {
>         this->Annotation = annotation; 
>     }
>  
>     void SetViewer(vtkImageViewer2 *viewer) 
>     {
>       this->Viewer = viewer; 
>     }
>  
>  
>     virtual void Execute(vtkObject *, unsigned long event, void *)
>     {
>       this->Viewer;
>       vtkRenderWindowInteractor *interactor =
> this->Viewer->GetRenderWindow()->GetInteractor();
>       vtkRenderer* renderer = this->Viewer->GetRenderer();
>       vtkImageActor* actor = this->Viewer->GetImageActor();
>       vtkImageData* image = this->Viewer->GetInput();
>       vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
>           interactor->GetInteractorStyle());
>  
>       image->Update();
>  
>       // Pick at the mouse location provided by the interactor
>       this->Picker->Pick( interactor->GetEventPosition()[0],
>                           interactor->GetEventPosition()[1], 0.0, renderer
> );
>  
>       // There could be other props assigned to this picker, so 
>       // make sure we picked the image actor
>       vtkAssemblyPath* path = this->Picker->GetPath();
>       bool validPick = false;
>  
>       if( path )
>         {
>         vtkCollectionSimpleIterator sit;
>         path->InitTraversal( sit );
>         vtkAssemblyNode *node;
>         for( int i = 0; i < path->GetNumberOfItems() && !validPick; ++i )
>           {
>           node = path->GetNextNode( sit );
>           if( actor == vtkImageActor::SafeDownCast( node->GetViewProp() )
> )
>             {
>             validPick = true;
>             }
>           }
>         }
>  
>       if( !validPick )
>         {
>         this->Annotation->SetText( 0, "Off Image" );
>         interactor->Render();
>         // Pass the event further on
>         style->OnMouseMove();
>         return;
>         }
>  
>       // Get the world coordinates of the pick
>       double pos[3];
>       this->Picker->GetPickPosition( pos );
>       // Fixes some numerical problems with the picking
>       double *bounds = actor->GetDisplayBounds();
>       int axis = this->Viewer->GetSliceOrientation();
>       pos[axis] = bounds[2*axis];
>  
>       vtkPointData* pd = image->GetPointData();
>       if( !pd )
>         {
>         return;
>         }
>  
>       this->PointData->InterpolateAllocate( pd, 1, 1 );
>  
>       // Use tolerance as a function of size of source data
>       double tol2 = image->GetLength();
>       tol2 = tol2 ? tol2*tol2 / 1000.0 : 0.001;
>  
>       // Find the cell that contains pos
>       int subId;
>       double pcoords[3], weights[8];
>       vtkCell* cell = image->FindAndGetCell(
>           pos, NULL, -1, tol2, subId, pcoords, weights );
>       if( cell )
>         {
>         // Interpolate the point data
>         this->PointData->InterpolatePoint( pd, 0, cell->PointIds, weights
> );
>         int components =
> this->PointData->GetScalars()->GetNumberOfComponents();
>         double* tuple = this->PointData->GetScalars()->GetTuple( 0 );
>  
>         vtkStdString message = "Location: ( ";
>         message += vtkVariant( pos[0] ).ToString();
>         message += ", ";
>         message += vtkVariant( pos[1] ).ToString();
>         message += ", ";
>         message += vtkVariant( pos[2] ).ToString();
>         message += " )\nValue: ( ";
>  
>         for( int c = 0; c < components; ++c )
>           {
>           message += vtkVariant( tuple[ c ] ).ToString();
>           if( c != components - 1 ) 
>             {
>             message += ", ";
>             }
>           }
>         message += " )";
>         this->Annotation->SetText( 0, message.c_str() );
>         interactor->Render();
>         style->OnMouseMove();
>         }
>     }
>  
>   private:
>  
>   // Pointer to the viewer
>   vtkImageViewer2 *Viewer;
>  
>   // Pointer to the picker
>   vtkPropPicker *Picker;
>  
>   // Pointer to the annotation
>   vtkCornerAnnotation *Annotation;
>  
>   // Interpolator
>   vtkPointData* PointData; 
> };
> 
> 
> 
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/mouse-interaction-in-SDI-application%2C-vtkRenderwindowInteractor---tp27558047p27559620.html
Sent from the VTK - Users mailing list archive at Nabble.com.




More information about the vtkusers mailing list