[vtkusers] (Python) Extracting strips from vtkStripper?

James Urquhart j_urquhart at btinternet.com
Sun Jan 4 13:41:00 EST 2004


Hi there,

I am currently using the vtkStripper  class in VTK to generate triangle strips 
for my 3d models.

However, i am pretty much unfamiliar with the python vtk bindings, so i have 
become stuck.
I seem to have gotten my data into VTK properly, as the stripper seems to 
function with my test mesh (generates 16 strips).
My question is,  how do i get the data *OUT* of VTK and back into python?
I need to get the strip indices (indexing into my mesh vertices), however i 
cannot seem to figure out a way to do it.

I can get the a vtkCellArray via :

vtkNewStrips = newmesh.GetStrips()

However getting the data out of the vtkCellArray is puzzling me. The only 
thing i can see that may be useful is :

stripData = vtkNewStrips.GetData()

Which seems to return a vtkIdTypeArray....  Confusing.

What i need is some example code or explanation to get my head round this :)

A summary of the code i am currently using is as follows :
	def windStrip(self):
		print "Entered windStrip"
		if self.mtype == self.T_Skin:
			self.verts = self.iverts
			self.normals = self.inormals

		# We want to decimate primitives per material index
		flags = self.calcFlags()

		stripLists = []
		newIndices = array('H')
		newPrimitives = []
		for i in range(0, len(flags)):
			stripLists.append(array('H'))

		# Dump triangles
		for p in self.primitives:
			for f in range(0, len(flags)):
				if flags[f] == p.matindex:
					idx = f
					break
			for ind in self.indices[p.firstElement:p.firstElement+p.numElements]:
				stripLists[idx].append(ind)

		count = 0
		# Use VTK to strip each set of primitives
		for s in stripLists:
			print "Strip %d" % (count)
			# Set up vtk
			# Copy in mesh data

			# Points
			points = vtk.vtkPoints()
			for st in s:
				vert = self.verts[st]
				points.InsertNextPoint(vert[0], vert[1], vert[2])

			# Faces (triangles in vert list)
			faces = vtk.vtkCellArray()
			for st in range(0, len(s) / 3):
				faces.InsertNextCell(3) # All triangles
				faces.InsertCellPoint(s[st])
				faces.InsertCellPoint(s[st+1])
				faces.InsertCellPoint(s[st+2])

			newmesh = None
			mesh = vtk.vtkPolyData()
			mesh.SetPoints(points)
			mesh.SetPolys(faces)

			if mesh.GetNumberOfPolys() > 0:
				strip = vtk.vtkStripper()
				strip.DebugOn()
				strip.SetInput(mesh)
				strip.SetMaximumLength(self.maxStripSize)
				strip.Update()
				newmesh = strip.GetOutput()
				del strip # clear stripper
			else:
				print "Oddity: Mesh has no polygons!"

			# Import data we got back from stripper. Must convert indices etc.
			if newmesh != None:
				vtkNewPts = newmesh.GetPoints()
				vtkNewStrips = newmesh.GetStrips()

				print "DEBUG: Stripped mesh has %d points and %d strips" % 
(newmesh.GetNumberOfPoints(), newmesh.GetNumberOfStrips())

				npoints = []
				for i in range(0, newmesh.GetNumberOfPoints()):
					vpt = vtkNewPts.GetPoint(i)
					npoints.append([vpt[0], vpt[1], vpt[2]])

				nstrips= [] # inds to points array
				vtkNewStrips.InitTraversal()
				stripData = vtkNewStrips.GetData()

				# TODO: We need to get the strip indices!

				# Now we need to get points -> verts
				# points should really exist. If not,
				# scream!

				# Change npoints into a set of indices
				# that map to the original mesh verts
				for i in range(0, len(npoints)):
					vert2 = npoints[i]
					for v in range(0, len(self.verts)):
						vert = self.verts[v]
						if (vert[0] == vert2[0]) and (vert[1] == vert2[1]) and (vert[2] == vert2
[2]):
							npoints[i] = v
							break
					print "ERROR: Could not find vert", vert2, "in mesh!"

				# Add in strips we found
				# Using indicie map
				for p in nstrips:
					newPrimitives.append(Primitive(len(newIndices), 3, flags[count]))
					for s in p:
						newIndices.append(npoints[s])

				del vtkNewPts
				del vtkNewStrips
				del newmesh
				del nstrips
				del npoints


			del points
			del faces
			del mesh
			count += 1

		print "Setting back primitive and indices"
		# Set the sum of our efforts to the new indices and primitives
		self.primitives = newPrimitives
		self.indices = newIndices

		# Set back skin verts
		if self.mtype == self.T_Skin:
			self.iverts = self.verts
			self.inormals = self.normals


Thanks for any help,
-James




More information about the vtkusers mailing list