[vtkusers] Problems with classes in python (maybe a programming mistake)

Luca Penasa luca.penasa at email.it
Sat Jul 3 07:00:50 EDT 2010


Hi everybody, 
	I have some problems writing in python using vtk. Maybe is only a
programmin issue. 

I have implemented an extended version of vtkIdList for doing some tasks
i need (i am writing a segmentation algorithm for point clouds).
when i call the Region class from ipyhton everithing works as expected.
BUT when i create a new istance of the Region class from inside a
function it only work when the function is running. If the function
return multiple istances of the Region class what i obtain are istances
without attributes and functions I added. I really dont understand why!
I think is a simple python issue (i am not a good programmer).


this is my classes:

class Region(vtk.vtkIdList):
	def __init__(self):
		self.mean_value = 0
		
	def GetLastId(self):
		return self.GetId(self.GetNumberOfIds()-1)
	
	def GetFirstId(self):
		return self.GetId(0)
		
	def DeleteLastId(self):
		self.DeleteId(self.GetId(self.GetNumberOfIds()-1))
		
	def IsEmpty(self):
		'''
		return 0 if not empty, 1 if there are ids in list
		'''
		if self.GetNumberOfIds() == 0:
			return 1
		else:
			return 0
	
	def ExistId(self, id):
		'''
		return 0 if that id is not in list, 1 if exist
		'''
		#check if the list is empty, if so the specified id is obviuosly not
in list
		if self.GetNumberOfIds() == 0:
			return 0
		else:
			if self.IsId(id) == id:
				return 1
			else:
				return 0
				
	def SetMean(self, value):
		self.mean_value = value
					
	def GetMean(self):
		return self.mean_value


class RegionsContainer(vtk.vtkCollection):
	def CreateNewRegion(self):
		number_of_items = self.GetNumberOfItems()
		new_region = Region()
		self.AddItem(new_region)
		return self.GetItemAsObject(number_of_items)
		
	def SetAssociatedGrid(self, grid):
		self.AssociatedGrid = grid
	
	def GetAssociatedGrid(self):
		return self.AssociatedGrid
	
	def SetAssociatedArrayOfGrid(self, arrayname):
		self.AssociatedArrayOfGrid =
self.GetAssociatedGrid().GetPointData().GetArray(arrayname)
		
	def GetAssociatedArrayOfGrid(self):
		return self.AssociatedArrayOfGrid


in a function i called DoSegmentation i create a RegionContainer:
    
    regions = RegionsContainer()

then, in a while-loop i create multiple istances of my Region class:

    current_region = regions.CreateNewRegion()

then the code call some functions on this Region, like this:

    current_region.SetMean(current_mean)	

at the end my function returns the variable 'regions' that contain
multiple istances of the class Region()
BUT, when i try to have acces to the items contained, with;

    regions.GetItemAsObject(n)

i have only a vtkIdList as return, and not my Region() class

    if i try regions.GetItemAsObject(n).GetMean()

it raise an attribute error!!


anybody could spent some minutes and have a look at this code??
I think I made a mistake, but i dont know where...
Thank you!!

 
 
 --
 Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f
 
 Sponsor:
 Ultime 2 stanze dal 18 al 25 luglio con offerta speciale aquafan e oltremare per 4 giorni all'hotel Poker, 3 stelle, di Riccione
 Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=10633&d=3-7
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100703/435cc42b/attachment.htm>


More information about the vtkusers mailing list