[vtkusers] vtkCallbackCommand in ActiveViz

Jochen K. jochen.kling at email.de
Mon Jun 25 14:55:02 EDT 2012


Hi Dieter,

it took me the last three hours to figure it out. Whenever we should meet:
Toché! ;-)


Here we go:

      // Define interaction style
      public class InteractorStyle : vtkInteractorStyleRubberBandPick {
         private vtkPolyData Points;
         private vtkActor SelectedActor;
         private vtkDataSetMapper SelectedMapper;
         private bool IsInRubberbandMode;

         public InteractorStyle() {
            IsInRubberbandMode = false;
            this.SelectedMapper = vtkDataSetMapper.New();
            this.SelectedActor = vtkActor.New();
            this.SelectedActor.SetMapper(SelectedMapper);
            this.LeftButtonReleaseEvt += new
vtkObjectEventHandler(InteractorStyle_LeftButtonReleaseEvt);
            this.CharEvt += new
vtkObjectEventHandler(InteractorStyle_CharEvt);
         }


         void InteractorStyle_CharEvt(vtkObject sender, vtkObjectEventArgs
e) {
            // forward event to  base class
            base.OnChar();
            vtkInteractorStyleRubberBandPick caller = e.Caller as
vtkInteractorStyleRubberBandPick;
            if(caller != null) {
               switch(caller.GetInteractor().GetKeyCode()) {
                  // r oder R is pressed
                  case 82:
                  case 114:
                     IsInRubberbandMode = !IsInRubberbandMode;
                     break;
               }
            }
         }


         void InteractorStyle_LeftButtonReleaseEvt(vtkObject sender,
vtkObjectEventArgs e) {
            // forward event to  base class
            base.OnLeftButtonUp();
            if(!IsInRubberbandMode)
               return;
            Debug.WriteLine("InteractorStyle_LeftButtonReleaseEvt");
            vtkPlanes frustum = (
(vtkAreaPicker)this.GetInteractor().GetPicker() ).GetFrustum();

            vtkExtractGeometry extractGeometry = vtkExtractGeometry.New();
            extractGeometry.SetImplicitFunction(frustum);
            extractGeometry.SetInput(this.Points);
            extractGeometry.Update();

            vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
            glyphFilter.SetInputConnection(extractGeometry.GetOutputPort());
            glyphFilter.Update();

            vtkPolyData selected = glyphFilter.GetOutput();
            Debug.WriteLine("Selected " + selected.GetNumberOfPoints() + "
points.");
            Debug.WriteLine("Selected " + selected.GetNumberOfCells() + "
cells.");
            this.SelectedMapper.SetInput(selected);
            this.SelectedMapper.ScalarVisibilityOff();

            vtkIdTypeArray ids =
vtkIdTypeArray.SafeDownCast(selected.GetPointData().GetArray("OriginalIds"));
            for(int i = 0; i < ids.GetNumberOfTuples(); i++) {
               Debug.WriteLine("Id " + i + " : " + ids.GetValue(i));
            }

            this.SelectedActor.GetProperty().SetColor(1.0, 0.0, 0.0);
//(R,G,B)
            this.SelectedActor.GetProperty().SetPointSize(5);

            base.GetCurrentRenderer().AddActor(SelectedActor);
            this.GetInteractor().GetRenderWindow().Render();
            this.HighlightProp(null);
         }


         public void SetPoints(vtkPolyData points) {
            this.Points = points;
         }
      };

      private void HighLightPoints() { 
         vtkPointSource pointSource = vtkPointSource.New();
         pointSource.SetNumberOfPoints(20);
         pointSource.Update();

         vtkIdFilter idFilter = vtkIdFilter.New();
         idFilter.SetInputConnection(pointSource.GetOutputPort());
         idFilter.SetIdsArrayName("OriginalIds");
         idFilter.Update();

         vtkDataSetSurfaceFilter surfaceFilter =
vtkDataSetSurfaceFilter.New();
         surfaceFilter.SetInputConnection(idFilter.GetOutputPort());
         surfaceFilter.Update();

         vtkPolyData input = surfaceFilter.GetOutput();

         // Create a mapper and actor
         vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
         mapper.SetInputConnection(input.GetProducerPort());
         mapper.ScalarVisibilityOff();

         vtkActor actor = vtkActor.New();
         actor.SetMapper(mapper);
         actor.GetProperty().SetPointSize(5);

         // Visualize
         // get a reference to the renderwindow of our renderWindowControl1
         vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
         // renderer
         vtkRenderer renderer =
renderWindow.GetRenderers().GetFirstRenderer();
         // set background color
         renderer.SetBackground(.2, .3, .4);

         // tell interactor to use an area picker
         vtkAreaPicker areaPicker = vtkAreaPicker.New();
         vtkRenderWindowInteractor renderWindowInteractor =
renderWindow.GetInteractor();
         renderWindowInteractor.SetPicker(areaPicker);


         // tell interactor to use our own style inherit from
vtkInteractorStyleRubberBandPick
         InteractorStyle style = new InteractorStyle();
         
         // give our style a reference to the polydata
         style.SetPoints(input);
         renderWindowInteractor.SetInteractorStyle(style);

         renderer.AddActor(actor);
         renderWindow.Render();
      }



I'm a bit confused because inheritance from vtkInteractorStyleRubberBandPick
is working more or less except methods can't be overridden. I will
investigate further in a calm hour.


with best regards

Jochen

--
View this message in context: http://vtk.1045678.n5.nabble.com/vtkCallbackCommand-in-ActiveViz-tp5714143p5714204.html
Sent from the VTK - Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120625/cf38bc59/attachment.htm>


More information about the vtkusers mailing list