[vtkusers] help with displaying a volume

anna han wan anna.han.wan at gmail.com
Wed Jul 20 12:11:13 EDT 2011


Dear all,

I am trying to display a volume and inserting a sphere once the left button
of the mouse is clicked.

My code is as bellow which is just a modification of one code I’ve found in
vtk mailing list.

The output of my code is not as I have expected: I got only the spheres on
the click but the input volume in which I want to insert the sphere is not
displayed.

Can someone point me on what is wrong in my code as I am newbie to vtk and I
need really your help?

Thank you in advance for your time.

Anna



// displaying a sphere in a volume on a mouse click

#include <vtkRenderWindow.h>

#include <vtkRenderWindowInteractor.h>

#include <vtkRenderer.h>

#include <vtkSphereSource.h>

#include <vtkPolyDataMapper.h>

#include <vtkActor.h>

#include <vtkSmartPointer.h>

#include <vtkPointPicker.h>

#include <vtkCamera.h>

#include <vtkInteractorStyleTrackballCamera.h>

#include <vtkObjectFactory.h>

#include <vtkCoordinate.h>

#include <vtkProperty.h>

#include <vtkImageDataGeometryFilter.h>

///

#include <iostream>

#include <fstream>

#include "itkImage.h"

#include "itkImageFileReader.h"

#include "itkImageFileWriter.h"

#include "itkVTKImageExport.h"

#include "itkVTKImageImport.h"

#include "itkRescaleIntensityImageFilter.h"

#include "vtkImageImport.h"

#include "vtkImageExport.h"

#include "vtkVolumeRayCastCompositeFunction.h"

#include "vtkVolume.h"

#include "vtkVolumeProperty.h"

#include "vtkPiecewiseFunction.h"

#include "vtkFixedPointVolumeRayCastMapper.h"

#include "vtkColorTransferFunction.h"

#include "vtkRenderer.h"

#include "vtkRenderWindow.h"

#include "vtkRenderWindowInteractor.h"

#include "vtkInteractorStyleTrackballCamera.h"

#include "vtkVolumeRayCastMapper.h"

#include "vtkCamera.h"

#include <vtkVolumePicker.h>

#include <vtkSmartPointer.h>

#include <vtkRendererCollection.h>

#include <vtkSphereSource.h>

// new

#include <vtkSphereSource.h>

#include <vtkPolyDataMapper.h>

#include <vtkActor.h>

#include <vtkSmartPointer.h>

#include <vtkPointPicker.h>

#include <vtkInteractorStyleTrackballCamera.h>

#include <vtkObjectFactory.h>

#include <vtkCoordinate.h>

#include <vtkProperty.h>

typedef float PixelType;

const unsigned int Dimension = 3;

typedef itk::Image< PixelType, Dimension > ImageType;

ImageType::SpacingType gVoxelSpacing;

// This function will connect the given itk::VTKImageExport filter to the
given vtkImageImport filter.

template <typename ITK_Exporter, typename VTK_Importer>

void ConnectPipelines(ITK_Exporter exporter, VTK_Importer* importer)

{

importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());

importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());

importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());

importer->SetSpacingCallback(exporter->GetSpacingCallback());

importer->SetOriginCallback(exporter->GetOriginCallback());

importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());

importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());

importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());

importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());

importer->SetDataExtentCallback(exporter->GetDataExtentCallback());

importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());

importer->SetCallbackUserData(exporter->GetCallbackUserData());

}

// Define interaction style

class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera

{

public:

static MouseInteractorStyle* New();

vtkTypeRevisionMacro(MouseInteractorStyle,
vtkInteractorStyleTrackballCamera);

virtual void OnLeftButtonDown()

{

cout << "Pressed left mouse button." << endl;

// forward events

double x = this->Interactor->GetEventPosition()[0];

double y = this->Interactor->GetEventPosition()[1];

cout << x << " " << y << endl;

vtkSmartPointer<vtkCoordinate> coordinate =
vtkSmartPointer<vtkCoordinate>::New();

coordinate->SetCoordinateSystemToDisplay();

coordinate->SetValue(x,y);

double *pt = new double[3];

pt = coordinate->GetComputedWorldValue(render);

cout << pt[0] << " " <<pt[1] << " " << pt[2] << endl;

vtkCamera *camera = render->GetActiveCamera();

double *n = new double[2];

n = camera->GetClippingRange();

cout << "Cuureent n[0] " << n[0]<< endl;

vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();

sphereSource->SetCenter(pt[0],pt[1],pt[2]*1*n[0]);

sphereSource->SetRadius(0.01);

sphereSource->Update();

vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();

mapper->SetInputConnection(sphereSource->GetOutputPort());

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();

actor->SetMapper(mapper);

render->AddActor(actor);

actor->GetProperty()->SetColor(1,0,0);

//render->ResetCamera();

//render->ResetCameraClippingRange();

render->GetRenderWindow()->GetInteractor()->Render();

// vtkInteractorStyleTrackballCamera::OnLeftButtonDown();

}

virtual void OnMiddleButtonDown()

{

cout << "Pressed middle mouse button." << endl;

// forward events

//vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();

}

virtual void OnRightButtonDown()

{

cout << "Pressed right mouse button." << endl;

// forward events

vtkInteractorStyleTrackballCamera::OnRightButtonDown();

}

virtual void OnRightButtonUp()

{

vtkCamera *camera = render->GetActiveCamera();

camera->SetClippingRange(1,30);

vtkInteractorStyleTrackballCamera::OnRightButtonUp();

}

vtkSmartPointer<vtkRenderer> render;

};

vtkCxxRevisionMacro(MouseInteractorStyle, "$Revision: 1.1 $");

vtkStandardNewMacro(MouseInteractorStyle);

 int main ( int argc, char* argv[] )

{

typedef itk::ImageFileReader< ImageType > ReaderType;

typedef itk::ImageFileWriter <ImageType> WriterType;

ReaderType::Pointer reader = ReaderType::New();

WriterType::Pointer writer = WriterType::New();

reader->SetFileName("MyVolume.hdr");

try

{

reader->Update();

const ImageType::SizeType imageSize
=reader->GetOutput()->GetLargestPossibleRegion().GetSize();

std::cout << "ImageSize= ";

std::cout << imageSize[0] << ", " << imageSize[1] << ", " << imageSize[2] <<
std::endl;

gVoxelSpacing =reader->GetOutput()->GetSpacing();

std::cout << "Spacing= ";

std::cout << gVoxelSpacing[0] << ", " << gVoxelSpacing[1] << ", " <<
gVoxelSpacing[2] << std::endl;

}

catch( itk::ExceptionObject & err )

{

std::cerr << "ExceptionObject caught !" << std::endl;

std::cerr << err << std::endl;

return EXIT_FAILURE;

}

// convert from float to unsigned short since vtkVolumeRayCastMapper Cannot
volume render data of type float, only unsigned char or unsigned short.

typedef unsigned short DisplayPixelType; // Display Pixel = 16bit

typedef itk::Image< DisplayPixelType, Dimension > DisplayImageType; //
define DisplayImageType

typedef itk::RescaleIntensityImageFilter<ImageType, DisplayImageType >
FilterType;

FilterType::Pointer filter = FilterType::New();

filter->SetOutputMinimum( 0 );

filter->SetOutputMaximum( 20000 );

filter->SetInput( reader->GetOutput() );

// Create the vtkImageImport and connect it to the itk::VTKImageExport
instance.

typedef itk::VTKImageExport< DisplayImageType > ExportFilterType;

ExportFilterType::Pointer itkExporter = ExportFilterType::New();

itkExporter->SetInput( filter->GetOutput() );

vtkImageImport* vtkImporter = vtkImageImport::New();

ConnectPipelines(itkExporter, vtkImporter);

// Convert the vtkImageData to a vtkPolydata

vtkSmartPointer<vtkImageDataGeometryFilter> imageDataGeometryFilter =
vtkSmartPointer<vtkImageDataGeometryFilter>::New();

imageDataGeometryFilter->SetInputConnection(vtkImporter->GetOutputPort());

imageDataGeometryFilter->Update();

 //rendering --------------------------------------------------

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();

renderer->SetBackground(1,1,1); // Background color white

vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();

renderWindow->AddRenderer(renderer);

renderWindow->SetSize(500,500);

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();

renderWindowInteractor->SetRenderWindow ( renderWindow );

vtkSmartPointer<MouseInteractorStyle> style =
vtkSmartPointer<MouseInteractorStyle>::New();

renderWindowInteractor->SetInteractorStyle( style );

style->render = renderer;

renderWindowInteractor->Initialize();

renderer->ResetCamera();

//renderer->ResetCameraClippingRange();

renderWindow->Render();

vtkCamera *camera = renderer->GetActiveCamera();

camera->SetClippingRange(1,30);

double *n = new double[2];

n = camera->GetClippingRange();

vtkSmartPointer<vtkCoordinate> coordinate =
vtkSmartPointer<vtkCoordinate>::New();

coordinate->SetCoordinateSystemToDisplay();

coordinate->SetValue(250,147);

double *pt = new double[3];

pt = coordinate->GetComputedWorldValue(renderer);

cout << pt[0] << " " <<pt[1] << " " << pt[2] << endl;

vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();

/* vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();

sphereSource->SetCenter(pt[0],pt[1],pt[2]*1*n[0]);

sphereSource->SetRadius(0.1);

sphereSource->Update();

mapper->SetInputConnection(sphereSource->GetOutputPort());*/

mapper->SetInputConnection(imageDataGeometryFilter->GetOutputPort());

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();

actor->SetMapper(mapper);

renderer->AddActor(actor);

renderWindowInteractor->Start();

return EXIT_SUCCESS;

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110720/39de6d1b/attachment.htm>


More information about the vtkusers mailing list