[vtkusers] Converting Python code to C++

Brad King brad.king at kitware.com
Mon Feb 16 18:04:33 EST 2004


Vetria Byrd wrote:
> I am trying to convert the following call back functions written in python C++
> 
> # Callback functions that actually generate streamlines.
> def BeginInteraction(obj, event):
>     global streamline
>     streamline.VisibilityOn() 
> 
> def GenerateStreamlines(obj, event):
>     global seeds, renWin
>     obj.GetPolyData(seeds)
>     renWin.Render()
> 
> def BeginInteraction2(obj, event):
>     global streamline2
>     streamline2.VisibilityOn()
> 
> def GenerateStreamlines2(obj, event):
>     global seeds2, renWin
>     obj.GetPolyData(seeds2)
>     renWin.Render()
> 
> C++ code:
> 
> //globals
> vtkActor *streamline = vtkActor::New()
> ;
> vtkPolyData *seeds = vtkPolyData::New()
> ;
> vtkRenderWindow *renWin = vtkRenderWindow::New()
> ;
> vtkActor *streamline2 = vtkActor::New()
> ;
> vtkPolyData *seeds2 = vtkPolyData::New()
> ;
> 
> class vtkMyCallback : public vtkCommand
> {
> public:
>   static vtkMyCallback *New() 
>     { return new vtkMyCallback; }
>   
>   virtual void BeginInteraction(vtkObject obj, unsigned long ) {
>       streamline->VisibilityOn() ;
> }
> 
>   virtual void GenerateStreamlines(vtkObject *obj, unsigned long ){
>     obj->GetPolyData(seeds)
> ;
>     renWin->Render();
> }
> 
>   virtual void BeginInteraction2(vtkObject *obj, unsigned long ) {
>     streamline2->VisibilityOn();
> }
> 
>   virtual void GenerateStreamlines2(vtkObject obj, unsigned long ) {
>      obj->GetPolyData(seeds2);
>     renWin->Render()
>  ; }
>   
> };
> 
> 
> In the program:
> :
> :
> vtkMyCallback *BeginInteraction = vtkMyCallback::New();
> vtkMyCallback *GenerateStreamlines = vtkMyCallback::New();
> vtkMyCallback *BeginInteraction2 = vtkMyCallback::New();
> vtkMyCallback *GenerateStreamlines2 = vtkMyCallback::New();
> 
> 
> // Associate the line widget with the interactor and setup callbacks.
> :
> :
> lineWidget->AddObserver(vtkCommand::StartInteractionEvent, BeginInteraction)
> ;
> lineWidget->AddObserver(vtkCommand::InteractionEvent, GenerateStreamlines)
> ;
> 
> :
> :
> lineWidget2->AddObserver(vtkCommand::StartInteractionEvent, BeginInteraction2)
> ;
> lineWidget2->AddObserver(vtkCommand::EndInteractionEvent, GenerateStreamlines2)
> ;
> 
> 
> I am getting the following error messages:
> 
> In method `void vtkMyCallback::GenerateStreamlines(vtkObject *, long unsigned int)':
> no matching function for call to `vtkObject::GetPolyData (vtkPolyData *)'
> 
> In method `void vtkMyCallback::GenerateStreamlines2(vtkObject *, long unsigned int)':
> no matching function for call to `vtkObject::GetPolyData (vtkPolyData *)'

Python is a runtime language, so the callbacks get the objects 
downcasted automatically.  In C++, you have to downcast by hand when 
implementing the callbacks:

virtual void GenerateStreamlines(vtkObject *obj, unsigned long )
{
   vtkLineWidget* lw = vtkLineWidget::SafeDownCast(obj);
   if(lw)
     {
     lw->GetPolyData(seeds);
     }
}

-Brad




More information about the vtkusers mailing list