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