47 {
48
49 typedef itk::Image<TPixelType, Dimension> ImageType;
50
51 vtkMatrix4x4 *ijkToRasMatrix = vtkMatrix4x4::New();
52
53 if (rasToIjkMatrix == NULL)
54 {
55 std::cerr << "ITKWriteVTKImage: rasToIjkMatrix is null" << std::endl;
56 }
57 else
58 {
59 vtkMatrix4x4::Invert(rasToIjkMatrix, ijkToRasMatrix);
60 }
61 ijkToRasMatrix->Transpose();
62
63 typename ImageType::DirectionType direction;
64 typename ImageType::PointType origin;
65 direction.SetIdentity();
66
67 double mag[3];
68 int i;
69 for (i=0; i<3; i++)
70 {
71
72 mag[i] = 0;
73 for (int j=0; j<3; j++)
74 {
75 mag[i] += ijkToRasMatrix->GetElement(i,j)* ijkToRasMatrix->GetElement(i,j);
76 }
77 if (mag[i] == 0.0)
78 {
79 mag[i] = 1;
80 }
81 mag[i] = sqrt(mag[i]);
82 }
83
84 for ( i=0; i<3; i++)
85 {
86 int j;
87 for (j=0; j<3; j++)
88 {
89 ijkToRasMatrix->SetElement(i, j, ijkToRasMatrix->GetElement(i,j)/mag[i]);
90 }
91 }
92
93
94
95
96 vtkMatrix4x4* rasToLpsMatrix = vtkMatrix4x4::New();
97 rasToLpsMatrix->Identity();
98 rasToLpsMatrix->SetElement(0,0,-1);
99 rasToLpsMatrix->SetElement(1,1,-1);
100
101 vtkMatrix4x4* ijkToLpsMatrix = vtkMatrix4x4::New();
102 vtkMatrix4x4::Multiply4x4(ijkToRasMatrix, rasToLpsMatrix, ijkToLpsMatrix);
103
104 for ( i=0; i<Dimension; i++)
105 {
106 origin[i] = ijkToRasMatrix->GetElement(3,i);
107 int j;
108 for (j=0; j<Dimension; j++)
109 {
110 if (Dimension == 2)
111 {
112 direction[j][i] = (i == j) ? 1. : 0;
113 }
114 else
115 {
116 direction[j][i] = ijkToLpsMatrix->GetElement(i,j);
117 }
118 }
119 }
120
121 rasToLpsMatrix->Delete();
122 ijkToRasMatrix->Delete();
123 ijkToLpsMatrix->Delete();
124
125 origin[0] *= -1;
126 origin[1] *= -1;
127
128
129 typedef typename itk::VTKImageImport<ImageType> ImageImportType;
130 typename ImageImportType::Pointer itkImporter = ImageImportType::New();
131
132
133 vtkImageExport* vtkExporter = vtkImageExport::New();
134 vtkImageFlip* vtkFlip = vtkImageFlip::New();
135
136
137 typedef typename itk::ImageFileWriter<ImageType> ImageWriterType;
138 typename ImageWriterType::Pointer itkImageWriter = ImageWriterType::New();
139
141 {
142 itkImageWriter->UseCompressionOn();
143 }
144 else
145 {
146 itkImageWriter->UseCompressionOff();
147 }
148
149
150
151 vtkFlip->SetInputData( inputImage );
152 vtkExporter->SetInputData ( inputImage );
153 vtkFlip->SetFilteredAxis(1);
154 vtkFlip->FlipAboutOriginOn();
155
157
158
160 {
161 itk::LightObject::Pointer objectType =
163 itk::ImageIOBase* imageIOType = dynamic_cast< itk::ImageIOBase * >(
164 objectType.GetPointer());
165 if(imageIOType){
166 itkImageWriter->SetImageIO(imageIOType);
167 }
168 }
169 itkImageWriter->SetInput(itkImporter->GetOutput());
170
171 if (MeasurementFrameMatrix != NULL)
172 {
173 typedef std::vector<std::vector<double> > DoubleVectorType;
174 typedef itk::MetaDataObject<DoubleVectorType> MetaDataDoubleVectorType;
175 const itk::MetaDataDictionary & dictionary = itkImageWriter->GetMetaDataDictionary();
176
177 itk::MetaDataDictionary::ConstIterator itr = dictionary.Begin();
178 itk::MetaDataDictionary::ConstIterator end = dictionary.End();
179
180 while( itr != end )
181 {
182
183 itk::MetaDataObjectBase::Pointer entry = itr->second;
184 MetaDataDoubleVectorType::Pointer entryvalue
185 = dynamic_cast<MetaDataDoubleVectorType *>( entry.GetPointer() );
186 if( entryvalue )
187 {
188 int pos = itr->first.find( "NRRD_measurement frame" );
189 if( pos != -1 )
190 {
191 DoubleVectorType tagvalue;
192 tagvalue.resize( 3 );
193 for( int i = 0; i < 3; i++ )
194 {
195 tagvalue[i].resize( 3 );
196 for( int j = 0; j < 3; j++ )
197 {
198 tagvalue[i][j] = MeasurementFrameMatrix->GetElement(i, j);
199 }
200 }
201 entryvalue->SetMetaDataObjectValue( tagvalue );
202 }
203 }
204 ++itr;
205 }
206 }
207
208 try
209 {
210 itkImporter->GetOutput()->SetDirection(direction);
211 itkImporter->GetOutput()->Update();
212 itkImporter->GetOutput()->SetOrigin(origin);
213 itkImporter->GetOutput()->SetSpacing(mag);
214 itkImageWriter->SetFileName( fileName );
215 itkImageWriter->Update();
216 }
217 catch (itk::ExceptionObject& exception)
218 {
219 exception.Print(std::cerr);
220 throw exception;
221 }
222
223 vtkExporter->Delete();
224 vtkFlip->Delete();
225}
virtual char * GetImageIOClassName()
Set/Get the ImageIO class name.
virtual int GetUseCompression()
use compression if possible
void ConnectPipelines(ITK_Exporter exporter, VTK_Importer *importer)