2014-01-06 89 views
3

我想在選擇vtkActor時能夠訪問我的底層數據結構。從vtkActor派生出來的類將ptr保存到我的數據結構中似乎是最簡單的方法。如何繼承vtkActor

我得到的子類編譯得很好,但演員似乎並沒有被添加到渲染器。

所以,這裏是我的課:

//.h 
#include <vtkActor.h> 
#include <vtkObjectFactory.h> 

class Node; 

struct Actor : public vtkActor { 
    static Actor* New(); 
    vtkTypeMacro(Actor, vtkActor) 

    Node* holding_node; 
}; 

//.cpp 
#include "actor.h" 
vtkStandardNewMacro(Actor) 

在我的渲染步驟:如果我發起與vtkActor一切男主角顯示爲預期,採摘作品,等等

vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New(); 

但是,如果我使用我的話,沒有演員加入Actor

vtkSmartPointer<Actor> sphereActor = vtkSmartPointer<Actor>::New(); 

代碼中沒有其他更改。有什麼不對的想法?

回答

2

所以,事實證明,有一些功能需要重載,並且有一些功能需要使用宏魔法才能實現。

我粘貼在現在爲我工作的例子下。它主要來自vtkFollower代碼(來自vtkActor的派生類)。希望這可以幫助!

#include <vtkSmartPointer.h> 
    #include <vtkRenderer.h> 
    #include <vtkObjectFactory.h> 
    #include <vtkRenderingCoreModule.h> 
    #include <vtkProperty.h> 


    class Node; 

    class VTKRENDERINGCORE_EXPORT NodeActor : public vtkActor { 
     public: 
      vtkTypeMacro(NodeActor, vtkActor); 

     static NodeActor *New(); 

     virtual void ReleaseGraphicsResources(vtkWindow *window) { 
      this->Device->ReleaseGraphicsResources(window); 
      this->Superclass::ReleaseGraphicsResources(window); 
     } 

     virtual int RenderOpaqueGeometry(vtkViewport *viewport){ 
      if (! this->Mapper) { 
       return 0; 
      } 
      if (!this->Property) { 
       this->GetProperty(); 
      } 
      if (this->GetIsOpaque()) { 
       vtkRenderer *ren = static_cast<vtkRenderer *>(viewport); 
       this->Render(ren); 
       return 1; 
      } 
      return 0; 
     } 

     virtual int RenderTranslucentPolygonalGeometry(vtkViewport *viewport){ 
      if (! this->Mapper) { 
       return 0; 
      } 
      if (!this->Property) { 
       this->GetProperty(); 
      } 
      if (!this->GetIsOpaque()) { 
       vtkRenderer *ren = static_cast<vtkRenderer *>(viewport); 
       this->Render(ren); 
       return 1; 
      } 
      return 0; 
     } 

     virtual void Render(vtkRenderer *ren){ 
      this->Property->Render(this, ren); 
      this->Device->SetProperty (this->Property); 
      this->Property->Render(this, ren); 
      if (this->BackfaceProperty) { 
       this->BackfaceProperty->BackfaceRender(this, ren); 
       this->Device->SetBackfaceProperty(this->BackfaceProperty); 
      } 
      if (this->Texture) { 
       this->Texture->Render(ren); 
      } 
      this->ComputeMatrix(); 
      this->Device->SetUserMatrix(this->Matrix); 
      this->Device->Render(ren,this->Mapper); 
     } 

     void ShallowCopy(vtkProp *prop) { 
      NodeActor *f = NodeActor::SafeDownCast(prop); 
      this->vtkActor::ShallowCopy(prop); 
     } 

     //****************************************// 
     //    my member 
     //****************************************// 
     Node* node_i_represent{nullptr}; 

    protected: 
     vtkActor* Device; 

     NodeActor() { 
      this -> Device = vtkActor::New(); 
     } 

     ~NodeActor() { 
      this -> Device -> Delete(); 
     } 
    private: 
}; 

vtkStandardNewMacro(NodeActor)