[vtkusers] Pick points from polydata C#

gajo gajo.ic at gmail.com
Tue Aug 18 06:19:49 EDT 2015


Hi,
  I'm just learning about how VTK/ActiViz works, so this might be the
dumbest question, but I'd appreciate it very much if anyone could help (at
least point me in the direction of some code or documentation). I'd like to
do a dumbest thing ever on a polydata set I've loaded and that is to
select/pick a couple of points (since it's multiple points I'm guessing I
should have some button interfaces to let choose which point should be
selected once the polydata model is left clicked)?
  But I'm having trouble even picking one point with this code and the
solutions seem to smell:

  ...

            vtkInteractorStyleTrackballActor interactorStyle =
vtkInteractorStyleTrackballActor.New();
            interactorStyle.RightButtonPressEvt += new
vtkObject.vtkObjectEventHandler(interactor_LeftButtonPressEvt);
           
renderWindow.GetInteractor().SetInteractorStyle(interactorStyle);
            renderWindow.GetInteractor().SetRenderWindow(renderWindow);  //
seems too much?
            renderWindow.GetInteractor().Start();

  ...

        private void interactor_LeftButtonPressEvt(vtkObject sender,
vtkObjectEventArgs e)
        {
            vtkInteractorStyleTrackballActor interactor = sender as
vtkInteractorStyleTrackballActor;
            int x = interactor.GetInteractor().GetEventPosition()[0]; //
works
            int y = interactor.GetInteractor().GetEventPosition()[1]; //
works
            interactor.FindPokedRenderer(x, y); // returns true

            var picker = vtkPointPicker.New(); // how to connect this picker
from event handler to polydata
            
            picker.Pick(interactor.GetInteractor().GetEventPosition()[0],
                       interactor.GetInteractor().GetEventPosition()[1],
                       0,  // always zero.
                      
interactor.GetInteractor().GetRenderWindow().GetRenderers().GetFirstRenderer());

            if (picker.GetPointId() >= 0)
            {

                int SelectedPoint = picker.GetPointId();
                Console.WriteLine("Dragging point " + SelectedPoint);

                vtkPolyData Data = e as vtkPolyData; // not working, but I'd
like there be a way like this

                IntPtr pP =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * 3);

                Data.GetPoint(SelectedPoint, pP);

                double[] p = new double[3];

                Marshal.Copy(pP, p, 0, 3);
                Marshal.FreeHGlobal(pP);

                Console.WriteLine("p: " + p[0] + " " + p[1] + " " + p[2]);
            }
        }

What I can't comprehend is that the various interactors seem quite oblivious
to what they are being called upon to event handle (i.e. I can't reach the
polyData I'm trying to pick and determine it's world coordinate points so I
can later perform distance calculations from the EventArgs object in its
event handler code)? I'm guessing this could be or has already been done
with a widget, but I didn't see any C# code on how to use them, and I also
tried to react to a pick event of a vtkPointPicker but that event is not
occurring, I'm guessing it should be initiated by a interactor
programmatically first (only I can't reach the polydata I'd like to pick
points from within the event handler code - and making a standalone class
for it and passing the polydata to it somehow or even worse making global
variables for polydata seems kinda code smelly to me - at least compared to
what I've come to be familiar when using WinForms). Could anyone please
provide any C# code, even using widgets, which I can't figure out?
TIA  




--
View this message in context: http://vtk.1045678.n5.nabble.com/Pick-points-from-polydata-C-tp5733536.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list