[vtkusers] Slice numbers of a 3D image

frency v frencyvarghese at yahoo.com
Fri Apr 24 11:04:51 EDT 2009


Hello
I am viewing a 3D image in the renderwindow. What i want to do is When i click mouse at a point on the image in the renderwindow i want to know to which slice does that point picked by the mouse belong to? i am getting the x and y position but the z cordinate (which is basicaly the slice number in case of a 3D image ) is in fractions. 
please help.

Frency
here is the code
#include <iostream>
#include <fstream>
#include <string>

#include "vtkImageImport.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkContourFilter.h"
#include "vtkPolyDataNormals.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkOutlineFilter.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkCellPicker.h"
#include "vtkPointPicker.h"
#include "vtkProp3D.h"
vtkImageImport *Importer = vtkImageImport::New();
vtkContourFilter *skinExtractor = vtkContourFilter::New();
vtkPolyDataNormals *skinNormals = vtkPolyDataNormals::New();
vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New();
vtkActor *skin = vtkActor::New();
vtkRenderer *aRenderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();

//** constants and variables defined**//

int t=0;

 // No. of iterations to be done//
int x_max=54;
int y_max=47;
int z_max=63;
const int x_neigh=50000;
const int y_neigh=50000;
const int z_neigh=50000;
int count_vv;

//**reading image into an array**//

int x,y,z;


unsigned short* ImageArray= new unsigned short[x_max*y_max*z_max];


//**call  back function for keypress**//

class vtkMyCallback : public vtkCommand
{
public:
  static vtkMyCallback *New()
    { return new vtkMyCallback;}
  void Delete()
    { delete this; }

  virtual void Execute(vtkObject *caller, unsigned long, void*)
    {

      vtkRenderWindowInteractor *iren= reinterpret_cast<vtkRenderWindowInteractor*>(caller);
        {
            int mouseX, mouseY;
float z;
iren->GetMousePosition(&mouseX, &mouseY);
z=aRenderer->GetZ(mouseX,mouseY);

            cout<<"z"<<z<<"mouseX"<<mouseX<<"mouseY"<<mouseY<<endl;

        }


         //**Update the pipeline so that  the renderwindow is updated**//
         Importer->Modified();
         skinExtractor->Modified();
         skinNormals->Modified();
         skinMapper->Modified();
         skinExtractor->SetInputConnection(Importer->GetOutputPort());
         skin->SetMapper(skinMapper);
         renWin->Render();



    }
};

using namespace std;

//*****Main program starts****//

int main()
 {
   int x;
   int y;
   int z;

//**reading the .raw image into and array **//

   ifstream inputFile;
   inputFile.open("/home/owner/Desktop/iteration/15s.raw",ios::binary);
     for(z=0; z<z_max; z++)
        {
        for(y=0; y<y_max; y++)
           {
           for(x=0; x<x_max; x++)
              {
                inputFile.read((char*) &(ImageArray[z*x_max*y_max+y*x_max+x]),2);

              }

            }
         }



 //** Create the renderer, the render window, and the interactor.**//
 //** The renderer draws into the render window, the interactor keyboard-based interaction with the data within the render window.**//


     renWin->AddRenderer(aRenderer);
     vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
     iren->SetRenderWindow(renWin);

//**using image import class the inmage in the array is moved into the buffer**//

     Importer->SetDataExtent(0, 53, 0, 46, 0,  62);
     Importer->SetWholeExtent(0, 53, 0, 46, 0, 62);
     Importer->SetDataSpacing(1.0,1.0,1.0);
     Importer->SetNumberOfScalarComponents(1);
     Importer->SetDataScalarTypeToUnsignedShort();
     Importer->SetImportVoidPointer(ImageArray);

// An isosurface, or contour value of 100 is known to correspond to the
  //inner surface. Once generated, a vtkPolyDataNormals filter is
  // is used to create normals for smooth surface shading during rendering.

     skinExtractor->SetInputConnection(Importer->GetOutputPort());
     skinExtractor->SetValue(0,109);

     vtkPolyDataNormals *skinNormals = vtkPolyDataNormals::New();
     skinNormals->SetInputConnection(skinExtractor->GetOutputPort());
     skinNormals->SetFeatureAngle(60);



     skinMapper->SetInputConnection(skinNormals->GetOutputPort());
     skinMapper->ScalarVisibilityOff();


     skin->SetMapper(skinMapper);

// It is convenient to create an initial view of the data. The FocalPoint
  // and Position form a vector direction. Later on (ResetCamera() method)
  // this vector is used to position the camera to look at the data in this direction.

     vtkCamera *aCamera = vtkCamera::New();
     aCamera->SetViewUp (0, 0, -1);
     aCamera->SetPosition (0, 1, 0);
     aCamera->SetFocalPoint (0, 0, 0);
     aCamera->ComputeViewPlaneNormal();

// Actors are added to the renderer. An initial camera view is created.
  // The Dolly() method moves the camera towards the FocalPoint,thereby enlarging the image.
 // aRenderer->AddActor(outline);

     aRenderer->AddActor(skin);
     aRenderer->SetActiveCamera(aCamera);
     aRenderer->ResetCamera ();
     aCamera->Dolly(1.5);

//** defining the vtk windowinteractor**//

     iren =vtkRenderWindowInteractor::New();
     iren->SetRenderWindow(renWin);
     renWin->Render();
     renWin->SetSize(500, 500 );

//**adding observer to the interactor**//
        vtkPointPicker *picker = vtkPointPicker::New();
        picker->SetTolerance(0.01);
        iren->SetPicker(picker);

     vtkMyCallback *callback = vtkMyCallback::New();

     iren->AddObserver( vtkCommand::LeftButtonPressEvent, callback );

     iren->Initialize();
     iren->Start();

//**delete all objects created previously to preventmemory leaks**//



     inputFile.close();

     Importer->Delete();
     skinExtractor->Delete();
     skinMapper->Delete();
     skin->Delete();
     aCamera->Delete();
     renWin->Delete();
     aRenderer->Delete();
  return 0;
 }








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


More information about the vtkusers mailing list