File: TestDICOMReader.h

package info (click to toggle)
volview 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 25,204 kB
  • sloc: cpp: 132,585; ansic: 11,612; tcl: 236; sh: 64; makefile: 25; xml: 8
file content (129 lines) | stat: -rw-r--r-- 4,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*=========================================================================

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkDICOMReader.h"
#include "vtkDICOMCollector.h"
#include "vtkDICOMCollectorOptions.h"
#include "vtkGlobFileNames.h"
#include "vtkImageData.h"

#include "vtkKWCommonProBuildConfigure.h" // for VolView_DATA_ROOT

#include <vtksys/SystemTools.hxx>


int TestDICOMRead(const char *filename, bool inverted = false)
{
  // Let the vtkDICOMCollector do it's job:
  vtkDICOMReader* reader1 = vtkDICOMReader::New();
  reader1->SetFileName( filename );
  reader1->Update();

  // Ok we know the correct answer:
  // Let's contruct a glob expression.
  // Data/Coactiv/HelicalCT-ToshibaAquilion-Head/00010001/0001.*
  // Data/Coactiv/HelicalCT-ToshibaAquilion-Head/00010001/0002.*
  // Data/Coactiv/HelicalCT-ToshibaAquilion-Head/00010001/0003.*
  // Data/Coactiv/HelicalCT-ToshibaAquilion-Head/00010001/0004.*
  // Data/Coactiv/HelicalCT-ToshibaAquilion-Head/00010001/0005.*
  vtkGlobFileNames *globFileNames = vtkGlobFileNames::New();
  std::string path = vtksys::SystemTools::GetFilenamePath( filename );
  std::string fn = path + "/" + 
    vtksys::SystemTools::GetFilenameWithoutLastExtension( filename ) + ".*";
  //cerr << fn << endl;
  globFileNames->AddFileNames( fn.c_str() );

  vtkDICOMReader* reader2 = vtkDICOMReader::New();
  reader2->SetFileNames( globFileNames->GetFileNames() );
  reader2->GetDICOMCollector()->GetOptions()->ExploreDirectoryOff();
  reader2->Update();

  // Let's verify a couple of things:
  if ( reader1->GetNumberOfOutputPorts() != 1
    || reader2->GetNumberOfOutputPorts() != 1 )
    {
    cerr << "Wrong Number Of Ouput Ports" << endl;
    return 1;
    }
  vtkImageData *img1 = reader1->GetOutput();
  vtkImageData *img2 = reader2->GetOutput();
  if ( img1->GetNumberOfPoints() != img2->GetNumberOfPoints() )
    {
    cerr << "Wrong Number Of Points: " << img1->GetNumberOfPoints() <<
      " vs " << img2->GetNumberOfPoints() << endl;
    return 1;
    }
  double *range1 = img1->GetScalarRange();
  double *range2 = img2->GetScalarRange();
  if( range1[0] != range2[0] || range1[1] != range2[1] )
    {
    cerr << "Wrong range" << endl;
    return 1;
    }

  // Final tests, the list should be ordered in the ascending order, this works
  // nicely for a couple of dataset where filename also indicate order (very rare !)
  if( reader1->GetDICOMCollector()->GetNumberOfCollectedSlices() != 
      reader2->GetDICOMCollector()->GetNumberOfCollectedSlices() )
    {
    cerr << "Wrong Number of Collected Slices" << endl;
    return 1;
    }
  int n = reader1->GetDICOMCollector()->GetNumberOfCollectedSlices();
  for(int i = 0; i < n; ++i)
    {
    const char *fn1 = reader1->GetDICOMCollector()->GetSliceFileName(i);
    const char *fn2 = reader2->GetDICOMCollector()->GetSliceFileName(i);
    // filename should be identical
    if ( strcmp(fn1, fn2) != 0 )
      {
      cerr << "Wrong filenames: " << fn1 << " should be: " << fn2 << endl;
      return 1;
      }
    //cerr << fn << endl;
    vtksys_stl::string lastext = vtksys::SystemTools::GetFilenameLastExtension( fn1 );
    const char *ext = lastext.c_str();
    // The extension is increasing from 1 to n or n to 1
    int comp = inverted ? (n-i) : (i+1);
    
    if( atoi( ext+1 ) != comp )
      {
      cerr << "ext: " << ext << " instead of " << comp << endl;
      return 1;
      }
    }

  globFileNames->Delete();
  reader1->Delete();
  reader2->Delete();

  return 0;
}

// Let's see if we handle separating the series:
int TestDICOMMultiOutput(const char *filename)
{
  vtkGlobFileNames *globFileNames = vtkGlobFileNames::New();
  globFileNames->AddFileNames( filename );

  vtkDICOMReader* reader = vtkDICOMReader::New();
  reader->SetFileNames( globFileNames->GetFileNames() );
  reader->GetDICOMCollector()->GetOptions()->ExploreDirectoryOff();
  reader->Update();

  cerr << reader->GetNumberOfOutputPorts() << endl;

  reader->Delete();
  globFileNames->Delete();

  return 0;
}