[vtkusers] Activiz.NET class derived from vtkCommand

Sean Larkin sean.larkin at lickenbrocktech.com
Mon Jul 25 14:49:47 EDT 2011


I am trying to implement a class in C# with Activiz.NET derived from
vtkCommand.  It is based off the example code found here:

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Animation/AnimateActors

When I run my code and try to instantiate my derived class, some internal
error occurs and the code stops execution and the render window appears with
the actors that I've already added.  Here's my code in C#:

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Kitware.VTK;

 

namespace AnimateActorsTest

{

    public partial class Form1 : Form

    {

        vtkRenderWindow vRendWin;

        vtkRenderWindowInteractor vInteractor;

        vtkRenderer vRend;

        public Form1()

        {

            InitializeComponent();

        }

 

        private void renderWindowControl1_Load(object sender, EventArgs e)

        {

            vRendWin = renderWindowControl1.RenderWindow;

            vRend =
renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer();

            vInteractor = vRendWin.GetInteractor();

            vRendWin.SetMultiSamples(0);

            //vRendWin.AddRenderer(vRend);

 

            // Generate a sphere

            vtkSphereSource sphereSource = vtkSphereSource.New();

            vtkPolyDataMapper sphereMapper = vtkPolyDataMapper.New();

            sphereMapper.SetInputConnection(sphereSource.GetOutputPort());

            vtkActor sphere = vtkActor.New();

            sphere.SetMapper(sphereMapper);

            vRend.AddActor(sphere);

 

              // Generate a cone

              vtkConeSource coneSource = vtkConeSource.New();

              vtkPolyDataMapper coneMapper = vtkPolyDataMapper.New();

              coneMapper.SetInputConnection( coneSource.GetOutputPort());

              vtkActor cone = vtkActor.New();

              cone.SetMapper(coneMapper);

              vRend.AddActor(cone);

 

            vtkAnimationScene scene = vtkAnimationScene.New();

            //scene.SetModeToRealTime();

            scene.SetModeToSequence();

            scene.SetLoop(0);

            scene.SetFrameRate(5);

            scene.SetStartTime(0);

            scene.SetEndTime(20);

 

            AnimationSceneObserver sceneObserver =
AnimationSceneObserver.New();

 

 

            vtkCommand c = (vtkCommand)vtkCommand.New();

 

            sceneObserver.SetRenderWindow(vRendWin);

 
scene.AddObserver((uint)vtkCommand.EventIds.AnimationCueTickEvent,
sceneObserver, 1.0f);

 

            vtkAnimationCue cue1 = vtkAnimationCue.New();

            cue1.SetStartTime(5);

            cue1.SetEndTime(23);

            scene.AddCue(cue1);

 

            vtkAnimationCue cue2 = vtkAnimationCue.New();

            cue2.SetStartTime(1);

            cue2.SetEndTime(23);

            scene.AddCue(cue2);

 

            ActorAnimator animateSphere = new ActorAnimator();

            animateSphere.SetActor(sphere);

            animateSphere.AddObserversToCue(cue1);

 

            ActorAnimator animateCone = new ActorAnimator();

            List<double> endCone = new List<double>();

            endCone[0] = -1;

            endCone[1] = -1;

            endCone[2] = -1;

            animateCone.SetEndPosition(endCone);

            animateCone.SetActor(cone);

            animateCone.AddObserversToCue(cue2);

 

            vRendWin.Render();

            //vRendWin.ResetCamera();

 

            scene.Play();

            scene.Stop();

 

            vInteractor.Start();

 

        }

    }

 

    public class ActorAnimator

    {

        vtkActor Actor;

        AnimationCueObserver Observer;

        List<double> StartPosition;

        List<double> EndPosition;

 

        public ActorAnimator()

        {

            this.Actor=null;

            this.Observer = AnimationCueObserver.New();

            this.Observer.Animator = this;

            this.StartPosition.Capacity = 3;

            this.StartPosition.Insert(0, 0.0);

            this.StartPosition.Insert(1, 0.0);

            this.StartPosition.Insert(2, 0.0);

            this.EndPosition.Capacity = 3;

            this.EndPosition.Insert(0,.5);

            this.EndPosition.Insert(1, .5);

            this.EndPosition.Insert(2, .5);

        }

 

        public void AddObserversToCue(vtkAnimationCue cue)

        {

 
cue.AddObserver((uint)vtkCommand.EventIds.StartAnimationCueEvent,this.Observ
er,1.0f);

 
cue.AddObserver((uint)vtkCommand.EventIds.EndAnimationCueEvent,this.Observer
,1.0f);

 
cue.AddObserver((uint)vtkCommand.EventIds.AnimationCueTickEvent,this.Observe
r,1.0f);

        }

 

        public void Start(vtkAnimationCue info)

        {

            this.Actor.SetPosition(this.StartPosition[0],

                                 this.StartPosition[1], 

                                 this.StartPosition[2]);

        }

        public void End(vtkAnimationCue info)

        {

            this.Actor.SetPosition(this.EndPosition[0],

                                     this.EndPosition[1], 

                                     this.EndPosition[2]);

        }

        public void Tick(vtkAnimationCue info)

        {

            double t = (info.GetAnimationTime() - info.GetStartTime()) /
(info.GetEndTime() - info.GetStartTime());

            double[] position = new double[3];

            for (int i = 0; i < 3; i++)

            {

                position[i] = this.StartPosition[i] + (this.EndPosition[i] -
this.StartPosition[i]) * t;

            }

            this.Actor.SetPosition(position[0], position[1], position[2]);

     

        }

 

        public void SetActor(vtkActor actor)

        {

            this.Actor = actor;

            //this.Actor.Register(0);

        }

        public void SetStartPosition(List<double> position)

        {

            this.StartPosition = position;

        }

        public void SetEndPosition(List<double> position)

        {

            this.EndPosition = position;

        }

 

 

        protected class AnimationCueObserver : vtkCommand

        {

            public static AnimationCueObserver New()

            {

                return new AnimationCueObserver();

            }

 

            public override void Execute(vtkObject caller, uint eventId,
IntPtr callData)

            {

                if (this.Animator != null)

                {

                    vtkAnimationCue info = new vtkAnimationCue(callData,
true, true);

                    switch (eventId)

                    {

                        case
(uint)vtkCommand.EventIds.StartAnimationCueEvent:

                            this.Animator.Start(info);

                            break;

                        case (uint)vtkCommand.EventIds.EndAnimationCueEvent:

                            this.Animator.End(info);

                            break;

                        case
(uint)vtkCommand.EventIds.AnimationCueTickEvent:

                            this.Animator.Tick(info);

                            break;

 

                    }

                    

                }

                base.Execute(caller, eventId, callData);

            }

            public ActorAnimator Animator;

            public AnimationCueObserver()

            {

                this.Animator = null;

            }

        }

    }

 

    public class AnimationSceneObserver : vtkCommand

    {

        vtkRenderWindow RenderWindow;

        public static AnimationSceneObserver New()

        {

            return new AnimationSceneObserver();

        }

 

        public void SetRenderWindow(vtkRenderWindow renWin)

        {

            if (this.RenderWindow!=null)

            {

                this.RenderWindow.RemoveObserver(this);

            }

            this.RenderWindow = renWin;

            this.RenderWindow.Register(this); // AddObserver?

 

        }

 

        public override void Execute(vtkObject caller, uint eventId, IntPtr
callData)

        {

            if(this.RenderWindow != null)

              {

              switch(eventId)

                {

                case (uint)vtkCommand.EventIds.AnimationCueTickEvent:

                  this.RenderWindow.Render();

                  break;

                }

              }

        }

 

        private AnimationSceneObserver()

        {

            this.RenderWindow = null;

        }

    }

}

 

 

The line: AnimationSceneObserver sceneObserver =
AnimationSceneObserver.New();

is where the error occurs, which is the first time I use a vtkCommand
derived class.  I've stepped through the code in the debugger and it reaches
the constructor, but as soon as it executes the constructor some kind of
error occurs.  Anyone have any ideas?

 

Thanks,

Sean

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110725/9271ce8c/attachment.htm>


More information about the vtkusers mailing list