[vtkusers] Python vtkSphere Subclass

David Gobbi david.gobbi at gmail.com
Sun Dec 9 15:25:03 EST 2012


Hi Ryan,

This won't work, because your EvaluateFunction() and EvalutateGradient()
methods will never get called.  Overriding virtual methods through python
doesn't work in the VTK wrappers.  Maybe it will work at some point in
the future.

For sources and filters, people use vtkProgrammableSource and
vtkProgrammableFilter to do what you are trying to do.  You could use
these as an example and write a vtkProgrammableImplicitFunction
(in C++) that you could then use as a "hook" to write implicit functions
in python.

 - David

On Sun, Dec 9, 2012 at 12:05 PM, Ryan Irvin <ryan.irvin at gmail.com> wrote:
> I'm trying to create a custom implicit surface function to represent a conic
> asphere and my approach was to try and subclass vtkSphere.  I had previously
> tried to subclass vtkImplicitFunction without success and saw some posts
> about not being able to subclass abstract classes in Python (I don't know if
> this is still true).
>
> The subclass I've put together is shown below, where the attribute surf_obj
> is a python class representing the conic asphere and contains methods for
> evaluating the surface function and gradient.
>
> When I run my full script, the result is that the default vtkSphere is
> always rendered.  It's pretty apparent that I'm not understanding the proper
> approach to this problem and/or am not properly subclassing vtkSphere.
>
> I will be happy to provide the full *.py file which contains all of the code
> but wasn't sure of the protocol and have not provided it with this post.
>
> If anyone has any suggestions about the correct way to approach this problem
> or can provide some advice for properly subclassing, I would very much
> appreciate it.
>
> -Ryan
>
>
> class VtkImplicitConic( vtk.vtkSphere ):
>
>     def __init__( self, con_surf ):
>         self.surf_obj = con_surf
>         self.SetRadius( self.surf_obj.GetRadius() )
>
>
>     def EvaluateFunction( self, x, y, z ):
>         return self.surf_obj.EvaluateFunction( x, y, z )
>
>
>     def EvaluateGradient( self, x, n ):
>         n[0],n[1],n[2] = self.surf_obj.EvaluateGradient( x[0], x[1], x[2] )
>
>
>     def GetRadius( self ):
>         return self.surf_obj.GetRadius()
>
>
>     def SetRadius( self, radius ):
>         self.surf_obj.SetRadius( radius )



More information about the vtkusers mailing list