[vtkusers] [Fwd: vtkcellpick help needed]

Anka Kochanowska anka at bic.mni.mcgill.ca
Thu Apr 23 07:37:55 EDT 2009


Hi!
This mail should go to vtkusers. If you are not subscribed to the 
mailing list,  you may do it at:
http://public.kitware.com/mailman/listinfo/vtkusers

I do not recognize the program :-)
The text will be displayed at the picked point coordinates in your case. 
You decide where it is displayed passing text coordinates in:
          textActor->SetPosition(x, y);
 You might do:
          textActor->SetPosition(10, 10);
for text at the bottom
or
          textActor->SetPosition(x+5, y);
for text next to the selected poinr.

Read   vtkCellPicker and  vtkActor2D documentation.
Anka
 


-------- Original Message --------
Subject: 	vtkcellpick help needed
Date: 	Wed, 22 Apr 2009 14:15:26 -0700 (PDT)
From: 	frency v <frencyvarghese at yahoo.com>
Reply-To: 	frencyvarghese at yahoo.com
To: 	anka at bic.mni.mcgill.ca



Hello
I am a new commer to VTK. I want to know the x,y and z cordinates of the 
point picked by mouse. I was not able to do that. So i tried to use your 
program .

 I was trying to use your program from vtk user list to to get the 
information about the x,y,z cordinates.
I have a question .
whne i click mouse, where will the information be displayed? is it on 
the screen or somewhere else?

please  help
Thank You
 frency
 Here is my code. In this code few parts are mine.
 #include <iostream>
#include <fstream>
#include <string>
#include "vtkTextProperty.h"
#include "vtkTextMapper.h"
#include "vtkInteractorStyleTrackballCamera.h"
int MouseMotion;
#include "vtkCallbackCommand.h"
#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 "vtkActor2D.h"
#include  "vtkLabeledDataMapper.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();
vtkCellPicker *picker = vtkCellPicker::New();
//** constants and variables defined**//
//vtkCellPicker *picker;
vtkActor2D *textActor;
vtkTextMapper *textMapper;
vtkRenderWindowInteractor *iren;

class PickCommand : public vtkCommand
{
public:

    static PickCommand *New() { return new PickCommand; }
    void Delete() { delete this; }

    virtual void Execute(vtkObject *caller, unsigned long l, void *callData)
    {
        if (picker->GetCellId() < 0 )
        {
            textActor->VisibilityOff();
        }
        else
        {
            double selpt[3];
            picker->GetSelectionPoint(selpt);
            double x = selpt[0];
            double y = selpt[1];
            double pickPos[3];
            picker->GetPickPosition( pickPos );
            double xp = pickPos[0];
            double yp = pickPos[1];
            double zp = pickPos[2];

            char text[120];
            sprintf( text, "(%5.5f,  %5.5f,  %5.5f)", xp, yp, zp );
            textMapper->SetInput( text );
            textActor->SetPosition(x, y);
            textActor->VisibilityOn();
        }

        renWin->Render();
    }
};

void PickerInteractionCallback( vtkObject* vtkNotUsed(object),
                                       unsigned long event,
                                       void* clientdata,
                                       void* vtkNotUsed(calldata) )
{
    vtkInteractorStyleTrackballCamera * style =
(vtkInteractorStyleTrackballCamera*)clientdata;
    switch( event )
    {
    case vtkCommand::LeftButtonPressEvent:
        MouseMotion = 0;
        style->OnLeftButtonDown();
        break;
    case vtkCommand::LeftButtonReleaseEvent:
        if (MouseMotion == 0)
        {
            int *pick = iren->GetEventPosition();
            picker->Pick((double)pick[0], (double)pick[1], 0.0, aRenderer);
        }
        style->OnLeftButtonUp();
        break;
    case vtkCommand::MouseMoveEvent:
        MouseMotion = 1;
        style->OnMouseMove();
        break;
    }
}


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;

int* vvx=new int[x_neigh];
int* vvy=new int[y_neigh];
int* vvz=new int[z_neigh];
//**reading image into an array**//

int x,y,z;
int Frame=0;
int a=100;

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

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

unsigned short* Iteration4D= new unsigned short[x_max*y_max*z_max*a];

unsigned short FrameDisplay(unsigned short*, unsigned short*, int, int&, 
int&, int&, int&, int&, int& );

unsigned short SaveFile(unsigned short*, int, int, int, int, int,int);

unsigned short ReLoad(unsigned short*, int, int, int, int, int,int);

using namespace std;

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

int main()
 {
     MouseMotion = 0;

   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);

              }

            }
         }

//**Iteration being done without the Image being loaded into the 
Readerwindow**//

            int voxel_count=50000;

           int* voxels= new int[voxel_count];
           int* iteration = new int[voxel_count];



           const int perfuse_max_vox_value=191;
           const int perfuse_min_vox_value=109;
           int vvn;

           int count_vv=1;
                                                                                                             
//  cout  << " count_vv " << endl;
           vvx[0]=24;
           vvy[0]=38;
           vvz[0]=59;
           int c=0;


           int numb_iterations=0;
           int numb_voxels=1;
           int numb_voxels_added=1; //this variable would help in going 
through that many times (numb_voxels) through the loop
           int k=0;

           for(int i=0; i<a; i++)
            //do
             {

            int 
current_vv=0;                                                                                                                                  
//int vvnmax=7;
            for(current_vv=0; current_vv<numb_voxels_added; current_vv++)
             {

              //cout << " if loop works" << count_vv<< endl;
              x=vvx[k];
              y=vvy[k];
              z=vvz[k];
               
k++;                                                                                                      
//char match_found=0;
                 int check_vv;


                
if((ImageArray[z*x_max*y_max+y*x_max+(x-1)]>(perfuse_min_vox_value))&&((ImageArray[z*x_max*y_max+y*x_max+(x-1)])<(perfuse_max_vox_value)) 
&& (x-1)>0 && (x-1)<x_max && (y>0) && (y<y_max)&& (z>0) && (z<z_max))
                     {

                        char match_found=0;
                            for(check_vv=0; check_vv<count_vv; check_vv++)
                             {
                                if (vvx[check_vv]==(x-1) && 
vvy[check_vv]==y && vvz[check_vv]==z)
                                   {
                                     match_found=1;
                                   }
                             }
                          if  (!match_found==1)
                              {
                                  vvx[count_vv]=x-1;
                                  vvy[count_vv]=y;
                                  vvz[count_vv]=z;
                                  count_vv++;
                                   numb_voxels++;

                              }
                       }

                  
if((ImageArray[z*x_max*y_max+y*x_max+(x+1)]>(perfuse_min_vox_value))&&((ImageArray[z*x_max*y_max+y*x_max+(x+1)])<(perfuse_max_vox_value))&& 
(x+1)>0&&(x+1)<(x_max) && (y>=0) && (y<y_max)&& (z>0) && (z<z_max))
                      {


                         char match_found=0;

                            for( int check_vv=0; check_vv<count_vv; 
check_vv++)
                              {
                                 if (vvx[check_vv]==(x+1) && 
vvy[check_vv]==y && vvz[check_vv]==z)
                                    {
                                        match_found=1;
                                    }
                              }

                            if  (!match_found==1)
                              {
                                 vvx[count_vv]=x+1;
                                 vvy[count_vv]=y;
                                 vvz[count_vv]=z;
                                 count_vv++;
                                 numb_voxels++;

                              }
                      }
                                                                                                                                    
//cout<<"loop ente

                    
if((ImageArray[z*x_max*y_max+(y-1)*x_max+x])>(perfuse_min_vox_value)&&((ImageArray[z*x_max*y_max+(y-1)*x_max+x])<(perfuse_max_vox_value))&& 
(x)>0 &&(x)<(x_max) && (y-1)>0 && (y-1)<(y_max)&& (z>0) && (z<z_max))
                     {


                        char match_found=0;

                         for( int check_vv=0; check_vv<count_vv; check_vv++)
                          {
                             if(vvx[check_vv]==x && vvy[check_vv]==(y-1) 
&& vvz[check_vv]==z)
                                {
                                   match_found=1;
                                }
                          }

                      if (!match_found==1)
                        {
                            vvx[count_vv]=x;
                            vvy[count_vv]=y-1;
                            vvz[count_vv]=z;
                            count_vv++;
                            numb_voxels++;

                        }
                      }


                    
if((ImageArray[z*x_max*y_max+(y+1)*x_max+x])>(perfuse_min_vox_value)&&((ImageArray[z*x_max*y_max+(y+1)*x_max+x])<(perfuse_max_vox_value))&& 
(x)>0 &&(x)<(x_max) && (y+1)>0 && (y+1)<(y_max)&& (z>0) && (z<z_max))
                     {

                       char match_found=0;

                         for( int check_vv=0; check_vv<count_vv; check_vv++)
                           {
                              if (vvx[check_vv]==x && 
vvy[check_vv]==(y+1) && vvz[check_vv]==z)
                                {
                                   match_found=1;
                                }
                           }

                       if  (!match_found==1)
                            {
                               vvx[count_vv]=x;
                               vvy[count_vv]=y+1;
                               vvz[count_vv]=z;
                               count_vv++;
                               numb_voxels++;

                            }
                     }


                     
if((ImageArray[(z-1)*x_max*y_max+y*x_max+x])>(perfuse_min_vox_value)&&((ImageArray[(z-1)*x_max*y_max+y*x_max+x])<(perfuse_max_vox_value))&& 
(x)>0 &&(x)<(x_max) && (y>0) && (y<y_max)&& (z-1)>0 && (z-1)<(z_max))
                      {

                      char match_found=0;

                        for( int check_vv=0; check_vv<count_vv; check_vv++)

                          {
                             if (vvx[check_vv]==x && vvy[check_vv]==y && 
vvz[check_vv]==(z-1))
                              {
                                 match_found=1;
                              }
                          }
                      if  (!match_found==1)
                           {
                               vvx[count_vv]=x;
                               vvy[count_vv]=y;
                               vvz[count_vv]=z-1;
                               count_vv++;
                               numb_voxels++;

                           }
                      }

                      
if((ImageArray[(z+1)*x_max*y_max+y*x_max+x])>(perfuse_min_vox_value)&&((ImageArray[(z+1)*x_max*y_max+y*x_max+x])<(perfuse_max_vox_value))&& 
(x)>0 &&(x)<(x_max) && (y>0) && (y<y_max)&& (z+1)>0 && (z+1)<(z_max))
                       {


                          char match_found=0;

                              for( int check_vv=0; check_vv<count_vv; 
check_vv++)
                                  {
                                     if (vvx[check_vv]==x && 
vvy[check_vv]==y && vvz[check_vv]==(z+1))
                                        {
                                           match_found=1;
                                        }
                                  }
                              if  (!match_found==1)
                                  {
                                     vvx[count_vv]=x;
                                     vvy[count_vv]=y;
                                     vvz[count_vv]=z+1;
                                     count_vv++;
                                     numb_voxels++;

                                  }
                          }
             }

       iteration[c]=numb_iterations;
       voxels[c]=numb_voxels;
       c++;
       numb_iterations++;

       for(z=0; z<z_max; z++)
             {
               for(y=0;y<y_max; y++)
                   {
                     for(x=0; x<x_max; x++)
                         {


                             
Iteration4D[i*z_max*y_max*x_max+z*x_max*y_max+y*x_max+x]=ImageArray[z*x_max*y_max+y*x_max+x];

                         }
                  }

            }

for (int j=0; j<count_vv; j++)
  {
    x=vvx[j];
    y=vvy[j];
    z=vvz[j];
    Iteration4D[i*z_max*y_max*x_max+z*x_max*y_max+y*x_max+x]=0;
 }
numb_voxels_added=numb_voxels;
numb_voxels=0;


 }

 //** 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);
// Create a cell picker.
    PickCommand* pickObserver = PickCommand::New();
    picker = vtkCellPicker::New();
    picker->AddObserver( vtkCommand::EndPickEvent, pickObserver );

// Create a text mapper and actor to display the results of picking.
    textMapper = vtkTextMapper::New();
    vtkTextProperty *tprop = textMapper->GetTextProperty();
    tprop->SetFontFamilyToArial();
    tprop->SetFontSize(12);
    tprop->BoldOn();
    tprop->SetColor(1, 0, 0);
    textActor = vtkActor2D::New();
    textActor->VisibilityOff();
    textActor->SetMapper(textMapper);


vtkInteractorStyleTrackballCamera *style = 
vtkInteractorStyleTrackballCamera::New();
    vtkCallbackCommand *pickerCommand = vtkCallbackCommand::New();
    pickerCommand->SetClientData(style);
    pickerCommand->SetCallback(PickerInteractionCallback);
    style->AddObserver(vtkCommand::LeftButtonPressEvent, pickerCommand);
    style->AddObserver(vtkCommand::MouseMoveEvent, pickerCommand);
    style->AddObserver(vtkCommand::LeftButtonReleaseEvent, pickerCommand);

    aRenderer->AddActor2D(textActor);
    aRenderer->SetBackground(1.5, 1.5, 1.5);

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


     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 );

     iren->Initialize();


     iren->Start();

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

 picker->RemoveObserver( pickObserver );
 picker->Delete();
    textMapper->Delete();
    textActor->Delete();
    pickerCommand->Delete();
    style->Delete();

pickObserver->Delete();
inputFile.close();
     //picker->Delete();
     Importer->Delete();
     skinExtractor->Delete();
     skinMapper->Delete();
     skin->Delete();
     aCamera->Delete();
     renWin->Delete();
     aRenderer->Delete();
  return 0;
 }







More information about the vtkusers mailing list