本文介绍了如何子类化vtkActor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择一个vtkActor时,我希望能够访问我的底层数据结构.从vtkActor派生一个对我的数据结构保持ptr的类似乎是最简单的方法.

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.

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

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

所以,这是我的课:

//.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,那么一切都会按预期显示,进行拾取等...

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();

但是如果我使用我的Actor类,则不会添加任何演员

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?

推荐答案

因此,事实证明,有一堆函数需要重载,并且需要执行一些宏魔术操作才能使其正常工作.

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.

我将下面的示例粘贴到现在对我有用的示例中.它主要来自vtkFollower代码(来自vtkActor的派生类).希望这会有所帮助!

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)

这篇关于如何子类化vtkActor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-02 04:30