[vtkusers] vtk Example: Render window wthout a.ui file: my 3D image displayed as 2D image !
Adem breckon
adem.breckon at gmail.com
Thu Dec 5 07:20:33 EST 2013
Dear Vtk users,
I am trying to modify the example in:
http://www.cmake.org/Wiki/VTK/Examples/Cxx/Qt/RenderWindowNoUiFile
so it can display an 3D input image(image.hdr) instead of the sphere. the
input image is
itk::Image.
I can manage to display the 3D image however it is displayed as 2D image as
I can not rotate or zoon in or out the 3D image and this is oppsite to the
original code in which the sphere can be rotated or zoomed.
I attach my code for your attention.
I really need your help as I need to do a GUI for my project (I am using
itk, vtk and QT)
Thanks,
Adem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20131205/c59ea1ee/attachment.htm>
-------------- next part --------------
// now QT using itk and vtk
#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 <QApplication>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkImageViewer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <QVTKWidget.h>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QVTKWidget widget;
widget.resize(256,256);
//// original code for the sphere display
//// Setup sphere
//vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
//sphereSource->Update();
//vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
//sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
//vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
//sphereActor->SetMapper(sphereMapper);
//// Setup window
//vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
//// Setup renderer
//vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
//renderWindow->AddRenderer(renderer);
//renderer->AddActor(sphereActor);
//renderer->ResetCamera();
//widget.SetRenderWindow(renderWindow);
//// end of original code for the sphere display
// My new code: Disply Volume
typedef itk::Image<float, 3> VisualizingImageType;
typedef itk::ImageFileReader< VisualizingImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( "C:/Images/3Dimage.hdr" );
reader->Update();
VisualizingImageType::Pointer image=reader->GetOutput();
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
ren1->SetBackground(1.0f,1.0f,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<vtkGPUVolumeRayCastMapper> volumeMapper = vtkSmartPointer<vtkGPUVolumeRayCastMapper>::New();
volumeMapper->SetInputData(image2);
vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
compositeOpacity->AddPoint(0.0, 0.0);
compositeOpacity->AddPoint(60000.0, 1.0);
volumeProperty->SetScalarOpacity(compositeOpacity);
vtkSmartPointer<vtkColorTransferFunction> color = vtkSmartPointer<vtkColorTransferFunction>::New();
color->SetColorSpaceToRGB();
color->AddRGBPoint(0.0, 0.0, 0.0, 1.0);
color->AddRGBPoint(9500.0, 0.0, 1.0, 0.0);
color->AddRGBPoint(46000.0, 1.0, 0.0, 0.0);
color->AddRGBPoint(60000.0, 1.0, 0.0, 0.0);
volumeProperty->SetColor(color);
vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
ren1->AddVolume( volume );
ren1->ResetCamera();
widget.SetRenderWindow(renWin);
//end of my new code
widget.show();
app.exec();
return EXIT_SUCCESS;
}
-------------- next part --------------
cmake_minimum_required(VERSION 2.8)
if(POLICY CMP0020)
cmake_policy(SET CMP0020 NEW)
endif()
PROJECT(RenderWindowNoUiFile)
FIND_PACKAGE ( ITK)
IF ( ITK_FOUND)
INCLUDE( ${USE_ITK_FILE} )
ENDIF( ITK_FOUND)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
if(${VTK_VERSION} VERSION_GREATER "6" AND VTK_QT_VERSION VERSION_GREATER "4")
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
find_package(Qt5Widgets REQUIRED QUIET)
else()
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
file(GLOB UI_FILES *.ui)
file(GLOB QT_WRAP *.h)
file(GLOB CXX_FILES *.cxx)
if(${VTK_VERSION} VERSION_GREATER "6" AND VTK_QT_VERSION VERSION_GREATER "4")
qt5_wrap_ui(UISrcs ${UI_FILES} )
# CMAKE_AUTOMOC in ON so the MocHdrs will be automatically wrapped.
add_executable(RenderWindowNoUiFile MACOSX_BUNDLE
${CXX_FILES} ${UISrcs} ${QT_WRAP})
qt5_use_modules(RenderWindowNoUiFile Core Gui)
target_link_libraries(RenderWindowNoUiFile ${VTK_LIBRARIES} ${ITK_LIBRARIES})
else()
QT4_WRAP_UI(UISrcs ${UI_FILES})
QT4_WRAP_CPP(MOCSrcs ${QT_WRAP})
add_executable(RenderWindowNoUiFile MACOSX_BUNDLE ${CXX_FILES} ${UISrcs} ${MOCSrcs})
if(VTK_LIBRARIES)
if(${VTK_VERSION} VERSION_LESS "6")
target_link_libraries(RenderWindowNoUiFile ${VTK_LIBRARIES} ${ITK_LIBRARIES} QVTK)
else()
target_link_libraries(RenderWindowNoUiFile ${VTK_LIBRARIES} ${ITK_LIBRARIES})
endif()
else()
target_link_libraries(RenderWindowNoUiFile vtkHybrid QVTK vtkViews ${QT_LIBRARIES} ${ITK_LIBRARIES})
endif()
endif()
More information about the vtkusers
mailing list