[vtkusers] Rotating and Translating (for rotating and reversing sagittal and axial views) for a vtkPolyData file?
John Drozd
john.drozd at gmail.com
Mon May 14 15:20:46 EDT 2012
Hi,
I read in a .vtp file using vtkXMLPolyDataReader.
But my output is rotated when viewing the outputted Meta Image File in
Slicer3.3.6.
Is there an online reference for applying rotation and translation
transformations to a vtkPolyData file.
Below is my code: (I modified ReadPolyDataFileAndConvertToImageData.cxx):
(I apologize for the commented lines of code which I use for back referencing)
Thank you,
John
#include <vtkXMLPolyDataReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkImageData.h>
#include <vtkMetaImageWriter.h>
#include <vtkPolyDataToImageStencil.h>
#include <vtkImageStencil.h>
#include <vtkPointData.h>
#include <vtkTransform.h>
#include <vtkAxesActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkImageActor.h>
#include <vtkImageMapper.h>
#include <vtkImageCast.h>
#include <vtkImageReslice.h>
#include "vtkImageToImageStencil.h"
int main ( int argc, char *argv[] )
{
// Parse command line arguments
if(argc != 2)
{
std::cerr << "Usage: " << argv[0]
<< " Filename(.vtp)" << std::endl;
return EXIT_FAILURE;
}
std::string filename = argv[1];
// Read all the data from the file
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();
vtkSmartPointer<vtkPolyData> pd = reader->GetOutput();
vtkSmartPointer<vtkImageData> whiteImage =
vtkSmartPointer<vtkImageData>::New();
double bounds[6];
pd->GetBounds(bounds);
double spacing[3]; // desired volume spacing
spacing[0] = 0.94772;
spacing[1] = 0.94352;
spacing[2] = 1.2021;
whiteImage->SetSpacing(spacing);
// compute dimensions
int dim[3];
for (int i = 0; i < 3; i++)
{
dim[i] = static_cast<int>(ceil((bounds[i * 2 + 1] - bounds[i * 2])
/ spacing[i]));
}
// revised dimensions
dim[0] = 166;
dim[1] = 256;
dim[2] = 256;
// end of revised dimensions
whiteImage->SetDimensions(dim);
//whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1);
whiteImage->SetExtent(166 - 1, 330, 0, 256 - 1, 0, 256 - 1);
double origin[3];
//added
std::cout<<"bounds[0]: "<<bounds[0]<<std::endl;
std::cout<<"bounds[2]: "<<bounds[2]<<std::endl;
std::cout<<"bounds[4]: "<<bounds[4]<<std::endl;
std::cout<<"spacing[0]: "<<spacing[0]<<std::endl;
std::cout<<"spacing[1]: "<<spacing[1]<<std::endl;
std::cout<<"spacing[2]: "<<spacing[2]<<std::endl;
origin[0] = bounds[0] + spacing[0] / 2;
origin[1] = bounds[2] + spacing[1] / 2;
origin[2] = bounds[4] + spacing[2] / 2;
std::cout<<"origin[0]: "<<origin[0]<<std::endl;
std::cout<<"origin[1]: "<<origin[1]<<std::endl;
std::cout<<"origin[2]: "<<origin[2]<<std::endl;
origin[0] = -99.0-99.0-99.0+41.63-0.004;
origin[1] = -156.31;
origin[2] = 178.0179-62.989+0.06;
std::cout<<"origin[0]: "<<origin[0]<<std::endl;
std::cout<<"origin[1]: "<<origin[1]<<std::endl;
std::cout<<"origin[2]: "<<origin[2]<<std::endl;
whiteImage->SetOrigin(origin);
#if VTK_MAJOR_VERSION <= 5
whiteImage->SetScalarTypeToUnsignedChar();
whiteImage->AllocateScalars();
#else
whiteImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
#endif
// fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = whiteImage->GetNumberOfPoints();
for (vtkIdType i = 0; i < count; ++i)
{
whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval);
}
//////
//added per vtklectureb_7.ppt
/// Scaling along the x-axis, y-axis, z-axis as a percentage i.e.
100 = identity
//float _sx,_sy,_sz;
/// Skew angle in the x-y ... plane (in degrees)
//float _sxy, _syx,_syz,_szy,_szx,_sxz;
/// Translation along the x-axis (in mm)
float _tx,_ty, _tz;
/// Rotation around the x-axis (in degrees)
//float _rx,_ry,_rz ;
// Additional options for mirroring of an image
//int FlipX;
//int FlipY;
//int FlipZ;
_tx = 0.0;
_ty = 0.0;
//tz is origin_labelmap minus origin_greyscale
_tz = 0.0;
// Create the transformation
vtkSmartPointer<vtkTransform> tr =
vtkSmartPointer<vtkTransform>::New();
tr->PostMultiply();
//transform1a->Translate(10.0, 0.0, 0.0);
tr->Identity(); tr->PostMultiply();
tr->Translate(_tx,_ty,_tz);
float angle;
float pi;
pi = 4*(atan(1.0));
angle = pi/2;
tr->RotateZ(angle);
//added
tr->PostMultiply();
vtkImageReslice* resamp=vtkImageReslice::New();
resamp->SetInput(whiteImage);
resamp->SetResliceTransform(tr);
//added
//resamp->UpDate();
///////
// polygonal data --> image stencil:
vtkSmartPointer<vtkPolyDataToImageStencil> pol2stenc =
vtkSmartPointer<vtkPolyDataToImageStencil>::New();
#if VTK_MAJOR_VERSION <= 5
pol2stenc->SetInput(pd);
#else
pol2stenc->SetInputData(pd);
#endif
pol2stenc->SetOutputOrigin(origin);
pol2stenc->SetOutputSpacing(spacing);
pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent());
//whiteimage->Update();
// cut the corresponding white image and set the background:
vtkSmartPointer<vtkImageStencil> imgstenc =
vtkSmartPointer<vtkImageStencil>::New();
#if VTK_MAJOR_VERSION <= 5
imgstenc->SetInput(resamp->GetOutput());
imgstenc->SetStencil(pol2stenc->GetOutput());
#else
imgstenc->SetInputData(resamp->GetOutput());
imgstenc->SetStencilConnection(pol2stenc->GetOutputPort());
#endif
imgstenc->ReverseStencilOff();
imgstenc->SetBackgroundValue(outval);
imgstenc->Update();
vtkSmartPointer<vtkMetaImageWriter> writer =
vtkSmartPointer<vtkMetaImageWriter>::New();
writer->SetFileName("VentricleVolumesp01vtkitkorientcorrected.mhd");
#if VTK_MAJOR_VERSION <= 5
writer->SetInput(imgstenc->GetOutput());
#else
writer->SetInputData(imgstenc->GetOutput());
#endif
writer->Write();
return EXIT_SUCCESS;
}
--
John Drozd, Ph.D.
Financial Secretary
Knights of Columbus
St. Pius X council 10561
Holy Family Parish
Post-Doctoral Scholar (Fellow)
Robarts Research Institute
Western University
London, ON, Canada
cell: 519-282-3340
http://publish.uwo.ca/~jdrozd2
More information about the vtkusers
mailing list