How to subclass vtkActor

不羁岁月 提交于 2019-12-12 14:46:10

问题


I wanted to be able to access my underlaying data structure when I pick a vtkActor. A class derived from vtkActor holding a ptr to my data structure seemed the easiest approach.

I get the subclass to compile just fine but the actor does not seem to be added to the renderer.

So, here's my class:

//.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)

In my rendering step: if I instantiate the actor with a vtkActor everything shows up as expected, picking works, etc...

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

But no actor is added if I use my Actor class

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

Nothing else changes in the code. Any ideas of what's wrong?


回答1:


So, it turns out that there are a bunch of functions that need to be overloaded and a couple touches of macro magic to get this to work.

I pasted below the example that's working for me now. It is mostly from the vtkFollower code (a derived class from vtkActor). Hope this helps!

    #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)


来源:https://stackoverflow.com/questions/20945017/how-to-subclass-vtkactor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!