VMTK
Loading...
Searching...
No Matches
vtkvmtkITKArchetypeImageSeriesReader.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Copyright Brigham and Women's Hospital (BWH) All Rights Reserved.
4
5 See COPYRIGHT.txt
6 or http://www.slicer.org/copyright/copyright.txt for details.
7
8 Program: vtkITK
9 Module: $HeadURL$
10 Date: $Date$
11 Version: $Revision$
12
13==========================================================================*/
14
15// VTKITK includes
17
18// VTK includes
19#include <vtkImageData.h>
20#include <vtkMath.h>
21#include <vtkMatrix4x4.h>
22#include <vtkInformation.h>
23#include <vtkInformationVector.h>
24#include <vtkObjectFactory.h>
25#include <vtkStreamingDemandDrivenPipeline.h>
26
27// ITK includes
28#include <itkMetaDataDictionary.h>
29#include <itkMetaDataObjectBase.h>
30#include <itkMetaDataObject.h>
31#include <itkTimeProbe.h>
32
33// STD includes
34#include <vector>
35
36#include "itkArchetypeSeriesFileNames.h"
37#include "itkOrientImageFilter.h"
38#include "itkImageSeriesReader.h"
39#include "itkGDCMSeriesFileNames.h"
40#include "itkGDCMImageIO.h"
41
43
44//----------------------------------------------------------------------------
46{
47 this->Archetype = NULL;
48 this->IndexArchetype = 0;
49 this->SingleFile = 1;
50 this->UseOrientationFromFile = 1;
51 this->RasToIjkMatrix = NULL;
52 this->MeasurementFrameMatrix = vtkMatrix4x4::New();
53 this->MeasurementFrameMatrix->Identity();
56 this->FileNameSliceOffset = 0;
57 this->FileNameSliceSpacing = 1;
58 this->FileNameSliceCount = 0;
59 this->UseNativeOrigin = true;
60 this->OutputScalarType = VTK_FLOAT;
61 this->NumberOfComponents = 0;
62 this->UseNativeScalarType = 0;
63 for (int i = 0; i < 3; i++)
64 {
65 this->DefaultDataSpacing[i] = 1.0;
66 this->DefaultDataOrigin[i] = 0.0;
67 }
68
69 this->SeriesInstanceUIDs.resize( 0 );
70 this->ContentTime.resize( 0 );
71 this->TriggerTime.resize( 0 );
72 this->EchoNumbers.resize( 0 );
73 this->DiffusionGradientOrientation.resize( 0 );
74 this->SliceLocation.resize( 0 );
75 this->ImageOrientationPatient.resize( 0 );
76
77 this->AnalyzeHeader = true;
78
79 this->GroupingByTags = false;
80 this->IsOnlyFile = false;
81
82 this->SelectedUID = -1;
83 this->SelectedContentTime = -1;
84 this->SelectedTriggerTime = -1;
85 this->SelectedEchoNumbers = -1;
86 this->SelectedDiffusion = -1;
87 this->SelectedSlice = -1;
88 this->SelectedOrientation = -1;
89
90 this->SetNumberOfInputPorts(0);
91 this->SetNumberOfOutputPorts(1);
92}
93
94//----------------------------------------------------------------------------
96{
97 if (this->Archetype)
98 {
99 delete [] this->Archetype;
100 this->Archetype = NULL;
101 }
102 if (RasToIjkMatrix)
103 {
104 RasToIjkMatrix->Delete();
105 RasToIjkMatrix = NULL;
106 }
108 {
109 MeasurementFrameMatrix->Delete();
111 }
112
113}
114
116{
117 this->UpdateInformation();
118 return RasToIjkMatrix;
119}
120
122{
123 this->UpdateInformation();
125}
126
127//----------------------------------------------------------------------------
128void vtkvmtkITKArchetypeImageSeriesReader::PrintSelf(std::ostream& os, vtkIndent indent)
129{
130 int idx;
131
132 this->Superclass::PrintSelf(os,indent);
133
134 os << indent << "Archetype: " <<
135 (this->Archetype ? this->Archetype : "(none)") << "\n";
136
137 os << indent << "FileNameSliceOffset: "
138 << this->FileNameSliceOffset << "\n";
139 os << indent << "FileNameSliceSpacing: "
140 << this->FileNameSliceSpacing << "\n";
141 os << indent << "FileNameSliceCount: "
142 << this->FileNameSliceCount << "\n";
143
144 os << indent << "OutputScalarType: "
145 << vtkImageScalarTypeNameMacro(this->OutputScalarType)
146 << std::endl;
147 os << indent << "DefaultDataSpacing: (" << this->DefaultDataSpacing[0];
148 for (idx = 1; idx < 3; ++idx)
149 {
150 os << ", " << this->DefaultDataSpacing[idx];
151 }
152 os << ")\n";
153
154 os << indent << "DefaultDataOrigin: (" << this->DefaultDataOrigin[0];
155 for (idx = 1; idx < 3; ++idx)
156 {
157 os << ", " << this->DefaultDataOrigin[idx];
158 }
159 os << ")\n";
160
161}
162
163//----------------------------------------------------------------------------
164int vtkvmtkITKArchetypeImageSeriesReader::CanReadFile(const char* vtkNotUsed(filename))
165{
166 if (this->Archetype == NULL)
167 {
168 return false;
169 }
170
171 std::string fileNameCollapsed = itksys::SystemTools::CollapseFullPath( this->Archetype);
172
173 // First see if the archetype exists
174 if (!itksys::SystemTools::FileExists (fileNameCollapsed.c_str()))
175 {
176 vtkDebugMacro(<<"The filename does not exist.");
177 return false;
178 }
179 return true;
180}
181
182//----------------------------------------------------------------------------
183// This method returns the largest data that can be generated.
185 vtkInformation * vtkNotUsed(request),
186 vtkInformationVector **vtkNotUsed(inputVector),
187 vtkInformationVector *outputVector)
188{
189 // get the info objects
190 vtkInformation* outInfo = outputVector->GetInformationObject(0);
191
192 std::vector<std::string> candidateFiles;
193 std::vector<std::string> candidateSeries;
194 int extent[6];
195 std::string fileNameCollapsed = itksys::SystemTools::CollapseFullPath( this->Archetype);
196
197 if ( this->SingleFile )
198 {
199 this->AnalyzeHeader = false;
200 }
201
202 // Since we only need origin, spacing and extents, we can use one
203 // image type.
204 typedef itk::Image<float,3> ImageType;
205 itk::ImageRegion<3> region;
206
207 typedef itk::ImageSource<ImageType> FilterType;
208 FilterType::Pointer filter;
209
210 // First see if the archetype exists, if it's not a pointer into memory
211 if (fileNameCollapsed.find("slicer:") != std::string::npos &&
212 fileNameCollapsed.find("#") != std::string::npos)
213 {
214 vtkDebugMacro("File " << fileNameCollapsed.c_str() << " is a pointer to the mrml scene in memory, not checking for it on disk");
215 }
216 else
217 {
218 if (!itksys::SystemTools::FileExists (fileNameCollapsed.c_str()))
219 {
220 vtkErrorMacro( "vtkvmtkITKArchetypeImageSeriesReader::ExecuteInformation: Archetype file " << fileNameCollapsed.c_str() << " does not exist.");
221 return 0;
222 }
223 }
224
225 this->AllFileNames.resize( 0 );
226
227 // Some file types require special processing
228 itk::GDCMImageIO::Pointer dicomIO = itk::GDCMImageIO::New();
229
230 // Test whether the input file is a DICOM file
231 bool isDicomFile = dicomIO->CanReadFile(this->Archetype);
232
233 // if user already set up FileNames, we do not try to find candidate files
234 if ( this->GetNumberOfFileNames() > 0 )
235 {
236 unsigned int nFiles = this->GetNumberOfFileNames();
237 this->AllFileNames.resize( 0 );
238 for (unsigned int k = 0; k < nFiles; k++)
239 {
240 this->AllFileNames.push_back( this->FileNames[k] );
241 }
242 this->FileNames.resize( 0 );
243
244 // if this is the only file set by user
245 if (nFiles == 1)
246 {
247 this->IsOnlyFile = true;
248 }
249
250 // if we need to analyze the header
251 if ( AnalyzeHeader )
252 {
253 this->AnalyzeDicomHeaders();
254 }
255 }
256 else
257 {
258 if ( isDicomFile && !this->GetSingleFile() )
259 {
260 typedef itk::GDCMSeriesFileNames DICOMNameGeneratorType;
261 DICOMNameGeneratorType::Pointer inputImageFileGenerator = DICOMNameGeneratorType::New();
262 std::string fileNamePath = itksys::SystemTools::GetFilenamePath( this->Archetype );
263 if (fileNamePath == "")
264 {
265 fileNamePath = ".";
266 }
267 inputImageFileGenerator->SetDirectory( fileNamePath );
268
269 // determine if the file is diffusion weighted MR file
270
271 // Find the series that contains the archetype
272 candidateSeries = inputImageFileGenerator->GetSeriesUIDs();
273
274 // Find all dicom files in the directory
275 for (unsigned int s = 0; s < candidateSeries.size(); s++)
276 {
277 std::vector<std::string> seriesFileNames;
278 seriesFileNames = inputImageFileGenerator->GetFileNames( candidateSeries[s] );
279 for (unsigned int f = 0; f < seriesFileNames.size(); f++)
280 {
281 this->AllFileNames.push_back( seriesFileNames[f] );
282 }
283 }
284 //int nFiles = this->AllFileNames.size(); UNUSED
285
286 // analysis dicom files and fill the Dicom Tag arrays
287 if ( AnalyzeHeader )
288 {
289 this->AnalyzeDicomHeaders();
290 }
291
292 // the following for loop set up candidate files with same series number
293 // that include the given Archetype;
294 int found = 0;
295 for (unsigned int s = 0; s < candidateSeries.size() && found == 0; s++)
296 {
297 candidateFiles = inputImageFileGenerator->GetFileNames(candidateSeries[s]);
298 for (unsigned int f = 0; f < candidateFiles.size(); f++)
299 {
300 if (itksys::SystemTools::CollapseFullPath(candidateFiles[f].c_str()) ==
301 fileNameCollapsed)
302 {
303 found = 1;
304 break;
305 }
306 }
307 }
308
309 // do we have just one candidate file
310 if ( candidateFiles.size() == 1 )
311 {
312 this->IsOnlyFile = true;
313 }
314 }
315 else if( !this->GetSingleFile() )
316 { // not dicom
317 // check the dimensions of the archetype - if there
318 // is more then one slice, use only the archetype
319 // but if it is a single slice, try to generate a
320 // series of filenames
321 itk::ImageFileReader<ImageType>::Pointer imageReader =
322 itk::ImageFileReader<ImageType>::New();
323 imageReader->SetFileName(this->Archetype);
324 imageReader->UpdateOutputInformation();
325 region = imageReader->GetOutput()->GetLargestPossibleRegion();
326 if ( region.GetSize()[2] > 1 )
327 {
328 candidateFiles.push_back( this->Archetype );
329 this->AllFileNames.push_back( this->Archetype );
330 this->IsOnlyFile = true;
331 }
332 else
333 {
334 // Generate filenames from the Archetype
335 itk::ArchetypeSeriesFileNames::Pointer fit = itk::ArchetypeSeriesFileNames::New();
336 fit->SetArchetype (this->Archetype);
337 candidateFiles = fit->GetFileNames();
338 this->AllFileNames.resize( candidateFiles.size() );
339 for (int f = 0; f < (int)(candidateFiles.size()); f ++)
340 {
341 this->AllFileNames[f] = candidateFiles[f];
342 }
343 if ( candidateFiles.size() == 1 )
344 {
345 this->IsOnlyFile = true;
346 }
347 else if ( AnalyzeHeader )
348 {
349 this->AnalyzeDicomHeaders();
350 }
351 }
352 }
353 else
354 {
355 this->AllFileNames.push_back( this->Archetype );
356 this->IsOnlyFile = true;
357 }
358 }
359
360 // figure out the index of Archetype in AllFileNames
361 for (unsigned int k = 0; k < this->AllFileNames.size(); k++)
362 {
363 if (this->AllFileNames[k] == this->Archetype)
364 {
365 this->IndexArchetype = k;
366 break;
367 }
368 }
369
370 // Reduce the selection of filenames
371 if ( this->IsOnlyFile || this->SingleFile )
372 {
373 this->FileNames.resize( 0 );
374 this->FileNames.push_back( this->Archetype );
375 }
376 else
377 {
378 if ( !GroupingByTags )
379 {
381 }
382 else
383 {
391 }
392 }
393
394 if (RasToIjkMatrix)
395 {
396 RasToIjkMatrix->Delete();
397 }
398 RasToIjkMatrix = vtkMatrix4x4::New();
399
400 vtkMatrix4x4* IjkToLpsMatrix = vtkMatrix4x4::New();
401
402 RasToIjkMatrix->Identity();
403 IjkToLpsMatrix->Identity();
404
405 double spacing[3];
406 double origin[3];
407
408
409 itk::ImageIOBase::Pointer imageIO = nullptr;
410
411 try
412 {
413 // If there is only one file in the series, just use an image file reader
414 if (this->FileNames.size() == 1)
415 {
416 itk::OrientImageFilter<ImageType,ImageType>::Pointer orient =
417 itk::OrientImageFilter<ImageType,ImageType>::New();
418 itk::ImageFileReader<ImageType>::Pointer imageReader =
419 itk::ImageFileReader<ImageType>::New();
420 imageReader->SetFileName(this->FileNames[0].c_str());
421 if (isDicomFile)
422 {
423 imageReader->SetImageIO(dicomIO);
424 }
425
427 {
428 imageReader->UpdateOutputInformation();
429 filter = imageReader;
430 }
431 else
432 {
433 orient->SetInput(imageReader->GetOutput());
434 orient->UseImageDirectionOn();
435 orient->SetDesiredCoordinateOrientation(this->DesiredCoordinateOrientation);
436 orient->UpdateOutputInformation();
437 filter = orient;
438 }
439 for (int i = 0; i < 3; i++)
440 {
441 spacing[i] = filter->GetOutput()->GetSpacing()[i];
442 origin[i] = filter->GetOutput()->GetOrigin()[i];
443
444 // Get IJK to RAS direction vector
445 for ( unsigned int j=0; j < filter->GetOutput()->GetImageDimension (); j++ )
446 {
447 IjkToLpsMatrix->SetElement(j, i, spacing[i]*filter->GetOutput()->GetDirection()[j][i]);
448 }
449 }
450 region = filter->GetOutput()->GetLargestPossibleRegion();
451 extent[0] = region.GetIndex()[0];
452 extent[1] = region.GetIndex()[0] + region.GetSize()[0] - 1;
453 extent[2] = region.GetIndex()[1];
454 extent[3] = region.GetIndex()[1] + region.GetSize()[1] - 1;
455 extent[4] = region.GetIndex()[2];
456 extent[5] = region.GetIndex()[2] + region.GetSize()[2] - 1;
457
458 typedef std::vector<std::vector<double> > DoubleVectorType;
459 typedef itk::MetaDataObject<DoubleVectorType> MetaDataDoubleVectorType;
460 const itk::MetaDataDictionary & dictionary = imageReader->GetMetaDataDictionary();
461 itk::MetaDataDictionary::ConstIterator itr = dictionary.Begin();
462 itk::MetaDataDictionary::ConstIterator end = dictionary.End();
463 while( itr != end )
464 {
465 itk::MetaDataObjectBase::Pointer entry = itr->second;
466 MetaDataDoubleVectorType::Pointer entryvalue1
467 = dynamic_cast<MetaDataDoubleVectorType *>( entry.GetPointer() );
468 if( entryvalue1 )
469 {
470 int pos = itr->first.find( "NRRD_measurement frame" );
471 if( pos != -1 )
472 {
473 DoubleVectorType tagvalue = entryvalue1->GetMetaDataObjectValue();
474 for( int i = 0; i < 3; i++ )
475 {
476 for( int j = 0; j < 3; j++ )
477 {
478 this->MeasurementFrameMatrix->SetElement(i,j, tagvalue.at( j ).at( i ));
479 }
480 }
481 }
482 }
483 ++itr;
484 }
485
486
487 imageIO = imageReader->GetImageIO();
488 if (imageIO.GetPointer() == NULL)
489 {
490 vtkErrorMacro( "vtkvmtkITKArchetypeImageSeriesReader::ExecuteInformation: ImageIO for file " << fileNameCollapsed.c_str() << " does not exist.");
491 return 0;
492 }
493 }
494 else
495 {
496 itk::OrientImageFilter<ImageType,ImageType>::Pointer orient =
497 itk::OrientImageFilter<ImageType,ImageType>::New();
498 itk::ImageSeriesReader<ImageType>::Pointer seriesReader =
499 itk::ImageSeriesReader<ImageType>::New();
500 seriesReader->SetFileNames(this->FileNames);
501 if (isDicomFile)
502 {
503 seriesReader->SetImageIO(dicomIO);
504 }
505 else
506 {
507 itk::ImageFileReader<ImageType>::Pointer imageReader =
508 itk::ImageFileReader<ImageType>::New();
509 imageReader->SetFileName(this->FileNames[0].c_str());
510 imageReader->UpdateOutputInformation();
511 imageIO = imageReader->GetImageIO();
512 seriesReader->SetImageIO(imageIO);
513 }
515 {
516 seriesReader->UpdateOutputInformation();
517 filter = seriesReader;
518 }
519 else
520 {
521 orient->SetInput(seriesReader->GetOutput());
522 orient->UseImageDirectionOn();
523 orient->SetDesiredCoordinateOrientation(this->DesiredCoordinateOrientation);
524 orient->UpdateOutputInformation();
525 filter = orient;
526 }
527 for (int i = 0; i < 3; i++)
528 {
529 spacing[i] = filter->GetOutput()->GetSpacing()[i];
530 origin[i] = filter->GetOutput()->GetOrigin()[i];
531 // Get IJK to RAS direction vector
532 for ( unsigned int j=0; j < filter->GetOutput()->GetImageDimension (); j++ )
533 {
534 IjkToLpsMatrix->SetElement(j, i, spacing[i]*filter->GetOutput()->GetDirection()[j][i]);
535 }
536 }
537
538 region = filter->GetOutput()->GetLargestPossibleRegion();
539 extent[0] = region.GetIndex()[0];
540 extent[1] = region.GetIndex()[0] + region.GetSize()[0] - 1;
541 extent[2] = region.GetIndex()[1];
542 extent[3] = region.GetIndex()[1] + region.GetSize()[1] - 1;
543 extent[4] = region.GetIndex()[2];
544 extent[5] = region.GetIndex()[2] + region.GetSize()[2] - 1;
545 imageIO = seriesReader->GetImageIO();
546
547 }
548 }
549 catch (itk::ExceptionObject& e)
550 {
551 IjkToLpsMatrix->Delete();
552 vtkErrorMacro( "vtkvmtkITKArchetypeImageSeriesReader::ExecuteInformation: Cannot open " << fileNameCollapsed.c_str() << ". "
553 << "ITK exception info: error in " << e.GetLocation() << ": "<< e.GetDescription());
554 return 0;
555 }
556 // Transform from LPS to RAS
557 vtkMatrix4x4* LpsToRasMatrix = vtkMatrix4x4::New();
558 LpsToRasMatrix->Identity();
559 LpsToRasMatrix->SetElement(0,0,-1);
560 LpsToRasMatrix->SetElement(1,1,-1);
561
562 vtkMatrix4x4* LPSMeasurementFrameMatrix = vtkMatrix4x4::New();
563 LPSMeasurementFrameMatrix->DeepCopy(this->MeasurementFrameMatrix);
564
565 vtkMatrix4x4::Multiply4x4(LpsToRasMatrix,IjkToLpsMatrix, RasToIjkMatrix);
566 vtkMatrix4x4::Multiply4x4(LpsToRasMatrix,LPSMeasurementFrameMatrix, this->MeasurementFrameMatrix);
567 LpsToRasMatrix->Delete();
568 LPSMeasurementFrameMatrix->Delete();
569
570 // If it looks like the pipeline did not provide the spacing and
571 // origin, modify the spacing and origin with the defaults
572 for (int j = 0; j < 3; j++)
573 {
574 if (spacing[j] == 1.0)
575 {
576 spacing[j] = this->DefaultDataSpacing[j];
577 }
578 if (origin[j] == 0.0)
579 {
580 origin[j] = this->DefaultDataOrigin[j];
581 }
582 }
583
584 origin[0] *= -1; // L -> R
585 origin[1] *= -1; // P -> A
586
587 if (this->UseNativeOrigin)
588 {
589 for (int j = 0; j < 3; j++)
590 {
591 RasToIjkMatrix->SetElement(j, 3, origin[j]);
592 }
593 RasToIjkMatrix->Invert();
594 }
595 else
596 {
597 RasToIjkMatrix->Invert();
598 for (int j = 0; j < 3; j++)
599 {
600 RasToIjkMatrix->SetElement(j, 3, (extent[2*j+1] - extent[2*j])/2.0);
601 }
602 }
603
604 outInfo->Set(vtkDataObject::SPACING(), spacing, 3);
605 outInfo->Set(vtkDataObject::ORIGIN(), origin, 3);
606 RasToIjkMatrix->SetElement(3,3,1.0);
607 IjkToLpsMatrix->Delete();
608
609 if ( !this->GetUseOrientationFromFile() )
610 {
611 RasToIjkMatrix->Identity();
612 for ( unsigned int j=0; j < 3; j++ )
613 {
614 RasToIjkMatrix->SetElement(j, j, 1.0/spacing[j]);
615 }
616 }
617
618 outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), extent, 6);
619 int scalarType = this->OutputScalarType;
620 if (this->UseNativeScalarType)
621 {
622 // If there is only one file in the series
623 if (this->FileNames.size() == 1)
624 {
625 if (imageIO.GetPointer() == NULL)
626 {
627 scalarType = VTK_SHORT; // TODO - figure out why multi-file series doen't have an imageIO
628 }
629#if (ITK_VERSION_MAJOR >= 5)
630 else if (imageIO->GetComponentType() == itk::IOComponentEnum::UCHAR)
631#else
632 else if (imageIO->GetComponentType() == itk::ImageIOBase::UCHAR)
633#endif
634 {
635 scalarType = VTK_UNSIGNED_CHAR;
636 }
637#if (ITK_VERSION_MAJOR >= 5)
638 else if (imageIO->GetComponentType() == itk::IOComponentEnum::CHAR)
639#else
640 else if (imageIO->GetComponentType() == itk::ImageIOBase::CHAR)
641#endif
642 {
643 scalarType = VTK_CHAR;
644 }
645#if (ITK_VERSION_MAJOR >= 5)
646 else if (imageIO->GetComponentType() == itk::IOComponentEnum::USHORT)
647#else
648 else if (imageIO->GetComponentType() == itk::ImageIOBase::USHORT)
649#endif
650 {
651 scalarType = VTK_UNSIGNED_SHORT;
652 }
653#if (ITK_VERSION_MAJOR >= 5)
654 else if (imageIO->GetComponentType() == itk::IOComponentEnum::SHORT)
655#else
656 else if (imageIO->GetComponentType() == itk::ImageIOBase::SHORT)
657#endif
658 {
659 scalarType = VTK_SHORT;
660 }
661#if (ITK_VERSION_MAJOR >= 5)
662 else if (imageIO->GetComponentType() == itk::IOComponentEnum::UINT)
663#else
664 else if (imageIO->GetComponentType() == itk::ImageIOBase::UINT)
665#endif
666 {
667 scalarType = VTK_UNSIGNED_INT;
668 }
669#if (ITK_VERSION_MAJOR >= 5)
670 else if (imageIO->GetComponentType() == itk::IOComponentEnum::INT)
671#else
672 else if (imageIO->GetComponentType() == itk::ImageIOBase::INT)
673#endif
674 {
675 scalarType = VTK_INT;
676 }
677#if (ITK_VERSION_MAJOR >= 5)
678 else if (imageIO->GetComponentType() == itk::IOComponentEnum::ULONG)
679#else
680 else if (imageIO->GetComponentType() == itk::ImageIOBase::ULONG)
681#endif
682 {
683 scalarType = VTK_UNSIGNED_LONG;
684 }
685#if (ITK_VERSION_MAJOR >= 5)
686 else if (imageIO->GetComponentType() == itk::IOComponentEnum::LONG)
687#else
688 else if (imageIO->GetComponentType() == itk::ImageIOBase::LONG)
689#endif
690 {
691 scalarType = VTK_LONG;
692 }
693#if (ITK_VERSION_MAJOR >= 5)
694 else if (imageIO->GetComponentType() == itk::IOComponentEnum::FLOAT)
695#else
696 else if (imageIO->GetComponentType() == itk::ImageIOBase::FLOAT)
697#endif
698 {
699 scalarType = VTK_FLOAT;
700 }
701#if (ITK_VERSION_MAJOR >= 5)
702 else if (imageIO->GetComponentType() == itk::IOComponentEnum::DOUBLE)
703#else
704 else if (imageIO->GetComponentType() == itk::ImageIOBase::DOUBLE)
705#endif
706 {
707 scalarType = VTK_DOUBLE;
708 }
709 }
710 else
711 {
712 double min = 0, max = 0;
713
714 for( unsigned int f = 0; f < this->FileNames.size(); f++ )
715 {
716 imageIO->SetFileName( this->FileNames[f] );
717 imageIO->ReadImageInformation();
718
719#if (ITK_VERSION_MAJOR >= 5)
720 if ( imageIO->GetComponentType() == itk::IOComponentEnum::UCHAR )
721#else
722 if ( imageIO->GetComponentType() == itk::ImageIOBase::UCHAR )
723#endif
724 {
725 min = std::numeric_limits<uint8_t>::min() < min ? std::numeric_limits<uint8_t>::min() : min;
726 max = std::numeric_limits<uint8_t>::max() > max ? std::numeric_limits<uint8_t>::max() : max;
727 }
728#if (ITK_VERSION_MAJOR >= 5)
729 if ( imageIO->GetComponentType() == itk::IOComponentEnum::CHAR )
730#else
731 if ( imageIO->GetComponentType() == itk::ImageIOBase::CHAR )
732#endif
733 {
734 min = std::numeric_limits<int8_t>::min() < min ? std::numeric_limits<int8_t>::min() : min;
735 max = std::numeric_limits<int8_t>::max() > max ? std::numeric_limits<int8_t>::max() : max;
736 }
737#if (ITK_VERSION_MAJOR >= 5)
738 if ( imageIO->GetComponentType() == itk::IOComponentEnum::USHORT )
739#else
740 if ( imageIO->GetComponentType() == itk::ImageIOBase::USHORT )
741#endif
742 {
743 min = std::numeric_limits<uint16_t>::min() < min ? std::numeric_limits<uint16_t>::min() : min;
744 max = std::numeric_limits<uint16_t>::max() > max ? std::numeric_limits<uint16_t>::max() : max;
745 }
746#if (ITK_VERSION_MAJOR >= 5)
747 if ( imageIO->GetComponentType() == itk::IOComponentEnum::SHORT )
748#else
749 if ( imageIO->GetComponentType() == itk::ImageIOBase::SHORT )
750#endif
751 {
752 min = std::numeric_limits<int16_t>::min() < min ? std::numeric_limits<int16_t>::min() : min;
753 max = std::numeric_limits<int16_t>::max() > max ? std::numeric_limits<int16_t>::max() : max;
754 }
755#if (ITK_VERSION_MAJOR >= 5)
756 if ( imageIO->GetComponentType() == itk::IOComponentEnum::UINT )
757#else
758 if ( imageIO->GetComponentType() == itk::ImageIOBase::UINT )
759#endif
760 {
761 min = std::numeric_limits<uint32_t>::min() < min ? std::numeric_limits<uint32_t>::min() : min;
762 max = std::numeric_limits<uint32_t>::max() > max ? std::numeric_limits<uint32_t>::max() : max;
763 }
764#if (ITK_VERSION_MAJOR >= 5)
765 if ( imageIO->GetComponentType() == itk::IOComponentEnum::INT )
766#else
767 if ( imageIO->GetComponentType() == itk::ImageIOBase::INT )
768#endif
769 {
770 min = std::numeric_limits<int32_t>::min() < min ? std::numeric_limits<int32_t>::min() : min;
771 max = std::numeric_limits<int32_t>::max() > max ? std::numeric_limits<int32_t>::max() : max;
772 }
773#if (ITK_VERSION_MAJOR >= 5)
774 if ( imageIO->GetComponentType() == itk::IOComponentEnum::ULONG )
775#else
776 if ( imageIO->GetComponentType() == itk::ImageIOBase::ULONG )
777#endif
778 { // note that on windows ULONG is only 32 bit
779 min = std::numeric_limits<uint64_t>::min() < min ? std::numeric_limits<uint64_t>::min() : min;
780 max = std::numeric_limits<uint64_t>::max() > max ? std::numeric_limits<uint64_t>::max() : max;
781 }
782#if (ITK_VERSION_MAJOR >= 5)
783 if ( imageIO->GetComponentType() == itk::IOComponentEnum::LONG )
784#else
785 if ( imageIO->GetComponentType() == itk::ImageIOBase::LONG )
786#endif
787 { // note that on windows LONG is only 32 bit
788 min = std::numeric_limits<int64_t>::min() < min ? std::numeric_limits<int64_t>::min() : min;
789 max = std::numeric_limits<int64_t>::max() > max ? std::numeric_limits<int64_t>::max() : max;
790 }
791#if (ITK_VERSION_MAJOR >= 5)
792 if ( imageIO->GetComponentType() == itk::IOComponentEnum::FLOAT )
793#else
794 if ( imageIO->GetComponentType() == itk::ImageIOBase::FLOAT )
795#endif
796 {
797 // use -max() as min() for both float and double as temp workaround
798 // should switch to lowest() function in C++ 11 in the future
799 min = -std::numeric_limits<float>::max() < min ? -std::numeric_limits<float>::max() : min;
800 max = std::numeric_limits<float>::max() > max ? std::numeric_limits<float>::max() : max;
801 }
802#if (ITK_VERSION_MAJOR >= 5)
803 if ( imageIO->GetComponentType() == itk::IOComponentEnum::DOUBLE )
804#else
805 if ( imageIO->GetComponentType() == itk::ImageIOBase::DOUBLE )
806#endif
807 {
808 min = -std::numeric_limits<double>::max() < min ? -std::numeric_limits<double>::max() : min;
809 max = std::numeric_limits<double>::max() > max ? std::numeric_limits<double>::max() : max;
810 }
811 }
812 assert( min <= max );
813 if( min >= 0 ) // unsigned
814 {
815 if( max <= std::numeric_limits<uint8_t>::max() )
816 {
817 scalarType = VTK_UNSIGNED_CHAR;
818 }
819 else if( max <= std::numeric_limits<uint16_t>::max() )
820 {
821 scalarType = VTK_UNSIGNED_SHORT;
822 }
823 else if( max <= std::numeric_limits<uint32_t>::max() )
824 {
825 scalarType = VTK_UNSIGNED_INT;
826 }
827 else if( max <= std::numeric_limits<uint64_t>::max() )
828 {
829 scalarType = VTK_UNSIGNED_LONG;
830 }
831 else if( max <= std::numeric_limits<float>::max() )
832 {
833 scalarType = VTK_FLOAT;
834 }
835 else if( max <= std::numeric_limits<double>::max() )
836 {
837 scalarType = VTK_DOUBLE;
838 }
839 else
840 {
841 assert(0);
842 }
843 }
844 else
845 {
846 if( max <= std::numeric_limits<int8_t>::max()
847 && min >= std::numeric_limits<int8_t>::min() )
848 {
849 scalarType = VTK_CHAR;
850 }
851 else if( max <= std::numeric_limits<int16_t>::max()
852 && min >= std::numeric_limits<int16_t>::min() )
853 {
854 scalarType = VTK_SHORT;
855 }
856 else if( max <= std::numeric_limits<int32_t>::max()
857 && min >= std::numeric_limits<int32_t>::min() )
858 {
859 scalarType = VTK_INT;
860 }
861 else if( max <= std::numeric_limits<int64_t>::max()
862 && min >= std::numeric_limits<int64_t>::min() )
863 {
864 scalarType = VTK_LONG;
865 }
866 else if ( max <= std::numeric_limits<float>::max()
867 && min >= -std::numeric_limits<float>::max() )
868 {
869 scalarType = VTK_FLOAT;
870 }
871 else if( max <= std::numeric_limits<double>::max()
872 && min >= -std::numeric_limits<double>::max() )
873 {
874 scalarType = VTK_DOUBLE;
875 }
876 else
877 {
878 assert(0);
879 }
880 }
881 }
882 }
883
884 int numberOfComponents = 1;
885 if (imageIO.GetPointer() != NULL)
886 {
887 numberOfComponents = imageIO->GetNumberOfComponents();
888 }
889 this->SetNumberOfComponents(numberOfComponents);
890 this->SetOutputScalarType(scalarType);
891
892
893 vtkDataObject::SetPointDataActiveScalarInfo(outInfo,
894 scalarType, numberOfComponents);
895
896 // Copy the MetaDataDictionary from the ITK layer to the VTK layer
897 if (imageIO.GetPointer() != NULL)
898 {
899 this->Dictionary = imageIO->GetMetaDataDictionary();
900 }
901 else
902 {
903 this->Dictionary = itk::MetaDataDictionary();
904 }
906 return 1;
907}
908
909//----------------------------------------------------------------------------
911{
912 this->FileNames.resize( 0 );
913 // int nFiles = this->AllFileNames.size(); UNUSED
914
915 unsigned int nSlices = this->GetNumberOfSliceLocation();
916
917 for (unsigned int k = 0; k < nSlices; k++)
918 {
919 const char* name = GetNthFileName( 0, -1, -1, -1, 0, k, 0, n );
920 if (name == NULL)
921 {
922 continue;
923 }
924 std::string nameInString (name);
925 this->FileNames.push_back(nameInString);
926 }
927
928}
929
930//----------------------------------------------------------------------------
932{
933 return this->FileNames.size();
934}
935
936//----------------------------------------------------------------------------
938{
939 return this->FileNames;
940}
941
942//----------------------------------------------------------------------------
943const char* vtkvmtkITKArchetypeImageSeriesReader::GetNthFileName ( int idxSeriesInstanceUID,
944 int idxContentTime,
945 int idxTriggerTime,
946 int idxEchoNumbers,
947 int idxDiffusionGradientOrientation,
948 int idxSliceLocation,
949 int idxImageOrientationPatient,
950 int n )
951{
952 int nFiles = this->AllFileNames.size();
953 int count = 0;
954 std::string FirstName;
955 for (int k = 0; k < nFiles; k++)
956 {
957 if ( (this->IndexSeriesInstanceUIDs[k] != idxSeriesInstanceUID && this->IndexSeriesInstanceUIDs[k] >= 0 && idxSeriesInstanceUID >= 0) ||
958 (this->IndexContentTime[k] != idxContentTime && this->IndexContentTime[k] >= 0 && idxContentTime >= 0) ||
959 (this->IndexTriggerTime[k] != idxTriggerTime && this->IndexTriggerTime[k] >= 0 && idxTriggerTime >= 0) ||
960 (this->IndexEchoNumbers[k] != idxEchoNumbers && this->IndexEchoNumbers[k] >= 0 && idxEchoNumbers >= 0) ||
961 (this->IndexDiffusionGradientOrientation[k] != idxDiffusionGradientOrientation && this->IndexDiffusionGradientOrientation[k] >= 0 && idxDiffusionGradientOrientation >= 0) ||
962 (this->IndexSliceLocation[k] != idxSliceLocation && this->IndexSliceLocation[k] >= 0 && idxSliceLocation >= 0) ||
963 (this->IndexImageOrientationPatient[k] != idxImageOrientationPatient && this->IndexImageOrientationPatient[k] >= 0 && idxImageOrientationPatient >= 0) )
964 {
965 continue;
966 }
967 else
968 {
969 if (count == n)
970 {
971 return this->AllFileNames[k].c_str();
972 }
973 else if( count == 0 )
974 {
975 FirstName = this->AllFileNames[k];
976 count ++;
977 }
978 else
979 {
980 count ++;
981 }
982 }
983 }
984 return NULL;
985}
986
987//----------------------------------------------------------------------------
989 int idxContentTime,
990 int idxTriggerTime,
991 int idxEchoNumbers,
992 int idxDiffusionGradientOrientation,
993 int idxSliceLocation,
994 int idxImageOrientationPatient )
995{
996 this->FileNames.resize( 0 );
997 int nFiles = this->AllFileNames.size();
998 for (int k = 0; k < nFiles; k++)
999 {
1000 if ( (this->IndexSeriesInstanceUIDs[k] != idxSeriesInstanceUID && this->IndexSeriesInstanceUIDs[k] >= 0 && idxSeriesInstanceUID >= 0) ||
1001 (this->IndexContentTime[k] != idxContentTime && this->IndexContentTime[k] >= 0 && idxContentTime >= 0) ||
1002 (this->IndexTriggerTime[k] != idxTriggerTime && this->IndexTriggerTime[k] >= 0 && idxTriggerTime >= 0) ||
1003 (this->IndexEchoNumbers[k] != idxEchoNumbers && this->IndexEchoNumbers[k] >= 0 && idxEchoNumbers >= 0) ||
1004 (this->IndexDiffusionGradientOrientation[k] != idxDiffusionGradientOrientation && this->IndexDiffusionGradientOrientation[k] >= 0 && idxDiffusionGradientOrientation >= 0) ||
1005 (this->IndexSliceLocation[k] != idxSliceLocation && this->IndexSliceLocation[k] >= 0 && idxSliceLocation >= 0) ||
1006 (this->IndexImageOrientationPatient[k] != idxImageOrientationPatient && this->IndexImageOrientationPatient[k] >= 0 && idxImageOrientationPatient >= 0) )
1007 {
1008 continue;
1009 }
1010 else
1011 {
1012 this->FileNames.push_back( this->AllFileNames[k] );
1013 }
1014 }
1015 return;
1016}
1017
1018//----------------------------------------------------------------------------
1020{
1021 itk::TimeProbe AnalyzeTime;
1022 AnalyzeTime.Start();
1023
1024 int nFiles = this->AllFileNames.size();
1025 typedef itk::Image<float,3> ImageType;
1026
1027 this->IndexSeriesInstanceUIDs.resize( nFiles );
1028 this->IndexContentTime.resize( nFiles );
1029 this->IndexTriggerTime.resize( nFiles );
1030 this->IndexEchoNumbers.resize( nFiles );
1031 this->IndexDiffusionGradientOrientation.resize( nFiles );
1032 this->IndexSliceLocation.resize( nFiles );
1033 this->IndexImageOrientationPatient.resize( nFiles );
1034 this->IndexImagePositionPatient.resize( nFiles );
1035
1036 this->SeriesInstanceUIDs.resize( 0 );
1037 this->ContentTime.resize( 0 );
1038 this->TriggerTime.resize( 0 );
1039 this->EchoNumbers.resize( 0 );
1040 this->DiffusionGradientOrientation.resize( 0 );
1041 this->SliceLocation.resize( 0 );
1042 this->ImageOrientationPatient.resize( 0 );
1043 this->ImagePositionPatient.resize( 0 );
1044
1045
1046 itk::GDCMImageIO::Pointer gdcmIO = itk::GDCMImageIO::New();
1047 if ( !gdcmIO->CanReadFile(this->Archetype) )
1048 {
1049 itk::ImageIOBase::Pointer lastImageIO;
1050 for (int f = 0; f < nFiles; f++)
1051 {
1052 itk::ImageFileReader<ImageType>::Pointer imageReader =
1053 itk::ImageFileReader<ImageType>::New();
1054 const std::string& fileName = this->AllFileNames[f];
1055 imageReader->SetFileName( fileName );
1056 // Try first to reuse the same imageIO for each file. If it fails, then use
1057 // the default imageIO
1058 if (lastImageIO && (lastImageIO->CanReadFile(fileName.c_str())))
1059 {
1060 imageReader->SetImageIO(lastImageIO);
1061 }
1062 else
1063 {
1064 imageReader->UpdateOutputInformation();
1065 lastImageIO = imageReader->GetImageIO();
1066 }
1067
1068 // Don't read the image header if it is a 2D file type.
1069 std::string IOType = imageReader->GetImageIO()->GetNameOfClass();
1070 bool ioHas3DInformation =
1071 IOType.find("BPMImageIO") != std::string::npos &&
1072 IOType.find("JPEGImageIO") != std::string::npos &&
1073 IOType.find("PNGImageIO") != std::string::npos &&
1074 IOType.find("TIFFImageIO") != std::string::npos &&
1075 IOType.find("RawImageIO") != std::string::npos;
1076 if (ioHas3DInformation)
1077 {
1078 imageReader->UpdateOutputInformation();
1079 lastImageIO = imageReader->GetImageIO();
1080 }
1081
1082 // insert series
1083 int idx = this->InsertSeriesInstanceUIDs( "Non-Dicom Series" );
1084 this->IndexSeriesInstanceUIDs[f] = idx;
1085
1086 // for now, assume ContentTime, TriggerTime, and DiffusionGradientOrientation do not exist
1087 this->IndexContentTime[f] = -1;
1088 this->IndexTriggerTime[f] = -1;
1089 this->IndexEchoNumbers[f] = -1;
1091
1092 // Slice Location
1093 if (ioHas3DInformation)
1094 {
1095 ImageType::PointType origin = imageReader->GetOutput()->GetOrigin();
1096 idx = this->InsertSliceLocation( origin[2] );
1097 }
1098 else
1099 {
1100 idx = this->InsertNextSliceLocation();
1101 }
1102 this->IndexSliceLocation[f] = idx;
1103
1104 // Orientation
1105 float sliceOrientation[6] = {1., 0., 0.,
1106 0., 1., 0.};
1107 if (ioHas3DInformation)
1108 {
1109 ImageType::DirectionType orientation =
1110 imageReader->GetOutput()->GetDirection();
1111 for (int k = 0; k < 3; k++)
1112 {
1113 sliceOrientation[k] = orientation[0][k];
1114 sliceOrientation[k+3] = orientation[1][k];
1115 }
1116 }
1117 idx = this->InsertImageOrientationPatient( sliceOrientation );
1118 this->IndexImageOrientationPatient[f] = idx;
1119 }
1120 return;
1121 }
1122
1123 // if Archetype is a Dicom File
1124 gdcmIO->SetFileName( this->Archetype );
1125 for (int f = 0; f < nFiles; f++)
1126 {
1127 gdcmIO->SetFileName( this->AllFileNames[f] );
1128 gdcmIO->ReadImageInformation();
1129 itk::MetaDataDictionary &dict = gdcmIO->GetMetaDataDictionary();
1130 std::string tagValue;
1131
1132 // series instance UID
1133 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0020|000e", tagValue );
1134 if ( tagValue.length() > 0 )
1135 {
1136 int idx = InsertSeriesInstanceUIDs( tagValue.c_str() );
1137 this->IndexSeriesInstanceUIDs[f] = idx;
1138 }
1139 else
1140 {
1141 this->IndexSeriesInstanceUIDs[f] = -1;
1142 }
1143
1144 // content time
1145 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0008|0033", tagValue );
1146 if ( tagValue.length() > 0 )
1147 {
1148 int idx = InsertContentTime( tagValue.c_str() );
1149 this->IndexContentTime[f] = idx;
1150 }
1151 else
1152 {
1153 this->IndexContentTime[f] = -1;
1154 }
1155
1156 // trigger time
1157 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0018|1060", tagValue );
1158 if ( tagValue.length() > 0 )
1159 {
1160 int idx = InsertTriggerTime( tagValue.c_str() );
1161 this->IndexTriggerTime[f] = idx;
1162 }
1163 else
1164 {
1165 this->IndexTriggerTime[f] = -1;
1166 }
1167
1168 // echo numbers
1169 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0018|0086", tagValue );
1170 if ( tagValue.length() > 0 )
1171 {
1172 int idx = InsertEchoNumbers( tagValue.c_str() );
1173 this->IndexEchoNumbers[f] = idx;
1174 }
1175 else
1176 {
1177 this->IndexEchoNumbers[f] = -1;
1178 }
1179
1180 // diffision gradient orientation
1181 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0010|9089", tagValue );
1182 if ( tagValue.length() > 0 )
1183 {
1184 float a[3];
1185 sscanf( tagValue.c_str(), "%f\\%f\\%f", a, a+1, a+2 );
1187 this->IndexDiffusionGradientOrientation[f] = idx;
1188 }
1189 else
1190 {
1192 }
1193
1194 // slice location
1195 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0020|1041", tagValue );
1196 if ( tagValue.length() > 0 )
1197 {
1198 float a;
1199 sscanf( tagValue.c_str(), "%f", &a );
1200 int idx = InsertSliceLocation( a );
1201 this->IndexSliceLocation[f] = idx;
1202 }
1203 else
1204 {
1205 this->IndexSliceLocation[f] = -1;
1206 }
1207
1208 // image orientation patient
1209 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0020|0037", tagValue );
1210 if ( tagValue.length() > 0 )
1211 {
1212 float a[6];
1213 sscanf( tagValue.c_str(), "%f\\%f\\%f\\%f\\%f\\%f", a, a+1, a+2, a+3, a+4, a+5 );
1214 int idx = InsertImageOrientationPatient( a );
1215 this->IndexImageOrientationPatient[f] = idx;
1216 }
1217 else
1218 {
1219 this->IndexImageOrientationPatient[f] = -1;
1220 }
1221 // image position patient
1222 tagValue.clear(); itk::ExposeMetaData<std::string>( dict, "0020|0032", tagValue );
1223 if( tagValue.length() > 0 )
1224 {
1225 float a[3];
1226 sscanf( tagValue.c_str(), "%f\\%f\\%f", a, a+1, a+2 );
1227 int idx = InsertImagePositionPatient( a );
1228 this->IndexImagePositionPatient[f] = idx;
1229 }
1230 else
1231 {
1232 this->IndexImagePositionPatient[f] = -1;
1233 }
1234 }
1235
1236 AnalyzeTime.Stop();
1237
1238 // double timeelapsed = AnalyzeTime.GetMean(); UNUSED
1239 AnalyzeHeader = false;
1240 return;
1241}
1242
1243//----------------------------------------------------------------------------
1244const itk::MetaDataDictionary&
1245vtkvmtkITKArchetypeImageSeriesReader
1246::GetMetaDataDictionary() const
1247{
1248 return this->Dictionary;
1249}
1250
1251//----------------------------------------------------------------------------
1253{
1254 int nItems = this->Dictionary.GetKeys().size();
1255 this->Tags.resize(0);
1256 this->TagValues.resize(0);
1257
1258 if (nItems == 0)
1259 {
1260 return;
1261 }
1262
1263 std::vector<std::string> keys = this->Dictionary.GetKeys();
1264 for (int k = 0; k < nItems; k++)
1265 {
1266 this->Tags.push_back( keys[k] );
1267 std::string tagvalue;
1268 itk::ExposeMetaData<std::string>(this->Dictionary, keys[k], tagvalue);
1269 this->TagValues.push_back( tagvalue );
1270 }
1271}
1272
1273//----------------------------------------------------------------------------
1275{
1276 return this->Tags.size();
1277}
1278
1279//----------------------------------------------------------------------------
1281{
1282 return this->Dictionary.HasKey( tag );
1283}
1284
1285//----------------------------------------------------------------------------
1287{
1288 if (n >= this->Tags.size())
1289 {
1290 return NULL;
1291 }
1292 return this->Tags[n].c_str();
1293}
1294
1295//----------------------------------------------------------------------------
1297{
1298 if (n >= this->TagValues.size())
1299 {
1300 return NULL;
1301 }
1302 return this->TagValues[n].c_str();
1303}
1304
1305//----------------------------------------------------------------------------
1307{
1308 std::string tagstr (tag);
1309 for (unsigned int k = 0; k < this->Tags.size(); k++)
1310 {
1311 if (this->Tags[k] == tagstr)
1312 {
1313 return this->TagValues[k].c_str();
1314 }
1315 }
1316 return NULL;
1317}
1318
1319//----------------------------------------------------------------------------
1321{
1322 if ( n >= this->GetNumberOfFileNames() )
1323 {
1324 return NULL;
1325 }
1326
1327 return this->FileNames[n].c_str();
1328
1329}
1330
1331//----------------------------------------------------------------------------
1332unsigned int vtkvmtkITKArchetypeImageSeriesReader::AddFileName( const char* filename )
1333{
1334 std::string filenamestr (filename);
1335 this->FileNames.push_back( filenamestr );
1336 return this->FileNames.size();
1337}
1338
1339//----------------------------------------------------------------------------
1341{
1342 this->FileNames.resize( 0 );
1343 this->AllFileNames.resize( 0 );
1344 this->SeriesInstanceUIDs.resize( 0 );
1345 this->ContentTime.resize( 0 );
1346 this->TriggerTime.resize( 0 );
1347 this->EchoNumbers.resize( 0 );
1348 this->DiffusionGradientOrientation.resize( 0 );
1349 this->SliceLocation.resize( 0 );
1350 this->ImageOrientationPatient.resize( 0 );
1351}
1352
1353//----------------------------------------------------------------------------
1355{
1356 // we will set FileNames to have only volumes that match the archetype
1357 // and we will return the size of the list.
1358 // - if the files have ImagePositionPatient tags, we will sort
1359 // the files based on this information
1360 this->FileNames.resize(0);
1361
1362 // Note: Since IndexArchetype is unsigned int, it's always postive
1363 if (this->IndexArchetype >= this->IndexSeriesInstanceUIDs.size()
1364 || this->IndexArchetype >= this->IndexTriggerTime.size()
1365 || this->IndexArchetype >= this->IndexDiffusionGradientOrientation.size()
1366 || this->IndexArchetype >= this->IndexImageOrientationPatient.size())
1367 {
1368 vtkErrorMacro("AssembleVolumeContainingArchetype: index archetype "
1369 << this->IndexArchetype << " is out of bounds 0-" << this->IndexSeriesInstanceUIDs.size());
1370 return 0;
1371 }
1372
1373
1374 long int iArchetypeSeriesUID = this->IndexSeriesInstanceUIDs[this->IndexArchetype];
1375 long int iArchetypeEchoNumbers = this->IndexEchoNumbers[this->IndexArchetype];
1376 long int iArchetypeDiffusion = this->IndexDiffusionGradientOrientation[this->IndexArchetype];
1377 long int iArchetypeOrientation = this->IndexImageOrientationPatient[this->IndexArchetype];
1378
1379 // keep track of the locations for the selected files
1380 std::vector<std::pair <double, int> > fileNameSortKey;
1381 bool originSet = false;
1382
1383 // Sort good files based on distance from Origin to ImagePositionPatient along ScanAxis
1384 // Follows logic originally in LoadVolume.tcl
1385 for (unsigned int k = 0; k < this->AllFileNames.size(); k++)
1386 {
1387 if ( (this->IndexSeriesInstanceUIDs[k] != iArchetypeSeriesUID &&
1388 this->IndexSeriesInstanceUIDs[k] >= 0 && iArchetypeSeriesUID >= 0)
1389 ||
1390 (this->IndexEchoNumbers[k] != iArchetypeEchoNumbers &&
1391 this->IndexEchoNumbers[k] >= 0 && iArchetypeEchoNumbers >= 0)
1392 ||
1393 (this->IndexDiffusionGradientOrientation[k] != iArchetypeDiffusion &&
1394 this->IndexDiffusionGradientOrientation[k] >= 0 && iArchetypeDiffusion >= 0)
1395 ||
1396 (this->IndexImageOrientationPatient[k] != iArchetypeOrientation &&
1397 this->IndexImageOrientationPatient[k] >= 0 && iArchetypeOrientation >= 0) )
1398 {
1399 // file doesn't match our criteria
1400 continue;
1401 }
1402 else
1403 {
1404 // find the position and orientation corresponding to this
1405 // file - it could be that all files in the set have the same index
1406 // (1) or that there is no position information, in which case the
1407 // position index will be -1
1408 int kth_orientation = -1;
1409 int kth_position = -1;
1410 if ( ImagePositionPatient.size() != 0 )
1411 {
1412 kth_orientation = this->IndexImageOrientationPatient[k];
1413 kth_position = this->IndexImagePositionPatient[k];
1414 }
1415 if (kth_orientation > 0 && kth_position > 0)
1416 {
1417 if (!originSet)
1418 {
1419 std::vector<float> iopv = this->ImageOrientationPatient[kth_orientation];
1420 float iopf1[] = {iopv[0], iopv[1], iopv[2]};
1421 float iopf2[] = {iopv[3], iopv[4], iopv[5]};
1422
1423 vtkMath::Cross( iopf1, iopf2, this->ScanAxis );
1424 this->ScanOrigin[0] = ImagePositionPatient[kth_position][0];
1425 this->ScanOrigin[1] = ImagePositionPatient[kth_position][1];
1426 this->ScanOrigin[2] = ImagePositionPatient[kth_position][2];
1427 originSet = true;
1428 }
1429 float tempiop[3], diff[3];
1430 tempiop[0] = ImagePositionPatient[kth_position][0];
1431 tempiop[1] = ImagePositionPatient[kth_position][1];
1432 tempiop[2] = ImagePositionPatient[kth_position][2];
1433
1434 vtkMath::Subtract( tempiop, this->ScanOrigin, diff );
1435 float dist = vtkMath::Dot( diff, ScanAxis );
1436
1437 fileNameSortKey.push_back( std::make_pair(dist, k) );
1438 }
1439 else
1440 {
1441 // no position info, so use the file index
1442 fileNameSortKey.push_back( std::make_pair(k, k) );
1443 }
1444 }
1445 }
1446
1447 std::sort(fileNameSortKey.begin(), fileNameSortKey.end());
1448 std::vector<std::pair <double, int> >::iterator keyiter;
1449 for (keyiter = fileNameSortKey.begin(); keyiter != fileNameSortKey.end(); ++keyiter)
1450 {
1451 FileNames.push_back(AllFileNames[keyiter->second]);
1452 }
1453
1454 return this->FileNames.size();
1455}
Read a series of files that have a common naming convention.
virtual void SetOutputScalarType(int)
Get the file format. Pixels are this type in the file.
void PrintSelf(std::ostream &os, vtkIndent indent) override
virtual void SetNumberOfComponents(unsigned int)
Get number of scalars.
std::vector< std::vector< float > > ImagePositionPatient
virtual int CanReadFile(const char *filename)
is the given file name a NRRD file?
void SetDesiredCoordinateOrientationToAxial()
Set the orientation of the output image.
unsigned int GetNumberOfFileNames()
See how many file names were generated during ExecuteInformation.
vtkMatrix4x4 * GetRasToIjkMatrix()
Returns an IJK to RAS transformation matrix.
std::vector< std::vector< float > > DiffusionGradientOrientation
vtkMatrix4x4 * GetMeasurementFrameMatrix()
Returns the Measurement frame matrix.
virtual int RequestInformation(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
std::vector< long int > IndexSeriesInstanceUIDs
index of each dicom file into the above arrays
const char * GetNthFileName(int idxSeriesInstanceUID, int idxContentTime, int idxTriggerTime, int idxEchoNumbers, int idxDiffusionGradientOrientation, int idxSliceLocation, int idxImageOrientationPatient, int n)
const std::vector< std::string > & GetFileNames()
Return all the file names.
void GroupFiles(int idxSeriesInstanceUID, int idxContentTime, int idxTriggerTime, int idxEchoNumbers, int idxDiffusionGradientOrientation, int idxSliceLocation, int idxImageOrientationPatient)
std::vector< std::vector< float > > ImageOrientationPatient
vtkStandardNewMacro(vtkvmtkITKArchetypeImageSeriesReader)