[vtkusers] vtkPolyLine - Add new points dynamically?

Eric E. Monson emonson at cs.duke.edu
Sat Jul 31 13:40:11 EDT 2010


Hey Markus,

I'm on vacation, but I see nobody else replied. The vtkTemporalPathLineFilter is really for data that VTK sees has time steps already. I just mentioned it in case they way they do it internally might help you since it has a lot of nice features. (You could also try it in ParaView if you have your data saved out as a time series.)

There are probably many ways to do this, and I haven't tried before, but if I was naively going to try adding points dynamically to polydata I would do it like this (python script, sorry not Java, following). It's probably not optimal for speed since it's allocating as it's going with the insertions, but it works, and someone else is welcome to chime in with a better method. You could also just create a new polydata each time using your internal list of point coordinates, knowing how many points you need each time to pre-allocate memory, but I don't really know what the pros and cons of each method would be. Creating a new polydata each time will probably be better if you're wanting to do some post-processing to simplify the earlier-time line segments or drop points from the end. 

Here's my attempt that works at least for a small example (but doesn't limit the line length, or any of those more advanced features that you want):

# --------------------
# Adapted from Examples/DataManipulation/Python/CreateStrip.py

import vtk
import random

# First we'll create an initial point to build on
points = vtk.vtkPoints()
points.InsertPoint(0, 0.0, 0.0, 0.0)

# The cell array can be thought of as a connectivity list.  Here we
# specify the number of points followed by that number of point
# ids. This can be repeated as many times as there are primitives in
# the list.
# This first one is just a point to connect to as we add more points
lines = vtk.vtkCellArray()
lines.InsertNextCell(1) # number of points
lines.InsertCellPoint(0)

track = vtk.vtkPolyData()
track.SetPoints(points)
track.SetLines(lines)

mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(track)

actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(0.3800, 0.7000, 0.1600)

# Create the usual rendering stuff.
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Add the actors to the renderer, set the background and size
ren.AddActor(actor)

ren.SetBackground(1, 1, 1)
renWin.SetSize(250, 250)

iren.Initialize()
renWin.Render()

# Loop to add points to line
for ii in range(1,1000):
	prev_pt = points.GetPoint(ii-1)
	new_x = prev_pt[0] + (2.0*random.random()-1.0)
	new_y = prev_pt[1] + (2.0*random.random()-1.0)
	new_z = prev_pt[2] + (2.0*random.random()-1.0)
	
	# Insert the new point
	# then tell VTK that the data has been modified so the Render() call
	# will update the view with the new data
	points.InsertPoint(ii, new_x, new_y, new_z)
	points.Modified()
	
	# To get lines we need a beginning and end point in the connectivity list,
	# then again tell VTK that the data has been modified
	lines.InsertNextCell(2)
	lines.InsertCellPoint(ii-1)
	lines.InsertCellPoint(ii)
	lines.Modified()
	
	ren.ResetCamera()
	renWin.Render()

iren.Start()
# --------------------

Talk to you later,
-Eric


On Jul 26, 2010, at 1:10 PM, Markus Ott wrote:

> Thanks Eric, this sounds great.
> 
> I tried it but think i made a mistake somewhere.
> Does anybody see my error? (Java snippet below)
> 
> 		points = new vtkPoints();
> 	
> 		double origin[] = {0.0, 0.3, 0.1};
> 		double p0[] = {1.0, 0.5, 0.8};
> 		double p1[] = {0.3, 0.4, 0.7};
> 		double p2[] = {0.4, 0.2, 0.2};
> 		double p3[] = {0.4, 0.8, 0.3};
> 		
> 		points.InsertNextPoint(origin);
> 		points.InsertNextPoint(p0);
> 		points.InsertNextPoint(p1);
> 		points.InsertNextPoint(p2);
> 		points.InsertNextPoint(p3);
> 		
> 		polyData = new vtkPolyData();
> 		polyData.SetPoints(points);
> 		polyData.BuildCells();
> 		
> 		path = new vtkTemporalPathLineFilter();
> 		path.UsePointIndexForIdsOn();
> 		path.SetInput(polyData);
> 	
> 
> 		vtkPolyDataMapper mapper = new vtkPolyDataMapper();
> 		mapper.SetInputConnection(path.GetOutputPort());
> 	
> 		traceActor = new vtkActor();
> 		traceActor.SetMapper(mapper);	
> 		traceActor.VisibilityOn();		
> 
> 		VisualizationPanel.getInstance().addActor(traceActor);
> 
> Eric E. Monson schrieb:
>> Hey Markus,
>> 
>> I don't know if this will quite work for your application, but in case you didn't know about it, there is the vtkTemporalPathLineFilter:
>> 
>> http://www.vtk.org/doc/nightly/html/classvtkTemporalPathLineFilter.html
>> 
>> I haven't looked through the code, but it may help with your methods. I use it in ParaView all the time to trace out particle paths in time-dependent data sets.
>> 
>> -Eric
>> 
>> ------------------------------------------------------
>> Eric E Monson
>> Duke Visualization Technology Group
>> 
>> 
>> On Jul 26, 2010, at 10:15 AM, Markus Ott wrote:
>> 
>>> Hi vtkusers.
>>> 
>>> What i want to do is some kind of real time position tracing. An object
>>> moves through 3d space and I want to trace this movement with a line.
>>> 
>>> I read the polyline example
>>> (http://vtk.org/Wiki/VTK/Examples/GeometricObjects/Display/PolyLine)
>>> 
>>> I need to dynamically add new points to the line in real time and show
>>> the changes immediately.
>>> 
>>> In the example the positions are already known before creating the line.
>>> How can i update this polyline, add new points and show the changes as
>>> the line updates?
>>> 
>>> Another thing: If I add more and more points the line will have too much
>>> points eventually. Can i filter out not needed points to keep the
>>> polydata simple?
>>> 
>>> Regards,
>>> Markus
>>> _______________________________________________
>>> 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
>> 
>> 
>> 
>> 
>> 




More information about the vtkusers mailing list