]> Creatis software - clitk.git/blob - tools/clitkImageExtractLine.cxx
removed headers
[clitk.git] / tools / clitkImageExtractLine.cxx
1 #ifndef CLITKIMAGEEXTRACTLINE_CXX
2 #define CLITKIMAGEEXTRACTLINE_CXX
3 /**
4    -------------------------------------------------
5    * @file   clitkImageExtractLine.cxx
6    * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
7    * @date   23 Feb 2008 08:37:53
8    -------------------------------------------------*/
9
10 // clitk include
11 #include "clitkImageExtractLine_ggo.h"
12 #include "clitkIO.h"
13 #include "clitkImageCommon.h"
14 #include <itkLineConstIterator.h>
15
16 //--------------------------------------------------------------------
17 int main(int argc, char * argv[]) {
18
19   // Init command line
20   GGO(clitkImageExtractLine, args_info);
21   CLITK_INIT;
22
23   // Declare main types
24   typedef float PixelType;
25   const unsigned int Dimension=3;
26   typedef itk::Image<PixelType, Dimension> ImageType;
27   
28   // Check options
29   if (args_info.firstIndex_given != Dimension) {
30     std::cerr << "Please give " << Dimension << "values to --firstIndex option" << std::endl;
31     exit(0);
32   }
33   if (args_info.lastIndex_given != Dimension) {
34     std::cerr << "Please give " << Dimension << "values to --lastIndex option" << std::endl;
35     exit(0);
36   }
37
38   // Read image
39   ImageType::Pointer input = clitk::readImage<ImageType>(args_info.input_arg, args_info.verbose_flag);
40
41   // Get first and last index
42   typedef ImageType::IndexType IndexType;
43   IndexType firstIndex;
44   IndexType lastIndex;
45   ImageType::SpacingType spacing = input->GetSpacing();
46   double length = 0.0;
47   for(unsigned int i=0; i<Dimension; i++) {
48     firstIndex[i] = args_info.firstIndex_arg[i];
49     lastIndex[i] = args_info.lastIndex_arg[i];    
50     if (args_info.mm_flag) {
51       firstIndex[i] /= spacing[i];
52       lastIndex[i] /= spacing[i];
53     }
54     length += pow(lastIndex[i]*spacing[i]-firstIndex[i]*spacing[i],2);
55   }
56   length = sqrt(length);
57
58   // Loop
59   std::vector<double> depth;
60   std::vector<double> values;
61   itk::LineConstIterator<ImageType> iter(input, firstIndex, lastIndex);
62   iter.GoToBegin();
63   while (!iter.IsAtEnd()) {
64     values.push_back(iter.Get());
65     ++iter;
66   }
67   double step = length/values.size();
68   
69   // If isocenter is used
70   double isoDistance = 0.0;
71   if (args_info.isocenter_given) { // isoCenter is in mm
72     IndexType isoCenter;
73     for(unsigned int i=0; i<Dimension; i++) { 
74       isoCenter[i] = args_info.isocenter_arg[i]; 
75       isoDistance += pow(isoCenter[i] - firstIndex[i]*spacing[i],2);
76     }
77     DD(isoCenter);
78     isoDistance = sqrt(isoDistance);
79     DD(isoDistance);
80   }
81
82   // Write result
83   std::ofstream os(args_info.output_arg);
84   os << "# clitkImageExtractLine from " << args_info.input_arg << std::endl
85      << "# \t firstIndex = " << firstIndex << std::endl
86      << "# \t lastIndex  = " << lastIndex << std::endl
87      << "# \t nb values  = " << values.size() << std::endl
88      << "# \t length   = " << length << std::endl
89      << "# \t step       = " << step << std::endl;
90   if (args_info.depth_flag) {
91     double lg = -isoDistance;
92     for(unsigned int i=0; i<values.size(); i++) {
93       os << lg << " " << values[i] << std::endl;
94       lg += step;
95     }
96     os.close();
97   }
98   else {
99     for(unsigned int i=0; i<values.size(); i++) {
100       os << values[i] << std::endl;
101     }
102     os.close();
103   }
104
105   // this is the end my friend  
106   return 0;
107 } // end main
108
109 #endif //define CLITKIMAGEEXTRACTLINE_CXX