[vtkusers] Draw a 2D cross on vtkImageActor

Jochen K. jochen.kling at email.de
Sun Jun 24 05:37:35 EDT 2012


Hi Jerry,

do you want something like this?

http://vtk.1045678.n5.nabble.com/file/n5714177/VTK_Examples_CSharp_vtkImageActor_With_Overlayed_Renderer.png 


I have written an example in C#. The basic idea is to overlay an additional
renderer where the cross is displayed. Both renderers are synchronized.

Whenever you click the image the cross will follow the new position.


using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

using Kitware.VTK;

namespace TestActiViz {
   public partial class Form1 : Form {
      vtkCamera _CameraOverlay;
      vtkActor _Cross;
      vtkPropPicker _PropPicker;
      vtkRenderWindow _RenderWindow;

      
      public Form1() {
         InitializeComponent();
      }


      protected override void OnLoad(EventArgs e) {
         base.OnLoad(e);
         try {
            TestImageActorOverlay();
         }
         catch(Exception ex) {
            MessageBox.Show("Exception: " + ex.Message);
         }
      }


      private void TestImageActorOverlay() {
         // Path to vtk data must be set as an environment variable
         // VTK_DATA_ROOT = "C:\VTK\vtkdata-5.8.0"
         vtkTesting test = vtkTesting.New();
         string root = test.GetDataRoot();
         string filePath = System.IO.Path.Combine(root,
@"Data\clouds.jpeg");
         vtkJPEGReader reader = vtkJPEGReader.New();
         if(reader.CanReadFile(filePath) == 0) {
            MessageBox.Show("Cannot read file \"" + 
               filePath + 
               "\"", "Error", MessageBoxButtons.OK);
            return;
         }
         reader.SetFileName(filePath);
         reader.Update();
         // we need the imagedata to get the world coordinates of the
center,
         // get the bounds and get the scalar range
         vtkImageData imageData = reader.GetOutput();

         // Create an imageActor
         vtkImageActor imageActor = vtkImageActor.New();
         imageActor.SetInput(imageData);

         // get bounds and center of the imagedata to size and position the
_Cross
         double[] bounds = imageData.GetBounds();
         SizeF imageSize = new SizeF(
            (float)( bounds[1] - bounds[0] ), 
            (float)( bounds[3] - bounds[2] ));
         double[] imageCenter = imageData.GetCenter();
         float max = Math.Max(imageSize.Width, imageSize.Height);


         vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
         mapper.SetInput(CreateCross(max / 10));
         _Cross = vtkActor.New();
         _Cross.GetProperty().SetLineWidth(5);
         _Cross.SetPosition(
                imageCenter[0],
                imageCenter[1],
                imageCenter[2]);
         _Cross.SetMapper(mapper);

         // get a reference to the renderwindow of our renderWindowControl1
         _RenderWindow = renderWindowControl1.RenderWindow;
         // renderer
         vtkRenderer renderer =
_RenderWindow.GetRenderers().GetFirstRenderer();
         // set background color
         renderer.SetBackground(0.2, 0.3, 0.4);
         // add our imageActor to the renderer
         renderer.AddActor(imageActor);
         // important call to make entire image visible
         renderer.ResetCamera();

         // create an interactorstyle and event handler for pressing left
mouse button
         vtkInteractorStyleImage interactorStyleImage =
vtkInteractorStyleImage.New();
        
_RenderWindow.GetInteractor().SetInteractorStyle(interactorStyleImage);
         interactorStyleImage.LeftButtonPressEvt += 
            new
vtkObject.vtkObjectEventHandler(InteractorStyleImage_LeftButtonPressEvt);

         // create a transparent overlay renderer
         _RenderWindow.SetNumberOfLayers(2);
         vtkRenderer rendererOverlay = vtkRenderer.New();
         rendererOverlay.SetLayer(1);
         // important, cause we want the renderer which contains 
         // the imageActor to be interactive and not the overlay
         rendererOverlay.SetInteractive(0);
         _RenderWindow.AddRenderer(rendererOverlay);

         rendererOverlay.AddActor(_Cross);
         _CameraOverlay = rendererOverlay.GetActiveCamera();

         // synchronize cameras
         _CameraOverlay.ShallowCopy(renderer.GetActiveCamera());

         // event handler to synchronize cameras
         renderer.GetActiveCamera().ModifiedEvt += 
            new vtkObject.vtkObjectEventHandler(Camera_ModifiedEvt);
         _PropPicker = vtkPropPicker.New();
      }


      void InteractorStyleImage_LeftButtonPressEvt(vtkObject sender,
vtkObjectEventArgs e) {
         vtkInteractorStyleImage caller = e.Caller as
vtkInteractorStyleImage;
         if(caller != null) {
            int[] mousePosition =
caller.GetInteractor().GetLastEventPosition();
            //Debug.WriteLine("left mouse button pressed -> mouse
position: " 
            //   + mousePosition[0] 
            //   + " " 
            //   + mousePosition[1]);
            int ret = _PropPicker.Pick(
               mousePosition[0], 
               mousePosition[1], 
               0, 
               _RenderWindow.GetRenderers().GetFirstRenderer());
            if(ret == 1) {
               double[] pos = _Cross.GetPosition();
               double[] pickPosition = _PropPicker.GetPickPosition();
               //Debug.WriteLine("Pick Position: " 
               //   + pickPosition[0] + " " 
               //   + pickPosition[1] + " " 
               //   + pickPosition[2]);
               _Cross.SetPosition(
                  pickPosition[0],
                  pickPosition[1],
                  pickPosition[2]);
               _RenderWindow.Render();
            }
         }
      }


      void Camera_ModifiedEvt(vtkObject sender, vtkObjectEventArgs e) {
         _CameraOverlay.ShallowCopy((vtkCamera)sender);
      }


      // create a cross
      vtkPolyData CreateCross(float size) {
         // Create a vtkPoints object and store the points in it
         vtkPoints pts = vtkPoints.New();
         pts.InsertNextPoint(-size / 2, 0, 0);
         pts.InsertNextPoint(size / 2, 0, 0);
         pts.InsertNextPoint(0, -size / 2, 0);
         pts.InsertNextPoint(0, size / 2, 0);

         // Setup the colors array
         byte[] color = new byte[] { 255, 128, 0 };
         vtkUnsignedCharArray colors = vtkUnsignedCharArray.New();
         colors.SetNumberOfComponents(3);
         colors.SetName("Colors");

         // Add the colors we created to the colors array
         colors.InsertNextValue(color[0]);
         colors.InsertNextValue(color[1]);
         colors.InsertNextValue(color[2]);

         colors.InsertNextValue(color[0]);
         colors.InsertNextValue(color[1]);
         colors.InsertNextValue(color[2]);

         // Create the first line
         vtkLine line0 = vtkLine.New();
         line0.GetPointIds().SetId(0, 0);
         line0.GetPointIds().SetId(1, 1);

         // Create the second line
         vtkLine line1 = vtkLine.New();
         line1.GetPointIds().SetId(0, 2);
         line1.GetPointIds().SetId(1, 3);

         // Create a cell array to store the lines in and add the lines to
it
         vtkCellArray lines = vtkCellArray.New();
         lines.InsertNextCell(line0);
         lines.InsertNextCell(line1);

         // Create a polydata to store everything in
         vtkPolyData linesPolyData = vtkPolyData.New();
         // Add the points to the dataset
         linesPolyData.SetPoints(pts);
         // Add the lines to the dataset
         linesPolyData.SetLines(lines);
         // Color the lines
         linesPolyData.GetCellData().SetScalars(colors);
         return linesPolyData;
      }
   }
}

Hope that's what your looking for.

with kind regards

Jochen



--
View this message in context: http://vtk.1045678.n5.nabble.com/Draw-a-2D-cross-on-vtkImageActor-tp5714166p5714177.html
Sent from the VTK - Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120624/5d2c5dbf/attachment.htm>


More information about the vtkusers mailing list