第04章-VTK基础(7)

【译者:这个系列教程是以Kitware公司出版的《VTK User’s Guide -11th edition》一书作的中文翻译(出版时间2010年。ISBN: 978-1-930934-23-8)。因为时间关系,我们不能保证每周都能更新本书内容,但尽量做到一周更新一篇到两篇内容。敬请期待^_^。欢迎转载,另请转载时注明本文出处,谢谢合作!

同一时候。因为译者水平有限,出错之处在所难免,欢迎指出订正!

【本小节内容相应原书的第83页至第87页】

4.16 动画

  • 动画是可视化等系统的重要模块。
  • 通过编写改动某个Filter和渲染器Render參数的循环,能够实现简单的动画效果。可是一旦有多个參数须要改动时。这样的方法就会变得比較复杂。
  • VTK提供了一个由vtkAnimationCue和vtkAnimationScene等类所组成的框架,支持动画场景的创建和回放。
  • vtkAnimationCue相应一个可随时间改变的实体,比方Actor的空间位置;vtkAnimationScene则表示一个场景或者由vtkAnimationCue实例所组成的动画场景。

动画场景(vtkAnimationScene)

vtkAnimationScene表示一个场景或者是动画场景的建立。动画场景通常是通过渲染一系列的帧,在渲染每一帧时改变某些可视化參数来建立的。所渲染的每一帧都关联一个动画时间,这个时间用来确定动画中每一帧的位置。基于不同的播放模式,动画时间在动画播放过程中是一个计数不断添加的简单变量。

下面列出了类vtkAnimationScene中一些比較重要的方法:

SetStartTime()/SetEndTime()

    设置动画场景的開始和结束时间。也是动画回放时的时间范围。

SetPlayMode()

    用于控制动画回放的模式,也就是动画时间是怎样改变的。有两种模式能够设置。

SequenceMode (PLAYMODE_SEQUENCE)

    这样的模式下,每帧的动画时间添加(1/frame-rate)的间隔。直至达到所设置的EndTime。因此。所渲染的总帧数是固定的,与渲染每一帧时所用的时间的长短无关。

RealTimeMode (PLAYMODE_REALTIME)

    这样的模式下,整个动画的执行时间大约是(EndTime-StartTime)秒,当中第n帧的执行时间是:第(n-1)帧的执行时间+渲染第(n-1)帧所用的时间。因此。所渲染的总帧数随着渲染每一帧所用时间的不同而不同。

SetFrameRate()

    帧率是指单位时间内渲染的帧数。主要用于Sequence模式。

AddCue(),RemoveCue(), RemoveAllCue()

    加入vtkAnimationCue实例到场景中或者从场景中移除vtkAnimationCue实例。

SetAnimationTime()

    指定某一帧的动画时间。

GetAnimationTime()

    动画回放时。获取动画的时钟时间。

Play()

    開始播放动画。

SetLoop()

    假设设置为true。则调用Play()方法后将进入动画循环。

AnimationCue (vtkAnimationCue)

    vtkAnimationCue相应动画场景中随时间改变的实体。vtkAnimationCue实例本身并不知道与动画相关的參数是假设改变的。

因此,用户必须从vtkAnimationCue中派生出子类或者使用观察者模式监听事件。来改变动画过程中须要改变的參数。

 

动画场景中的Cue实体都有一个開始时间(start-time)和结束时间(end-time)。动画回放时,当场景的动画时间是处于所指定的開始时间与结束时间的范围内时,Cue实体就会被激活。Cue实体一旦激活后,它本身会发出vtkCommand::StartAnimationCueEvent事件。而对于动画系列中的每一帧。则发出vtkCommand::AnimationCueTickEvent事件,当动画时间递增至Cue实体的结束时间时,会发出vtkCommand::EndAnimationCueEvent事件。下面是vtkAnimationCue类中一些比較重要的方法:

    SetTimeMode

        TimeMode定义了Cue实体的起始时间和结束时间是怎样指定的,有两种模式可选。

    Relative (TIMEMODE_RELATIVE)

        这样的模式下,动画场景中的Cue实体的时间是相对动画场景的開始时间来指定的。

    Normalized (TIMEMODE_NORMALIZED)

        这样的模式下,Cue实体的開始时间和结束时间的取值范围为[0,1],当中0相应动画场景的開始。1相应结束。

    SetStartTime/SetEndTime

        这两个方法主要是当Cue实体被激活时,标识动画时间的范围。

当TimeMode取值为TIMEMODE_RELATIVE时。与动画场景的起始和结束时间有同样的单位。当TimeMode取值为TIMEMODE_NORMALIZED时。Cue实体的開始时间和结束时间的取值范围为[0,1],当中0相应动画场景的開始,1相应结束。

    GetAnimationTime()

        用于vtkCommand::AnimationCueTickEvent事件的处理。

处理事件时。可用来确定动画场景中的当前帧。其值依赖于TimeMode。假设TimeMode取值为TIMEMODE_RELATIVE时,其值是Cue实体激活后的时间单位的倍数;TimeMode取值为TIMEMODE_NORMALIZED时。Cue实体的開始时间和结束时间的取值范围为[0,1],当中0相应动画场景的開始,1相应结束。

    GetClockTime()

        该方法与vtkAnimationScene::GetAnimationTime()中的动画时钟时间相似。

仅仅有当处理vtkCommand::AnimationCueTickEvent事件时才有效。

    TickInternal (double currenttime, doubledeltatime, double clocktime)

        正如前面所提的。我们能够派生一个vtkAnimationCue子类来取代编写处理动画事件的函数。子类派生时,须要重载该函数。当中该函数的參数分别相应GetAnimationTime(),GetDeltaTime()和GetClockTime()的返回值。

    StartCueInternal(), EndCueInternal()

        这两个方法可在子类中重载。主要做一些创建、清除等工作以及动画回放时控制Cue实体的開始和结束。用户也能够加入事件观察者,通过监听vtkCommand::StartAnimationCueEvent和vtkCommand::EndAnimationCueEvent事件来实现相似的功能。

下面的演示样例中,我们创建了一个简单的动画:vtkSphereSource的StartTheta随着动画时间而改变。

演示样例中的Cue实体使用Normalized时间模式,因此,能够通过改变场景时间或者Cue实体的时间来改变StartTheta的值。

class vtkCustomAnimationCue : public vtkAnimationCue
{
public:
       static vtkCustomAnimationCue* New();
       vtkTypeRevisionMacro (vtkCustomAnimationCue,vtkAnimationCue);
 
       vtkRenderWindow *RenWin;
       vtkSphereSource *Sphere;
 
protected:
       vtkCustomAnimationCue()
       {
              this->RenWin = 0;
              this->Sphere = 0;
       }
 
       // Overridden to adjust the sphere'sradius depending on the frame we
       // are rendering. In this animation wewant to change the StartTheta
       // of the sphere from 0 to 180 over thelength of the cue.
       virtual void TickInternal(doublecurrenttime, double deltatime, double clocktime)
       {
              double new_st = currenttime * 180;
              // since the cue is in normalizedmode, the currenttime will be in the
              // range[0,1], where 0 is start ofthe cue and 1 is end of the cue.
              this->Sphere->SetStartTheta(new_st);
              this->RenWin->Render();
       }
};
vtkStandardNewMacro(vtkCustomAnimationCue);
vtkCxxRevisionMacro(vtkCustomAnimationCue,"$Revision$");
 
int main(int argc, char *argv[])
{
       // Create the graphics sturctures. Therenderer renders into the
       // render window.
       vtkRenderer *ren1 = vtkRenderer::New();
       vtkRenderWindow *renWin =vtkRenderWindow::New();
       renWin->SetMultiSamples(0);
       renWin->AddRenderer(ren1);
 
       vtkSphereSource *sphere =vtkSphereSource::New();
       vtkPolyDataMapper *mapper =vtkPolyDataMapper::New();
       mapper->SetInputConnection(sphere->GetOutputPort());
       vtkActor *actor = vtkActor::New();
       actor->SetMapper(mapper);
       ren1->AddActor(actor);
 
       ren1->ResetCamera();
       renWin->Render();
 
       //Create an Animation Scene
       vtkAnimationScene *scene =vtkAnimationScene::New();
       scene->SetModeToSequence();
       scene->SetFrameRate(30);
       scene->SetStartTime(0);
       scene->SetEndTime(60);
 
       // Create an Animation Cue to animate thecamera.
       vtkCustomAnimationCue *cue1 =vtkCustomAnimationCue::New();
       cue1->Sphere = sphere;
       cue1->RenWin = renWin;
       cue1->SetTimeModeToNormalized();
       cue1->SetStartTime(0);
       cue1->SetEndTime(1.0);
       scene->AddCue(cue1);
       scene->Play();
       scene->Stop();
 
       ren1->Delete();
       renWin->Delete();
       scene->Delete();
       cue1->Delete();
       return 0;
}

【第4章-VTK基础 所有翻译完毕】
原文地址:https://www.cnblogs.com/mqxnongmin/p/10559370.html