当我选择vtkActor时,我希望能够访问我的底层数据结构。从vtkActor派生的类对我的数据结构具有ptr,这似乎是最简单的方法。

我得到了可以很好地编译的子类,但是actor似乎没有添加到渲染器中。

所以,这是我的课:

//.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实例化actor,那么一切都会按预期显示,进行拾取等。
vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();

但是如果我使用Actor类,则不会添加任何actor
vtkSmartPointer<Actor>    sphereActor = vtkSmartPointer<Actor>::New();

代码中没有其他更改。有什么问题的想法吗?

最佳答案

因此,事实证明,有许多功能需要重载,并且需要执行一些宏魔术操作才能使其正常工作。

我将下面的示例粘贴到现在为我工作的示例中。它主要来自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)

07-24 14:04