[vtkusers] Problem with Callback function

vishal itkhelpacc at gmail.com
Mon Jan 11 01:04:57 EST 2016


hi,
Im trying to printout the rotation values(rotation abt x,y,z) of a volume
using callback function... im follwing the example
[1]http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/ClientData ....
the flow of mycode goes something  like this..

read 3D volume in ITK--> Visualize in VTK(using
itkImageToVTKImageFilter.h)--> interact with volume -> upon press any key
through callbackFunction called and using volume->GetOrientation() method in
the callback function....I can get the angles values-> print to console...
upon building the example code the radius was printed out ... but then i did
some changes so that it matches my requirements the angles are not printed
out... no errors were found during building
the code is found below->  

#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
////////////////////////////
#include <itkImageFileReader.h>
#include "itkImageToVTKImageFilter.h"
 
#include <vtkSmartPointer.h>
#include <vtkGPUVolumeRayCastMapper.h>
#include <vtkColorTransferFunction.h>
#include <vtkPiecewiseFunction.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkVolumeProperty.h>
#include <vtkMatrix4x4.h>
#include <vtkAxesActor.h>
#include "vtkSmartVolumeMapper.h"
#include <vtkImageActor.h>
#include <vtkImageMapper3D.h>
#include <vtkImageCast.h>
#include <vtkDICOMImageReader.h>
#include <vtkTransform.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkObjectFactory.h>
///////////////////////////
static void KeypressCallbackFunction ( vtkObject* caller, long unsigned int
eventId,
          void* clientData, void* callData );
double printOrient[3];
double org[3];
int main(int argc, char *argv[])
{
 
	argv[1] ="OS_Volume.dcm"; // 3D volume that i want to display
	 typedef itk::Image<signed short, 3> VisualizingImageType;
	
    typedef itk::ImageFileReader< VisualizingImageType >  ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName( argv[1] );
    reader->Update();
    VisualizingImageType::Pointer image=reader->GetOutput();
 
    vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
    vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
    ren1->SetBackground(0.5f,0.5f,1.0f);
 
    renWin->AddRenderer(ren1);
    renWin->SetSize(1280,1024);

    vtkSmartPointer<vtkRenderWindowInteractor> iren = 
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);
    renWin->Render(); // make sure we have an OpenGL context.
    typedef itk::ImageToVTKImageFilter<VisualizingImageType>
itkVtkConverter;
    itkVtkConverter::Pointer conv=itkVtkConverter::New();
    conv->SetInput(image);
    conv->Update();
 
    vtkSmartPointer<vtkImageData>
image2=vtkSmartPointer<vtkImageData>::New();
    image2->ShallowCopy(conv->GetOutput());
    //shallow copy is vtk's equivalent of disconnect pipeline
	vtkSmartPointer<vtkSmartVolumeMapper> volumeMapper = 
    vtkSmartPointer<vtkSmartVolumeMapper>::New();
    volumeMapper->SetInputData(image2);
	vtkSmartPointer<vtkVolumeProperty> volumeProperty = 
    vtkSmartPointer<vtkVolumeProperty>::New();
 
    vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity = 
    vtkSmartPointer<vtkPiecewiseFunction>::New();

	compositeOpacity->AddPoint(-3024, 0, 0.5, 0.0 );
    compositeOpacity->AddPoint(-16, 0, .49, .61 );
    compositeOpacity->AddPoint(641, .72, .5, 0.0 );
    compositeOpacity->AddPoint(3071, .71, 0.5, 0.0);
    volumeProperty->SetScalarOpacity(compositeOpacity);
	volumeMapper->SetBlendModeToComposite();
    vtkSmartPointer<vtkColorTransferFunction> color = 
    vtkSmartPointer<vtkColorTransferFunction>::New();
    color->AddRGBPoint( -3024, 0, 0, 0, 0.5, 0.0 );
    color->AddRGBPoint( -16, 0.73, 0.25, 0.30, 0.49, .61 );
    color->AddRGBPoint( 641, .90, .82, .56, .5, 0.0 );
    color->AddRGBPoint( 3071, 1, 1, 1, .5, 0.0 );
    volumeProperty->SetColor(color);
 //changes
	volumeProperty->SetInterpolationTypeToLinear();
	volumeProperty->ShadeOn();
    volumeProperty->SetAmbient(0.1);
    volumeProperty->SetDiffuse(0.9);
    volumeProperty->SetSpecular(0.2);
    volumeProperty->SetSpecularPower(10.0);
    volumeProperty->SetScalarOpacityUnitDistance(0.8919);
    vtkSmartPointer<vtkVolume> volume = 
    vtkSmartPointer<vtkVolume>::New();
    volume->SetMapper(volumeMapper);
    volume->SetProperty(volumeProperty);
	volume->SetScale(1.5);
    //Here we take care of position and orientation
    //so that volume is in DICOM patient physical space
    VisualizingImageType::DirectionType d=image->GetDirection();
    vtkMatrix4x4 *mat=vtkMatrix4x4::New(); //start with identity matrix
    for (int i=0; i<3; i++)
        for (int k=0; k<3; k++)
            mat->SetElement(i,k, d(i,k));
	//counteract the built-in translation by origin
    VisualizingImageType::PointType origin=image->GetOrigin();
    volume->SetPosition(-origin[0], -origin[1], -origin[2]);
    //add translation to the user matrix
    for (int i=0; i<3; i++)
        mat->SetElement(i,3, origin[i]);
    volume->SetUserMatrix(mat);
 
		 vtkSmartPointer<vtkCallbackCommand> keypressCallback =
    vtkSmartPointer<vtkCallbackCommand>::New();
		 
	  // // Allow the observer to access the sphereSource
	 keypressCallback->SetClientData(volume);
	keypressCallback->SetCallback(KeypressCallbackFunction );
	renWin->AddObserver(vtkCommand::KeyPressEvent, keypressCallback);
    //Add coordinate system axes, so we have a reference for position and
orientation
    vtkSmartPointer<vtkAxesActor> axes =
vtkSmartPointer<vtkAxesActor>::New();
    axes->SetTotalLength(250,250,250); //ORIGINAL
    axes->SetShaftTypeToCylinder();
    axes->SetCylinderRadius(0.01);
	
	ren1->AddActor(axes);
    ren1->AddVolume( volume ); 
    ren1->ResetCamera();
 
    renWin->Render();


 
    iren->Start();
 
  return EXIT_SUCCESS;
}

void KeypressCallbackFunction(vtkObject *, long unsigned int
vtkNotUsed(eventId), void* clientData, void* vtkNotUsed(callData) )
{
	
  // Prove that we can access the sphere source
	vtkVolume* volume =
    static_cast<vtkVolume*>(clientData);
	volume->GetOrigin(org);
	volume->GetOrientation(printOrient);
	std::cout << "Radius is " << printOrient[1]<<"	"<<printOrient[2]<<"
"<<printOrient[3]<< std::endl;
	std::cout << "ORGIN" << org[1]<<"	"<<org[2]<<"	"<<org[3]<< std::endl;
}

please help me out...
Regards
Vishal



--
View this message in context: http://vtk.1045678.n5.nabble.com/Problem-with-Callback-function-tp5735905.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list