[vtkusers] vtkgraph and adding properties/fields to a vertex/node

Jeff Baumes jeff.baumes at kitware.com
Thu Apr 26 12:22:16 EDT 2012


Vertex (and edge) "handles" in VTK are simply indices that range from 0 to
#verts - 1. To access properties, you must go to the vertex (and edge)
data, which is simply a collection of arrays that should be the same
length. The developer generally creates and fills these arrays manually, so
some extra care is usually needed to make sure things match up correctly.

When removing vertices or edges, the removed indices make "holes" in the
vertex or edge data, which are filled by moving the highest-index vertices
or edges to those indices. This means that vertex/edge "handles" may become
invalid after each removal.

Here is a python session that might clarify things some.

First, create a graph with three vertices (note AddVertex returns the
created vertex index):

>>> import vtk
>>> g = vtk.vtkMutableDirectedGraph()
>>> g.AddVertex()
0L
>>> g.AddVertex()
1L
>>> g.AddVertex()
2L

Create a name property and fill it with three values to match the three
vertices:

>>> name = vtk.vtkStringArray()
>>> name.InsertNextValue("a")
0L
>>> name.InsertNextValue("b")
1L
>>> name.InsertNextValue("c")
2L

Give the property a name and add to vertex data:
>>> name.SetName("name")
>>> g.GetVertexData().AddArray(name)

Test that removing vertex 0 (named "a") decreases the length of our
property array and moves vertex 2 (named "c") to position 0:
>>> name.GetNumberOfTuples()
3L
>>> g.RemoveVertex(0)
>>> name.GetNumberOfTuples()
2L
>>> name.GetValue(0)
'c'
>>> name.GetValue(1)
'b'

On Mon, Apr 23, 2012 at 7:56 PM, planar3d <planar3d at gmail.com> wrote:

> I can't figure out how to do this. I have a vtkgraph and I need to be able
> to
> get a vertex by its unique identifier (aka vtkIdType) and add
> properties/fields like an int or a string to it. I need to be able to
> modify
> these fields as I pass through the graph and perform various
> transformations
> on it like adding and deleting nodes.
>
> As far as I can tell, you can sort of define fields by declaring these
> property arrays as seen in the example here:
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/Graphs/LabelVerticesAndEdges
>
> The problem with this is I see no correlation/synchronization between the
> nodes/vertexID and these property arrays. The arrays have to be created up
> front and then added to the vtkgraph. So I declare an array of NumVertices,
> call it "weights" or "color" and then add it to the graph but what happens
> when I call vtkgraph.removeVertex(ID)? A vertex is deleted but that array
> is
> still NumVertices in size. Does the removeVertex function also delete the
> corresponding vertex entry in array? I see no enforcement of this in
> documentation? Do I have to manually update the property arrays whenever I
> remove a vertex?
>
> Also, I see no vtkgraph.getVertex(vtkIdType id) function and I see no way
> to
> modify the properties/attributes of that vertex even if I had a handle to
> said vertex. The GetVertexData function returns a vtkDataSetAttributes
> array. I don't need a giant attributes array for all the vertices. I just
> want the properties/attributes for one vertex so I can change/update some
> of
> the values of those attributes.
>
> --
> View this message in context:
> http://vtk.1045678.n5.nabble.com/vtkgraph-and-adding-properties-fields-to-a-vertex-node-tp5660744p5660744.html
> Sent from the VTK - Users mailing list archive at Nabble.com.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120426/283043f3/attachment.htm>


More information about the vtkusers mailing list