VTK RotatingSphere

工程环境

visual studio 2019

项目模板

Cmake项目

Example

        程序功能:vtk动画,利用定时器以及回调函数来使球体围绕z轴旋转的动画。

代码:

RotatingSphere.cpp

// RotatingSphere.cpp: 定义应用程序的入口点。
//

#include "RotatingSphere.h"

#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>

class vtkTimerCallback2 : public vtkCommand
{
public:
    vtkTimerCallback2() = default;
    ~vtkTimerCallback2() = default;

    int timerId = 0;
    static vtkTimerCallback2* New()
    {
        vtkTimerCallback2* cb = new vtkTimerCallback2;
        cb->TimerCount = 0;
        return cb;
    }
    virtual void Execute(vtkObject* caller, unsigned long eventId,
        void* vtkNotUsed(callData))
    {
        vtkRenderWindowInteractor* iren =
            dynamic_cast<vtkRenderWindowInteractor*>(caller);
        if (vtkCommand::TimerEvent == eventId)
        {
            ++this->TimerCount;
        }
        if (TimerCount < 36)
        {
            actor->RotateZ(5);
            iren->GetRenderWindow()->Render();
        }
        else
        {
            iren->DestroyTimer();
        }
    }

private:
    int TimerCount = 0;

public:
    vtkActor* actor;
};

int main(int, char* [])
{
    vtkNew<vtkNamedColors> colors;

    // Create a sphere
    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->SetCenter(0.0, 0.0, 0.0);
    sphereSource->SetRadius(1.0);
    sphereSource->SetThetaResolution(15);
    sphereSource->SetPhiResolution(15);
    sphereSource->Update();

    // Create a mapper and actor
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(sphereSource->GetOutputPort());
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->RotateX(90);
    actor->GetProperty()->SetRepresentationToWireframe();

    // Create a renderer, render window, and interactor
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize(640, 480);

    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Add the actor to the scene
    renderer->AddActor(actor);

    renderer->SetBackground(colors->GetColor3d("Seashell").GetData());
    // Render and interact
    renderWindow->SetWindowName("RotatingSphere");
    renderWindow->Render();

    // Initialize must be called prior to creating timer events.
    renderWindowInteractor->Initialize();

    // Sign up to receive TimerEvent
    vtkNew<vtkTimerCallback2> cb;
    cb->actor = actor;

    renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);
    int timerId = renderWindowInteractor->CreateRepeatingTimer(100);
    cb->timerId = timerId;

    // Start the interaction and timer
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

说明

1、使用vtkNamedColors获取对应颜色的rgb。

 2、使用vtkSphereSource创建球体

     

 3、使用vtkRenderWindowInteractor交互器设置一个定时器。

      使用CreateRepeatingTimer创建一个100毫秒调一次的重复定时器

4、使用vtkTimerCallback2回调函数实现动画功能

      通过第三步设置的定时器重复触发TimerEvent事件来使球体actor绕z轴旋转5度,达到动画的效果。

GitHub地址

VTKExamples/Animation/RotatingSphere at main · 18810186103/VTKExamples · GitHub

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

混元太极马保国

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值