当前位置: 首页 > article >正文

自定义vtkActor动画场景及事件_vtkAnimationScene


开发环境:

  1. Windows 11 家庭中文版
  2. Microsoft Visual Studio Community 2019
  3. VTK-9.3.0.rc0
  4. vtk-example
  5. 参考代码

demo解决问题:自定义vtkActor动画场景及事件

  1. vtkAnimationScene表示动画的场景或设置,它由单个线索或其他场景组成,设置动画场景的开始时间,结束时间,添加监听事件类型vtkCommand::AnimationCueTickEvent。因为scene->SetEndTime(20)短于cue1->SetEndTime(23);,所以很明显看到动画最后第二十秒的时候,scene动画事件结束了,tick事件不起作用了,sphere没有平滑移动,最后变成了结束事件vtkCommand::EndAnimationCueEvent,sphere直接移动到了最终的位置

      scene->SetLoop(0);//Enable/Disable animation loop.
      scene->SetFrameRate(5);
      scene->SetStartTime(0);
      scene->SetEndTime(20);
      scene->AddObserver(vtkCommand::AnimationCueTickEvent, renWin.GetPointer(),
                         &vtkWindow::Render);
    
  2. vtkAnimationCue表示随时间变化/动画的实体,设置开始事件、结束时间,并添加到动画场景中AddCue

      // Create an Animation Cue for each actor
      vtkNew<vtkAnimationCue> cue1;
      cue1->SetStartTime(5);
      cue1->SetEndTime(23);
      scene->AddCue(cue1);
    
      vtkNew<vtkAnimationCue> cue2;
      cue2->SetStartTime(1);
      cue2->SetEndTime(10);
      scene->AddCue(cue2);
    
  3. ActorAnimator :自定义根据时间(开始时间、结束时间、中间滴答时间)vtkActor变换,添加对应时间到cue的观察者列表中

      void AddObserversToCue(vtkAnimationCue* cue)
      {
        cue->AddObserver(vtkCommand::StartAnimationCueEvent, this,
                         &ActorAnimator::Start);
        cue->AddObserver(vtkCommand::EndAnimationCueEvent, this,
                         &ActorAnimator::End);
        cue->AddObserver(vtkCommand::AnimationCueTickEvent, this,
                         &ActorAnimator::Tick);
      }
    

prj name: AnimateActors

#include "AnimateActors.h"

#include <vtkAnimationCue.h>
#include <vtkAnimationScene.h>
#include <vtkCamera.h>
#include <vtkCommand.h>
#include <vtkConeSource.h>
#include <vtkLogger.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

int main(int argc, char* argv[])
{
  vtkLogger::Init(argc, argv);

  // Colors
  vtkNew<vtkNamedColors> colors;
  vtkColor3d coneColor = colors->GetColor3d("Tomato");
  vtkColor3d sphereColor = colors->GetColor3d("Banana");
  vtkColor3d backgroundColor = colors->GetColor3d("Peacock");

  // Create the graphics structure. The renderer renders into the
  // render window.
  vtkNew<vtkRenderWindowInteractor> iren;
  vtkNew<vtkRenderer> ren1;
  ren1->SetBackground(backgroundColor.GetData());

  vtkNew<vtkRenderWindow> renWin;
  iren->SetRenderWindow(renWin);
  renWin->AddRenderer(ren1);

  // Generate a sphere
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetPhiResolution(31);
  sphereSource->SetThetaResolution(31);

  vtkNew<vtkPolyDataMapper> sphereMapper;
  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkNew<vtkActor> sphere;
  sphere->SetMapper(sphereMapper);
  sphere->GetProperty()->SetDiffuseColor(sphereColor.GetData());
  sphere->GetProperty()->SetDiffuse(.7);
  sphere->GetProperty()->SetSpecular(.3);
  sphere->GetProperty()->SetSpecularPower(30.0);

  ren1->AddActor(sphere);

  // Generate a cone
  vtkNew<vtkConeSource> coneSource;
  coneSource->SetResolution(31);

  vtkNew<vtkPolyDataMapper> coneMapper;
  coneMapper->SetInputConnection(coneSource->GetOutputPort());
  // auto cone = vtkSmartPointer<vtkActor>::New();
  vtkNew<vtkActor> cone;
  cone->SetMapper(coneMapper);
  cone->GetProperty()->SetDiffuseColor(coneColor.GetData());

  ren1->AddActor(cone);

  // Create an Animation Scene
  vtkNew<vtkAnimationScene> scene;
  if (argc >= 2 && strcmp(argv[1], "-real") == 0)
  {
    vtkLogF(INFO, "real-time mode");
    scene->SetModeToRealTime();
  }
  else
  {
    vtkLogF(INFO, "sequence mode");
    scene->SetModeToSequence();
  }
  scene->SetLoop(0);
  scene->SetFrameRate(5);
  scene->SetStartTime(0);
  scene->SetEndTime(20);
  scene->AddObserver(vtkCommand::AnimationCueTickEvent, renWin.GetPointer(),
                     &vtkWindow::Render);

  // Create an Animation Cue for each actor
  vtkNew<vtkAnimationCue> cue1;
  cue1->SetStartTime(5);
  cue1->SetEndTime(23);
  scene->AddCue(cue1);

  vtkNew<vtkAnimationCue> cue2;
  cue2->SetStartTime(1);
  cue2->SetEndTime(10);
  scene->AddCue(cue2);

  // Create an ActorAnimator for each actor;
  ActorAnimator animateSphere;
  animateSphere.SetActor(sphere);
  animateSphere.AddObserversToCue(cue1);

  ActorAnimator animateCone;
  animateCone.SetEndPosition(vtkVector3d(-1, -1, -1));
  animateCone.SetActor(cone);
  animateCone.AddObserversToCue(cue2);

  renWin->SetWindowName("AnimateActors");

  renWin->Render();
  ren1->ResetCamera();
  ren1->GetActiveCamera()->Dolly(.5);
  ren1->ResetCameraClippingRange();

  // Create Cue observer.
  scene->Play();
  scene->Stop();

  iren->Start();
  return EXIT_SUCCESS;
}


http://www.kler.cn/a/132705.html

相关文章:

  • 深入探索Scala迭代器:实用技巧与最佳实践
  • nodejs+mysql+vue3 应用实例剖析
  • 解决 IDEA 修改代码重启不生效的问题
  • Python数据分析NumPy和pandas(三十五、时间序列数据基础)
  • 《Java核心技术 卷I》用户界面中首选项API
  • 另外一种缓冲式图片组件的用法
  • Feature Pyramid Networks for Object Detection(2017.4)
  • [数据集][目标检测]斑马数据集VOC+yolo格式375张1类别
  • 03_SHELL编程之嵌套循环+随机数及综合案例
  • os.path.join函数用法
  • OpenCV C++ 张正友相机标定【相机标定原理、相机标定流程、图像畸变矫正】
  • 【Python 千题 —— 基础篇】输出列表方差
  • 【机器学习基础】对数几率回归(logistic回归)
  • 2024年山东省职业院校技能大赛中职组“网络安全”赛项竞赛试题-C
  • 提升 Python 执行速度:Codon、C/C++、Rust、Numba(JIT)、Taichi、Nuitka、MatxScript
  • 如何分析伦敦金的价格走势预测?
  • HMM与LTP词性标注之依存句法分析、词性标注实现
  • Android 中字符串空格占位
  • 【Qt之QStandardItemModel】使用,tableview、listview、treeview设置模型
  • AcWing 4520:质数 ← BFS
  • Vue watch属性
  • C语言之for while语句详解
  • 数据库表字段以表格形式写入Word
  • 交换两个变量的值
  • [单片机课程设计报告汇总] 单片机设计报告常用硬件元器件描述
  • Egress Gateway