[vtkusers] Picking help: how do I map from picked prop3d back to graph node or vertex?

Dennis N Bromley dbromley at uw.edu
Tue Aug 9 14:24:50 EDT 2011


Hi All,

I hope this is a newbie question.  I've searched the archives so I apologize
if I missed something.

I have a pipeline that renders a graph using spheres for vertices and the
tube filter for the edges.  I would like to be able to use a rubber band
interactor and select a few nodes or edges, map back to the original nodes
and edges in my graph, and do something intelligent with those nodes and
edges.  Pretty normal graph selection scenario, I would expect.  I would
LOVE it if I'm just being an idiot. :)  Also, any comments on my VTK code
are very much appreciated.  I'm a total VTK newbie.

I am currently having these problems.  All help is really, really
appreciated.
1. My interactor is obviously doing *something* because it draws the rubber
band line, but I don't get a selection callback from rubber banding nor does
it color the selected nodes (I do get a callback when I press 'p' in the
render window, though.)  So something is not quite right.
2. Assuming I can get that to work, how do I map back from one of the two
actors (vertex actor with sphere source glyph and edges with a tube filter)
back to the original nodes and edges?

Thanks everyone-
-denny-



-------------------------------------
--Code - I hope someone finds this useful as well as can find my bug. :)
--P.S. This is in C# for the Activiz VTK port, but all of the examples I've
seen port perfectly.  So if you're not a C# person, feel free to squint your
eyes and pretend it's python. :)
-------------------------------------

        vtkMutableDirectedGraph g;
        vtkGraphLayout layout;
        RenderWindowControl mRenWinCtrl;


        //initial load at the beginning of time.
        void Form2_Load(object sender, EventArgs e)
        {
            layout = vtkGraphLayout.New();
            var strategy = vtkForceDirectedLayoutStrategy.New();
            strategy.ThreeDimensionalLayoutOn();
            layout.SetLayoutStrategy(strategy);


            RefreshGraph();

            layout.SetInput(g);
            layout.Modified();
            layout.Update();

            SetupMyPipeline();


        }


        //this function just regenerates a new random graph when a button is
pressed.
        private void mCreateGraphButton_Click(object sender, EventArgs e)
        {
            RefreshGraph();

            layout.SetInput(g);
            layout.Modified();
            layout.Update();

            mRenWinCtrl.RenderWindow.Modified();
            mRenWinCtrl.RenderWindow.Render();
        }



        private void RefreshGraph()
        {
            /**************************************************************/
            g = new vtkMutableDirectedGraph();

            List<long> ids = new List<long>();
            for (int i = 0; i < 100; i++)
            {
                long id = g.AddVertex();
                ids.Add(id);
            }

            Random rand = new Random();
            long edgeCounter = 0;


             //randomly link up some vertices to make the topology
interesting
            foreach (long id1 in ids)
            {
                foreach (long id2 in ids)
                {
                    if (id1 != id2 && rand.NextDouble() < .01)
                    {
                        g.AddGraphEdge(id1, id2);
                        edgeCounter++;
                    }
                }
            }


        }


        private void SetupMyPipeline()
        {

            ///////////////////////////////
            //edge handling
            var edge_geom = vtkGraphToPolyData.New();
            edge_geom.SetInputConnection(layout.GetOutputPort());
            edge_geom.EdgeGlyphOutputOn();
            edge_geom.SetEdgeGlyphPosition(.5);
            edge_geom.Update();


            var tube_filter = vtkTubeFilter.New();
            tube_filter.SetInput(edge_geom.GetOutput());
            tube_filter.SetRadius(.01);
            tube_filter.SetNumberOfSides(10);


            var edge_mapper = vtkPolyDataMapper.New();
            edge_mapper.SetInput(tube_filter.GetOutput());
            edge_mapper.Update();

            var edge_actor = vtkActor.New();
            edge_actor.SetMapper(edge_mapper);
            edge_actor.GetProperty().SetColor(.4, .4, .6);
            //////////////////////////////////////////////




            ////////////////////////////////////
            //vertex handling
            var vertex_geom = vtkGraphToPoints.New();
            vertex_geom.SetInputConnection(layout.GetOutputPort());

            var vertex_sphere = vtkSphereSource.New();
            vertex_sphere.SetRadius(.025);

            var vertex_glyph = vtkGlyph3D.New();
            vertex_glyph.SetInputConnection(vertex_geom.GetOutputPort());
            vertex_glyph.SetSource(vertex_sphere.GetOutput());

            var vertex_mapper = vtkPolyDataMapper.New();
            vertex_mapper.SetInputConnection(vertex_glyph.GetOutputPort());

            var vertex_actor = vtkActor.New();
            vertex_actor.SetMapper(vertex_mapper);
            vertex_actor.GetProperty().SetColor(0, 0, 1);
            ///////////////////////////////////////////////////



            //render it
            var ren = vtkRenderer.New();
            ren.AddActor(edge_actor);
            ren.AddActor(vertex_actor);
            ren.ResetCamera();
            mRenWinCtrl.RenderWindow.AddRenderer(ren);


            //picking
            var picker = vtkAreaPicker.New();
            picker.PickEvt += new
vtkObject.vtkObjectEventHandler(picker_PickEvt);
            picker.AddPickList(edge_actor);
            picker.AddPickList(vertex_actor);
            mRenWinCtrl.RenderWindow.GetInteractor().SetPicker(picker);

            //interactor
            var interactorStyle = vtkInteractorStyleRubberBand3D.New();
            interactorStyle.SetPickColor(.5, .1, .9);

mRenWinCtrl.RenderWindow.GetInteractor().SetInteractorStyle(interactorStyle);

mRenWinCtrl.RenderWindow.GetInteractor().SetRenderWindow(mRenWinCtrl.RenderWindow);
            mRenWinCtrl.RenderWindow.GetInteractor().Start();

        }


        //this never gets called except when I press 'p' in the render
window.
        //But it never gets called via the rubber banding.
        void picker_PickEvt(vtkObject sender, vtkObjectEventArgs e)
        {
            vtkAreaPicker picker = sender as vtkAreaPicker;
            vtkProp3DCollection collection = picker.GetProp3Ds();
            int num = collection.GetNumberOfItems();
            for (int i = 0; i < num; i++)
            {
                var prop = collection.GetNextProp3D();
            }
        }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110809/45ba1025/attachment.htm>


More information about the vtkusers mailing list